//cookies "usb007breadCrumbs" for store titles and URLs
//cookies "usb007breadCrumbsCount" for store count of breadCrumbs

// THE NAME OF COOKIES ARE CHANGED        13 SEP 2007
//cookies "usb007breadCrumbsUrl" for store titles and URLs
//cookies "usb007breadCrumbsCounter" for store count of breadCrumbs


//max count of bread crumbs which will be displayed
var maxBreadCrumbs = 10;
//the path of bullet image
//var bulletImages = "images/bullet.gif";
var trailIconPath = "./images/trail/";

function addBreadCrumbs(title, urlLocation){
	if(title != undefined && urlLocation!= undefined){
		var maxcount = maxBreadCrumbs + 1;
		//set expiration date of bread crumbs cookies
		var expdate = new Date();
		expdate.setTime (expdate.getTime() +  (24 * 60 * 60 * 1000 * 365));//1 year
		var content = getCookie("usb007breadCrumbsUrl");

		if (content!=null){//not first time visit
			var count = getCookie("usb007breadCrumbsCounter");
			count = count*1;
			if(count < maxcount){
				//separate 'title' and 'urlLocation' with '$'
				setCookie("usb007breadCrumbsUrl", content+"$"+title+"$"+ urlLocation, expdate);
				count += 1;
			}else{
				//first in first out bread crumbs,
				//new content not include the oldest
				var newcontent = "";
				var name = content.split("$")
				for (i=2 ; i <maxcount*2-1 ; i+=2){
					var currenttitle = name[i];
					var currentlink = name[i+1];
					newcontent += currenttitle+"$"+currentlink+"$";
				}
				setCookie("usb007breadCrumbsUrl", newcontent+title +"$"+ urlLocation, expdate);
				count = maxcount;
			}
			setCookie("usb007breadCrumbsCounter",count, expdate);
		}else{//first time visit
			setCookie("usb007breadCrumbsUrl", title +"$"+ urlLocation, expdate);
			setCookie("usb007breadCrumbsCounter",1, expdate);
		}
	}
}

function showBreadCrumbs(){
	var rect = getCookie("usb007breadCrumbsUrl");
	var count = getCookie("usb007breadCrumbsCounter");
	if(rect != null && count != null){
		if(count >1){//display if not first time visit
			var name = rect.split("$")
			//reverse array for display descending order
			name.reverse()

			//not display the most new breadCrumb,
			//becasue it is current page breadCrumb
			document.write("<table class='breadCrumbslist' style='width:100%'>");
			for (i=2 ; i <count*2-1 ; i+=2){
				var urlLocation = name[i];
				var title = name[i+1];
				if(title != undefined && urlLocation!= undefined){
					var icon = "";
					var pagename = urlLocation.substring(urlLocation.lastIndexOf('/') + 1);
					if(pagename == ""){
						icon = trailIconPath + "index.png";
					}else{
						icon = trailIconPath + pagename.substring(0, pagename.lastIndexOf('.')) + ".png";
					}

					document.write ( "<tr><td><img src='"+ icon + "' style='margin-left:10px'></td><td><a class='breadCrumbs' href ='" +urlLocation+ "'>"+title +"</a></td></tr>");
				}
			}
			document.write("</table>");
		}
	}
}

function clearBreadCrumbs(){
	deleteCookie("usb007breadCrumbsUrl");
	deleteCookie("usb007breadCrumbsCounter");
	location.reload();
}
