AnimDone = true; 
SlideDone = true; 
tailletotale = 0;
Nb_li = 0;

function Anim() { 	AnimDone = true;}
function Slide() {SlideDone = true;}
/*
* Défilement en cliquant sur le bouton de gauche
*/
function defil_g() {
$("#slide>.produit").each(function() {
AnimDone= false;
	if(Nb_li == 4) {
		if(parseInt($(this).css('left')) == 363) {
			$(this).remove();
		}
		if(parseInt($(this).css('left')) == 242) {
			$(this).clone().insertBefore('#slide>.produit:first').css('left','-121px');
		}
	 } else {
   if(parseInt($(this).css('left')) >= tailletotale - 242) {
      $(this).clone().insertBefore('#slide>.produit:first').css('left','-121px');
      $(this).remove();
		}
	}
var left = parseInt($(this).css('left'));  
       $(this).animate({ 
        left: 121+left+'px'
      }, 500 );
    });
	 window.setTimeout(function() {Anim()},1200);
	
}

/*
* Défilement en cliquant sur le bouton de droite
*/
function defil_d() {
//if (defilStopD) return;
AnimDone = false;
$("#slide>.produit").each(function() {
	if(Nb_li == 4) {
		if(parseInt($(this).css('left')) == -121) {
			$(this).remove();
		}
		if(parseInt($(this).css('left')) == 0) {
			$(this).clone().insertAfter('#slide>.produit:last').css('left','363px');
		}
	 } else {
      if(parseInt($(this).css('left')) <= -121) {
      $(this).clone().insertAfter('#slide>.produit:last').css('left',tailletotale - 242+'px');
      $(this).remove();
       }
	  }
var left = parseInt($(this).css('left'));
      $(this).animate({ 
        left: left-121+'px'
      }, 500 );
     
    });
	window.setTimeout(function() {Anim()},1200);
}   


$(document).ready(function() {
if($('#vignette2 > img').length == 0) {$("#next").hide(); }
  $("#galerie").show();
/*
* Placement de chaque miniature 
*/
$("#galerie").find(".produit").each(function(i) {
	Nb_li = i+1;
});
$("#galerie").find(".produit").each(function(i) {
		var left = tailletotale - 121;
		if(Nb_li == 4) {
			var left = tailletotale;
			tailletotale = tailletotale + 121;
			$(this).css('left',left+'px');
		} else if(Nb_li <= 3) {
			var left = tailletotale;
			tailletotale = tailletotale + 121;
			$(this).css('left',left+'px');
		} else {
		tailletotale = tailletotale + 121;
		$(this).css('left',left+'px');
		}
});

if(Nb_li == 4) {
	$('#slide>.produit:last').clone().insertBefore('#slide>.produit:first').css('left',-121+'px');
} 
if(Nb_li > 3) {
	$('#btn_prev').show();
	$('#btn_next').show();
	}

/*
* Clic sur la fleche de gauche
* On teste si l'anim est finie, si non on teste jusqu'à ce qu'elle se termine, si oui on teste la direction de l'anim
* si la direction est l'inverse de l'anim courante on arrete l'anim courante et on repart dans l'autre sens
* si non on ne fait rien (permet de désactiver le bouton pour ne pas lancer 2 fois l'anim dans le meme sens)                        
*/
$("#btn_prev").click(function clickG(){
if(!window.AnimDone) { 
  window.setTimeout(function() {clickG()},10);
  } else {  
	defil_g();
  }
});

$("#btn_next").click(function ClickD(){
  if(!window.AnimDone) { 
  window.setTimeout(function() {ClickD()},10);
  } else {  
	defil_d();
	}      
});
$("#vignette01").live('click', function () {
    $("#photo01").siblings('.photo').fadeOut("slow");
    $("#photo01").fadeIn('slow');
});
$("#vignette02").live('click', function () {
    $("#photo02").siblings('.photo').fadeOut("slow");
    $("#photo02").fadeIn('slow');
});
$("#vignette03").live('click', function () {
    $("#photo03").siblings('.photo').fadeOut("slow");
    $("#photo03").fadeIn('slow');
});
$("#vignette04").live('click', function () {
    $("#photo04").siblings('.photo').fadeOut("slow");
    $("#photo04").fadeIn('slow');
});
$("#vignette05").live('click', function () {
	$('#vignette1').hide();
    $("#photo05").siblings('.photo').fadeOut("slow");
    $("#photo05").fadeIn('slow');
});
$("#vignette06").live('click', function () {
	$('#vignette1').hide();
    $("#photo06").siblings('.photo').fadeOut("slow");
    $("#photo06").fadeIn('slow',function() { $('#vignette1').hide();});
});
$("#vignette07").live('click', function () {
    $('#vignette1').hide();
	$("#photo07").siblings('.photo').fadeOut("slow");
    $("#photo07").fadeIn('slow');
});

$("#vignette08").live('click', function () {
    $('#vignette1').hide();
	$("#photo08").siblings('.photo').fadeOut("slow");
    $("#photo08").fadeIn('slow');
});

$('#next').live('click', function () {
	if($('#vignette2 > img').length > 0) {
		if(window.SlideDone) { 
			SlideDone = false;
			$(this).attr('id','prev');
			$(this).attr('src','templates/defaut/images/arrow_prev.gif');
			$(".listevignette").each(function() {
				var left = parseInt($(this).css('left'));
			$(this).animate({ 
				left: left-139+'px'
			}, 500,'',function () { SlideDone = true});
			});
		}
	}
});
$('#prev').live('click', function () {
		if(window.SlideDone) { 
			SlideDone = false;
			$(this).attr('id','next');
			$(this).attr('src','templates/defaut/images/arrow_next.gif');
			$(".listevignette").each(function() {
				var left = parseInt($(this).css('left'));
			$(this).animate({ 
				left: left+139+'px'
			}, 500,'',function () { SlideDone = true});
			});
		}
	});
    
    $("#moreinfo_a").simpletooltip({ showEffect: "fadeIn", hideEffect: "fadeOut" });

});



/*
 * jQuery Simple Tooltip 0.9.1
 * Copyright (c) 2009 Pierre Bertet (pierrebertet.net)
 * Licensed under the MIT (MIT-LICENSE.txt)
 *
*/
;(function($){
	// simpletooltip
	$.fn.simpletooltip = function(settings) {
		
		var options = $.extend({
			hideOnLeave: true,
			margin: 5,
			showEffect: false,
			hideEffect: false,
			click: false,
			hideDelay: 0,
			showDelay: .1,
			showCallback: function(){},
			hideCallback: function(){},
			customTooltip: false,
			customTooltipCache: true
		}, settings);
		
		this.each(function () {
			
			// Get and hide tooltip
			if (! $.isFunction(options.customTooltip)) {
				$(this).data("$tooltip", getTooltip(this).hide());
			}
			
			if (options.click) {
				$(this).bind("click", {"options": options, "target": this}, openTooltip);
			}
			else {
				var tipTimeOut;
				
				$(this)
				// On mouseenter, init delay
				.bind("mouseenter", {"options": options, "target": this}, function(e) {
					var mouseEvent = e;
					
					tipTimeOut = window.setTimeout(function() {
						openTooltip(mouseEvent);
					}, (options.showDelay * 1000));
				})
				// On mouseleave, reset delay
				.bind("mouseleave", function() {
					window.clearTimeout(tipTimeOut);
				});
			}
			
		});
		
		return this;
	};
	
	function getTooltip(target) {
		
		// Anchor only
		var currentHrefMatch = $(target).attr("href").match(/#.+/);
		if (!!currentHrefMatch){
			var $tooltip = $(currentHrefMatch[0]);
		}
		
		return $tooltip;
	};
	
	function initTooltip($tooltip) {
		
		$tooltip
		
		// Append to body
		.appendTo(document.body)
		
		// Store dimensions
		.data("width", $tooltip.outerWidth())
		.data("height", $tooltip.outerHeight())
		
		// Set CSS
		.css({"position": "absolute", "zIndex": "9998", "display": "none"})
		
		// Close tooltip btn
		.find("a[rel=close]").click(function (e) {
			e.preventDefault();
			$tooltip.trigger("hide");
		}).end()
		
		// Init ok.
		.data("init", true);
	};
	
	function openTooltip(e){
		
		if (e.type == "click") {
			e.preventDefault();
		}
		
		var opts = e.data.options;
		
		var $target = $(e.data.target);
		
		// Custom tooltip
		if (!opts.customTooltipCache && $target.data("$tooltip")) {
			$target.data("$tooltip").remove();
			$target.data("$tooltip", false);
		}
		
		if (!$target.data("$tooltip")) {
			$target.data("$tooltip", $(opts.customTooltip($target.get(0))));
		}
		
		var $tooltip = $target.data("$tooltip");
		
		if (!$tooltip.data("init")) {
			initTooltip($tooltip);
		}
		
		var winWidth = $(window).width();
		var winHeight = $(window).height();
		var winOffsetY = $(window).scrollTop();
		var winOffsetX = $(window).scrollLeft();
		
		// Remove show / hide triggers
		$tooltip.unbind("show").unbind("hide");
		
		// Show
		if (opts.showEffect && ( opts.showEffect.match(/^fadeIn|slideDown|show$/) ) ) {
			$tooltip.bind("show", function(){
				$tooltip[opts.showEffect](200);
				opts.showCallback($target[0], this);
			});
		}
		else {
			$tooltip.bind("show", function(){
				$tooltip.show();
				opts.showCallback($target[0], this);
			});
		}
		
		// Hide
		if (opts.hideEffect && ( opts.hideEffect.match(/^fadeOut|slideUp|hide$/) ) ) {
			$tooltip.bind("hide", function(){
				opts.hideCallback($target[0], this);
				$tooltip[opts.hideEffect](200);
			});
		}
		else {
			$tooltip.bind("hide", function(){
				opts.hideCallback($target[0], this);
				$tooltip.hide();
			});
		}
		
		// Initial tooltip position
		var tooltipPosX = e.pageX - ($tooltip.data("width")/2);
		var tooltipPosY = e.pageY - ($tooltip.data("height")/2);
		
		// Replace tooltip position
		if (tooltipPosX < winOffsetX + opts.margin) { // Left
			tooltipPosX = winOffsetX + opts.margin;
		} else if (tooltipPosX + $tooltip.data("width") > (winOffsetX + winWidth - opts.margin)) { // Right
			tooltipPosX = winOffsetX + winWidth - $tooltip.data("width") - opts.margin;
		}
		
		if (tooltipPosY < winOffsetY + opts.margin) { // Top
			tooltipPosY = winOffsetY + opts.margin;
		} else if (tooltipPosY + $tooltip.data("height") > (winOffsetY + winHeight - opts.margin)) { // Bottom
			tooltipPosY = winOffsetY + winHeight - $tooltip.data("height") - opts.margin;
		}
		
		// Delay
		if (opts.hideDelay > 0 && opts.hideOnLeave) {
			var timer;
			$tooltip.hover(
				function(){
					window.clearTimeout(timer);
				},
				function(){
					timer = window.setTimeout(function(){
						$tooltip.trigger("hide").unbind("mouseenter, mouseleave");
					}, opts.hideDelay * 1000);
				}
			);
		}
		
		// No delay
		else if (opts.hideOnLeave){
			$tooltip.bind("mouseleave", function(){
				$tooltip.trigger("hide").unbind("mouseleave");
			});
		}
		
		// Apply CSS and show
		$tooltip
			.css({"left": tooltipPosX + "px", "top": tooltipPosY + "px"})
			.trigger("show");
		
	};
})(jQuery);