/* Author: www.pixelsmill.com
	
*/

$(document).ready(init);

function init(){
//	alert($("html").attr("class"));
	antiIE6();
	initAccueil();
	initUnivers();
	initBordures();
	initActus();
	initAjoutBordure();
	initLiens();
	
}


(function($){
	$.fn.typewriter = function(speed) {
		this.each(function() {
			var $ele = $(this), str = $ele.text(), htmlstr=$ele.html(), progress = 0;
			$ele.text('');
			var timer = setInterval(function() {
				progress++;
				if (progress < str.length){
					$ele.text(str.substring(0, progress) + (progress & 1 ? '_' : ''));
				} else {
					$ele.html(htmlstr + (progress & 4 ? '_' : ''));
	//				clearInterval(timer);
				}
			}, speed);
		});
		return this;
	};

	$.fn.nextCircular = function() {
		if (this.nextAll().length>0){
			return this.next();
		} else {
			return this.siblings().eq(0);
		}
	};
	
	$.fn.affichePhotoDiaporama= function(){
		var h=this.height()-425;		
		var t=7000;
		var tin=500;
		var tout=500;
		this.animate({marginTop: "-"+h+"px"}, 0 ).fadeIn(tin).animate({marginTop: "0px"}, t-tin-tout).fadeOut(tout,function(){
			$(this).nextCircular().affichePhotoDiaporama();
		});
		return this;
	}
}(jQuery));





function antiIE6(){
	if (!$("html.ie6").length) return;
	document.location="miseajour.html";
}

function initAccueil(){
	if (!$("body.accueil").length) return;
	initDiaporama();
	$("#bienvenue .texte p:last-child").typewriter(150);
}

function initDiaporama(){
	$("#photoComptoir li").hide();
	$("#photoComptoir li:eq(0)").affichePhotoDiaporama();
}

function initUnivers(){
	if (!$("body.univers").length) return;
	initUniversVignettes();
	initUniversRefComptoir();
	initLightBox();
	initUniversAjax();
}
function initUniversVignettes(){
	var largeur=0;
	$("#vignettesUnivers ul li").each(function(){
		largeur+=$(this).width()+10;
	});
	$("#vignettesUnivers ul").width(largeur);
	
	$("#vignettesUnivers").mousemove(function(e){
		var x = e.pageX;		
		var y = e.pageY;
		
		var offset = $("#vignettesUnivers").offset();
		var x0=offset.left;
		var y0=offset.top;

		var dx=x-x0;
		var dy=y-y0;
		
		if (dy>90) return;
		
		var p=dx/980;
		positionneUniversVignettes(p);
	});
	
	positionneUniversVignettes(0.05);
	var timer = setInterval(function() {
		animeUniversVignettes();
	}, 42);

}
function positionneUniversVignettes(p){
	var marge=200;
	var d0=0;
	var dmax=$("#vignettesUnivers ul").width()+10+2*marge-980;
	var cible=d0-p*dmax;

	$("#vignettesUnivers ul").data("cible", cible);
}
function animeUniversVignettes(){
	var pos = $("#vignettesUnivers ul").css("margin-left");
	pos=pos.substr(0,pos.length-2);
	
	var cible=$("#vignettesUnivers ul").data("cible");
	
	var delta=Math.abs(cible-pos);
	if (delta<3){
		pos=cible;
	}
		
	var consigne=(cible+4*pos)/5;
	$("#vignettesUnivers ul").css("margin-left", consigne);
}
function initUniversRefComptoir(){
	var ref=$("#vignettesUnivers ul li.on span").html();
	$("#comptoir span.code").html(ref);
	$("#comptoir h1").typewriter(150);
}	
function initUniversAjax(){
	$("#vignettesUnivers a").click(function(){
		var id=$(this).attr("rel");
		$("#ajax").html("").load("spip.php?page=inc-univers&id_article="+id,function(){
			initLightBox();
		});
		return false;
	});
}


function initBordures(){
	if (!$("body.bordures").length) return;
	initBorduresVignettes();
	initBorduresRef();
	initLightBox();
	initTypesBorduresAjax();
	initBorduresAjax()
}
function initBorduresVignettes(){
	var largeur=0;
	$("#bordures ul li").each(function(){
		largeur+=$(this).width()+10;
	});
	$("#bordures ul").width(largeur);
	
	if (largeur+2*200<=980){
		return;
	}
	
	$("#bordures ul").css("margin-left", 0);
	
	$("#bordures").mousemove(function(e){
		var x = e.pageX;		
		var y = e.pageY;
		
		var offset = $("#bordures").offset();
		var x0=offset.left;
		var y0=offset.top;

		var dx=x-x0;
		var dy=y-y0;
		
		var p=dx/980;
		positionneBorduresVignettes(p);
	});
	
	positionneBorduresVignettes(0.05);
	var timer = setInterval(function() {
		animeBorduresVignettes();
	}, 42);

}
function positionneBorduresVignettes(p){
	var marge=200;
	var d0=0;
	var dmax=$("#bordures ul").width()+10+2*marge-980;
	var cible=d0-p*dmax;

	$("#bordures ul").data("cible", cible);
}
function animeBorduresVignettes(){
	var pos = $("#bordures ul").css("margin-left");
	if (typeof(pos)=='undefined'){
		return;
	}
	pos=parseInt(pos.substr(0,pos.length-2));
	var cible=parseInt($("#bordures ul").data("cible"));
	
	var delta=Math.abs(cible-pos);
	if (delta<3){
		pos=cible;
	}
		
	var consigne=(cible+2*pos)/3;
	
	$("#bordures ul").css("margin-left", consigne);
}
function initBorduresRef(){
	$("#bordure h1").typewriter(150);
}	
function initBorduresPhotosComptoir(){
	$("#comptoir ul.photos li").hide();
	$("#comptoir ul.photos li").eq(0).fadeIn(1000);
	$("#comptoir .photos li.item1").addClass("on");
	
	$("#comptoir .autresPhotos a").click(function(){
		var index=1+$(this).parent("li").prevAll().length;
		
		$("#comptoir .photos li.on").fadeOut(500, function(){
			$(this).hide().removeClass("on");
			$("#comptoir .photos li").eq(index).addClass("on").fadeIn(500);
		});
		
		return false;
	});
}
function initTypesBorduresAjax(){
	$("#typesBordures a").click(function(){
		$("#typesBordures li").removeClass("on");
		$(this).parent().addClass("on");
		var id=$(this).attr("rel");
		$("#bordures").html("").load("spip.php?page=inc-bordures&id_groupe="+id,function(){
			$(this).hide().fadeIn(300);
			initBorduresVignettes();
			initBorduresAjax();
		});
		return false;
	});
}
function initBorduresAjax(){
	$("#bordures a").click(function(){
//		if ($("html.ie8").length) return true;
		$("#bordures li").removeClass("on");
		$(this).parent().addClass("on");
		var id=$(this).attr("rel");
		$("#bordure").html("").load("spip.php?page=inc-bordure&id_mot="+id,function(){
			$(this).hide().fadeIn(300, function(){
				initBorduresRef();
				initLightBox();
				initAjoutBordure();
			});
		});
		return false;
	});
	
}




function initLightBox(){
	if (!$('a.zoom').length) return;
	$('a.zoom').colorbox({transition:"none", width:"980", height:"90%", current:"{current} / {total}", loop: false});
	/*lightBox({
		fixedNavigation:true,
		overlayBgColor: '#000',
		overlayOpacity: 0.8,
		containerResizeSpeed: 500,
		txtImage: 'Photo',
		txtOf: 'sur'
	});/**/
}

function initActus(){
	if (!$("body.actus").length) return;
	initActusTitre();
	initActusMore();
	initActuParEmail();
}

function initActusTitre(){
	$("#actu h1").typewriter(50);
}
function initActusMore(){
	actusMore();
	$(".colActus a.plus").click(function(){
		actusMore();
		return false;
	});
}
function actusMore(){
	var nb=actusNbVisibles();
	nb+=9;
	if (nb>$(".listeActus li").length){
		nb=$(".listeActus li").length;
		$(".listeActus a.plus").hide();
	}
	for (var i=0; i<nb; i++){
		$(".listeActus li.item-"+(i+1)).show(150);
	}
}
function actusNbVisibles(){
	var i=0;
	while ($(".listeActus li.item-"+(i+1)).is(":visible")){
		i++;
	}
	return i;
}
function initAjoutBordure(){
	if (!$("body.bordures").length) return;
	
	afficheBorduresSelectionnees(1);
	afficheBorduresSelectionnees(2);
	
	$("#bordure a.choix1").click(function(){
		var id=$(this).attr("rel");
		setChoix(1,id);
		afficheBorduresSelectionnees(1);
		return false;
	});
	$("#bordure a.choix2").click(function(){
		var id=$(this).attr("rel");
		setChoix(2,id);
		afficheBorduresSelectionnees(2);
		return false;
	});
	$("#bordure a.choixSuppr1").click(function(){
		setChoix(1,"");
		afficheBorduresSelectionnees(1);
		return false;
	});
	$("#bordure a.choixSuppr2").click(function(){
		setChoix(2,"");
		afficheBorduresSelectionnees(2);
		return false;
	});
}

function afficheBorduresSelectionnees(rang){
	var id=getChoix(rang);
	if (id>0){
/*		$.get("spip.php", { page: "inc-photoBordureSm", id_mot: id }, function(data){
			$("#bordure .vignetteChoix"+rang+" span").html(data);
		});/**/
		$.ajax({
			type: "GET",
			url: "spip.php",
			data: "page=inc-photoBordureSm&id_mot="+id,
			success: function(data){
				$("#bordure .vignetteChoix"+rang).html(data).hide().show();
			},
			timeout: 1000,
			error: function(obj,str, exeption){
				alert("error !");
			}
		});/**/
		
//		$("#bordure .vignetteChoix"+rang+" span").load("spip.php?page=inc-photoBordureSm&id_mot="+id+"&"+Math.random(), function(){});
	} else {
		$("#bordure .vignetteChoix"+rang+" span").html("...");
	}	
}
function initActuParEmail(){
	$("#formulaire_envoyer").hide();
	$("#actu .socials a.item6").click(function(){
		$("#formulaire_envoyer").toggle(500);
		return false;
	});
}

function initLiens(){
	$("a[type='image/jpeg']").colorbox({transition:"none", width:"980", height:"90%", current:"{current} / {total}", loop: false});
	$("a[type='application/pdf']").attr("target","_blank");
	$("a[rel=external]").attr("target","_blank");
	$("a.spip_out").attr("target","_blank");
}
