//Declare Global Variables
var cookie_information = new Array();
var currloc = location.toString();
var subloc = currloc.substring(currloc.indexOf("/")+2);
var firstone = "no";


//Makes sure there is a cookie - if not initializes one
//Sets FIRSTONE = YES  Note: If this is the first page the user is acessing
function navarray(){
    if (document.cookie == '') { 
    
   // No Cookie - Establish one
	var  x = location.toString();
	x = x.substring(x.indexOf("/")+2);
	var cookiedough = "location0:"+x+":title0:"+crumbTitle; 
	document.cookie= "navCookie=" + escape(cookiedough);
	firstone = "yes";
	} else {
	return false;		
  }
}


navarray();

//Variable for loops
var arraycount = 0;

//TThis Function will read the cookie and break all of it's values out into a new array
//Checks for duplicate entry  -  If duplicate bailes out thus crumbTitleening the array that will create the crumbs
function readTheCookie(the_info){
	// load the cookie into a variable and unescape it
	var the_cookie = document.cookie;
	var the_cookie = unescape(the_cookie);
	// separate the values from the cookie name
	var broken_cookie = the_cookie.split("=");
	var the_values = broken_cookie[1];
	// break each name:value pair into an array
	var separated_values = the_values.split(",");
	// loop through the list of name:values and load
	var property_value = "";
	for (var loop = 0; loop < separated_values.length; loop++){
		property_value = separated_values[loop];
		var broken_info = property_value.split(":");
		var the_property = broken_info[0];
		var the_value = broken_info[1];
		var  the_property2 = broken_info[2];
		var  the_value2 = broken_info[3];
		if (firstone == "yes"){
		     the_info[the_property] = the_value;
		     the_info[the_property2] = the_value2;
		     arraycount = arraycount + 1;
		}else{
		//Check to see if current URL already exists in array, If so, crumbTitleen breadcrumb list by not building the rest ot if
		     //The current page does not equal this cookie value - add it to array and proceed
		     if (subloc != the_value){
		         the_info[the_property] = the_value;
		         the_info[the_property2] = the_value2;
		         arraycount = arraycount + 1;
			  //The current page does exist in the cookie value but it's the first value so add to array
			  //Preserve the '0' spot in the array
		      }else{
			     if (loop == 0){
				 the_info["location0"] = subloc;
		         the_info["title0"] = crumbTitle;
				 arraycount = arraycount + 1;
		         return;
				 }else{
				 //The current pages does not exist in the cookie value so rearrange the array to accomidate just the values up to the duplicate 
				 the_info[("location" + arraycount)] = (the_info[("location" + (arraycount - 1))]);
				 the_info[("title" + arraycount)] = (the_info[("title" + (arraycount - 1))]);
				 
				 if (arraycount > 2){
				 for (var subloop = (arraycount -1); subloop > 1; subloop--){
				 the_info[("location" + subloop)] = (the_info[("location" + (subloop - 1))]);
				 the_info[("title" + subloop)] = (the_info[("title" + (subloop - 1))]);
				 }
				 }
				 
				 the_info["location1"] = (the_info["location0"]);
				 the_info["title1"] = (the_info["title0"]);
				 arraycount = arraycount + 1;				 
				 
				 
				 return;
				 }
		      }
	     }
	 }
	
}
//Output the breadcrumbs to the page
function breadmaker(){
  for (var loop = 0; loop <= (arraycount - 1); loop++){
          //First page don't worry about writing a crumb
          if(firstone == "yes"){
		  return;
		  }
		  //Not first page but only one cookie value so don't worry about writing any crumbs
		  if(firstone == "no"){
		  if (arraycount < 2){
		  return;
		  }
		  }
		  //Not first page and more than one cookie value - write crumbs
		  if (arraycount >= 2){
		        //First crumb gets written even if it matches the 0 spot
		        if (loop == 0){
				document.write("<a class='bclink' href=http://"+(cookie_information[("location" + (loop + 1))])+">"+(cookie_information[("title" + (loop + 1))])+"</a> ");
				}else{
				//All subsequent crumbs get written if they are not equal to the proceeding crumb or not the current page (LAST CRUMB)
				   if (loop != (arraycount - 1)){
		              if ((cookie_information[("location" + (loop + 1))]) != (cookie_information[("location" + loop)])){
	                  document.write("<a class='bclink' href=http://"+(cookie_information[("location" + (loop + 1))])+">"+(cookie_information[("title" + (loop + 1))])+"</a> ");
				      }
				   }
		        }
		    }
  

  }
}



readTheCookie(cookie_information);
breadmaker();

//This function will create a new cookie tacking on the current page as the last cookie value

var new_cookie="";
function newNavCookie(){
    //Under 5 crumbs just add the value on
    if (arraycount < 6) { 
    for (var loop = 0; loop <= (arraycount - 1); loop++){  
	//document.write("<br> location" + loop + "=" + cookie_information[("location" + loop)]);
	new_cookie = new_cookie + "location" + loop + ":" + cookie_information[("location" + loop)] + ":" + "title" + loop + ":" + cookie_information[("title" + loop)] + ",";
	//document.write(cookie_information[("location" + loop)]);
	}
	//Crumb quota at 5 so drop first crumb and rearrange values to accomdate new one
	}else{
	 for (var loop = 0; loop < (arraycount -  1); loop++){   
	// document.write("<br> location" + loop + "=" + cookie_information[("location" + (loop + 1))]);
	 new_cookie = new_cookie + "location" + loop + ":" + cookie_information[("location" + (loop + 1))] + ":" + "title" + loop + ":" + cookie_information[("title" + (loop + 1))] + ",";
	 }
	}
	
	//Determine current location
	var  x = location.toString();
	x = x.substring(x.indexOf("/")+2);

	//Add new value
	if (arraycount < 6){
	new_cookie = new_cookie + "location" + arraycount + ":" + x + ":" + "title" + arraycount + ":" + crumbTitle;
	document.cookie= "navCookie=" + escape(new_cookie);
	//Add new value for quota list
	}else{
	new_cookie = new_cookie + "location" + (arraycount - 1) + ":" + x  + ":" + "title" + (arraycount - 1) + ":" + crumbTitle;
	document.cookie= "navCookie=" + escape(new_cookie);
	}
}

newNavCookie();