/*
	** Fez-Soft
	** 2009
*/

$(function() {
				$("#viewer").removeClass("js-disabled");
			  //créer nouveau contenent
				$("<div>").attr("id", "container").css({ position:"absolute"}).width($(".wrapper").length * 170).height(170).appendTo("div#viewer");
				//ajouté l'images dans ce div
				$(".wrapper").each(function() {
					$(this).appendTo("div#container");
				});
				// la durée avec les secondes 1000 = 1s pour chaque image
				var duration = $(".wrapper").length * 1500;
				//resultat du vitess = la distance/temp
				var speed = (parseInt($("div#container").width()) + parseInt($("div#viewer").width())) / duration;
				//déinir la direction
				var direction = "rtl";
				//définir initial direction
				(direction == "rtl") ? $("div#container").css("left", $("div#viewer").width()).addClass("rtl") : $("div#container").css("left", 0 - $("div#container").width()).addClass("ltr") ;
				//animation function
				var animator = function(el, time, dir) {
					//si la direction est rtl
					if(dir == "rtl") {
					  //ajouter la class de direction
						el.removeClass("ltr").addClass("rtl");
						el.animate({ left:"-" + el.width() + "px" }, time, "linear", function() {				
							//restart la position du div
							$(this).css({ left:$("div#imageScroller").width(), right:"" });
							//restart animation
							animator($(this), duration, "rtl");
							($("div#controls").length > 0) ? $("div#controls").slideUp("slow").remove() : null ;
						});
					} else {
					  //la meme operation de rtl repeté pour ltr , mais avec contraire
						el.removeClass("rtl").addClass("ltr");
						el.animate({ left:$("div#viewer").width() + "px" }, time, "linear", function() {
							$(this).css({ left:0 - $("div#container").width() });
							animator($(this), duration, "ltr");
							($("div#controls").length > 0) ? $("div#controls").slideUp("slow").remove() : null ;
						});
					}
				}
				//commencer l'animation'
				animator($("div#container"), duration, direction);
				//faire pause a mouseover
				$("a.wrapper").live("mouseover", function() {
					//stop animation
					$("div#container").stop(true);
					//affichage duboutton de controlls
					($("div#controls").length == 0) ? $("<div>").attr("id", "controls").appendTo("div#outerContainer").css({ opacity:0.7 }).slideDown("slow") : null ;
					($("a#rtl").length == 0) ? $("<a>").attr({ id:"rtl", href:"#", title:"rtl" }).appendTo("#controls") : null ;
					($("a#ltr").length == 0) ? $("<a>").attr({ id:"ltr", href:"#", title:"ltr" }).appendTo("#controls") : null ;
					
					var title = $(this).attr("title");
					($("p#title").length == 0) ? $("<p>").attr("id", "title").text(title).appendTo("div#controls") : $("p#title").text(title) ;
				});
				
				//reien a mouseout
				$("a.wrapper").live("mouseout", function(e) {
				  
					//caché les bouttons de controlle
					(e.relatedTarget == null) ? null : (e.relatedTarget.id != "controls") ? $("div#controls").slideUp("slow").remove() : null ;
					var totalDistance = parseInt($("div#container").width()) + parseInt($("div#viewer").width());
					var distanceLeft = ($("div#container").hasClass("ltr")) ? totalDistance - (parseInt($("div#container").css("left")) + parseInt($("div#container").width())) : totalDistance - (parseInt($("div#viewer").width()) - (parseInt($("div#container").css("left")))) ;	
					var newDuration = distanceLeft / speed;
					animator($("div#container"), newDuration, $("div#container").attr("class"));
				});
				
				//changer la dirction de trajectoire
				$("#ltr").live("click", function() {
					$("div#container").stop(true);
					$("div#container").removeClass("rtl").addClass("ltr");
					var totalDistance = parseInt($("div#container").width()) + parseInt($("div#viewer").width());
					var distanceLeft = totalDistance - (parseInt($("div#container").css("left")) + parseInt($("div#container").width()));
					var newDuration = distanceLeft / speed;
					animator($("div#container"), newDuration, "ltr");
				});
				$("#rtl").live("click", function() {
					$("div#container").stop(true);
					$("div#container").removeClass("ltr").addClass("rtl");
					var totalDistance = parseInt($("div#container").width()) + parseInt($("div#viewer").width());
					var distanceLeft = totalDistance - (parseInt($("div#viewer").width()) - (parseInt($("div#container").css("left"))));
					var newDuration = distanceLeft / speed;
					animator($("div#container"), newDuration, "rtl");
				});
			});
// merci de votre lecture :)
