$(document).ready(function(){	

 //$(document).bind("contextmenu",function(e){return false;});
	photobook();
	$('#closePopup').click(function(){hidePopup();})
	$('#search').click(function(){ document.quickSearch.submit();})
	
	$('#send2friend #close').click(function(){hideSend2friend();});		
	$('#send2friend #send').click(function(){		
		var email = $('#send2friend #email').val();		

		if (validateEmail(email) == false){
			$('#invalidEmail').fadeIn(400).delay(1500,function(){$('#invalidEmail').fadeOut(400);});
		}else{
			var link = location.href;
			$.ajax({
				type: "GET",
				url: "ajax/send2friend.php",
				data: "email="+escape(email)+"&link="+escape(link),
				dataType: "html",
				success: function(html){
					$('#send2friend #msg').html(html);	
					$('#msg').fadeIn(400);					
				}
		});		
			
		
		}
	});

});

/********POPUP PART***********/
 function showPopup(html){
		$('#popupContent').html(html);					
		$('#closePopup img').click(function(){hidePopup();});
		$('#popup').fadeIn();
		$('#wrapper').animate({"opacity": "0.00"}, 500);
		$('#footer').animate({"opacity": "0.00"}, 500); 
 }
 function hidePopup(){
		$('#popup').fadeOut();
		$('#wrapper').animate({"opacity": "100"}, 500);
		$('#footer').animate({"opacity": "100"}, 500);
		$('#popupContent').html(''); 
 }

/********SEND2FRIEND PART***********/
function send2friend(){	
		$('#send2friend').fadeIn();
		$('#page').animate({"opacity": "0.10"}, 500);	
 }
 
 function hideSend2friend(){
		$('#send2friend').fadeOut();
		$('#page').animate({"opacity": "1.0"}, 500);	
		$('#msg').fadeOut('normal');		
		$('#send2friend #msg').html();			
 }
 
function validateEmail(elementValue){  
	var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;  
	return emailPattern.test(elementValue);  
}  

function photoLoadImage(img,title){
			$("#imageFader").fadeOut(200,function(){	

				$("#imageFader img").attr('src',img);					
				$("#photobook #title").html(title);
				$('#imageFader img').load(function() {$("#imageFader").fadeIn(500);});

			});

}
function popupPhotoBookNav(){
			$("#popupContent #prev").click(function(){
					photobookPrev();
			     	var src = $("#photobook li.active img").attr('src');
					$('#popupPhotoBookImage').html('<img src="'+src+'">');
			
            });
			$("#popupContent #next").click(function(){
			    	photobookNext();
					var src = $("#photobook li.active img").attr('src');
					$('#popupPhotoBookImage').html('<img src="'+src+'">');		
			});
}
function photobook(){
	//Init le popup
		$("#mainImage img").click(function(){
			var src = $(this).attr('src');
			var popupHtml = '<div id="prev"><img src="images/prevBig.png"></div><div id="popupPhotoBookImage"> <img src="'+src+'"></div><div id="next"><img src="images/nextBig.png"></div>';
			showPopup(popupHtml);
            popupPhotoBookNav();
		});       
			
		//move prev&next img de base
		$("#photobook #prev").click(function(){photobookPrev();});
		$("#photobook #next").click(function(){photobookNext();});


			//Load image on click
			$('#photobook li img').click(function(){
				photoLoadImage($(this).attr('src'),$(this).attr('title'));
				$("#photobook li.active").removeClass('active');
				$(this).parent("li").addClass('active');
			});

			
		//tout est ready on peu afficher
		//detect & load first picture


                photoLoadImage($("#photobook li.active img").attr('src'),$("#photobook li.active img").attr('title'));
   

}
function photobookPrev(){
				$("#photobook li.active").each(function(){
					//test if not already first	
					if ($('#photobook  li').index(this) > 0){
						$("#photobook #container").animate({"left":"+=58px"},200);
						$(this).prev("#photobook li").addClass('active');
						$(this).removeClass('active');
						photoLoadImage($("#photobook li.active img").attr('src'),$("#photobook li.active img").attr('title'));
					}
				});
}
function photobookNext(){			
				$("#photobook li.active").each(function(){
					//test if not already last
					if ($('#photobook li').index(this) < $("#photobook li").length-1){
							$("#photobook #container").animate({"left":"-=58px"},200);		
							$(this).next("#photobook li").addClass('active');
							$(this).removeClass('active');	
							photoLoadImage($("#photobook li.active img").attr('src'),$("#photobook li.active img").attr('title'));
					}
				});
}
