$(document).ready(function() {

	//$('a[rel*=facebox]').facebox();

	$(document).bind('beforeReveal.facebox', function() {
		var newTop = parseInt($('#facebox').css('top').replace(/px$/, '')) + 50;
		if ($(window).height() < 600)
			newTop = $(window).height() / 2 - 80;
		else if (newTop < 360)
			newTop = 360;
		$('#facebox').css({
			'top': newTop
//			'width': '500px'
		});
	});

	$('ul#menu_gauche li .sub_menu').hide();
	$('ul#menu_gauche li').mouseenter(function(){
		if ($(this).find('.sub_menu').length) {
			$(this).siblings().find('.sub_menu li').stop(true, true);
			$(this).siblings().find('.sub_menu').stop(true, true);
			$(this).siblings().find('.sub_menu').hide();
			$(this).find('.sub_menu').stop(true, true).show(400);
		}
	});
	$('ul#menu_gauche li').mouseleave(function(){
		$(this).find('.sub_menu').delay(700).hide(400);
	});
	$('ul#menu_gauche .sub_menu li').mouseenter(function(){
		$('ul#menu_gauche li').clearQueue();
		var n = $('ul#menu_gauche .sub_menu').queue("fx");
		if (n.length == 0) {
			$('ul#menu_gauche .sub_menu').clearQueue();
			$(this).stop(true, true).show(400);
		}
	});
	$('ul#menu_gauche').mouseleave(function(){
		//$('ul#menu_gauche ul').stop(true);
		$('ul#menu_gauche .sub_menu').stop(true, true).delay(700).hide('fast');
	});

	/*$('#btn_panier').toggle(function() {
		ouvrirPanier();
	},function() {
		fermerPanier();
	});*/
	$('#btn_panier').click(function(event){
		event.stopPropagation();
		ouvrirPanier();
	});
	$('#top_panier').click(function(event){
		event.stopPropagation();
		fermerPanier();
	});
	$('#panier').click(function(event){
		event.stopPropagation();
	});
	$('body').click(function(){
		fermerPanier();
		//if($('#panier').is(':visible'))
		//$('#btn_panier').click();
	});

	// left menu need an iframe in the background to overlap "select" element
	$('ul#menu_gauche li ul').bgiframe();

	$('div#panier').bgiframe();

	// apply "html5 like" placeholder attribute to browser that doesn't support it natively
	$('input').each(function (i){
		try {
			if ($(this).attr('placeholder').length)
				$(this).placeholder();
		}catch(err){};
	});
	
	$('.btn_rouge, .btn_bleu, .bt-ajouter, .btn_promo img').mouseenter(function(){
		//$('.btn_rouge, .btn_bleu, .bt-ajouter, .btn_promo img').clearQueue();
		//$('.btn_rouge, .btn_bleu, .bt-ajouter, .btn_promo img').css('opacity','1.0');
		$(this).animate({
			opacity: 0.7
		}, 300)
	});
	$('.btn_rouge, .btn_bleu, .bt-ajouter, .btn_promo img').mouseleave(function(){
		//$('.btn_rouge, .btn_bleu, .bt-ajouter, .btn_promo img').clearQueue();
		$(this).animate({
			opacity: 1.0
		}, 200)
	});
});

function ouvrirPanier()
{
	$('#panier').slideDown('fast');
	//$('#btn_panier div div div:first, #btn_panier_off div div div:first').css('display','none');
	//$('#btn_panier div div div:last, #btn_panier_off div div div:last').css('display','inline').css('line-height','3em').css('vertical-align','middle');
}
function fermerPanier()
{
	$('#panier').stop(true, true).slideUp('fast');
	/*
	if($('#btn_panier div.not_logged_in').length)
		$('#btn_panier div.not_logged_in div:first, #btn_panier_off div.not_logged_in div:first').css('display','block');
	else
		$('#btn_panier div.logged_in div:first, #btn_panier_off div.logged_in div:first').css('display','inline');
	*/
	//$('#btn_panier div div:last, #btn_panier_off div div:last').css('display','none');
}


/*** Secure ***/
function PanierUpdate(input){
	form = $(input).parents("form");
	return submitForm(form[0], "update_panier.php");
}

function submitForm(form, action)
{
	action = action ? action : form.action;
	if(action.indexOf("https:") == 0 && (''+window.location).indexOf("https:") == -1)
		action = action.replace("https:", "http:");

	$('#basket_content').parent().fadeOut('fast');
	$.ajax({
		url: action,
		type: "POST",
		data: $(form).serialize(),
		success: function(response){
	  		$("#basket_content").parent().html(response);
			var t = $(response).find("#sub_total");
			var bo = $(response).find("#sub_total_bo");
			var total_text;
			var total_bo_text;
			var total;
			var total_bo;

			total_text = $(t).text();
			total = total_text.replace(/(\s|\$)+/g,"").replace(",",".");
			total = parseFloat(total);

			total_bo_text = $(bo).text();
			total_bo = total_bo_text.replace(/(\s|\$)+/g,"").replace(",",".");
			total_bo = parseFloat(total_bo);

			if((total + total_bo) < 50)
				$(".extra_msg").show();
			else
				$(".extra_msg").hide();
		},
		complete: function(){
			$('#basket_content').parent().fadeIn('fast');
		}
	});

	return false;
}


/*** End Secure ***/

/*** Start search ***/
function searchOpenMore() {
	$("#rech-filtres .open").hide(200);
	$("#rech-filtres ul.more").slideDown(200);
	$("#rech-filtres .close").fadeIn(1000);
	$("#rech-filtres").addClass('opened');
	return false;
}

function searchCloseMore() {
	$("#rech-filtres .close").hide();
	$("#rech-filtres ul.more").slideUp(200);
	$("#rech-filtres .open").fadeIn(1000);
	$("#rech-filtres").removeClass('opened');
	return false;
}

function filterSearch(filterName, value) {
	searchCloseMore();

	if (filterName == "cat") {
		if (! searchFilter.cat2)
			searchFilter.cat2 = value;
		else
			searchFilter.cat3 = value;
	} else if (filterName == "brand") {
		searchFilter.brand = value;
	} else if (filterName == "prange") {
		searchFilter.prange = value;
	}


	var newLocation = "../recherche/index.html?type=keyword&R=" + searchFilter.keyword;

    if (searchFilter.cat2)
                newLocation += "&cat2=" + searchFilter.cat2;
	if (searchFilter.cat3)
                newLocation += "&cat3=" + searchFilter.cat3;
	if (searchFilter.brand)
                newLocation += "&brand=" + searchFilter.brand;
	if (searchFilter.prange)
                newLocation += "&prange=" + searchFilter.prange;
	document.location = newLocation;
}
/*** End search ***/

/*** Start cartouches search ***/

function cartoucheMarqueCouleur(o) {
	var src = $(o).attr('src');
	src = src.replace(/_nb\./, '\.');
	$(o).attr('src', src);
}

function cartoucheMarqueNoir(o) {
	var src = $(o).attr('src');
	if (src.indexOf('_nb') != -1) return;
	src = src.replace(/\.([^.]+)$/, '_nb\.$1');
	$(o).attr('src', src);
}

$(document).ready(function() {
	var context = $('#cartouche_brands .cartoucheBlockContent');
	if (context.length > 0) {
		$('img', context).hover(function() {
			rechCartouchesMarque ? cartoucheMarqueCouleur(this) : cartoucheMarqueNoir(this);
		}, function() {
			if ($(this).hasClass(rechCartouchesMarque)) return;
			rechCartouchesMarque ? cartoucheMarqueNoir(this) : cartoucheMarqueCouleur(this);
		});
		if (rechCartouchesMarque) {
			$('img', context).each(function(index, el) {
				cartoucheMarqueNoir(el);
			});
			var current = $('img.' + rechCartouchesMarque, context);
			cartoucheMarqueCouleur(current);
		}
	}
});
/*** End search ***/


