//**************************************************************
//cookie.js is needed for successfull execution of this script!!
//**************************************************************

var del = '-+-'				//String delimiter used to seperate different items in cookie
var delurl = '-*-'			//String delimiter used to seperate title from url in cookie

var breadcrumbElement = 'tdBreadCrumb'	//id of html element in which the breadcrumb is shown
var cookieName	= 'breadcrumb'		//name of the session cookie used to store the breadcrumdata

var breadcrumbDivider = ' &gt; '	//visual divider between breadcrumb items

var noCookieMessage="Om het kruimelpad te gebruiken moeten cookies geactiveerd zijn.";	//Message shown when no cookies disabled

var BreadcrumbDepth = 5;		//Number of items shown in the breadcrumb

//This function must be called from the onload of the page, and handles the tracking title + url into the cookie.
function handleBreadCrumb(t) {
	var bc=retrieveCookie(cookieName);
	var bcurl=window.location;
	
	if(bc==null||bc=='') {
		//No cookie, store first title - url combination
		bc=t + delurl + bcurl;
	} else {
		//cookie available. Add title - url combination
		var abc=bc.split(del)
		if(!(abc[abc.length-1].split(delurl)[1]==bcurl)) {	//Check if the current url is the same as the last url in cookie. If so, do nothing...
			bc+=del + t + delurl + bcurl;			//Otherwise, add the new title+url to the cookiedata...
		}
	}

	if(setCookie(cookieName, bc)) {
		//Cookies available, show breadcrumb...
		showBreadCrumb();
	} else {
		//Cookies disabled, show message...
		setCookieDisabledMessage();
	}
}

//This function shows an message when cookies are disabled
function setCookieDisabledMessage() {
	var el=document.getElementById(breadcrumbElement)
	el.innerHTML+=noCookieMessage;
}

//This function shows the current breadcrumb
function showBreadCrumb() {
	var bc=retrieveCookie(cookieName);
	var abc=bc.split(del)			//Array of Breadcrumb elements
	var el=document.getElementById(breadcrumbElement) //Element where Breadcrum is shown
	var t=''					//Temporary string to store result
	var s=abc.length-1-BreadcrumbDepth;		//Arrayitem from where breadcrumb should be showed
	var h=BreadcrumbDepth*-1;			//Helper var to determine how many items back in history is a breadcrumb item.

  if(!el) return;
	
	if(s<1){				//if the start < 1, then less then <breadcrumbDepth> items are in the cookie
		h=h-s;				//so we must make some modifications to the start.
		s=0;
	}

	for(var i=s;i<abc.length-1;i++) {	//loop through breadcrumb-items and put in temp string
		if(t!='') t+=breadcrumbDivider
		t+='<a onclick="clickBreadCrumb(' + h + ', \'' + abc[i].split(delurl)[1] + '\');" href="#">' + abc[i].split(delurl)[0].substring(0, 20) + '</a>'
		h++;
	}
	if(t=='') {
		el.innerHTML='';
	} else {
		el.innerHTML+=t;			//show temp string
	}
}

//This function handles the onclick event of a breadcrum item
function clickBreadCrumb(h,u) {
	var bc=retrieveCookie(cookieName);
	var abc=bc.split(del)			//Array of Breadcrumb elements
	var i=abc.length-1+h;			//Determine to which item the breadcrumb must be saved
	bc='';					
	for(var t=0;t<i;t++) {			//recreate cookie data
		if(bc=='') {
			bc=abc[t];
		} else {
			bc+= del + abc[t];
		}
	}
	if(bc=='') {
		//Cookiedata is empty, set time to -1, so it will be erased
		setCookie(cookieName, bc,-1);
	}else{
		//Set cookiedata
		setCookie(cookieName, bc);
	}
	
	//redirect to url clicked in breadcrumb...
	window.location.replace(u);
	return false;
}