$(document).ready(function(){

	// fonction parallax
	function parallaxEffect() {
		// lorsque le curseur de la souris se déplace dans la zone BODY
		$("body").mousemove(function(elem){
			// on déplace pour chaque calque le fond d'image de droite à gauche selon le mouvement de la sours
			// c'est le coefficient multiplicateur différent d'un calque à l'autre qui rends cet effet de parallax
			$("#footer.parallax>div>div").css({ backgroundPosition: (elem.clientX * 0.25) + 'px bottom'});
			$("#footer.parallax>div").css({ backgroundPosition: (elem.clientX * 0.08) + 'px bottom'});
			$("#footer.parallax").css({ backgroundPosition: (elem.clientX * 0.03) + 'px bottom'});
		});
	}
	// variable regroupant les navigateurs IE antérieur à la version 7
	var old_ie = $.browser.msie && /MSIE\s(5\.5|6\.)/.test(navigator.userAgent);
	// au chargement de la page
	$(document).ready(function(){
		// on exclu les anciens navigateurs IE (pour cette fois seulement ;) )
		if ( !old_ie ) {
			parallaxEffect();
		}
	});

	$('#logo').hover(
		function(){
			$('.logo_hover').fadeIn('normal');
		},function(){
			$('.logo_hover').fadeOut('normal');
		}
	);

	$('#newsletter_1').submit(function() {
		$('#list_msg').hide().siblings('.loading').show();
		$('#newsletter_msg').fadeIn('normal');
		$(this).ajaxSubmit({
			target: '#list_msg', //ecrire le message
			success: function() { 
				$('#newsletter_msg .loading').hide().siblings('#list_msg').show();
    		} 
		});
		return false;
	});
	$('#newsletter_input').keypress(function() {
		$('#newsletter_msg').fadeOut('normal');
	});

	$(document).click(function(ev){
	  if(!$(ev.target).is('#bag_check') && !$(ev.target).parents('#bag_check').length && !$(ev.target).is('#bag_popin') && !$(ev.target).parents('#bag_popin').length) {
			$('#bag_popin').css('visibility','hidden');
			$("#bag_check").removeClass('on');
	  }
	});
	$("#bag_check").click(
		function () {
			if($('#bag_popin').css('visibility')=='hidden') {
				$(this).addClass('on');
				$('#bag_popin').css('visibility','visible');
			} else {
				$(this).removeClass('on');
				$('#bag_popin').css('visibility','hidden');
			}
		}
	);

	/*product_id_var=0;
	$('#products .foo .img').hover(
		function(){
			var ID=$(this).attr('id');
			if (product_id_var!=ID || !$('#controls').is(':visible')) {
				product_id_var=ID;
				$('#controls').css('top', 100).css('left',$(this).offset().left-20).fadeIn(300);
			}
			if(typeof(window['products_mouseout'])!="undefined") clearTimeout(products_mouseout);
		},function(){
			products_mouseout=setTimeout("$('#products #controls').hide();",300);
		}
	);
	$('#products #controls').hover(
		function(){
			if(typeof(window['products_mouseout'])!="undefined") clearTimeout(products_mouseout);
		},function(){
			products_mouseout=setTimeout("$('#products #controls').hide();",300);
		}
	);*/

	$('#search_input').focus(function() {
		if (this.value === this.defaultValue) this.value = ''; else this.select();
		$('#search_options').fadeIn('fast');
	});
	$('#search_input').click(function() { $('#search_options').fadeIn('fast'); });
	$('#search').mouseleave(function() { $('#search_options').fadeOut('fast'); });
	$('#search_input').blur(function() { if ($.trim(this.value) === '') this.value = (this.defaultValue ? this.defaultValue : ''); });

	$('#products_menu_link').hover(
		function() {
			$('#products_menu').css('visibility','visible');
			$("#products_menu_link").addClass('on');
			if(typeof(window['products_menu_mouseout'])!="undefined") clearTimeout(products_menu_mouseout);
		},function(ev){
			products_menu_mouseout=setTimeout("$('#products_menu').css('visibility','hidden'); $('#products_menu_link').removeClass('on');",300);
		}
	).siblings().mouseenter(function(){
		$('#products_menu').css('visibility','hidden'); $('#products_menu_link').removeClass('on');
	});
	$('#products_menu').hover(
		function() {
			if(typeof(window['products_menu_mouseout'])!="undefined") clearTimeout(products_menu_mouseout);
		},function(){
			products_menu_mouseout=setTimeout("$('#products_menu').css('visibility','hidden'); $('#products_menu_link').removeClass('on');",300);
		}
	);
});

function bag_change(quantity,cart_item_id,price)
{
	lien=base_url+site_lang+'/payment/cart/update_item/'+cart_item_id;
	$('#bag_loader').css({'display':'block'});
	$.ajax({
		   type: "POST",
		   url: ''+lien,
		   data: 'quantity='+quantity+'&price='+price,
		   success: function(msg){
			   var myObject = eval('(' + msg + ')');
				$('#bag_loader').css({'display':'none'});
				$('#price_item_'+cart_item_id).html(myObject.response.price_item);
				$('#nbre_articles').html(myObject.response.nbre);
				$('#total_price').html(myObject.response.total+' $CAD');
		   }
	});
}

function delete_item(cart_item_id)
{
	lien=base_url+site_lang+'/payment/cart/delete_ajax/'+cart_item_id;
	$('#bag_loader').css({'display':'block'});
	$.ajax({
		   type: "POST",
		   url: ''+lien,
		   success: function(msg){
			    var myObject = eval('(' + msg + ')');
				$('#bag_loader').css({'display':'none'});
				$('#nbre_articles').html(myObject.response.nbre);
				if(myObject.response.nbre==0){
					$('#total_price').html(no_item);
					$("#bag").removeClass('full');
					$(".ajax_checkout").css({'display':'none'});
				}
				else
					$('#total_price').html(myObject.response.total+' $CAD');
		   }
	});
}

function add_cart(product_id,quantity)
{
	lien=base_url+site_lang+'/payment/cart/add_item/'+product_id;	
	//$('#bag_loader').css({'display':'block'});
	$('#bag_outside_loader').show();
	$.ajax({
		   type: "POST",
		   url: ''+lien,
		   data: 'quantity='+quantity,
		   success: function(msg){
				 var myObject = eval('(' + msg + ')');
				//$('#bag_loader').css({'display':'none'});
				$('#nbre_articles').html(myObject.response.nbre);
				$("#bag").addClass('full');
				$('#total_price').html(myObject.response.total+' $CAD');
				cart_item_id=myObject.response.cart_item_id;
				lien1=base_url+site_lang+'/payment/cart/data_item/';	
				$.ajax({
					   type: "POST",
					   url: ''+lien1,
					   success: function(msg){
							$('#bag_outside_loader').hide();
							$('#item_cart').html(msg);
							$(".ajax_checkout").css({'display':''});
							$(function() { $(".jqtransform").jqTransform(); });
					   }
				});
		   }
	});
}
function add_compare(product_id)
{
	lien=base_url+site_lang+'/payment/product/add_compare/'+product_id;
	$.ajax({
		   type: "POST",
		   url: ''+lien,
		   success: function(msg){
			    if(msg){
			 	  	var myObject = eval('(' + msg + ')');
					html_item=$('#compare_block').html(); 
					html_item+='<div id="compare_item'+myObject.response.cart_compare_id+'" style="display:none;"><a class="delete" onclick="javascript:$(\'#compare_item'+myObject.response.cart_compare_id+'\').slideUp(1000); delete_compare('+myObject.response.cart_compare_id+');" href="javascript:void(0)">'+remove+'</a><a href="'+base_url+site_lang+'/payment/product/view_/'+product_id+'">'+myObject.response.title+'</a></div>';
					$('#no_items').css({'display':'none'});
					$('#comparator .btn_holder').css({'display':'block'});
					$('#compare_block').html(html_item);
					$('#compare_item'+myObject.response.cart_compare_id).slideDown(1000);
			   }
		   }
	});
}
function delete_compare(cart_compare_id)
{
	lien=base_url+site_lang+'/payment/product/delete_compare/'+cart_compare_id;
	$.ajax({
		   type: "POST",
		   url: ''+lien,
		   success: function(msg){
			   if(msg=='0'){
			   		$('#no_items').css({'display':'block'});
			   		$('#comparator .btn_holder').css({'display':'none'});
			   }
		   }
	});
}
function add_wishlist(product_id,type)
{
	if(type==1){
		lien=base_url+site_lang+'/payment/wish_list/add_ajax/'+product_id;
		//$('#bag_outside_loader').show();
		$.ajax({
			   type: "POST",
			   url: ''+lien,
			   success: function(msg){
				   $('#nbre_wishlist').html(msg);
				   //$('#bag_outside_loader').hide();
			   }
		});
	}else{
		lien=base_url+site_lang+'/payment/wish_list/add/'+product_id;
		javscript:window.location.replace(lien);
	}
}

function cart_change(quantity,cart_item_id,price)
{
	lien=base_url+site_lang+'/payment/cart/update_item/'+cart_item_id;
	$('#cart_loader_'+cart_item_id).css({'display':'block'});
	$.ajax({
		   type: "POST",
		   url: ''+lien,
		   data: 'quantity='+quantity+'&price='+price,
		   success: function(msg){
			   var myObject = eval('(' + msg + ')');
				$('#cart_loader_'+cart_item_id).css({'display':'none'});
				$('#price__'+cart_item_id).html(myObject.response.price_item);
				$('#total__').html(myObject.response.total+' $CAD');
				///bag TOP
				$('#price_item_'+cart_item_id).html(myObject.response.price_item);
				$('#nbre_articles').html(myObject.response.nbre);
				$('#total_price').html(myObject.response.total+' $CAD');
		   }
	});
}

function cart_delete_item(cart_item_id)
{
	lien=base_url+site_lang+'/payment/cart/delete_ajax/'+cart_item_id;
	$.ajax({
		   type: "POST",
		   url: ''+lien,
		   success: function(msg){
			    var myObject = eval('(' + msg + ')');
				if(myObject.response.nbre==0)
					$('#total__').html(no_item);
				else
					$('#total__').html(myObject.response.total+' $CAD');
				//TOP BAG
				$('#nbre_articles').html(myObject.response.nbre);
				if(myObject.response.nbre==0){
					$('#total_price').html(no_item);
					$("#bag").removeClass('full');
					$(".ajax_checkout").css({'display':'none'});
				}else
					$('#total_price').html(myObject.response.total+' $CAD');
				$('#item_'+cart_item_id).slideUp(1000);
		   }
	});
}

function wishlist_delete_item(wish_item_id)
{
	lien=base_url+site_lang+'/payment/wish_list/delete/'+wish_item_id;
	$.ajax({
		   type: "POST",
		   url: ''+lien,
		   success: function(msg){
			   $('#nbre_wishlist').html(msg);
		   }
	});
}

/*/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
								FUNCTIONS OF COOKIE 
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////*/
function EcrireCookie(nom, valeur)
{
	var argv=EcrireCookie.arguments;
	var argc=EcrireCookie.arguments.length;
	var expires=(argc > 2) ? argv[2] : null;
	var path=(argc > 3) ? argv[3] : null;
	var domain=(argc > 4) ? argv[4] : null;
	var secure=(argc > 5) ? argv[5] : false;
	document.cookie=nom+"="+escape(valeur)+
	((expires==null) ? "" : ("; expires="+expires.toGMTString()))+
	((path==null) ? "" : ("; path="+path))+
	((domain==null) ? "" : ("; domain="+domain))+
	((secure==true) ? "; secure" : "");
}
function getCookieVal(offset)
{
	var endstr=document.cookie.indexOf (";", offset);
	if (endstr==-1) endstr=document.cookie.length;
	return unescape(document.cookie.substring(offset, endstr));
}
function LireCookie(nom)
{
	var arg=nom+"=";
	var alen=arg.length;
	var clen=document.cookie.length;
	var i=0;
	while (i<clen)
	{
		var j=i+alen;
		if (document.cookie.substring(i, j)==arg) return getCookieVal(j);
		i=document.cookie.indexOf(" ",i)+1;
		if (i==0) break;
	}
	return null;
}

