// 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
	$("#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").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");
		});
		
	}
	
}

//------------------------------------------------------------------------------
// 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);
	}
	
	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("/fr/"));
	}

	// 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);
}


