function getCookie(c_name)
{
	if (document.cookie.length>0)
	  {
	  c_start=document.cookie.indexOf(c_name + "=");
	  if (c_start!=-1)
		{
		c_start=c_start + c_name.length+1;
		c_end=document.cookie.indexOf(";",c_start);
		if (c_end==-1) c_end=document.cookie.length;
		return unescape(document.cookie.substring(c_start,c_end));
		}
	  }
	return "";
}

function setCookie(c_name,value,expiredays)
{
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+
	((expiredays==null) ? "" : ";expires="+exdate.toUTCString());
}

function checkCookie()
{

	//restauration bienvenue!
	var $customer_id = getCookie('customer_id');
	var $customer_first_name = getCookie('customer_first_name');
	if ($customer_id!=null && $customer_id!="" && $customer_first_name!=null && $customer_first_name!="")
  	{
  		//alert('Welcome again '+customer_first_name+'!');
  		$('#logout').html("Déconnexion");
  		$('#logout-notme').html("(je ne suis pas "+ucFirst($customer_first_name)+")");
  		$('#log-welcome').html("Bienvenue "+ucFirst($customer_first_name));
  	}
  	
  	//restauration nb article panier
  	var $total_into_cart = getCookie('total_into_cart');
  	if ($total_into_cart!=null && parseInt($total_into_cart)>0 )
  	{
  		if (parseInt($total_into_cart)>1)
  			$('#basket-content').html("("+$total_into_cart+" articles)");
  		else
  			$('#basket-content').html("("+$total_into_cart+" article)");
  	}else{
  		$('#basket-content').html("");  		
	}
	
  	

}

/**
* 
*/
function ucFirst(str) {
  str += '';
  var f = str.charAt(0).toUpperCase();
  return f + str.substr(1);
}

/**
* obtenir un object du HASH
*/
function getHash(){

	$hash = window.location.hash;
	//alert($hash);
	$hash = $hash.substring(1, $hash.length);
	$hash_array = $hash.split('&');
	$hash_object = new Object();
	for(var i=0; i<$hash_array.length; i++){
		$temp = $hash_array[i].split('=');	
		$name = $temp[0];
		$value = $temp[1];
		if ($name != ''){
			$hash_object[$name] = $value;
			//alert($name+'->'+$value);
		}
	}
	return $hash_object;
	
}

function getQuerystring(key, default_)
{
  if (default_==null) default_="";
  key = key.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regex = new RegExp("[\\?&]"+key+"=([^&#]*)");
  var qs = regex.exec(window.location.href);
  if(qs == null)
    return default_;
  else
    return qs[1];
} 

/***
*   utilisation du hash pour l'autoconnexion & l'affiliation
*/
function useHash(){
	$hash = getHash();
	
	//autoconnexion
	if (($hash['email']!=null && $hash['hmail']!=null) || $hash['from']!=null)
  	{
  		 $query = window.location.hash;
  		 $query = $query.replace('#', '?');
  		 $.ajax({ 
  		 	 url: "ajax-cart.php"+$query, 
  		 	 dataType: "json", 
  		 	 success: function(){ 
  		 	 	 //alert('ok!'); 
  		 	 	 checkCookie();
  		 	 } 
  		 });
  	}else{
  		//SI AFFILIATION PAR QUERY
  		if (getQuerystring('from')!=''){
  		    //alert(getQuerystring('from'));
  		    $query = location.href.substring(location.href.indexOf("?"), location.href.length);
  			$.ajax({ 
  		 		 url: "ajax-cart.php"+$query, 
  		 		 dataType: "json", 
  		 		 success: function(){ 
  		 	 		 //checkCookie();
  		 		 } 
  			});
  		}
	}
	
}

/**
* fonction qui retourne l'id de la FP en cours, false sinon
*/
function getCurrentFP(){
	$currentUrl = window.location.href;
	$res = $currentUrl.match('^(.*)-a-([0-9]+).html(.*)$');
	if ($res)
		return parseInt($res[2]);	
	return 0;
}

/**
* 
*/
function getPreviousCategories(){
	$previousUrl = window.document.referrer;	
	$res = $previousUrl.match('^(.*)-c-([0-9_]+).html(.*)$');
	if ($res && ($res[2]).length > 0){
		return $res[2];
	}
	return '';
}

/**
* fonction qui retourne l'id de la FP en cours, false sinon
*/
function getPreviousMarque(){
	$previousUrl = window.document.referrer;
	$res = $previousUrl.match('^(.*)-m-([0-9]+).html(.*)$');
	if ($res)
		return parseInt($res[2]);	
	return 0;
}


/**
* 
*/
function loadBreadcrumb(){
	//on est sur une FP???
	$pId = getCurrentFP();
	if ($pId > 0){
		//on vient d'une marque???
		$mId = getPreviousMarque();
		if($mId > 0){
			//alert('mId:'+$mId);	
			//cherchons le fil d'ariane
			$.ajax({ 
  		 		type:'POST',
  		 		url: "ajax-ariane.php", 
  		 		data: ({pId : $pId, mId : $mId}),
                dataType: "json", 
  		 		success: function($data){ 
  		 			$('#content .navBarContent .breadCrumb').html($data['ariane']);
  		 			$('#content .col-left .sub-categories').remove();
  		 		} 
  			});
  			return;
		}
		//on vient d'une catégorie/thématique???
		$cId = getPreviousCategories();
		if (($cId).length > 0){
			//cherchons le fil d'ariane
			$.ajax({ 
  		 		type:'POST',
  		 		url: "ajax-ariane.php", 
  		 		data: ({pId : $pId, cId : $cId}),
                dataType: "json", 
  		 		success: function($data){ 
  		 			//ariane
  		 			$('#content .navBarContent .breadCrumb').html($data['ariane']);
  		 			//menu gauche
  		 			var n = ($data['thematiques']).length;
  		 			if (n > 0){
  		 				$('#themes-box').html($data['thematiques']);
  		 				$('#content .col-left .sub-categories').remove();
					}else{
  		 				$('#content .col-left .sub-categories').html($data['categories']);
					}
  		 		} 
  			});
  			return;
		}
	}
}



//c'est parti !
$(document).ready(function(){
	checkCookie();
	useHash();	
	loadBreadcrumb();
});
