//------------------------------------------
// Fonctions génériques
//------------------------------------------
var jz = {
		
	$ : function (id_el, tag)
	{
		return (typeof tag == "undefined") ? document.getElementById(id_el) : 
		(typeof id_el == "object") ? id_el.getElementsByTagName(tag) :
		document.getElementById(id_el).getElementsByTagName(tag);
	},
	
	addev : function (el, ev, fn)
	{
		return el.addevListener ? el.addevListener(ev, fn, false) :
		el.attachEvent ? el.attachEvent('on' + ev, fn) : el['on' + ev] = fn;
	},
	
	extend : function (target, ob)
	{
		for(var el in ob) {target[el] = ob[el];}
	},
	
	ev_target : function (ev) {return ev.target || ev.srcElement;},
	
	rollver : function (ev) //qqchose1.abc devient qqchose2.abc et vice-versa
	{
		var elsrc = jz.ev_target(ev);
		var sext = elsrc.src.substring((elsrc.src.length-4), (elsrc.src.length)).toString();
		var xnum = (elsrc.src.substring((elsrc.src.length-5), (elsrc.src.length-4)).toString() == "1") ? 2 : 1;
		elsrc.src = elsrc.src.substring(0, (elsrc.src.length-5)) + xnum + sext;
	},
	
	precharg : function (imgsource) //jz.precharg(["img/edit2.gif", "img/supp2.gif"]);
	{
		var imgprecharge = new Array();
		
		for(var i = 0; i< imgsource.length; i++)
		{
			imgprecharge[i] = new Image();
			imgprecharge[i].src = imgsource[i];
		}
	}
};
		
//------------------------------------------
// DOM chargé
//------------------------------------------
$(document).ready(function()
{	
	//------------------------------------------
	//Page complètement chargée
	//------------------------------------------
	$(window).bind("load", function()
	{
		//----------------------------------------------------------------------------
		// GERE le rollver des éléments via la propriété src
		//----------------------------------------------------------------------------
		var elmrol = jz.$(document.body, "img");
			
		for(var cp1 = 0, cl1 = elmrol.length; cp1 < cl1; cp1++)
		{
			if(elmrol[cp1].className.indexOf("nzrollver", 0) != -1)
			{
				jz.addev(elmrol[cp1], "mouseover", jz.rollver);
				jz.addev(elmrol[cp1], "mouseout", jz.rollver);
				elmrol[cp1].style.cursor = "pointer";
			}
		}
		
		//----------------------------------------------------------------------------
		// GERE l'effacement de l'input de la newsletter
		//----------------------------------------------------------------------------
		var el_input = {
			el : [
				".search_text",
				".newsletter_text"
			],
			txt : [
				"Recherche dans le catalogue:",
				"Votre adresse e-mail"
			]
		}
		for(var cp1 = 0, cl1 = el_input.el.length; cp1 < cl1; cp1++)
		{
			if($(el_input.el[cp1] + ":not(.noinfo)").get()[0])
			{					
				$(el_input.el[cp1] + ":not(.noinfo)").bind("focus", {input : el_input, cp : cp1}, function(ev)
				{
					var cp1 = ev.data.cp;
					if($(ev.data.input.el[cp1] + ":not(.noinfo)").attr("value") == ev.data.input.txt[cp1])
					{
						$(ev.data.input.el[cp1] + ":not(.noinfo)").attr("value", "");
					}
				});
				$(el_input.el[cp1] + ":not(.noinfo)").bind("blur", {input : el_input, cp : cp1}, function(ev)
				{
					var cp1 = ev.data.cp;
					if($(ev.data.input.el[cp1] + ":not(.noinfo)").attr("value") == "")
					{
						$(ev.data.input.el[cp1] + ":not(.noinfo)").attr("value", ev.data.input.txt[cp1]);
					}
				});
			}
		}
		
		//----------------------------------------------------------------------------
		// GERE l'apparition du bloc de connection/creation de compte
		//----------------------------------------------------------------------------
		if($("#identifier").get()[0])
		{
			$("#identifier").bind("click", function()
			{
				if(!$("#wrapper").get()[0]) //wrapper pas encore utilisé
				{
					//-------------------------------------------
					//création du wrapper
					//-------------------------------------------
					var el_wrapper = $('<div id="wrapper"></div>');
					$("body").append(el_wrapper);
					
					$("#wrapper").bind("click", function()
					{
						$("#block_identifier")
							.stop()
							.animate({
								'width' : '0px',
								'height' : '0px'
							}, 600);
						
						$("#wrapper")
							.stop()
							.animate({
								'opacity' : '0'
							}, 600, function() {
								$(this).remove("#wrapper");
							});
					});
					
					//-------------------------------------------
					//apparition du block de connection
					//-------------------------------------------
					$("#block_identifier")
						.stop()
						.css({
							'width' : '0px',
							'height' : '0px'
						})
						.animate({
							'width' : '748px',
							'height' : '222px'
						}, 600);
						
					//-------------------------------------------
					//apparition du wrapper
					//-------------------------------------------
					el_wrapper
						.stop()
						.css({
							'opacity' : '0',
							'display' : 'block'
						})
						.animate({
							'opacity' : '0.7'
						}, 600);
				}
			});
			$("#block_identifier").bind("click", function(ev)
			{
				ev.stopPropagation();
				return false;
			});
		}
		
		//----------------------------------------------------------------------------
		//Lien ayant la class "openvideo" -> shadowbox
		//----------------------------------------------------------------------------
		var vid = $('.openvideo');
		
		vid.each(function() {
			
			//----------------------------------------------------------------------------
			//récupère les informations
			//----------------------------------------------------------------------------
			var vimg = $(this).find("img");
			if(!vimg.get()[0]) return;
			$(this)
				.bind("click", function() {return false;})
				.css("cursor", "normal");
			
			var pos_vid = vimg.findPos();
			var info = {
				"width" : parseInt(vimg.attr("width")),
				"height" : parseInt(vimg.attr("height")),
				"real_width" : parseInt(vimg.attr("width")),
				"real_height" : parseInt($(this).attr("height")),
				"top" : parseInt(pos_vid.y),
				"left" : parseInt(pos_vid.x),
				"link" : $(this).attr("href")
			};
			//----------------------------------------------------------------------------
			//récupère les paramètres supplémentaires (facultatif)
			//----------------------------------------------------------------------------
			if($(this).attr("title") != "")
			{
				var fval = $(this).attr("title");
				if(fval.indexOf("x", 0) != -1)
				{
					fval = fval.split("x");
					if(fval.length == 2)
					{
						info.real_width = parseInt(fval[0]);
						if(info.real_width < 2) info.real_width = info.width;
						
						info.real_height = parseInt(fval[1]);
						if(info.real_height < 2) info.real_height = info.height;
					}
				}
			}
			//----------------------------------------------------------------------------
			//création du lien qui vient par dessus l'object pour ouvrir shadowbox
			//----------------------------------------------------------------------------
			$("body").append(
				
				//déclancheur principal
				$('<a href="' + info.link + '" rel="shadowbox;width=' + info.real_width + ';height=' + info.real_height + '"></a>')
					.attr("class", (info.link.indexOf(".swf", 0) == -1) ? "vid_trigger" : "vid_trigger vid_nofond")
					.css({
						"height" : info.height + "px",
						"width" : info.width + "px",
						"top" : parseInt(info.top) + "px",
						"left" : parseInt(info.left) + "px"
					}),
				
				//button play
				$('<a href="' + info.link + '" rel="shadowbox;width=' + info.real_width + ';height=' + info.real_height + '"></a>')
					.attr("class", "vid_play")
					.css({
						"top" : parseInt(info.top + (info.height / 2) - 31 + 5) + "px",
						"left" : parseInt(info.left + (info.width / 2) - 31) + "px"
					})
			);
		
		});
		//----------------------------------------------------------------------------
		//Initialise shadowbox
		//----------------------------------------------------------------------------
		Shadowbox.init({
			overlayOpacity: 0.8,
			continuous:true,
			counterType:'skip',
			autoplayMovies: true
			//flashParams: {flashVars: 'image=/medias/images/block_commentmarche_example2.jpg'}
			//http://gdumas.developpez.com/temp/shadowbox/
		});
	});
});




















