/**
 * 
 */
var nextIdToShow;
var prevIdToShow;
var currentId;
var heigthImg = "450";
var widthImg = "675";

function open_lightbox(id){
	currentId = id;
	
	//Recupera la foto e la visualizza dentro la popup
	slide_lightbox(currentId);
	
	prevIdToShow = $('#pic_'+currentId).prev().attr("id");
	nextIdToShow = $('#pic_'+currentId).next().attr("id");
	
	centerPopupGallery('580','960');
	//load popup
	loadPopup();
	
	$(document).keydown(function(e){
	    if (e.keyCode == 37) {
	       
	    	if(prevIdToShow!=undefined){
	    		if(LOAD_SINGLE_PHOTO_GALLERY=="javascript"){
	    			slide_lightbox(prevIdToShow.replace('pic_', ''));
	    		}else{
	    			currentURL = window.location.href;
	    			if(URL_REWRITE_ON){
	    				prevURL = currentURL.substr(0,currentURL.lastIndexOf("/"))+"/"+prevIdToShow.replace('pic_', '')+".html";
	    			}else{
	    				if(currentURL.lastIndexOf("photo=")=="-1"){
	    					prevURL = currentURL+"&photo="+prevIdToShow.replace('pic_', '');
	    				}else{
	    					prevURL = currentURL.substr(0,currentURL.lastIndexOf("&photo="))+"&photo="+prevIdToShow.replace('pic_', '');
	    				}
	    			}
	    			window.location = prevURL;
	    		}
	    	}
	    	return false;
	    }else if(e.keyCode == 39){
	    	
	    	if(nextIdToShow!=undefined){
	    		if(LOAD_SINGLE_PHOTO_GALLERY=="javascript"){
	    			slide_lightbox(nextIdToShow.replace('pic_', ''));
	    		}else{
	    			currentURL = window.location.href;
	    			if(URL_REWRITE_ON){
	    				nextURL = currentURL.substr(0,currentURL.lastIndexOf("/"))+"/"+nextIdToShow.replace('pic_', '')+".html";
	    			}else{
	    				if(currentURL.lastIndexOf("photo=")=="-1"){
	    					nextURL = currentURL+"&photo="+nextIdToShow.replace('pic_', '');
	    				}else{
	    					nextURL = currentURL.substr(0,currentURL.lastIndexOf("&photo="))+"&photo="+nextIdToShow.replace('pic_', '');
	    				}
	    			}
	    			window.location = nextURL;
	    		}
	    	}
		    return false;
	    }
	});
	
}

function slide_lightbox(id){
	
	currentId = id;
	
	prevIdToShow = $('#pic_'+currentId).prev().attr("id");
	nextIdToShow = $('#pic_'+currentId).next().attr("id");
	
	var srcImg = $('#pic_'+id+' a').find('img').attr("src");
	
	currentURL = window.location.href;
	if(nextIdToShow!=undefined){
		if(LOAD_SINGLE_PHOTO_GALLERY=="javscript"){
			slide_lightbox(nextIdToShow);
		}else{
			if(URL_REWRITE_ON){
				nextURL = currentURL.substr(0,currentURL.lastIndexOf("/"))+"/"+nextIdToShow.replace('pic_', '')+".html";
			}else{
				if(currentURL.lastIndexOf("photo=")=="-1"){
					nextURL = currentURL+"&photo="+nextIdToShow.replace('pic_', '');
				}else{
					nextURL = currentURL.substr(0,currentURL.lastIndexOf("&photo="))+"&photo="+nextIdToShow.replace('pic_', '');
				}
			}
		}
	}
	if(prevIdToShow!=undefined){
		if(LOAD_SINGLE_PHOTO_GALLERY=="javscript"){
			slide_lightbox(prevIdToShow);
		}else{
			if(URL_REWRITE_ON){
				prevURL = currentURL.substr(0,currentURL.lastIndexOf("/"))+"/"+prevIdToShow.replace('pic_', '')+".html";
			}else{
				if(currentURL.lastIndexOf("photo=")=="-1"){
					prevURL = currentURL+"&photo="+prevIdToShow.replace('pic_', '');
				}else{
					prevURL = currentURL.substr(0,currentURL.lastIndexOf("&photo="))+"&photo="+prevIdToShow.replace('pic_', '');
				}
			}
		}
	}
	/*var url = "";
	 currentURL = window.location.href;
	if(currentURL.lastIndexOf("photo=")=="-1"){
		currentURL = currentURL+"&photo="+currentId;
	}
	//Setto l'id della foto negli "sharer"
	var sharers = "<ul class=\"likers-orizontal\">"+
	"<li class=\"facebook\">"+
	"	<script src=\"http://connect.facebook.net/it_IT/all.js#xfbml=1\"></script>"+
	"	<fb:like href=\""+currentURL+"\" layout=\"box_count\" show_faces=\"true\" width=\"50\" font=\"\"></fb:like>"+
	"</li>"+
	"<li class=\"twitter\">"+
	"<a href=\"http://twitter.com/share?"+currentURL+"\" class=\"twitter-share-button\" data-count=\"vertical\" data-via=\"pietros23\" data-lang=\"it\">Tweet</a>"+
	"	<script type=\"text/javascript\" src=\"http://platform.twitter.com/widgets.js\"></script>"+
	"</li>"+
	"<li class=\"buzz\">"+
	"	<a title=\"Posta su Google Buzz\" class=\"google-buzz-button\" href=\"http://www.google.com/buzz/post?"+currentURL+"\" data-button-style=\"normal-count\" data-locale=\"it\" data-imageurl=\"javascript:$('.foto-dettaglio img').attr('src');\"></a>"+
	"	<script type=\"text/javascript\" src=\"http://www.google.com/buzz/api/button.js\"></script>"+
	"</li>"+
"</ul>";
	
	//http://www.facebook.com/plugins/like.php?channel_url=http%3A%2F%2Fstatic.ak.fbcdn.net%2Fconnect%2Fxd_proxy.php%3Fversion%3D0%23cb%3Df14e5694126278c%26origin%3Dhttp%253A%252F%252Flocalhost%253A8888%252Ff3aeac9e7d6ff48%26relation%3Dparent.parent%26transport%3Dpostmessage&href=http%3A%2F%2Flocalhost%3A8888%2Fomcpr%2Findex.php%3Fpage%3Ddettaglio-gallery.php%26id%3D50&layout=box_count&locale=it_IT&node_type=link&sdk=joey&show_faces=false&width=55
	/*var url = $('ul.likers-orizontal li.facebook fb\\:like span iframe').attr("src");
	if(url!=undefined){
		var indiceParametroPhoto = url.lastIndexOf("photo%3D");
		if(indiceParametroPhoto!="-1"){
			alert("ESISTE");
		}
		
		if(url.lastIndexOf('photo%3D')=="-1"){
			alert("VALE: "+url.lastIndexOf('photo%3D'));
			$('ul.likers-orizontal li.facebook fb\\:like').attr("href", window.location.href+"&photo="+id);
			
			var firstPartURL = url.substring(0,url.lastIndexOf('.php%26')+7);
			var secondPartURL = url.substring(url.lastIndexOf('.php%26')+7, url.length);
			var newURL = firstPartURL+"photo%3D"+currentId+"%26"+secondPartURL;
		
			$('ul.likers-orizontal li.facebook fb\\:like span iframe').attr("src", newURL);
		}
	}*/
	

				if(getLongestImageSize(srcImg)=="vertical"){
			    	var img = "<img src=\""+srcImg+"\" height=\""+heigthImg+"\"/>";
			    }else{
			    	var img = "<img src=\""+srcImg+"\" height=\""+heigthImg+"\" width=\""+widthImg+"\"/>";
			    }
			   	var navigator = "";
				if(prevIdToShow!=undefined){
					//Con caricamento javascript(Ajax)
					//navigator += "<a class=\"lightboxArrowLeft\" onclick=\"javascript:slide_lightbox("+prevIdToShow.replace('pic_', '')+");\" >Indietro</a>     ";
					//Con caricamento nuova pagina
					if(LOAD_SINGLE_PHOTO_GALLERY=="javascript"){
						navigator += "<a class=\"lightboxArrowLeft\" onclick=\"slide_lightbox('"+prevIdToShow.replace("pic_","")+"');\" >Indietro</a>     ";
					}else{
						navigator += "<a class=\"lightboxArrowLeft\" href=\""+prevURL+"\" >Indietro</a>     ";
					}
				}
				if(nextIdToShow!=undefined){
					if(LOAD_SINGLE_PHOTO_GALLERY=="javascript"){
						navigator += "<a class=\"lightboxArrowRight\" onclick=\"slide_lightbox('"+nextIdToShow.replace("pic_","")+"');\" >Indietro</a>     ";
					}else{
						navigator += "<a class=\"lightboxArrowRight\" href=\""+nextURL+"\" >Avanti</a>";
					}
				}
				$('.navigatorPopup').html(navigator);
				
				$('#gif_attend_popup').css("display", "none");
				$('#contentPopup').html("<div id=\"contentImg\">"+img+"</div>");
				
}

function getLongestImageSize(imgSrc){
	var newImg = new Image();
	newImg.src = imgSrc;
	curHeight = newImg.height;
	curWidth = newImg.width;
	if(curWidth>curHeight){
		return "orizontal";
	}else{
		return "vertical";
	}
}

