// Start JQuery Code

$(document).ready(function() {
initPage();
preloadImages();

});


// Init Page

function addOver(ref)
{
	if ($(ref).size() > 0)
	{
		$(ref).hover(
			function() {
				$(this).addClass("over");
			},
			function() {
				$(this).removeClass("over");
			});  			
	}
}

function initPage() {
	
/*			var zIndexNumber = 1000;
$('div').each(function() {
	$(this).css('zIndex', zIndexNumber);
	zIndexNumber -= 10;
});
*/
	
	// Home Page roll overs
	addOver("#panier");

	// START ----------------------
	// Setup Twitter ICON Animation
	$("#rss").hover(function() {
	
		// Hide facebook 
//		$("#facebook img.bigTF").stop().hide();
//		$("#textF").stop().hide();
	
		$("#rss img.bigTR").stop().fadeTo('fast',1,function() {
	
		// After the previous animation
		$("#textR").stop().animate({
										 backgroundPosition: '-663px -148px',
										 width: '151px',
										 opacity: 1},'fast');
																}).show();							  
		
								  }, function () {
	
	$("#rss img.bigTR").stop();
	$("#textR").stop().animate({
										 backgroundPosition: '-814px -148px',
										 width: 0,
										 opacity: 0},'fast', function() {
	// After the previous animation										 
	$("#rss img.bigTR").stop().fadeTo('fast',1).hide();							  
										 });
	});
	// Setup Twitter ICON Animation
	// END ------------------------



	// START ----------------------
	// Setup Twitter ICON Animation
	$("#twitter").hover(function() {
	
		// Hide facebook 
//		$("#facebook img.bigTF").stop().hide();
//		$("#textF").stop().hide();
	
		$("#twitter img.bigTF").stop().fadeTo('fast',1,function() {
	
		// After the previous animation
		$("#textT").stop().animate({
										 backgroundPosition: '-663px -108px',
										 width: '187px',
										 opacity: 1},'fast');
																}).show();							  
		
								  }, function () {
	
	$("#twitter img.bigTF").stop();
	$("#textT").stop().animate({
										 backgroundPosition: '-850px -108px',
										 width: 0,
										 opacity: 0},'fast', function() {
	// After the previous animation										 
	$("#twitter img.bigTF").stop().fadeTo('fast',1).hide();							  
										 });
	});
	// Setup Twitter ICON Animation
	// END ------------------------

		
	// START -----------------------
	// Setup Facebook ICON Animation
	$("#facebook").hover(function() {
		
		// Hide twitter 
//		$("#twitter img.bigTF").stop().hide();
//		$("#textT").stop().hide();
	
		$("#facebook img.bigTF").stop().fadeTo('fast',1,function() {
	
		// After the previous animation
		$("#textF").stop().animate({
										 backgroundPosition: '-663px -128px',
										 width: '208px',
										 opacity: 1},'fast');
																}).show();							  
		
								  }, function () {
	
	$("#facebook img.bigTF").stop();
	$("#textF").stop().animate({
										 backgroundPosition: '-871px -128px',
										 width: 0,
										 opacity: 0},'fast', function() {
	// After the previous animation										 
	$("#facebook img.bigTF").stop().fadeTo('fast',1).hide();							  
										 });
	});
	// Setup Facebook ICON Animation
	// END -------------------------
	
	// Menus
	$("#newmenu li a").click(function(event) {
		if ($(this).siblings(".subnm").size() > 0)
		{
			event.preventDefault();
		}
	});
	
	$("#newmenu li").hover(
		function() {
			
			if (! (($(this).children("a:first").hasClass("select")) || ($(this).hasClass("selectSub"))) ) {

				if ($(this).parent().parent().hasClass("subnm")) {					
					// Bug IE6 -- Si on ajoute, supprime une classe, il y a une décalage
					$(this).css('background-position','-371px -108px');
				
				} else {
					$(this).children("a:first").addClass("over");
				}

			}
			
			// Hide facebook and twitter
//			$("#twitter img.bigTF").stop().hide();
//			$("#facebook img.bigTF").stop().hide();
//			$("#textF").stop().hide();
//			$("#textT").stop().hide();
			
			if ($(this).children("div:first").size() > 0) {
				// Hide all over menus
				$(this).siblings().children("div.subnm").stop().hide();
				$(this).siblings().children("a").removeClass("over");
									
				$(this).children("div:first").stop().show();
			}
		},
		function() {
			
			if (! (($(this).children("a:first").hasClass("select")) || ($(this).hasClass("selectSub"))) ) {
				
				if ($(this).parent().parent().hasClass("subnm")) {					

					// Bug IE6 -- Si on ajoute, supprime une classe, il y a une décalage
					$(this).css('background-position','-371px -148px');

				} else {
					$(this).children("a:first").removeClass("over");
				}
														
			}
			
			if ($(this).children("div:first").size() > 0) {
				$(this).children("div:first").stop().hide();
			}				
		});
	
	// Add the menu for portraits page
	if ($(".menuPortrait img").size() > 0)
	{
		$(".menuPortrait img").hover(
			function() {
				$(this).addClass("over");
		},
			function () {
				$(this).removeClass("over");
		});
	}
	
	// Add the roll over for portraits page
	if ($(".imageCoffrets a").size() > 0)
	{
		$(".imageCoffrets a").hover(
			function() {
					$(this).parent().children("a:first").children("img").addClass("over");
		},
			function () {
					$(this).parent().children("a:first").children("img").removeClass("over");
		});
		
	}
	
	// For event galleries, start the zoom animation and navigation button
	if ($(".watermarkContainer").size() > 0)
	{
		startEventNavigation();
	}
	
}

//------------------------------------------------------------------------------
// EVENTS GALLERIES NAVIGATION
//------------------------------------------------------------------------------

function waitLoad() 
{
	// Display waiting loading
	$("#loader img").removeClass('hide');	
}

function endZoomOutAnimation() {
	
	// Hide overlay
	$("#overlay").css('display','none');

	// Unbind click
	$("#watermark").add("#overlay").unbind('click');
	
	// Display forms
	$("select").removeClass('hide');

	// Hide watermark image
	$("#watermark").css('display','none');
	
	// Display buttons
	$("#photowrapper").removeClass('stophover');
	$("#photowrapper .navbutton").removeClass('hide');
}

function endZoomAnimation() {
	$("#watermark").css('cursor','pointer');

	$(window).resize(function() 
		{
			// First resize the overlay for IE6
			if ($.browser.msie && $.browser.version.substr(0,1)<7) {
				$("#overlay").css({ 
								  'width' : $("#background").width(),
								  'height' : $("#background").height()
							  });
			}
			
			// Seconde, move the image
			var targetLeft = ($("#background").width() / 2)-30;
			$("#watermark").css('left',targetLeft);
			
		})
	
	// Event do hide the image
	$("#watermark").add("#overlay").click(function() {

				// Unbind resize of the window
				$(window).unbind('resize');

				// retrieve target position
				var pos = $("#wrap img:first").offset();
				
				var width=$("#wrap img:first").attr('width');
				var height=$("#wrap img:first").attr('height');
				
				// We are using the center of the image for the positionning
				var left = pos.left + width/2;
				var top = pos.top;
				var marginLeft = -width/2;

				// First, hide the overlay
				$("#overlay").animate( { 'opacity' : '0' },300);
				
				// Second, resize the image
				$("#watermark").animate( {
									'width' : width,
								   'height' : height,
								   'border-width' : '2px',
								   'left' : left,
								   'top' : top,
								   'margin-left' : marginLeft
										
										},300, endZoomOutAnimation);
				});
}

function startEventNavigation()
{
	
if ($.browser.msie && $.browser.version.substr(0,1)<7) {
	$(".navbutton img").addClass('ie6');
	
	// Remove image toolbar for images, otherwhize the zoom in and out are bad in ie6
	$("img").attr('GALLERYIMG','no');
}

$(".navbutton span").css('opacity', '0.2');

$(".navbutton").hover( function() { $(this).find("span").css( { 'background-color' : '#FFF', 'opacity' : '0.8' } ); },
					   function() { $(this).find("span").css( { 'background-color' : '#000', 'opacity' : '0.2' } ); }
);

$("#navleft").hover( function() { $("#navleft img").css('background-position','-100px -50px'); }, 
					 function() { $("#navleft img").css('background-position','-100px -0px');  } 
);

$("#navright").hover( function() { $("#navright img").css('background-position','-150px -50px'); }, 
				 	  function() { $("#navright img").css('background-position','-150px -0px');  } 
);

$("#navtop").hover( function() { $("#navtop img").css('background-position','0px -50px'); }, 
					function() { $("#navtop img").css('background-position','0px -0px');  } 
);

$("#navzoom").hover( function() { $("#navzoom img").css('background-position','-50px -50px'); }, 
				     function() { $("#navzoom img").css('background-position','-50px -0px');  } 
);

$("#photowrapper").hover( function() { if ($(this).hasClass('stophover') == false) { $(this).find(".navbutton").removeClass('hide');} },
						  function() { $(this).find(".navbutton").addClass('hide'); }
);

// function to zoom
$("#navzoom a").click(function(event) {
	
	event.preventDefault();
		
	// hide hover buttons
	$("#photowrapper").addClass('stophover');
	$("#photowrapper .navbutton").addClass('hide');
	
	// Load the image
	var img = new Image();
	
	var t = setTimeout("waitLoad()",30);
	
	$(img).load(function() {

		// first, stop the loader
		clearTimeout(t);

		// First hide all forms
		$("select").addClass('hide');

		// retrieve initial position
		var pos = $("#wrap img:first").offset();
		
		var width=$("#wrap img:first").attr('width');
		var height=$("#wrap img:first").attr('height');
				
		// We are using the center of the image for the positionning
		var left = pos.left + width/2;
		var top = pos.top;
		var marginLeft = -width/2;
		
		$("#watermark").css ({
							 'left' : left,
							 'top' : top,
							 'margin-left' : marginLeft,
							 'display' : 'block',
							 'width' : width,
							 'height' : height,
							 'border-color' : '#FFF'
							 });
		
		var targetWidth = img.width;
		var targetMargin = -img.width/2;
		var targetHeight = img.height;
		var targetLeft = ($("#background").width() / 2)-30;
		var targetTop = 30;
		
		// hide loading
		$("#loader img").addClass('hide');
		
		// display watermark
		$("#watermark").removeClass('hide');
		
		// animate it
		$("#watermark").animate( { 'width' : targetWidth,
								   'height' : targetHeight,
								   'border-width' : '30px',
								   'left' : targetLeft,
								   'top' : targetTop,
								   'margin-left' : targetMargin
								   }, 400);
		
		// Change the overlay
		$("#overlay").css({ 
						  'z-index' : 9999,
						  'display' : 'block',
						  'position' : 'fixed',
						  'left' : 0,
						  'top' : 0,
						  'width' : '100%',
						  'height' : '100%',
						  'opacity' : '0',
						  'background-color' : '#000',
						  'cursor' : 'pointer'
						  });

		// Fix overlay for ie6
		if ($.browser.msie && $.browser.version.substr(0,1)<7) {
				$("#overlay").css({ 
								  'position' : 'absolute',
								  'width' : $("#background").width(),
								  'height' : $("#background").height()
							  });
		}


		$("#overlay").animate( {
							  'opacity' : '0.8'
							  }, 400, endZoomAnimation);

	});
	
	// for IE problem, we must set the src after the load function
	img.src = $("#watermark").attr("src");
	});
}

//------------------------------------------------------------------------------
// EVENTS BOX HOME PAGE
//------------------------------------------------------------------------------

var current=1;
var nbLinks=null; // replaced later

var defTimeout=null;
var clickTimeout=null;
var dureeFad=null;

var timeout=null;
var timer=null;

function clickB(event)
{
	var c=event.target.id.substring(1);
		
	current = c-1;
	timeout=clickTimeout;
	clearTimeout(timer);
	runTimer();
}

function setLink(ref)
{
$('#imglink').attr('href', $('#r'+ref).attr('href'));
$('#imglink').attr('title',$('#r'+ref).attr('title'));
}

// Hide div to be able to go over link
function hide()
{
	var boucle=0;
	
	for (boucle=1; boucle <= nbLinks; boucle++)
	{
		if ($('#e'+boucle).css('opacity') == 0)
		{
			$('#e'+boucle).css('display','none');
		}
	}
}

function to(n)
{
	setLink(n);
	
	var boucle=0;
	
	for (boucle=1; boucle <= nbLinks; boucle++)
	{
		$('#i'+boucle).add('#e'+boucle).stop(true);
		if (boucle == n)
		{
			$('#b'+boucle).addClass('select');
			
			$('#e'+boucle).css('display','');
			$('#i'+boucle).add('#e'+boucle).animate({
				   opacity:1
				   },dureeFad,function() {
					   // animation completed
				   });
		}
		else
		{
			$('#b'+boucle).removeClass('select');
			
			$('#e'+boucle).css('display','');
			$('#i'+boucle).add('#e'+boucle).animate({
				   opacity:0
				   },dureeFad,function() {
					   hide();
					   // animation completed
				   });		
		}
	}
}

function nextImage()
{	
	to(current);
	timer=setTimeout(runTimer, timeout);
	timeout=defTimeout;
}

function runTimer()
{
	current = current + 1;
	if (current > nbLinks) {
		current = 1;}
	nextImage();
}

function startEve(tpsNorm,tpsClick,tpsFad)
{
	defTimeout = tpsNorm;
	clickTimeout = tpsClick;
	dureeFad = tpsFad;
	
	timeout = defTimeout;
	
	var nb=$('#nextgals p').children().length;
	
	nbLinks=nb;
	
	var i=1;
	for (i=1;i <= nbLinks; i++)
	{
		$('#b'+i).click(function(event) {event.preventDefault(); clickB(event); });
	}
	
	// Set z index of elements
	for (i=1; i <= nbLinks; i++)
	{
		$('#i'+i).add('#e'+i).css('z-index',51-i);
	}
	// Hide other elements
	for (i=2; i <= nbLinks; i++)
	{
		$('#e'+i).css( { 'display' : 'none',
					  	 'opacity' : '0' });
	}

	
	timer=setTimeout(runTimer, timeout);	
}


//------------------------------------------------------------------------------

function BrowserVersion () {
    var version = "O"; // we assume a sane browser
    if (navigator.appVersion.indexOf("MSIE") != -1)
      // bah, IE again, lets downgrade version number
      version = "IE" + parseFloat(navigator.appVersion.split("MSIE")[1]);
    return version;
}

function is_child_of(parent, child) {
	if( child != null ) {			
		while( child.parentNode ) {
			if( (child = child.parentNode) == parent ) {
				return true;
			}
		}
	}
	return false;
}
function fixOnMouseOut(element, event, JavaScript_code) {
	var current_mouse_target = null;
	event = event?event:window.event; 

	if( event.toElement ) {				
		current_mouse_target 			 = event.toElement;
	} else if( event.relatedTarget ) {				
		current_mouse_target 			 = event.relatedTarget;
	}
	if( !is_child_of(element, current_mouse_target) && element != current_mouse_target ) {
		eval(JavaScript_code);
	}
}

function hidemenulangue() {
	var menu=document.getElementById("menulangue");
	menu.className="hide";
}

function showmenulangue() {
	var menu=document.getElementById("menulangue");
	menu.className="";
}

function showNavPhoto() {
	var prev=document.getElementById("navPrev");
	var next=document.getElementById("navNext");
	var up=document.getElementById("navUp");
	
	prev.className="navShow";
	next.className="navShow";
	up.className="navShow";	
}

function hideNavPhoto() {
	var prev=document.getElementById("navPrev");
	var next=document.getElementById("navNext");
	var up=document.getElementById("navUp");
	
	prev.className="navHide";
	next.className="navHide";
	up.className="navHide";	
}


function imageon(name) {
	var panier=document.getElementById(name);
	var source = panier.src;
	source=source.replace("_off.jpg","_on.jpg");
	panier.src = source;
}

function imageoff(name) {
	var panier=document.getElementById(name);
	var source = panier.src;
	source=source.replace("_on.jpg","_off.jpg");
	panier.src = source;
}

var preloadGalerie = new Array();
var image = null;

function preloadImages() {

	// First, Preload common CSS background for menus
	var loc = window.location.href;
	var starturl = "";
	
	// Are we in a fr or en page ?
	if (loc.indexOf("/fr/") > 0)
	{
		starturl = loc.substring(0,loc.indexOf("/fr/"));
	}
	else if (loc.indexOf("/en/") > 0)
	{
		starturl = loc.substring(0,loc.indexOf("/en/"));
	}

	// Preload Input buttons
	var images = document.getElementsByTagName('input');
		
	for (var i=0;i<images.length; i++)
	{
		var source = images[i].src;
		if (source.indexOf("_off.jpg") > 0)
		{
			// Preload the image
			source=source.replace("_off.jpg","_on.jpg");
			var img = new Image();
			img.src = source;
		}
	}		


	// Preload Images for buttons
	images = document.getElementsByTagName('img');
		
	for (var i=0;i<images.length; i++)
	{
		var source = images[i].src;

		// Preload Button
		if (source.indexOf("_off.jpg") > 0)
		{
			// Preload the image
			source=source.replace("_off.jpg","_on.jpg");			
			var img = new Image(images[i].width, images[i].height);
			img.src = source;
		}
	}
	

	// Check if we are in a galerie page
	if (loc.indexOf("/galerie/") > 0)
	{
		// Preload Images for "galerie"
		images = document.getElementsByTagName('img');
		
		for (var i=0;i<images.length; i++)
		{
			var source = images[i].src;
	
			// Preload Images for "Galerie"
			if (source.indexOf("/galerie/") > 0)
			{
				// Preload the image
				source=source.replace("/galerie/","/photos/");
				preloadGalerie.push(source);

//				var img = new Image();
//				img.src = source;
			}
		}	
		
		// Start the preload
		startPreload(0);
	}
	
	// Check if we are in a galerie page
	if (loc.indexOf("/gallery/") > 0)
	{
		// Preload Images for "galerie"
		images = document.getElementsByTagName('img');
		
		for (var i=0;i<images.length; i++)
		{
			var source = images[i].src;
	
			// Preload Images for "Galerie"
			if (source.indexOf("/galerie/") > 0)
			{
				// Preload the image
				source=source.replace("/galerie/","/photos/");
				preloadGalerie.push(source);

//				var img = new Image();
//				img.src = source;
			}
		}	
		
		// Start the preload
		startPreload(0);
	}	
}

function startPreload(index)
{
	// Exit the function	
	if (index >= preloadGalerie.length)
	{
		return;
	}
	
	// Load the image
	if (image==null)
	{
		image = new Image();
		image.src=preloadGalerie[index];
	}
	
	var ind = index;
	var pause = 100;
	if (image.complete == true)	
	{
		pause=0;
		image = null;
		ind += 1;
	}

	setTimeout("startPreload("+ind+")",0);	
}

function coucou(index)
{
	alert("hello "+index);
}


