
var popupBasket = null;
var popupBasketHeight = null;
var popupBasketTimeout = null;
var hideTimeout = 10000;
var isBasketPage = /\/panier\.html$/.test(window.location) ;

var dynBasket = null;
var dynBasketHeight = null;
var dynBasketTimer = null;
var dynBasketIn = 1;
var dynBasketOut = 250;
var dynBasketShow = 300;
var dynBasketHide = 450;

$(document).ready(function(){

	if(!isIE || isIE8){
		popupBasket = $('div.popBasket');
		popupBasketHeight = popupBasket.height();
		if(isIE){
			popupBasket.css({ height: 0, display: 'none' });
		}else{
			popupBasket.fadeTo(1,0).css({display: 'none'});
		}
		dynBasket = $('.blockDynBasket');
		dynBasketHeight = dynBasket.height();
		dynBasket.css({height: 0});

		constructAjaxBasket();
		constructDynBasket();
		constructAddProductLink();
		if(AjaxReady){
			AjaxReady.click(function(){
				constructAjaxBasket();
				constructDynBasket();
			});
		}
	}
		constructCodePromo();
		constructShippingPrice();
});

function constructCodePromo(){
	$('#promocode').each(function(){
		$(this).unbind('focus').focus(function(){
			if($(this).val() == 'Code de réduction...') $(this).val('');
		});
		$(this).unbind('blur').blur(function(){
		if($(this).val() == '') $(this).val('Code de réduction...');
		});
	});

}

function constructAddProductLink(){
	var url = location.href;
	regex = /\/panier\.html$/
	if(regex.test(url)){
		$('#blockBigSell').find('.jsAddBasket').each(function(){
			var linkEl = $(this);
			var hrefEl = linkEl.find('a').attr('href');
			linkEl.find('a').attr('href',hrefEl+'&onBasket=true');
		});
	}
}

function constructDynBasket(){
	$('a.basketFilled.jsBasketOpener, .blockDynBasket').unbind('hover').hover(
		function(){ timerShowDynBasket(); },
		function(){ timerHideDynBasket() }
	);
}

function showDynBasket(){


	if(dynBasket.height() == 0){
		dynBasketHeight = dynBasket.css({height: 'auto'}).height();
		dynBasket.css({height: 0});
	}

	dynBasket.stop().css({
		//display: 'block'
	}).animate({
		height: dynBasketHeight
	},dynBasketShow,function(){
		dynBasket.css({
			height: 'auto'
		});		
		dynBasketHeight = dynBasket.height();
	});
	$('a.basketFilled.jsBasketOpener').addClass('activ');
}

function hideDynBasket(){
	dynBasket.stop().animate({
		height: 0
	},dynBasketHide,function(){
		$('a.basketFilled.jsBasketOpener').removeClass('activ');
		dynBasket.css({
			//display: 'none',
			height: 'auto'
		});
		dynBasketHeight = dynBasket.height();
		dynBasket.css({height:'0px'});
	});
}

function timerShowDynBasket(){
	timerResetDynBasket();
	dynBasketTimer = window.setTimeout("showDynBasket()",dynBasketIn)
}

function timerHideDynBasket(){
	timerResetDynBasket();
	dynBasketTimer = window.setTimeout("hideDynBasket()",dynBasketOut)
}

function timerResetDynBasket(){
	if(dynBasketTimer){
		clearTimeout(dynBasketTimer);
	}
}

function constructShippingPrice(){
	var blockEl = $('.deliveryMode');
	blockEl.find('a').each(function(){
		var aEl = $(this);
		aEl.click(function(){
			if(aEl.find('input').attr("checked") == false){
				aEl.find('input').attr("checked","checked");
				blockEl.find('label.selected').removeClass('selected');
				$(this).closest('label').addClass('selected');
				computeShippingPrice();
			}
			return false;
		});
	});
	//$('input[name$=ShippingSpeed]').unbind('change').change(function(){  	
		//blockEl.find('label.selected').removeClass('selected');
		//$(this).closest('label').addClass('selected');
		//computeShippingPrice();
	//});

}
function computeShippingPrice(){
		//var b = $('input[type=hidden][name^=vadPossibilityBase]:first').val() ;
		//var a = $('input[type=hidden][name^=vadPossibilityAdd]:first').val() ;
		shippingSpeed = getShippingSpeed('vad');
		//var url = '/ajax/shipping.html';
		//var datas = 'action=getShipping&t='+shippingSpeed+'&b=' + b +'&a=' + a + "&f=1" ;
		var url = '/panier.html';
		var datas = 'action=computeShipping&t='+shippingSpeed+ "&f=1&isAjax=1" ;
		$.ajax({
			url: url,
			type: 'GET',
			data: datas,
			dataType: "html",
			success: function(response){
				//$('.shippingCost').find('.price').html(response);
				// mise a jour du block panier si existant
				if($('#blockBasket').length == 1 && $('#blockBasket',response).length == 1 ){

					$('#blockBasket').html($('#blockBasket',response).html());
				}
				constructShippingPrice();
			}
		});
}
function getShippingSpeed(type){
	if(type == 'onSite'){
		speed = $('input[name="onSiteShippingSpeed"]:checked').val();
	}else if(type == 'vad'){
		speed = $('input[name="vadShippingSpeed"]:checked').val();
	}else if(type == 'perso'){
		speed = $('input[name="persoShippingSpeed"]:checked').val();
	}else{
		return 0;
	}

	if(speed == undefined || speed < 0 ) speed = 0;

	return speed;
}

function constructAjaxBasket(){
	$('a[href^=/panier.html?]').each(function(){
		var linkEl = $(this);	
		linkEl.unbind('click').click(function(){
			var url = linkEl.attr('href');
			// NE PAS METTRE CERTAINES ACTIONS EN AJAX 
			regex = /^\/panier\.html\?action=(deleteBasket|identification|restore|rescue)/
			if(regex.test(url)) return true;
			regexNoAjax = /^\/panier\.html\?deleteShippingOnSite=1/
			if(regexNoAjax.test(url)){
				var datas = null;
			}else{
				var datas = 'mode=ajax';
			}

			regexAdd = /^\/panier\.html\?action=add&product=([0-9]+)$/
			if(regexAdd.test(url)){
				productId = regexAdd.exec(url)[1];
			}

			$.ajax({
				url: url,
				type: "POST",
				data: datas,
				dataType: "html",
				success: function(response){

					popupText = $('.ajaxBasketReturn',response).html();
					errorText = $('.ajaxBasketError',response).html();

					// MISE A JOUR DE L'AFFICHAGE DU STOCK SUR FICHE PRODUIT 
					if(window.productId && $('.gameStockAlert').length == 1 && $('span.remainingStock[rel="pId:'+productId+'"]',response).length == 1 ){
						remainingStock = $('span.remainingStock[rel="pId:'+productId+'"]',response).html() ;
						if(remainingStock == 0){
							$('.gameStockAlert').show(500);
							$('div.stockInfos').html('<span class="ruptureColor">Non disponible</span>');
						}

						if(remainingStock.length >= 1){
							if(remainingStock.length == 1) remainingStock = '0'+remainingStock;
							remainingStock = remainingStock.match(new RegExp('.{1}','g'));
							content = '';
							for(i=0;i < remainingStock.length; i++){
								content = content + '<span>'+remainingStock[i]+'</span>';
							}
							$('.stockCount a').html(content);
						}
					}

					if(popupText != '' && popupText != undefined){
						setBasketPopupText(popupText,1);
					}
					if(errorText != '' && errorText != undefined){
						showAjaxBasketText(errorText);
					}

					// MISE A JOUR DU BLOCK PANIER SI EXISTANT
					if($('#blockBasket').length == 1 && $('#blockBasket',response).length == 1 ){
						$('#blockBasket').html($('#blockBasket',response).html());
					}
					if($('.blockDynBasket').length == 1 && $('.blockDynBasket',response).length == 1 ){
						$('.blockDynBasket').html($('.blockDynBasket',response).html());
					}
					

					// MISE A JOUR DES AFFICHAGES GLOBAUX 
					if($('.basketTotalQuantity').length >= 1 && $('.basketTotalQuantity:first',response).length == 1){
						$('.basketTotalQuantity').html($('.basketTotalQuantity:first',response).html());
					} else if($('.jsBasketInfos').length >= 1 && $('.jsBasketInfos:first',response).length == 1){
						$('.jsBasketInfos').html($('.jsBasketInfos:first',response).html());
					}
					constructShippingPrice();
					//computeShippingPrice();
				}
			});
			return false;
		});

	});

	popupBasket.find('a[href="#popBasketClose"]').each(function(){
		var linkEl = $(this);
		linkEl.unbind('click').click(function(){
			hideBasketPopup();
			return false;
		});
	});

	popupBasket.unbind('hover').hover(function(){
		clearPopupBasketTimeout();
	},function(){
		clearPopupBasketTimeout();
		setPopupBasketTimeout();
	});


}

function showAjaxBasketText(text){
	showPopupText(text, 1, 200, 1000, 200);
}

function setBasketPopupText(innerHtml,plusShow){
	if(!isBasketPage){
		popupBasket.find('div.content').html(innerHtml);
		if(plusShow == 1){
			showBasketPopup();
		}
	}
}

function setPopupBasketTimeout(){
	popupBasketTimeout = window.setTimeout("hideBasketPopup()",hideTimeout);
}
function clearPopupBasketTimeout(){
	window.clearTimeout(popupBasketTimeout);
}
	
function showBasketPopup(){
	if(!isBasketPage){
		if(isIE){
			popupBasket.stop().css({
				display: 'block'
			}).animate({
				height:  popupBasketHeight
			},500);
		}else{
			popupBasket.stop().css('display','block').fadeTo(500,1);
		}
	}
	clearPopupBasketTimeout();
	setPopupBasketTimeout();
}

function  hideBasketPopup(){
	if(!isBasketPage){
		if(isIE){
			popupBasket.stop().animate({
				height: 0 
			},300,function(){
				$(this).css('display','none');
			});
		}else{
			popupBasket.stop().fadeTo(300,0,function(){ $(this).css('display','none');});
		}
	}
}



