var majsep = "/";
var minsep = "|";

// --------------------------------------------------------------------------------------------------------------------

function getPageSize() 
{  
    var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	
	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}

	return [pageWidth,pageHeight];
}
 

// --------------------------------------------------------------------------------------------------------------------

function DisplayBasket()
{	
	var cookieval = getCookie("hc_panier3");
	$("panier_produits").innerHTML = "";	
	if ( cookieval )
	{
		var sales = cookieval.split(majsep);
		$("panier_produits").style.display = "block";
		$("panier_vide").style.display = "none";
		$("finish0").style.visibility = "visible";
		$("clear0").style.visibility = "visible";
		for(var i = sales.length-1 ; i >= 0 ; i--)
		{
			var infos = sales[i].split(minsep);		
			var ts = infos[0];
			var product_type = infos[1];
			var product_id = infos[2];
			var qty = infos[3];
			var reduc = infos[4];
			var intitule = infos[5];
			var imgstr;
			if ( infos[6] )
			{
				imgstr = "/images/produits/preview_"+infos[6];
			}
			else
			{
				imgstr = "/images/framework/commun/panier_blank.gif";
			}
			
			
			$("panier_produits").innerHTML += "<div class='produit' id='"+product_id+"'>"
				+"<div class='vignette'><img src='"+imgstr+"' /></div>"
				+"<div class='intitule'>"+unescape(intitule)+"</div>"
				+"<div class='retirer'>"
				+"<a href='javascript:void(0);' onClick=\"RemoveFromBasket('"+ts+"');\">"
				+"<img src='/images/framework/commun/panier/bt_retirez.jpg' />"
				+"</a>"
				+"</div>";
				+"</div><hr class='spacer' />";
		}
	}
	else
	{
		$("panier_produits").style.display = "none";
		$("panier_vide").style.display = "block";
		$("finish0").style.visibility = "hidden";	
		$("clear0").style.visibility = "hidden";	
	}
	var pageSize = getPageSize();
	var scrollX = window.pageXOffset || document.documentElement.scrollLeft || document.body.scrollLeft;
    var scrollY = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop;
	var x = (pageSize[0] - 620) / 2;
	var y = scrollY+50;
	$("popuppanier").style.left = x+"px";
	$("popuppanier").style.top = y+"px";
	$("popuppanier").style.display = "block";	

}

// --------------------------------------------------------------------------------------------------------------------

function RemoveFromBasket(ts,gotobasketpage)
{
	var newcookie = "";
	sales = getCookie("hc_panier3").split(majsep);
	for(i = 0 ; i < sales.length ; i++)
	{
		var infos = sales[i].split(minsep);
		if ( infos[0] == ts )
		{
			sales[i] = "";
		}
		// Ici on vire les cadeaux associés à un abonnement 
		// (le type est la référence à l'abonnement)
		if ( infos[1] == ts ) 
		{
			sales[i] = "";
		}
		if ( newcookie != "" && sales[i] != "" )
		{
			newcookie += majsep;
		}
		newcookie += sales[i];
	}
	setCookie("hc_panier3",newcookie);
	if ( gotobasketpage )
	{
		document.location = "/panier";
	}
	else
	{
		DisplayBasket();
	}
}

// --------------------------------------------------------------------------------------------------------------------

function ClearBasket()
{	
	setCookie("hc_panier3","");
	$("panier_produits").style.display = "none";
	$("panier_vide").style.display = "block";
	//$("finish").style.visibility = "hidden";	
	$("clear").style.visibility = "hidden";	
}

// --------------------------------------------------------------------------------------------------------------------
// Format du panier : ts x type x produit_id x q x reduc x intitule x imgstr

function JustAddToBasket(idstr,type,intitule,imgstr)
{
	var cookieval = getCookie("hc_panier3");
									
	ts = Date.parse(new Date()) / 1000;

	if ( cookieval )
	{
		cookieval += majsep;
	}
	// check and ensure that ts is unique
	while( 1 )
	{
		var found = false;
		var produits = cookieval.split(majsep);
		for(var i = 0 ; i < produits.length ; i++)
		{
			var produit = produits[i].split(minsep);
			if ( produit[0] == ts )
			{
				ts++;
				found = true;
			}
		}
		if ( !found )
		{
			break;			
		}
	}
	// ts x type x id x q x reduc x intitule x imgstr
	cookieval +=	ts
					+minsep+type
					+minsep+idstr
					+minsep+"1"
					+minsep+"0"
					+minsep+escape(intitule)
					+minsep+imgstr;	
	setCookie("hc_panier3",cookieval);	
	$("panier_menu").style.color = "#FFFF00";
	$("panier_menu").style.fontWeight = "bold";
	return( ts );
}

function AddToBasket(idstr,type,intitule,imgstr)
{
	var ts = JustAddToBasket(idstr,type,intitule,imgstr);
	DisplayBasket();
	return( ts );
}

// --------------------------------------------------------------------------------------------------------------------

function DoReminder()
{	
	if ( getCookie("reminder") == "done" )
	{
		return;
	}	
	if ( $("reminder") )
	{
		$("reminder").style.display = "block";
		alpha = 1;
		setTimeout("ReminderFadeout();",1*2000);
	}	
}

var alpha = 1;

function ReminderFadeout()
{	
	$("reminder").style.opacity = alpha;
	alpha -= 0.1;	
	if ( alpha <= 0 )
	{
		alpha = 1.0;
		$("reminder").style.opacity = alpha;
		$("reminder").style.display = "none";
//		$("header2").removeChild($("reminder"));
		setCookie("reminder","done");
	}	
	else
	{
		setTimeout("ReminderFadeout();",0.10*1000);	
	}
}

// --------------------------------------------------------------------------------------------------------------------

