	
	// DIALOG 
	function removeDialog(){$(".panes").remove();}
	function displayDialog(title,message){
				
		var alert_dialog = "<div class='panes'><div class='alert_shadow'><div class='alert'><div class='dialog_title'>"+title+"</div><div class='dialog_message'>"+message+"</div><div class='dialog_separator'></div><div class='dialog_tools'><span class='b_button' id='dialog_abort'>Annulla</span><span class='b_button def' id='dialog_action'>OK</span></div></div></div></div>";
		
		$("body").append(alert_dialog);
		
		$("#dialog_abort").click(function(){removeDialog();});
		
	};
	
	
	function shareFb(obj,f,pid,aobj,aid,atl,loc,desc,ad,md){
		var page_url = "http://www.facebook.com/sharer.php?u=http://www.bland.it/photos/photo.php";

		shareUrl=page_url+"?fph="+obj+"|"+f+"|"+pid+"|"+aobj+"|"+aid+"|"+atl+"|"+loc+"|"+desc+"|"+ad+"|"+md;
		window.open(shareUrl,'sharer','toolbar=0,status=0,resizable=1,width=700,height=500');
	}
	
	function shareTw(obj,f,pid,aobj,aid,atl,loc,desc){
		var page_url = "http://www.twitter.com/home?status=http://www.bland.it/photos/twitter.php";
	
		shareUrl = page_url+"?fph="+obj+"=="+f+"=="+pid+"|"+aobj+"=="+aid+"=="+atl+"=="+loc+"=="+desc;
		window.open(shareUrl,'sharer');
	}
	
	
	function jumpToPhoto(obj, f, pid, aobj, aid, atl, loc, desc, ad, md){
	
		$.ajax({
			type: "POST",
			url: "php/loadAlbums.php",
			data: "",
			beforeSend: function(){
				//$(".albums_list").html("Loading albums...");
				var loader="<div class='gallery_loading'>"+spinner_32+"</div>";
				$(".albums_list").show().html(loader);
			},
			success: function(response){
				
				$(".albums_list").html(response);
				loadPhotos(aobj, aid, f, atl, loc, desc, ad, md);
				viewPhoto(obj, f, pid, aobj, aid, atl, loc, desc, ad, md);
				
			}
		});

		
	};
	
	
	function new_loadAlbums(){
	
		$.ajax({
			type: "POST",
			url: "php/new_loadAlbums.php",
			data: "",
			beforeSend: function(){
				//$(".albums_list").html("Loading albums...");
				var loader="<div class='gallery_loading'>"+spinner_32+"</div>";
				$(".albums_list").show().html(loader);
			},
			success: function(response){
				
				$(".albums_list").html(response);
				
			}
		});
		
	};
	
	
	function new_loadPhotos(aobj, aid, f, atl, loc, desc, ad, md){
		
		$.scrollTo($(".album_details"), 300);
		
		$(document).unbind("keydown");
		
		$.ajax({
			type: "POST",
			url: "php/new_loadPhotos.php",
			data: "aobj="+aobj+"&aid="+aid+"&f="+f+"&atl="+atl+"&loc="+loc+"&desc="+desc+"&ad="+ad+"&md="+md,
			beforeSend: function(){
				var loader="<div class='gallery_loading'>"+spinner_32+"</div>";
				//$(".photogallery_container").show().html(loader);
				$("#thumbs .thumbs").show().html(loader);
				
				$(".album_details").html("");
				$(".photo_nav .album").unbind();
			
				$(".album_box").removeClass("selected");
				$(".album_box."+aobj+"_item").addClass("selected");
				
				$(".photo_enlarge").hide();
				$(".photo_nav").hide();
				$(".photo_details").hide();
			},
			success: function(response){
				
				$(".album_details").append("<div class='album_title'>"+atl+"</div>");				
				$("#thumbs .thumbs").html(response);
				
			}
		});
		
	};
	
	
	
	
	
	
	
	function loadAlbums(){
	
		$.ajax({
			type: "POST",
			url: "php/loadAlbums.php",
			data: "",
			beforeSend: function(){
				//$(".albums_list").html("Loading albums...");
				var loader="<div class='gallery_loading'>"+spinner_32+"</div>";
				$(".albums_list").show().html(loader);
			},
			success: function(response){
				
				$(".albums_list").html(response);
				
			}
		});

		
	};
	
	function loadPhotos(aobj, aid, f, atl, loc, desc, ad, md){
		
		$.scrollTo($(".album_details"), 300);
		
		$(document).unbind("keydown");
		
		$.ajax({
			type: "POST",
			url: "php/loadPhotos.php",
			data: "aobj="+aobj+"&aid="+aid+"&f="+f+"&atl="+atl+"&loc="+loc+"&desc="+desc+"&ad="+ad+"&md="+md,
			beforeSend: function(){
				var loader="<div class='gallery_loading'>"+spinner_32+"</div>";
				$(".photogallery_container").show().html(loader);
				
				$(".album_details").html("");
				$(".photo_nav .album").unbind();
			
				$(".album_box").removeClass("selected");
				$(".album_box."+aobj+"_item").addClass("selected");
				
				$(".photo_enlarge").html("").hide();
				$(".photo_nav").hide();
				$(".photo_details").hide();
				
			},
			success: function(response){
				
				$(".album_details").append("<div class='album_title'>"+atl+"</div>");
				//$(".album_details").append("<div class='album_dates'>Creato il "+ad+" - Aggiornato il "+md+" - "+openMaps(loc)+"</div>");
				//$(".album_details").append("<div class='album_description'>"+desc+"</div>");
				
				$(".photogallery_container").html(response);
				$(".photogallery_container img").load(function(){$(this).fadeIn(200);});
				
				$(".photo_thumb_box:first").addClass("first_photo");
				$(".photo_thumb_box:last").addClass("last_photo");
				
				//$(".photo_nav .album").click(function(){loadPhotos(obj, aid, f, atl, loc, desc, ad, md);});				
				
			}
		});
		
	};
	
	
	
	/*
	function viewPhoto(obj, folder, id_photo){
		location.href = "?fph="+obj+"|"+folder+"|"+id_photo;
	};
	*/
	
	function viewPhoto(obj, f, pid, aobj, aid, atl, loc, desc, ad, md){
		
		$(".current_photo").addClass("old");
		
		$(".photo_nav .album").unbind();
		$(".photo_nav .album").click(function(){loadPhotos(aobj, aid, f, atl, loc, desc, ad, md);});		
		
		$(".photogallery_container").hide();
		
		$("#fb_share").unbind("click");
		$(".photo_nav .next").unbind("click");
		$(".photo_nav .prev").unbind("click");
		$(document).unbind("keydown");
		
		$(document).keydown(function(e){
			if(e.which == 39 ){nextImage();}	
			else if(e.which == 37 ){prevImage();}	
    });
		
		//var current_photo_url = "http://www.facebook.com/sharer.php?u=http://www.bland.it/photos/photo.php";
		//location.href = "#"+obj+"|"+f+"|"+pid+"|"+aobj+"|"+aid+"|"+atl+"|"+loc+"|"+desc+"|"+ad+"|"+md;
		
		var photo_url = "http://www.bland.it/public/albums/"+f+"/photos/"+pid;
		var photo = "<img class='current_photo' src="+photo_url+" />";
		var loading = "<div class='photo_loader'><span class='photo_loader_container'>"+spinner_32_000+"</span></div>";
		
		$(".photo_enlarge").show().append(loading+photo);
		$(".photo_loader").fadeIn(100);
		
		$(".photo_nav").show();
		
		$(".current_photo").load(function(){
			
			var old_w = $(".current_photo.old").width();
			var old_h = $(".current_photo.old").height();
			
			$(".current_photo.old").animate({top:old_h+50+"px"}, 350, function(){$(this).remove();$(".photo_loader").remove();});
			
			var img_w = $(this).width();
			var img_h = $(this).height();
			
			var w, h;
			
			if((img_w > img_h) && (img_w > 620)){ w = 620; h = Math.round((w*img_h)/img_w);}
			else if((img_h > img_w) && img_h > 550){ h = 550; w = Math.round((h*img_w)/img_h);}
			else{ w = img_w; h = img_h;}
			
			var left, top, top_next;
			left = Math.round((670-w)/2);
			
			top = Math.round(-h+10);
			top_next = Math.round(h+50);
			
			$(this).css({width:w+"px", height:h+"px", left:left, top:top+"px"});
			
			//$(".photo_enlarge").css({height:h+"px"});
			$(".photo_enlarge").animate({height:h+"px"}, 350);
			
			$(".photo_loader").fadeOut(100);
			
			$(this).fadeIn(100).animate({top:"10px"}, 350);
			
			// fix ie bug ok
			nextImage = function(){
				if($(".photo_thumb_box."+obj+"_image").hasClass("last_photo")){
					$(".photo_thumb_box.first_photo").trigger("click");
				}
				else{
					$(".photo_thumb_box."+obj+"_image").next().trigger("click");
				}
				
			}
			
			prevImage = function(){
				if($(".photo_thumb_box."+obj+"_image").hasClass("first_photo")){
					$(".photo_thumb_box.last_photo").trigger("click");
				}
				else{
					$(".photo_thumb_box."+obj+"_image").prev().trigger("click");
				}
				
			}
			
			$(this).click(function(){nextImage();});
			$(".photo_nav .next").click(function(){nextImage();});
			$(".photo_nav .prev").click(function(){prevImage();});
			
			//load photo comments
			//loadComments(f, pid, true);
			
		});
		
		
		// comments
		$(".photo_details").show();
		$("#comment_bt").hide();
		$(".comment").focus(function(){
			$(document).unbind("keydown");
			if($(this).val() == "Scrivi un commento...") {
				$(this).removeClass("hint").val("");}
				$("#comment_bt").show();
				document.getElementById("comment_bt").onclick=function(){addComments(f, pid);
			}
		});
		
		$(".comment").blur(function(){if($(this).val() == ""){ 
																			$(this).addClass("hint").val("Scrivi un commento...");
																			$("#comment_bt").hide().unbind();
																	 }
		});
		
		
		// sharer
		
		//var fb_share = bButton("Condividi su Facebook", "share_fb", "small fb");
		//var tw_share = bButton("Condividi su Twitter", "share_tw", "small tw");
		//$(".photo_sharer").html(fb_share+tw_share+share_bt);
		//$("#share_fb").click(function(){shareFb(obj,f,pid,aobj,aid,atl,loc,desc,ad,md);});
		//$("#share_tw").click(function(){shareTw(obj,f,pid,aobj,aid,atl,loc,desc);});
		
		var s_link = "http://www.bland.it/photos/photo.php?fph="+obj+"|"+f+"|"+pid+"|"+aobj+"|"+aid+"|"+atl+"|"+loc+"|"+desc+"|"+ad+"|"+md;
		var share_bt = "<fb:share-button type='button' class='url' href='"+s_link+"'></fb:share-button>";
		$(".photo_sharer").html(share_bt);
		
		var like_url="http://www.bland.it/photos/photo.php?fph="+obj+"|"+f+"|"+pid+"|"+aobj+"|"+aid+"|"+atl+"|"+loc+"|"+desc+"|"+ad+"|"+md;
		var like_bt = "<fb:like href='"+like_url+"' layout='standard' show_faces='true' width='500' action='like' />"
		
		var comment_url="http://www.bland.it/photos/photo.php?fph="+obj+"|"+f+"|"+pid+"|"+aobj+"|"+aid+"|"+atl+"|"+loc+"|"+desc+"|"+ad+"|"+md;
				
		// togliere commento sotto per abilitare il pulsante i like this
		$(".photo_details .photo_like_box").html('<iframe src="http://www.facebook.com/plugins/like.php?href='+like_url+'&amp;layout=standard&amp;show_faces=true&amp;width=500&amp;action=like&amp;colorscheme=light&amp;height=80" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:400px; height:80px;" allowTransparency="true"></iframe>');
		
		// togliere commento sotto per abilitare commenti facebook
		/*
		var comment_box = "<fb:comments xid='"+pid+"' url='"+comment_url+"' numposts='10' width='530'></fb:comments>";
		$(".photo_details .photo_like_box").html(like_bt+comment_box);
		
		window.fbAsyncInit = function() {
			FB.init({
				appId: "117767404902233", status: true, cookie: true, xfbml: true
			});
		};
		(function() {
			var e = document.createElement('script'); e.async = true;
			e.src = document.location.protocol + '//connect.facebook.net/it_IT/all.js';
			document.getElementById('fb-root').appendChild(e);
		}());
		*/
	};
	
	
	// Button
	function bButton(label, b_id, b_cl){
		
		var bBland;
		
		if(b_id){ bId = "id='"+b_id+"'";}else{bId = "";}
		if(b_cl){ bClass = b_cl;}else{bClass = "";}
		
		bBland = "<span class='b_button "+bClass+"' "+bId+">"+label+"</span>";
		
		return bBland;
		
	};
	
	
	function loadComments(folder, id_photo, load){
		
		$.ajax({
				type: "POST",
				url: "xml/_xml_comment_load.php",
				data: "folder="+folder+"&id_photo="+id_photo,
				beforeSend: function(){
					if(load)$(".comment_list").html("");
					//$(".comment_list").html("");
				},
				success: function(response){
					
					$(".comment_list").html(response);
					
				}
			});
		
	};
	
	function addComments(folder, id_photo){
		
		var comment_txt = $(".comment").val();
		if(comment_txt != "Scrivi un commento..." || comment_txt != ""){
		
			$.ajax({
					type: "POST",
					url: "xml/_xml_comment_add.php",
					data: "folder="+folder+"&id_photo="+id_photo+"&comment_txt="+comment_txt,
					beforeSend: function(){
						$(".comment").addClass("hint").val("Scrivi un commento...");
						$("#comment_bt").hide().unbind();
					},
					success: function(response){
						loadComments(folder, id_photo);
					}
				});
		
		}
		
	};
	
	function deletingComment(folder, id_photo, id_comment){
		
		displayDialog("Eliminare il commento?", "Sei sicuro di voler eliminare questo commento?");
		
		$("#dialog_action").html("Elimina").click(function(){deleteComment(folder, id_photo, id_comment);});
		
	};
	
	
	function deleteComment(folder, id_photo, id_comment){
	
		$.ajax({
				type: "POST",
				url: "xml/_xml_comment_del.php",
				data: "folder="+folder+"&id_photo="+id_photo+"&id_comment="+id_comment,
				beforeSend: function(){
					removeDialog();
				},
				success: function(response){
					loadComments(folder, id_photo);
				}
			});

		
	};
	
	
	
	function resettingComments(folder, id_photo){
		
		displayDialog("Eliminare tutti i commenti?", "Sei sicuro di voler eliminare questo commento?");
		
		$("#dialog_action").click(function(){resetComments(folder, id_photo);});
		
	};
	
	
	function resetComments(folder, id_photo){
	
		$.ajax({
				type: "POST",
				url: "xml/_xml_comment_reset.php",
				data: "folder="+folder+"&id_photo="+id_photo,
				beforeSend: function(){
					removeDialog();
				},
				success: function(response){
					loadComments(folder, id_photo);
				}
			});

		
	};
	

