/*
		© Vincent Pauly - www.vincentpauly.com
		D'après une idée originale de Tobias Mueller.
*/

/*document.writeln('<link rel="stylesheet" type="text/css" href="../css/load.css" />');*/

var urls = [
	'../images/design/bg_paper.jpg',
	'../images/design/logo.png',
	'../images/design/bg-general.gif',
	'../images/design/bg_fields.gif',
];



var preloadImages = {
	imgs : new Array(),
	loaded : 0,
	errors : 0,
	status : null,
	
	start : function(urls,doOnComplete)
	{
		this.urls = urls;
		this.doOnComplete = doOnComplete || null;
		this.status = window.setInterval(function() { preloadImages.checkStatus() },150);
		for (var i = 0; i < this.urls.length; i++)
		{
			this.imgs[i] = new Image();
			this.imgs[i].onload = function() { preloadImages.loaded++; };
			this.imgs[i].onerror = function() { preloadImages.errors++; };
			this.imgs[i].src = this.urls[i];
		}
	},
	
	checkStatus : function()
	{
		if (this.loaded + this.errors == this.urls.length)
		{
			window.clearInterval(this.status);
			if (this.doOnComplete != null)
				this.doOnComplete();
			
			return this.imgs;
		}
	}
};



var SlideContent = function(slideElem,slideStepDivider,slideStepMin,slideTimeout)
{
	if (document.getElementById)
	{
		this.slideElem = slideElem;
		this.slideElemHeight = 0;
		this.slideStepDivider = slideStepDivider;
		this.slideStepMin = slideStepMin;
		this.slideTimeout = slideTimeout;
		this.initDone = false;
	}
}

SlideContent.prototype = {

	init : function()
	{
		if (document.getElementById)
		{
			this.slideElem = document.getElementById(this.slideElem);
			this.topPos = 76;
			this.sliding = false;
			this.oldObj = null;
			this.currentActive = null;
			this.hash = null;
			this.initDone = true;
		}
	},

	action : function(targ,obj)
	{
		if (this.initDone && !this.sliding)
		{
			/*
			if (!(document.all && !window.XMLHttpRequest))
			{
				this.slideElem.style.position = 'absolute';
				this.slideElem.style.top = -500 + 'px';
			}
			*/
			this.slide(targ,obj);
			if (arguments.length > 1)
			{
				this.nextFunc = arguments[0];
			}
			else
			{
				this.nextFunc = null;
			}
			
			var href = obj.getAttribute('href');
			href = href.split('#');
			this.hash = '#' + href[1];
			window.location.hash = this.hash;

			if (this.currentActive)
			{
				this.currentActive.fadeIn('normal');
			}

			if (!window.opera)
			{
				return false;
			}
		}
	},

	slide : function(targ,obj)
	{
		if (!this.sliding)
		{
			if (document.all && !window.opera)
			 {
				if (document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop))
				{
					this.yOffset = document.documentElement.scrollTop;
				}
				else if (document.body)
				{
					this.yOffset = document.body.scrollTop;
				}
			}
			else
			{
				this.yOffset = window.pageYOffset;
			}
			
			this.obj = obj;
			this.targ = targ;
			this.pos = this.yOffset;

			if (this.pos < this.targ)
				this.slideSteps = Math.round((this.targ - this.pos) / this.slideStepDivider);
			else if (this.pos > this.targ)
				this.slideSteps = Math.round((this.pos - this.targ) / this.slideStepDivider);
			
			this.sliding = true;
		}
		
		if ( (this.pos < this.targ - this.slideSteps) )
		{
			this.pos += this.slideSteps;
			window.scrollTo(0,this.pos);
			
			this.slideSteps -= (this.slideSteps > this.slideStepMin) ? Math.round(this.slideSteps / this.slideStepDivider) : 0;
			
			var $this = this;
			window.setTimeout(function(){$this.slide()},this.slideTimeout);
		}
		else if (this.pos - this.slideSteps > this.targ)
		{
			this.pos -= this.slideSteps;
			window.scrollTo(0,this.pos);

			this.slideSteps -= (this.slideSteps > this.slideStepMin) ? Math.round(this.slideSteps / this.slideStepDivider) : 0;

			var $this = this;
			window.setTimeout(function(){$this.slide()},this.slideTimeout);
		}
		else
		{
			window.scrollTo(0,this.targ);
			this.sliding = false;
			this.slideMenu(targ);
		}
	},

	slideMenu : function(targ) {
		if (!this.sliding) {
			this.targ += this.topPos;
			if (this.oldObj)
			{
				this.oldObj.className = '';
			}	
			this.obj.className = 'active';
			this.oldObj = this.obj;
			
			this.slideElem.style.top = parseInt(this.targ - 500) + 'px';
			
			this.pos = this.targ - 500;
			
			this.slideSteps = Math.round((this.targ - this.pos) / this.slideStepDivider);
			this.sliding = true;
		}
		
		if ( (this.pos < this.targ - this.slideSteps) )
		{
			this.pos += this.slideSteps;
			this.slideElem.style.top = this.pos + 'px';
			
			this.slideSteps -= (this.slideSteps > this.slideStepMin) ? Math.round(this.slideSteps / this.slideStepDivider) : 0;
			
			var $this = this;
			window.setTimeout(function(){$this.slideMenu()},this.slideTimeout);
		}
		else
		{
			/*
			if (document.all && !window.XMLHttpRequest)
			{
				this.slideElem.style.top = this.targ + 'px';
			}
			else
			{
				this.slideElem.style.position = 'fixed';
				this.slideElem.style.top = this.topPos + 'px';
			}
			*/
			this.slideElem.style.top = this.targ + 'px';
			
			this.currentActive = $(this.hash).find('a.show-nav');
			this.currentActive.fadeOut('normal');

			this.sliding = false;
		}
	}
};

var ContSlider = new SlideContent('NavBox',5,2,25);





var navBox = null;
var contentBox = null;
var footBox = null;

$(document).ready(function()
{
	ContSlider.init();
	navBox = $("#NavBox");
	navBox.css({ "top" : "-500px" });
	navBox.find("a[href*='#PhotographiesCouleur']").click(function(){ return ContSlider.action(4000,this); });
	navBox.find("a[href*='#PhotographiesNetB']").click(function(){ return ContSlider.action(2000,this); });
	navBox.find("a[href*='#Goodies']").click(function(){ return ContSlider.action(6000,this); });
	navBox.find("a[href*='#Accueil']").click(function(){ return ContSlider.action(0,this); });
	navBox.find("a[href*='#Coordonnees']").click(function(){ return ContSlider.action(8000,this); });
	
	contentBox = $("#ContentBox");
	contentBoxMenuLinks = contentBox.find("a[href*='#Top']");
	
	footBox = $('#Footer');
	footBox.css({ "height" : contentBox.height() + "px" });
	
	footBox.css( { "background-position" : "50% " + parseInt($(window).scrollTop() + 150) + "px"  } );

	$.each(contentBoxMenuLinks,
		function()
		{
			var self = $(this);
			self.click(function(){ navBox.find("a[href*='" + self.attr('rel') + "']").click(); return false; } );
		}
	);

	var msgObj = $("#FormMsg");
	function sendForm()
	{
		var action = "index-2.html";
		var str = $("#Contact").find("form").serialize();
		var url = action + '?' + str + '&ajax=1';
		
		msgObj.fadeOut("normal",
			function()
			{
				msgObj.text('Veuillez patienter pendant l\'envoi du message.');
				
				var mailVal = $("#EMail").val();
				if (mailVal != '' && !mailVal.match( new RegExp("^([-!#\$%&'*+./0-9=?A-Z^_`a-z{|}~])+@([-!#\$%&'*+/0-9=?A-Z^_`a-z{|}~]+\\.)+[a-zA-Z]{2,6}\$") ) )
				{
					msgObj.text("Erreur : votre adresse e-mail doit être valide."); 
					msgObj.fadeIn("normal");
					return false;
				}
				
				msgObj.fadeIn("normal",
					function()
					{
						$.getJSON(url, function(data)
						{
							var result = data.result;
							msgObj.fadeOut("normal",
								function()
								{
									var msg = '';
									if (result == 3)
									{
										msg = 'Erreur : votre message n\'a pas pu être envoyé.';
									}
									else if (result == 2)
									{
										msg = 'Erreur : les champs avec astérisque sont obligatoires.';
									}
									else
									{
										$("#Contact").find("form").find('input.i-text,textarea.i-textarea').val('');
										msg = 'Votre message a bien été envoyé.';
									}
									msgObj.text(msg); 
									msgObj.fadeIn("normal");
								}
							);
							
						});
					}

				);
			}
		);
		
	}
	
	$("#Contact").find("form").bind('submit',function(){ sendForm(); return false;});

});

$(window).load(
	function()
	{
		var hash = window.location.hash || "#Accueil";

		preloadImages.start(urls,
			function()
			{
				setTimeout(
					function()
					{
						footBox.css({ 'background-image' : 'none' })
						footBox.fadeOut(500,
							function()
							{
								navBox.find("a[href*='" + hash + "']").click();
							}
						);
					},
					1000
				);
			}

		);

	}
);

