function init_fil_ariane(active_enlever,param_attributs,param_u_nav,param_u_options)
{
  if(active_enlever === 0)
    write_cookie_fil_ariane(param_attributs,param_u_nav,param_u_options);
  
  if($('#fil_ariane').length==0)
  return;
 
  if( (cook_init = LireCookie('u_history')) === null)  // Je rentre dans une fiche produit via le moteur de recherche ou une news letter
  return;

  var cook_tab = cook_init.split('-');
  var history_tab = cook_tab[0].split("=");
  var cook_history = history_tab[1];
  var nav_tab = cook_tab[2].split("=");
  var cook_nav = nav_tab[1];
  var index_tab = cook_tab[1].split("=");
  var cook_index = index_tab[1];
  var debut_cook = cook_tab[0] + '-' + cook_tab[1] + '-' + cook_tab[2];
  
  if(cook_tab.length>3 && !param_u_options)
  {
    var u_option_tab = cook_tab[3].split("=>");
	  param_u_options = u_option_tab[1];
	  debut_cook += "-" + cook_tab[3];
  }
  if(!cook_history || !cook_nav) return;
  
  if(active_enlever === 1)
  {
    //écriture dans le cookie qu'on vient d'une fiche produit
    var chaine_cookie = debut_cook + '-' + 'u_fp=1';
  	EcrireCookie("u_history",chaine_cookie,null,"/");	
  }
	
  
  var url = '/m/_scripts/get_fil_ariane.php';    

  $.ajax({        url: url,
                  data:{'att':cook_nav, 'history':cook_history, 'active_enlever':active_enlever, 'url_retour_liste' : param_u_options},
                  type: 'POST',
                  dataType:'xml',
                  cache: false,                 
                  complete:function(rep,msg){     
                  set_attributes(rep.responseXML, active_enlever,cook_index);   
                  }
             });
  
  return;
  
}

function getCookieVal(offset)
{
	var endstr=document.cookie.indexOf (";", offset);
	if (endstr==-1) endstr=document.cookie.length;
	return unescape(document.cookie.substring(offset, endstr));
}
function LireCookie(nom)
{
	var arg=nom+"=";
	var alen=arg.length;
	var clen=document.cookie.length;
	var i=0;
	while (i<clen)
	{
	var j=i+alen;
	if (document.cookie.substring(i, j)==arg) return getCookieVal(j);
	i=document.cookie.indexOf(" ",i)+1;
	if (i==0) break;
	
	}
	return null;
}

function newXmlHttpRequest(){
  if (window.XMLHttpRequest)
    return new XMLHttpRequest();
  else
    return new ActiveXObject("Microsoft.XMLHTTP");
}

function set_attributes(xmlDoc,active_enlever,cook_index){
  set_fil_ariane(xmlDoc,cook_index);
  // Pas d'historique de navigation dans une fiche produit
  if(active_enlever) 
  {
    set_att_enlever(xmlDoc);
    $('#fil_ariane').css('display', 'none');
  }
    
  
}
function set_fil_ariane(xmlDoc,cook_index){

  var html_element_fil_ariane = $('#fil_ariane');
  var html_element_retour_liste = $('#retour_liste');
  var html_element = $('<ul />').addClass('txtm');
  html_element.append($('<li />').html('Historique de votre navigation :&nbsp;'));
  
  var elements_length = $(xmlDoc).find("fil_ariane").find("elements").length;
  cook_index = parseInt(cook_index);
  
  var premier_element = true;
  var citroen = '';
  
  if((elements_length>4) && cook_index>2){
    html_element.append($('<li />').html('...<span>|</span>'));
  }
  
  var href_retour_liste = $(xmlDoc).find("retour_liste").find("url").get(0).firstChild.nodeValue;

  if(href_retour_liste.indexOf("?")<0)
  {
    if(html_element_retour_liste && href_retour_liste)
      html_element_retour_liste.empty().append($('<span />').addClass('txtgris txtbold txts txtlien').html('<a href="'+href_retour_liste+'" class="txtbold txtl">&laquo;&nbsp;Retour liste articles</a>'));
  }
  
  $(xmlDoc).find("fil_ariane").find("elements").each(
  function(i){
      if(!premier_element)
        citroen = '<span>|</span>';
      
      var attName = $(this).find("name").get(0).firstChild.nodeValue;
      var attHref = $(this).find("href").get(0).firstChild.nodeValue;
      if((cook_index+1) != elements_length)
      {
          if((i>=cook_index-2 && i<=cook_index+2)){
              if(i==cook_index){
                html_element.append($('<li />').html(citroen+'<a style="color:red" href="'+attHref+'" >'+attName+'</a>'));
              }
              else
              {
                if(i>cook_index)
                   html_element.append($('<li />').html(citroen+'<a style="color:gray" href="'+attHref+'">'+attName+'</a>'));
                else
                {
                    html_element.append($('<li />').html(citroen+'<a href="'+attHref+'">'+attName+'</a>'));
                }
              }
              premier_element = false;
          }
          if(cook_index<2 && i>cook_index+2 && i<5){
            html_element.append($('<li />').html(citroen+'<a style="color:gray" href="'+attHref+'">'+attName+'</a>'));
          }
          
        
        
      }
      else
      {
        
		if(i>=cook_index-3){
          if(i==cook_index)
          {
            html_element.append($('<li />').html(citroen+'<a style="color:red" href="'+attHref+'" >'+attName+'</a>'));
          }
          else
          {
              html_element.append($('<li />').html(citroen+'<a href="'+attHref+'">'+attName+'</a>'));
          }
          premier_element = false;
        }
        
      }
  }
  ) 
  if((elements_length>4) && cook_index<elements_length-3){
    html_element.append($('<li />').html(citroen+'...'));
  }
  
  html_element_fil_ariane.empty().append(html_element);
  
}

function set_att_enlever(xmlDoc){
  var html_element_att_enlever = $('#att_enlever');
  var html_element_enlever = "";

  $(xmlDoc).find("att_enlever").find("elements").each(
  function(){
      var attName = $(this).find("name").get(0).firstChild.nodeValue;
      var attHref = $(this).find("href").get(0).firstChild.nodeValue;
      var attHrefTracker = $(this).find("href_tracker").get(0).firstChild.nodeValue;
      
        html_element_enlever += '<li class="margin_xs">' +
                                  '<a class="txtgris pad_left" onclick="pageTracker._trackPageview(\''+attHrefTracker+'\');rdc_click4(\''+attHref+'\',\'\',\'\');return false;" href="#" title="Supprimer le critère '+attName+'">' +
                                  '<span class="pictoCroix">Supprimer le critère</span>'+attName+'</a>' +
                                  '</li>';

  }
  )

  html_element_att_enlever.empty().append(html_element_enlever);
}

function write_cookie_fil_ariane(param_attributs,param_u_nav,param_u_options){
  
    if(LireCookie('u_history') === null)
		{
			var chaine_cookie = "u_history=" + param_attributs + "-u_index=" + 0 + "-u_nav=" + param_attributs + "-u_options_url=>" + param_u_options;
			EcrireCookie("u_history",chaine_cookie,null,"/");
			//EcrireCookie("u_nav",param_attributs,null,"/");
			//EcrireCookie("u_index",0,null,"/");
		}
		else
		{
			 var cook_init = LireCookie("u_history");
				var cook_tab = cook_init.split('-');
  			var history_tab = cook_tab[0].split("=");
  			var u_history = history_tab[1];
  			var nav_tab = cook_tab[2].split("=");
  			var cook_nav = nav_tab[1];
  			var index_tab = cook_tab[1].split("=");
  			var u_index = index_tab[1];
  			
		
			u_index = parseInt(u_index);
			var u_tab = u_history.split("|");
			var chaine =  param_attributs;
			if((u_index+1) != u_tab.count())
			{
			
  			/*
  			 * suppression des éléments du fil d'ariane :
  			 * que si l'index précédent choisi était dans l'historique
  			 * et que la nouvelle url choisie ne fait pas partie de l'historique suivant
  			 */
			 
				if(u_tab[u_index+1] != chaine && array_search(chaine,u_tab) === false)
				{
					var i_max = u_tab.count();
					for(i = (u_index+1);i<i_max;i++)
						u_tab = unset(u_tab, i);
						
				}

        u_history = u_tab.join('|');				
				
			}
			
			if((u_index = array_search(chaine,u_tab)) === false)
			{	
				u_index = u_tab.count();
				u_history = u_history+"|"+chaine;
			}
			
			u_tab = u_history.split("|");
			
			u_history = u_tab.join('|');
			
			
			var chaine_cookie = "u_history=" + u_history + "-u_index=" + u_index + "-u_nav=" + param_u_nav + "-u_options_url=>"+param_u_options;
			EcrireCookie("u_history",chaine_cookie,null,"/");
		}
  
}

function set_u_options(url, add){
   	    
  if((cook_init = LireCookie('u_history')) === null)  // Je rentre dans une fiche produit via le moteur de recherche ou une news letter ou moteur de recherche
    return;

  var cook_tab = cook_init.split('-');
  
  var debut_cook = cook_tab[0] + '-' + cook_tab[1] + '-' + cook_tab[2];
  
  if(add){ // ajouter l'option#
    if(cook_tab[3].indexOf("#"))
    {
      var tab_url = cook_tab[3].split('#');
      var url_sans_d = tab_url[0];
    }
    else  var url_sans_d = cook_tab[3];
    
    var chaine_cookie = debut_cook + '-' + url_sans_d + url;    
  }
  else
	 var chaine_cookie = debut_cook + '-' + 'u_options_url=>' + url;
	 
	EcrireCookie("u_history",chaine_cookie,null,"/");	
}

Array.prototype.count = function() {
  return this.length;
};



function array_search(val,arr) {
  for (var i=0; i<arr.length; i++)
  if (arr[i] == val)
    return i;
  return false;
}

function unset(array, valueOrIndex){
	var output=[];
	for(var i in array){
		if (i!=valueOrIndex)
			output[i]=array[i];
	}
	return output;
}


