function showOverlay(){
	// hide selects & textareas
	//hideObjects();
	document_top = $(document).scrollTop();
	document_height = $(document).height();
	window_width = $(window).width();
	window_height = $(window).height();
	//$('#messageOverlay').css({'height': document_height, 'width': window_width});
	//$('#messageOverlay').fadeIn();
	$('#messageContainer').fadeIn();
	return false;
};
function hideOverlay() {
	$('#messageContainer').fadeOut(function() {
		$('#messageContainer').html('');
	});
}

var moncompte_lock = 0;

var obj = 'product_image_';
var im = 1;
function switchImage(direction) {
	var imgCount = $('#visuel').find('.product_large').length;
	$('#' + obj + im).removeClass('active').hide();
	if(direction == 'n') {
		if(im < imgCount) {
			im++;
		}	else {
			im = 1;
		}
	} else {
		if(im > 1) {
			im--;
		}	else {
			im = imgCount;
		}
	}
	$('#' + obj + im).addClass('active').show();
	return true;
};

// usage: log('inside coolFunc', this, arguments);
// paulirish.com/2009/log-a-lightweight-wrapper-for-consolelog/
window.log = function(){
  log.history = log.history || [];   // store logs to an array for reference
  log.history.push(arguments);
  if(this.console) {
   arguments.callee = arguments.callee.caller;
   var newarr = [].slice.call(arguments);
   (typeof console.log === 'object' ? log.apply.call(console.log, console, newarr) : console.log.apply(console, newarr));
  }
};
// make it safe to use console.log always
(function(b){function c(){}for(var d="assert,count,debug,dir,dirxml,error,exception,group,groupCollapsed,groupEnd,info,log,timeStamp,profile,profileEnd,time,timeEnd,trace,warn".split(","),a;a=d.pop();){b[a]=b[a]||c}})((function(){try
{console.log();return window.console;}catch(err){return window.console={};}})());


function activeForm(form){
	/*elm = form.getElementsByTagName('INPUT');
	for(i=0; i<elm.length; i++){
		if( (elm[i].type == "text") || (elm[i].type == "password") ) {
			elm[i].onfocus=function(){ this.style.borderColor = '#AD6E4D'; };
			elm[i].onblur=function(){ this.style.borderColor = '#1F0C06'; };
		}
	};
	elm = form.getElementsByTagName('SELECT');
	for(i=0; i<elm.length; i++){
		elm[i].onfocus=function(){ this.style.borderColor = '#AD6E4D'; };
		elm[i].onblur=function(){ this.style.borderColor = '#1F0C06'; };
	};
	elm = form.getElementsByTagName('TEXTAREA');
	for(i=0; i<elm.length; i++){
		elm[i].onfocus=function(){ this.style.borderColor = '#AD6E4D'; };
		elm[i].onblur=function(){ this.style.borderColor = '#1F0C06'; };
	};*/
};
function showCBox(){
    arrayPageSize = document.getPageSize();
    Element.setHeight('messageOverlay', arrayPageSize[1]);
    arrayPageScroll = document.getPageScroll();
    messageTop = arrayPageScroll[1] + (arrayPageSize[3] / 4);
    Element.setTop('messageContainer', messageTop);
    new Effect.Appear('messageOverlay', { duration: 0.2, from: 0.0, to: 0.35 });
    new Effect.Appear('messageContainer', { duration: 0.2, from: 0.0, to: 1.0 });
    return false;
};
function hideCBox(){
	$("#messageContainer").hide();
};
function cAlert(msg, compl){
	content = '<table class="calert" cellpadding="0" cellspacing="0">'+
			'<tr><td class="close"><a href="javascript://" onclick="return hideCBox();"></a></td></tr>'+
			'<tr><td class="data">'+msg+'</td></tr>'+
			'<tr><td class="actions"><a href="javascript://" onclick="return hideCBox();"></a></td></tr>'+
	'</table>';
	
	var top = 0;
	if(window.scrollY) {
		top = window.scrollY;
	} else if(document.documentElement.scrollTop) {
		top = document.documentElement.scrollTop;
	}
	top+=150;
	
	$('#messageContainer').html(content);
	$('#messageContainer').css({top: top+"px"}).fadeIn();
	//showCBox();
	return false;
};

function cBasketAlert(msg, urlBasket) {
	var content = '<div class="cBasketAlert">';
	content+= '<p class="data">'+msg+'</p>';
	content+= '<p class="actions">';
	content+= '<a href="javascript://" onclick="return hideCBox();" class="btn_continuer"></a>';
	content+= '<a href="'+urlBasket+'" class="btn_panier"></a>';
	content+= '</p>';
	content+= '</div>';

	var top = 0;
	if(window.scrollY) {
		top = window.scrollY;
	} else if(document.documentElement.scrollTop) {
		top = document.documentElement.scrollTop;
	}
	top+=150;

	$('#messageContainer').html(content);
	$('#messageContainer').css({top: top+"px"}).fadeIn();
	
	return false;
}

function show_datePicker(date) {
    $('.bloc_datepicker').css({display: 'block'});

    $('#ndeDate').datepicker("option", "minDate", date);
    $('#ndeDate').datepicker("option", "defaultDate", date);
}

$(document).ready(function() {
        $('#note_footer_link').bind('mouseenter', function(e){
			e.preventDefault();
			$('#note_footer').fadeIn();
        });
        $('#note_footer').bind('mouseleave', function(e){
			e.preventDefault();
			$('#note_footer').fadeOut();
        });

        $("a.product_thumb").bind("click", function(e){
			e.preventDefault();
			href = $(this).attr('href');
			$('#product_large').attr('src', href);
        });

        $("a.payment").bind("click", function(e){
			if($('input[name=accept_condition]').is(':checked')) {
				xajax_processOrder();
			} else {
				cAlert($('#error_conditions').html());
			}
        });

        /*$('#select_language').bind("change", function(e){
            var value = $(this).val();
            changeLanguage(value);
        });*/
		
		$('#select_language').click(function(e) {
			$(this).find('ul').slideDown('fast');
		});
		
		$('#select_language').mouseleave(function() {
			$(this).find('ul').slideUp('fast');
		});
		
		$('.value_language').click(function() {
			var value = $(this).attr('name');
			changeLanguage(value);
		});

        $("#actions_header").find("#panier").bind("mouseenter", function(e){
                if($(this).parent().find("#compte_contenu").css("display")=="none")
					$(this).find("#panier_contenu").slideDown(200);
                else
					$("#compte_contenu").slideUp(100, function(e){
							$("#panier_contenu").slideDown(200);
					});
        });
        
        $("#actions_header").find("#panier").bind("mouseleave", function(e){
                $(this).find("#panier_contenu").stop(true, true);
                $(this).find("#panier_contenu").slideUp(200);
        });

        $("#actions_header").find("#compte input").bind("focus", function(e){
                moncompte_lock = 1;
        });

        $("#actions_header").find("#compte input").bind("blur", function(e){
                moncompte_lock = 0;
        });

        $("#actions_header").find("#compte").bind("mouseenter", function(e){
                if($(this).parent().find("#panier_contenu").css("display")=="none")
                        $(this).find("#compte_contenu").slideDown(200);
                else
                        $("#panier_contenu").slideUp(100, function(e){
                                $("#compte_contenu").slideDown(200);
                        });
        });
        
        $("#actions_header").find("#compte").bind("mouseleave", function(e){
			if(moncompte_lock == 0) {
                $(this).find("#compte_contenu").stop(true, true);
                $(this).find("#compte_contenu").slideUp(200);
			}
        });
        
        
        $(".panier_plus_one").bind("click", function(e){
			e.preventDefault();
                var value = $(this).parent().parent().parent().find(".boxProductQuantity").find(".quantityProduct").attr("value") *1 ;
                value += 1 ;
                if(value >99)
                        value = 99 ;
                $(this).parent().parent().parent().find(".boxProductQuantity").find(".quantityProduct").attr("value", value);
        });
        
        
        $(".panier_minus_one").bind("click", function(e){
			e.preventDefault();
				var value = $(this).parent().parent().parent().find(".boxProductQuantity").find(".quantityProduct").attr("value") *1 ;
                value -= 1 ;
                if(value <0){
                    value = 0 ;
					minus = false ;
				}
                $(this).parent().parent().parent().find(".boxProductQuantity").find(".quantityProduct").attr("value", value);
        });
        
		// Gestion de l'ajout d'un produit dans le panier
        $("#addcard_product").bind("click", function(e){
			e.preventDefault();
            url = $('#commander').attr('action');
            var params = {};
            xajaxargs = new Array();
            content = '<xjxobj><e><k>max</k><v><xjxobj>';

            $('#commander').find('.availabilityProduct').each(function() {
                var name = $(this).attr('name');
                var value = $(this).val();
                content += '<e><k>' + name + '</k><v>' + value +'</v></e>';
            });
            content += '</xjxobj></v></e><e><k>current</k><v><xjxobj>';
            $('#commander').find('.quantityProduct').each(function() {
                var name = $(this).attr('name');
                var value = $(this).val();
                content += '<e><k>' + name + '</k><v>' + value +'</v></e>';
				$(this).attr('value', 0);
            });
            content += '</xjxobj></v></e></xjxobj>';

            xajaxargs[0] = content;
            params['xajax'] = 'setBasket';
            params['xajaxr'] = 1314781078872;
            params['xajaxargs'] = xajaxargs;

            $.ajax({
                type: 'POST',
                url: url,
                dataType: 'xml',
                data: params,
                cache: false,
                success: function(xml) {
					var racine = xml.documentElement;
					var tagnom = racine.getElementsByTagName("v");

					if(tagnom[1] != undefined) {
						cBasketAlert(tagnom[0].childNodes[0].nodeValue, tagnom[1].childNodes[0].nodeValue);
					} else {
						cAlert(tagnom[0].childNodes[0].nodeValue);
					}

					var contenu = racine.getElementsByTagName("cmd") ;
					contenu = contenu[0].childNodes[0].nodeValue;
					$('#panier_contenu').html(contenu);
                }
            });
        });
		
		// supression d'une ligne dans le panier
		$("#panier .drop_line").live("click", function(e){
			url = $(this).data('url');
            var params = {};
            xajaxargs = new Array();
			
			xajaxargs[0] = $(this).attr('name');
            params['xajax'] = 'removeBasketLine';
            params['xajaxr'] = 1314781078872;
            params['xajaxargs'] = xajaxargs;
			
			$.ajax({
                type: 'POST',
                url: url,
                dataType: 'xml',
                data: params,
                cache: false,
                success: function(xml) {
					var racine = xml.documentElement;
					var contenu = racine.getElementsByTagName("cmd") ;
					contenu = contenu[0].childNodes[0].nodeValue;
					$('#panier_contenu').html(contenu);
					
					window.location.reload();
                }
            });
		});
		
		// modification d'une quantité à partir du panier dynamique
		$("#panier .quantityProduct").live("change", function(e) {
			url = $(this).closest('.boxProduct').data('url');
			var params = {};
			var xajaxargs = new Array();

			xajaxargs[0] = $(this).closest('.boxProduct').data('key');
			xajaxargs[1] = $(this).val();
			params['xajax'] = 'setQuantity';
            params['xajaxr'] = 1314781078872;
            params['xajaxargs'] = xajaxargs;
			
			$.ajax({
                type: 'POST',
                url: url,
                dataType: 'xml',
                data: params,
                cache: false,
                success: function(xml) {
					var racine = xml.documentElement;
					var contenu = racine.getElementsByTagName("cmd") ;
					contenu = contenu[0].childNodes[0].nodeValue;
					$('#panier_contenu').html(contenu);
					
					window.location.reload();
                }
            });
		});

        // $(".push_product").bind("mouseover", function(e){
                // var id = $(this).attr("id").substr(8);
                
                // if($("#popin_product").css("display")=="block"){
						// if($.browser.version*1<=7 && $.browser.msie){
							// $("#popin_product").css('display', 'none');
							// chargerPopinProduct(id) ;
						// }
						// else{
							// $("#popin_product").fadeOut(200, function(e){chargerPopinProduct(id) ;});
						// }
                // }
                // else{
                        // chargerPopinProduct(id) ;
                // }
        // }).bind("mouseleave", function(e){
			// if($.browser.version*1<=7 && $.browser.msie)
                // $("#popin_product").delay(1000).css('display', 'none');
			// else
                // $("#popin_product").delay(1000).fadeOut(200);
        // });
		
		$(".push_product").bind("mouseover", function(e){
			if($.browser.version*1<=7 && $.browser.msie){
				$(this).find(".popin_product").css('display', 'block');
			}
			else{
				$(this).find(".popin_product").fadeIn(500);
			}
        }).bind("mouseleave", function(e){
			if($.browser.version*1<=7 && $.browser.msie)
                $(this).find(".popin_product").css('display', 'none');
			else
                $(this).find(".popin_product").fadeOut(500);
        });
        
        // $("#popin_product").bind("mouseover", function(e){
                // $("#popin_product").stop(true, true);
                // $("#popin_product").fadeIn(200);
        // }).bind("mouseleave", function(e){
			// if($.browser.version*1<=7 && $.browser.msie)
                // $("#popin_product").delay(100).css('display', 'none');
			// else{
                // $("#popin_product").fadeOut(200);//delay(1000).
			// }
        // });
        
        // function chargerPopinProduct(id){
            // html = $('#popin_' + id).html();
            // $('#popin_product .push_product_content').html(html);
			// if($.browser.version*1<=7 && $.browser.msie){
                // $("#popin_product").delay(100).css('display', 'block'); 
			// }else{
				// $('#popin_product').delay(500).fadeIn(200);
			// }
        // }
        
        
        $("#informations_produit").find("a").bind("click", function(e){
                var allButtons = $(this).parents("#informations_produit").find("li") ;
                for(var i=0; i<allButtons.length; i++)
                        $(allButtons[i]).removeClass("selected");
                $(this).parent().parent().addClass("selected");
                $(this).parents("#product").find("#description").fadeOut(200);
                $(this).parents("#product").find("#assortiment").fadeOut(200);
                $(this).parents("#product").find("#details").fadeOut(200);
                $(this).parents("#product").find("#conseils").fadeOut(200);
				$("#"+$(this).attr("id").substr(21)).delay(200).fadeIn(200);
        });
		
		$(".block_adress_note").bind("mouseenter", function(e){
			var posTop = e.pageY*1 - 175  ;
			$("#block_adress_informations").css("left", e.pageX+10);
			$("#block_adress_informations").css("top", posTop+"px");
			$("#block_adress_informations").fadeIn(200);
		}).bind("mouseleave", function(e){
			$("#block_adress_informations").fadeOut(200);
		});
		
		$(".type_delivery").bind("mouseenter", function(e){
            $(this).find("h5").stop(true, true);
			$(this).find("h5").slideDown('fast');
		}).bind("mouseleave", function(e){
            $(this).find("h5").stop(true, true);
			$(this).find("h5").slideUp('fast');
		});
		
		$(".push_product").bind("click", function(e){
			if($(this).find(".full_link").find("a").attr("href")) {
				window.location = $(this).find(".full_link").find("a").attr("href");

			} else if($(this).find(".link").find("a").attr("href")) {
				window.location = $(this).find(".link").find("a").attr("href");
			}
			
		});
		
		$("input[name=dlvId]").bind("click", function(e){
			if($(this).attr("class") == "express"){
				$("table.calendarTable").hide();
			}
			else{			
				$("table.calendarTable").show();
			}
		});
		
		$(".first").bind("mouseenter", function(e){
			$(this).children("ul").show();
		}).bind("mouseleave", function(e){
			$(this).children("ul").hide();
		});
		
		$('#sac').live('click', function() { $('#debugbox').css({display: 'block'}); });
		
		$('.btn_zoom').bind('click', function() {
			// récupération de l'url de l'image à afficher
			/*var src = $('.product_large.active').find('img').attr('src');

			$.colorbox({
				html:		"<img src='"+src+"' alt='' />",
				opacity:	0.5
			});*/
			var cle = $('.product_large.active').data('key');
			
			$('#product_image_'+(cle+1)).find('.product_images').click();
		});
		$('.product_images').colorbox({
			rel:"product_images",
			returnFocus:false,
			previous:'<img src="/images/fr/arrow_left.gif" alt="" />',
			next:'<img src="/images/fr/arrow_right.gif" alt="" />',
			close:'<img src="/images/fr/basket/delete.png" alt="" />',
			current:'{current} / {total}'
		});

	$('.assortiment_image').bind('click', function(e) {
		e.preventDefault();
		href = $(this).attr('href');
		position = $(this).position();
		$('#ajaxProductZoom').hide();
		$('#ajaxProductZoom').html($(href).html());
		$('#ajaxProductZoom .product_zoom').css({'display': 'block'});
		/*left = position.left - 2 + 'px';
		$('#ajaxProductZoom .product_zoom').css({'left': left});
		top = position.top + 30 - 6 + 'px';
		$('#ajaxProductZoom .product_zoom').css({'top': top});*/
		$('#ajaxProductZoom').fadeIn();
	});
	$('#ajaxProductZoom .close').live('click', function(e) {
		e.preventDefault();
		$('#ajaxProductZoom').fadeOut();
	});
});
