function goTo(url){
	window.location = url;
}

function gallery_save_position(pos){
	
	$.ajax({
		url		:	"/gallery/savepositions",
		type	: 	"POST",
		data	:	({
			pos			: pos,
			use_ajax	: true,
			showWindow	: true
		}),
		datatype: 'html',
		success : function(data){
			
		},
		error: function(data){
		}
	})
}
function gallery_save_catposition(pos){
	
	$.ajax({
		url		:	"/gallery/savecatpositions",
		type	: 	"POST",
		data	:	({
			pos			: pos,
			use_ajax	: true,
			showWindow	: true
		}),
		datatype: 'html',
		success : function(data){
			
		},
		error: function(data){
		}
	})
}
function gallery_show(){
	$.ajax({
		url		:	"/gallery/index",
		type	: 	"POST",
		data	:	({
			use_ajax	: true,
			showWindow	: true
		}),
		datatype: 'html',
		success : function(data){
			$('#main').html(data);
		},
		error: function(data){
		}
	})
}


function gallery_hideonclick(container){
	$(container).find("li").each(function(){
		$(this).find("img").click(function(){
		}); 
	});
}


function gallery_create(container, admin){

	var count = 0;		
	$(container).find("li").each(function(){ 
		 
		var tmp_next = $(this).next().attr('id');
		var nextid = -1;
		if(tmp_next != null)
			nextid = tmp_next;

		var tmp_prev = $(this).prev().attr('id');
		var previd = -1;
		if(tmp_prev != null){
			previd = tmp_prev;
		}
		
			
		var image = $(container+' #'+this.id+' .image').html();
		var description = $(container+' #'+this.id+' .description').html();
		var submodulid = $(container+' #'+this.id+' .submodulid').html();
		var siteid = $(container+' #'+this.id+' .siteid').html();
		var id = this.id;		
		
		var e = 'img';
		if(admin == 1){
			e = ".magnifier";
			$(this).mouseover(function(){
						$(container+' #'+id+' .magnifier').fadeIn();
					})
					.mouseleave(function(){
						$(container+' .magnifier').fadeOut();
					});			
		}
					
		$(this).find(e).click(function(){
			gallery_loadimage(image, description, id, submodulid, siteid, nextid, previd);
		});
		
		
		
		count++;
	});
	
	if(admin == 1)
	$(container+' .magnifier').hide();
		
	
}

function gallery_showcategory_images(siteid, catid, submodulid){
	$.ajax({
		url		:	"/gallery/category",
		type	: 	"POST",
		data	:	({
			catid 		: catid,
			siteid		: siteid,
			submodulid	: submodulid,
			use_ajax	: true,
			showWindow	: true
		}),
		datatype: 'html',
		success : function(data){
			$('.gallery_category_'+catid+' .gallery_content').html(data).slideToggle("slow");
		},
		error: function(data){
		}
	})
}

function gallery_close_singleimage(){
	$(".single_image").fadeOut(500,function(){$("#single_image_1 .site_text").fadeIn()}).empty();
}

function gallery_nextimage(id){
	if($('.gallery_sortable #' + id+' .magnifier').length > 0){
		$('.gallery_sortable #' + id+' .magnifier').click();
	}else{
		
		$('.gallery_sortable #' + id+' img').click();
	}
}

function ConvertCssPxToInt(cssPxValueText) {

            var validChars = "0123456789.";
            var convertedValue = 0;
            for (i = 0; i < cssPxValueText.length; i++) {
                if (validChars.indexOf(cssPxValueText.charAt(i)) == -1) {
                    if (i > 0) {
                        convertedValue = parseInt(cssPxValueText.substring(0, i));
                        return convertedValue;
                    }
                }
            }

            return convertedValue;
}

function gallery_loadimage(image, description, id, submodulid, siteid, nextid, previd){
	if(submodulid == 0){ 
		loading('body',0,20);
	}else{
		loading('#main',20,-30);
	}

			$.ajax({
				url		:	"/gallery/loadimage",
				type	: 	"POST",
				data	:	({
					image 		: image,
					id			: id,
					siteid		: siteid,
					description : description,
					nextid		: nextid,
					previd		: previd,
					use_ajax	: true,
					showWindow	: true
				}),
				datatype: 'html',
				success : function(data){
					$("#single_image_1 .site_text").fadeOut(200);
					$(".single_image").html(data);
				},
				error: function(data){
				}
			})
}

function gallery_displayimage(){
	$('.single_image img').ready(function(){
	    $(".single_image").fadeIn(600);
	    $('.single_image .container').css({visibility:'hidden',display: 'block'});
	    
				  	        
		var width = $('.single_image img').css('width') ;
		width = ConvertCssPxToInt(width);
		$(".single_image .container").animate({
			left: $(".single_image").width()/2  - width/ 2,
			width: width+'px'
		},  
			function(){	       
				$('.single_image .container').css({visibility:'visible',display: 'none'}).fadeIn(600,function(){
					stopLoading();
					$("#single_image_0 .gallery_description").fadeIn(2000);
					
				});
			}
		);
		
	});
}

function gallery_savedescription(id, siteid){
	description = FCKeditorAPI.GetInstance('description').EditorWindow.parent.FCK.GetHTML() ; 
	$.ajax({
		url		:	"/gallery/savedescription",
		type	: 	"POST",
		data	:	({
			id 			: id,
			siteid		: siteid,
			description : description,
			use_ajax	: true,
			showWindow	: true
		}),
		datatype: 'html',
		success : function(data){
			if (siteid >= 0) {
				goTo("/index/site/nr/" + siteid);
			}else{
				goTo("/index");
			}
		},
		error: function(data){
		}
	})	
}

function gallery_newcategorie(catid, userid, siteid){
	$.ajax({
		url		:	"/gallery/newcat",
		type	: 	"POST",
		data	:	({
			catid		: catid,
			userid		: userid,
			siteid		: siteid,
			use_ajax	: true,
			showWindow	: true
		}),
		datatype: 'html',
		success : function(data){
			$('body').append(data);
			$("#window[name='gallerycatwindow']").fadeIn();
		},
		error: function(data){
		}
	})	
}

function gallery_shownewpicture(modulid, userid, siteid, catid){
	$.ajax({
		url		:	"/gallery/new",
		type	: 	"POST",
		data	:	({
			modulid		: modulid,
			userid		: userid,
			siteid		: siteid,
			catid		: catid,
			use_ajax	: true,
			showWindow	: true
		}),
		datatype: 'html',
		success : function(data){
			$('body').append(data);
			$("#window[name='gallerynewwindow']").fadeIn();
		},
		error: function(data){
		}
	})	
}

function gallery_sizeImage(id,mode){

}


function gallery_showUpload(){
	wait("Die Datei wird hochgeladen. Haben Sie bitte einen Moment Geduld...");
}


function gallery_deletecat(id, siteid){
	$.ajax({
		url		:	"/gallery/deletecat",
		type	: 	"POST",
		data	:	({
			id	 		: id,
			siteid		: siteid,
			use_ajax	: true,
			showWindow	: true
		}),
		datatype: 'html',
		success : function(data){
			if (siteid >= 0) {
				goTo("/index/site/nr/" + siteid);
			}else{
				goTo("/index");
			}
		},
		error: function(data){
		}
	})
}


function deleteGalleryPic(id, siteid){
		$('html').append("<div id='gallery_dialog' title='Galeriebild entfernen'>Möchten Sie dieses Bild wirklich entfernen?</div>");
		$('#gallery_dialog').dialog(
		   {"option":"modal",
		   	zIndex: 3999 ,
		   	buttons: {
		   		"Abbrechen": function(){
		   			$(this).dialog("close");
					if (siteid >= 0) {
						goTo("/index/site/nr/" + siteid);
					}else{
						goTo("/index");
					}
		   		},
		   		"Ok": function(){
		   			$.ajax({
						url		:	"/gallery/delete",
						type	: 	"POST",
						data	:	({
							id 			: id,
							use_ajax	: true,
							showWindow	: true
						}),
						datatype: 'html',
						success : function(data){
							if (siteid >= 0) {
								$('#gallery_dialog').remove();
							}else{
								goTo("/index");
								$(this).dialog("close");
							}
						},
						error: function(data){
						}
					})
					
		   		}
		   	}
		   }
		)
	
}


function gallery_toggle_description(){
	$('.gallery_description_edit').slideToggle('slow',function(){
	});
}

