﻿$(document).ready(function(){
	//alert($("body").innerWidth());
	//alert($("#main").innerWidth());
	showSlimbox();
	cancelLink();
	showLists();
	isVide();
	setInterval("animationPic()", 6000);
	setInterval("animationPicDecathlon()", 6000);
	animationPastille();
	initPopins();
});
function showSlimbox(){
	$(".slimbox ul li .title").unbind();
	$(".slimbox ul li .title").bind("click",function(){
		if(!$(this).parent().hasClass("active")){
			$(".slimbox ul li .title").next().slideUp("slow");
			$(".slimbox ul li").removeClass();
			$(this).parent().addClass("active");
			$(this).next().slideDown("slow");
		}
	});

}
function showLists(){
	$("#main #menu .liste ul.etat1>li>a").bind("click",function(){
		if($(this).parent().hasClass("full")){
			$("#main #menu .liste ul.etat1>li").each(function(i,obj){
				if($(obj).hasClass("full")){
					$(obj).find("ul").slideUp("slow");
					$(obj).removeClass();
					$(obj).addClass("on");
				}
				if($(obj).hasClass("on")){
					$(obj).find("ul").slideUp("slow");
					$(obj).removeClass();
					$(obj).addClass("full");
				}
				//return false;
			});
			
			
			
			$(this).next().slideDown("slow");
			$(this).parent().removeClass();
			$(this).parent().addClass("on");
			return false;
		}else if($(this).parent().hasClass("on")){
			$(this).next().slideUp("slow");
			$(this).parent().removeClass();
			$(this).parent().addClass("full");
			return false;
		}
		
	});
	

}
function isVide(){
	$("#main #content .contact .partContact div.inputs input,#main #content .contact .partContact div.inputs textarea").unbind();
	$("#main #content .contact .partContact div.inputs input,#main #content .contact .partContact div.inputs textarea").bind("click",function(){
		if($(this).attr("id")=="nom" && $(this).val()=="Nom" || $(this).attr("id")=="prenom" && $(this).val()=="Prénom" || $(this).attr("id")=="tel" && $(this).val()=="Téléphone" || $(this).attr("id")=="email" && $(this).val()=="Email" || $(this).attr("id")=="ville" && $(this).val()=="Ville" || $(this).attr("id")=="societe" && $(this).val()=="Société" || $(this).attr("id")=="message" && $(this).val()=="Message"){
			$(this).val("");
			if($(this).attr("id")=="message"){
				$(this).css("height","370px");
			}
		}
	}).bind("blur",function(){
		if($(this).attr("id")=="nom" && $(this).attr("value")==""){
			$(this).val("Nom");
		}
		if($(this).attr("id")=="prenom" && $(this).attr("value")==""){
			$(this).val("Prénom");
		}
		if($(this).attr("id")=="tel" && $(this).attr("value")==""){
			$(this).val("Téléphone");
		}
		if($(this).attr("id")=="email" && $(this).attr("value")==""){
			$(this).val("Email");
		}
		if($(this).attr("id")=="ville" && $(this).attr("value")==""){
			$(this).val("Ville");
		}
		if($(this).attr("id")=="societe" && $(this).attr("value")==""){
			$(this).val("Société");
		}
		if($(this).attr("id")=="message" && $(this).attr("value")==""){
			$(this).val("Message");
			if($(this).attr("id")=="message"){
				$(this).css("height","186px");
			}
		}
	
	});
	

}
function cancelLink(){
	$("#main #menu .liste ul.etat1 li.on ul li.on a,#main #menu .liste ul.etat1 li.full ul li.on a,#main #menu .liste ul.etat1 li.on>a").unbind();
	$("#main #menu .liste ul.etat1 li.on ul li.on a,#main #menu .liste ul.etat1 li.full ul li.on a,#main #menu .liste ul.etat1 li.on>a").bind("click",function(){
		return false;
	});

}
var j=0;
function animationPic(){
	var taille=0;
	$(".animation img").each(function(i,obj){
		taille=i;
	});
	if(taille==j){
		$(".animation img").fadeOut(1000);
			$(".animation img").each(function(k,objk){
				if(k==0){
					$(objk).fadeIn(5000);
				}
			});
		j=-1;
	}else{
		$(".animation img").each(function(i,obj){
			if(i==j && i!=taille){
				$(obj).fadeOut(1000);
				$(obj).next().fadeIn(5000);
			}
		});
	}
	j++;
	//alert(taille);

}
var jj=0;
function animationPicDecathlon(){
	var taille=0;
	$("#decathlon img").each(function(i,obj){
		taille=i;
	});
	if(taille==jj){
		$("#decathlon img").fadeOut(1000);
			$("#decathlon img").each(function(k,objk){
				if(k==0){
					$(objk).fadeIn(5000);
				}
			});
		jj=-1;
	}else{
		$("#decathlon img").each(function(i,obj){
			if(i==jj && i!=taille){
				$(obj).fadeOut(1000);
				$(obj).next().fadeIn(5000);
			}
		});
	}
	jj++;
	//alert(taille);

}
function animationPastille(){
	$(".pastille").unbind();
	$(".pastille").bind("click",function(){
		if(!$(this).hasClass("on")){
			$("#pastille").animate({width:"100%"}, 1000 );
			$("#pastille div").fadeIn(3000);
			$(this).addClass("on");
		}else{
			$("#pastille").animate({width:"0%"}, 1000 );
			$("#pastille div").fadeOut(500);
			$(this).removeClass("on");
		}
		
		return false;
	});
}



/********************************************************/
// Init des popins
/********************************************************/
function initPopins(){
	var dim = getPageSize();
	$('.linkPopin').bind('click',function(){
		var _this = this;
		loadPopin($(_this).attr('rel'))
		$.ajax({
			type: "GET",
			url: $(_this).attr('href'),
			dataType: "html",
			success: function(data){
				try{
					showPopin(data,$(_this).attr('rel'));
				}catch(e){}
			}
		});
		return false;
	});

}
function resizeOverlayForPopin(){
	$(window).bind('resize',function(){
		resizaOverlay();
	});
	
	$(window).bind('scroll',function(){
		resizaOverlay();
	});	
}	

function resizaOverlay(cond){
	var overlay = $('#popinOverlay');
	var scroll = getPageScroll();
	//alert(hi);
	if(cond){
		overlay.css({
			opacity: 0.7,
			display: 'block'
		}).fadeIn('slow',function(){
			var hi = getHeight() + scroll[1];
			overlay.height(hi);
		});
	}else{
		var hi = getHeight() + scroll[1];
		overlay.height(hi);
	}
}

/********************************************************/
// Affichage des popins
/********************************************************/
function showPopin(data,id){
	//alert("---->"+data);
	$('#popinTemp').html(data);
	$('#popinTemp').removeAttr('style');
	$('#popinTemp').show();
	setPopinPosition(id);
	//setEventSelectLayers();
	
	
	if($("#popinOverlay").length){
		$("#popinOverlay").unbind();
		$("#popinOverlay").bind('click',function(){	
				$("#popinOverlay").fadeOut();
				$("#popinTemp").fadeOut();		 
		 });		
	}
}
function getHeight(){
	if(window.innerHeight || window.innerWidth)
		return window.innerHeight ;
	return document.documentElement.clientHeight ;
}

function refreshPositionPopin(popin){
	

	$(window).bind('resize',function(){
		var dim = getPageSize();
			popin.css({
			left: (dim[2]-popin.width())/2
		})
	});

	
}

function setPopinPosition(id){
	var dim = getPageSize();
	var scroll = getPageScroll();
	var overlay = $('#popinOverlay');
	var popin = $('#'+id);
	resizaOverlay(1);
	popin.css({
		top: scroll[1] + (dim[3]/4),
		left: (dim[2]-$(popin).width())/2,
		opacity: 1,
		display: 'block'
	}).fadeIn();
	refreshPositionPopin(popin);
	
	popin.find('p.close a').bind('click', function(){
		overlay.fadeOut();
		popin.fadeOut();		
		return false;
	});	
}

function loadPopin(id){
	if(!$('#popinOverlay').length)
		$('#main').before('<div id="popinOverlay"></div>');
			
	if(!$('#popinTemp').length)
		$('#main').before('<div id="popinTemp"></div>');
	$('#popinTemp').html("<div id='"+id+"' >Loading..</div>");
	setPopinPosition("popinTemp");			
}

function setWidthForLayers(){
	$('#onglets > li').each(function(){
		var resultshearch = $('.resultSearch > div',$(this))
		nbrItems = $('ul',resultshearch).length;
		var wiFinale = Math.ceil((nbrItems * 135.3) + ( nbrItems * 14.85));
		resultshearch.width(wiFinale);
	});
}

/*************************************************/
// Fonctions générales
/*************************************************/

// Renvoie les dimensions de la page
function getPageSize() {
	var xScroll, yScroll;
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}
	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}
	
	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight);
	return arrayPageSize;
};

function getPageScroll() {
	var xScroll, yScroll;
	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
		xScroll = self.pageXOffset;
	} else if (document.documentElement && document.documentElement.scrollTop) {	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
		xScroll = document.documentElement.scrollLeft;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
		xScroll = document.body.scrollLeft;	
	}
	arrayPageScroll = new Array(xScroll,yScroll);
	return arrayPageScroll;
}

