
var token = 1 ;
var AjaxWorking = 0;
var AjaxLoadBlock = '';
var AjaxReady = '';

$(document).ready(function(){
	
	var ie55 = (navigator.appName == "Microsoft Internet Explorer" && parseInt(navigator.appVersion) == 4 && navigator.appVersion.indexOf("MSIE 5.5") != -1);
	var ie6 = (navigator.appName == "Microsoft Internet Explorer" && parseInt(navigator.appVersion) == 4 && navigator.appVersion.indexOf("MSIE 6.0") != -1);

	$('#blockGlobal #blockMenu #menu-x').each(function(){
		var menuEl = $(this);
		menuEl.find('ul.under').each(function(){
			if( !$(this).parent().hasClass('activ') ){
				$(this).hover(
					function(){ $(this).parent().addClass('activ'); },
					function(){ $(this).parent().removeClass('activ'); }
				);
			}
		});
		if (jQuery.browser.msie && (ie55 || ie6)) {
			menuEl.find('li.sub').hover(
				function(){ $(this).find('ul.under').css('display','block') },
				function(){ $(this).find('ul.under').css('display','none') }
			);
		}
	});

	//slide show left side with brand logos	
	$("#diapoBrands").simpleSlideShow();
	
	//target _blank
	$("a.newWindow").bind('click',function(){window.open(this.href,'_blank');return false;});
	
	//mails
	$("a.liame").each(function()
	{
		content = $(this).html();
		content = content.replace('|','@');
		$(this).html(content);
		$(this).attr("href","mailto:"+content);
	});

	//input recherche
	$("#qIntraPage").bind("focus",function()
	{
		if($(this).val() == "Votre recherche...")
			$(this).val("");
	}
	);

	if(typeof($.fn.blink) != "undefined")
		$("#livraisonGratuite>a>span").blink({delay:700});

	$("a.keepReferer").each(function()
	{
		elem = $(this);
		elem.attr({"href":elem.attr("href")+"?referer="+encodeURIComponent(window.location)});
	});

	// ajoute les smoothScroll automatiquement
	constructSmoothScrollLink();

	if(AjaxReady){ // retrigger la construction des smoothscroll après une requete ajax
		AjaxReady.click(function(){
			constructSmoothScrollLink();
		});
	}
});


function smoothScroll(destinationY,callbackFunction,timing){
	
	if(timing == undefined || timing == '') timing = 550 ;

	var MaxScroll = window.innerHeight || document.documentElement.clientHeight;
	MaxScroll = $('body').height() - MaxScroll;
	if(MaxScroll > 1 && destinationY > MaxScroll) destinationY = MaxScroll ;
	
	if(destinationY >= 1){
		$('html,body').animate({
			scrollTop: destinationY
		},timing,'easeOutExpo',function(){
			if( $.isFunction(callbackFunction) ) callbackFunction() ;
		});
	}else{
		if( $.isFunction(callbackFunction) ) callbackFunction() ;
	}
	return false;
}


function constructSmoothScrollLink(){

	$('a.jsSmoothScroll[href^=#]').each(function(){
		var linkEl = $(this);
		var destination = linkEl.attr('href');
		if( $(destination).length >= 1){
			linkEl.unbind('click').click(function(){
				smoothScroll($(destination).offset().top ,function(){
					window.location.hash = destination;
				},700);
				return false;
			});
		}
	});
}

function slideSwitch(id,linkOnImage) {
	var $active = $('#'+id+' IMG.active');
	if ( $active.length == 0 ) $active = $('#'+id+' IMG:last');

	if(linkOnImage)
		$next = $active.parents("a").next().children("img");
	else
		$next = $active.next();

	if($next.length == 0)
		$next = $('#'+id+' IMG:first');

	$active.addClass('last-active');

	$next.css({opacity: 0.0})
		.addClass('active')
		.animate({opacity: 1.0}, 1000, function() {
				$active.removeClass('active last-active');
				$("a.zoomImage_"+id).attr("href",$next.parents("a").attr("href"));
				});
}


(function($){

 $.fn.simpleSlideShow = function(options) {
 var opts = $.extend({},$.fn.simpleSlideShow.defaults,options);
 var imgs;
 var $this = $(this);
 return $this.each(function()
	 {
	 imgs = $(this).find("img");
	 id = $this.attr("id");
	 if(imgs.size()>1)
	 {
	 	$("a.zoomImage_"+id).attr("href",$(this).find("img:last").parents("a").attr("href"));
	 	setInterval("slideSwitch('"+id+"',"+opts.linkOnImage+")",opts.timeout);
	 }
	 else
		$("a.zoomImage_"+id).attr("href",imgs.parents("a").attr("href"));
	 });
 }
 $.fn.simpleSlideShow.defaults = {
  linkOnImage: false,
  timeout: 3000
 }
 }
 )(jQuery);

