jQuery(document).ready(function(){
	// Define needed cookie functions
	window.setCookie = function(name,value,days) {
		// alert("setting cookie "+name+"="+value);
		if (days) {
			var date = new Date();
			date.setTime(date.getTime()+(days*24*60*60*1000));
			var expires = "; expires="+date.toGMTString();
			}
		else var expires = "";
		document.cookie = name+"="+escape(value)+expires+"; path=/";
		}

	window.getCookie = function(name) {
		var nameEQ = name + "=";
		var ca = document.cookie.split(';');
		for(var i=0;i < ca.length;i++) {
			var c = ca[i];
			while (c.charAt(0)==' ') c = c.substring(1,c.length);
			if (c.indexOf(nameEQ) == 0) return unescape(c.substring(nameEQ.length,c.length));
			}
		return null;
		}

	window.eraseCookie = function(name) {
		createCookie(name,"",-1);
		}

	function setCrumb(hub_name) {
		var hub_path = window.location.pathname;
		var target=' target="_self"';;

		// Deal with differences between inside Cascade and outside Cascade
		if (hub_path.indexOf('/render/page.act') == 0) {
			hub_path = '/entity/open.act';
			target = ' target="_parent"';
			}
	
		hub_path += window.location.search;
		
		var crumb='<a href="'+hub_path+'"'+target+'>'+hub_name+'</a> &#187; ';
		setCookie('hub-crumb',crumb);
		}

	// Find hubParent spans and extract the information from them
	var parentInfo = jQuery("span.hubParent").text().split("|");
	if (parentInfo.length > 1) {
		setCookie("hub-id",parentInfo[0]);
		setCrumb(parentInfo[1]);
		}
		
	//Find hubChild spans and insert the breadcrumb into them
	var parentHubs = jQuery("span.hubChild").text();
	if (parentHubs != "") {
		currentHubID = getCookie("hub-id");
		currentHubCrumb = getCookie("hub-crumb");
		if (parentHubs.indexOf(currentHubID) != -1) {
			jQuery("span.hubChild").html(currentHubCrumb);
			jQuery("span.hubChild").css("display","inline");
			}
		}
});
