/* CVSINFO: $Author: ccx305 $ $Date: 2005/09/13 12:51:43 $ $Source: /opt/cvsroot/cmsroot/shared/js/breadcrumb.js,v $ $Revision: 1.17 $ */

/*
 * Copyright Coventry University
 *
 * Description: manages a breadcrumb trail as a fixed size list of last visited pages.  As new
 * pages are visited the oldest will drop of the start of the list once it has reached max size.
*/

/*
 * models the idea of a breadcrumb entry
 * @param url the url for the entry
 * @param title the title to display
 */
function BreadCrumbEntry(url, title){
	this._url = url;
	this._title = title;
}


BreadCrumbEntry.prototype.getUrl = function(){
	return this._url;
}

BreadCrumbEntry.prototype.getTitle = function(){
	return this._title;
}

/*
 * models the idea of a cookie based breadcrumb that provides a fixed length trail
 * of the last 'n' pages visited
 * attempts to read in any existing breadcrumb cookie, adding the current page to it if read
 * and the entry isn't already present - writing any updated cookie.  Otherwise it creates
 * the cookie with the current page as the initial value.
 * @param document the current document
 */
function BreadCrumb(context, document){
	var root = document.URL.substring(0, document.URL.indexOf('/', 7)) + context;

	if(document.URL.lastIndexOf('/') == document.URL.length-1){
		root += '/';
	}
	
	/* 
	 * use a sensible default where there is no context otherwise we can end up defaulting
	 * to an unsuitable value e.g. the current url path
	 */
	if(context.length == 0){
		this._context = '/';
	}else{
		this._context = context;
	}
	this._document = document;
	var title = getTrimmedTitle(document);

	/* reset breadcrumb each time we go to Home */
	if(this.read() && document.URL != root ){
		/* read the breadcrumb successfully */
		if(this.addEntry(document)){
			/* and updated it - so write the updates */
			this.write();
		}
	}else{
		/* create a new breadcrumb */
		this._entries = new Array();
		this._entries[0] = new BreadCrumbEntry(root, 'Home');
		if(document.URL != root){
			this._entries[this._entries.length] = new BreadCrumbEntry(document.URL, title);
		}	
		this.write();
	}
}

/*
 * strip off any Coventry University - title prefix
 */
function getTrimmedTitle(document){
	var trimmedTitle = document.title;
	var pos = trimmedTitle.indexOf(' - Coventry University');
	if(pos > -1){
		trimmedTitle = trimmedTitle.substring(0, pos);
	}
	return trimmedTitle;
}

/*
 * @return the array of breadcrumb entries
 */
BreadCrumb.prototype.getEntries = function(){
	return this._entries;
}

/*
 * provided the the page has changed, add the new entry to relect this
 * on the end of the breadcrumb trail.  if the trail is already at its
 * maximum size we lose the first item
 * @param document the current document
 * @return true if updated
 */
BreadCrumb.prototype.addEntry = function(document){
	var title = getTrimmedTitle(document);
	/* check we don't already have the current page as the last visited */
	if(this._entries[this._entries.length - 1].getUrl() != document.URL){
		if(this._entries[this._entries.length - 1].getTitle() == 'Search' && title == 'Search'){
			/* got an updated repeat search - save the current parameters in the breadcrumb */
			this._entries[this._entries.length - 1] = new BreadCrumbEntry(document.URL, title);
			return true;
		}
		/* find the insertion point */
		var insertionPoint;
		if(this._entries.length == 4){
			/* already at the max number of entries so shuffle them down 
			   to make room at the end */
			for(var i = 1; i < 3; i++){
				this._entries[i] = this._entries[i+1];
			}
			insertionPoint = 3;
		}else{
			insertionPoint = this._entries.length;
		}
		/* add the new entry */
		this._entries[insertionPoint] = new BreadCrumbEntry(document.URL, title);
		return true;
	}else{
		return false;
	}
}

/*
 * save the current breadcrumb
 */
BreadCrumb.prototype.write = function(){
	/* assemble the value as an '&' separated list of escaped key - value pairs themselves
	   separated by ':' */
	var breadCrumbValue = "";
	for(var i = 0; i < this._entries.length; i++){
		var entry = this._entries[i];
		if (breadCrumbValue != ""){
			breadCrumbValue += '&';
		}
		breadCrumbValue += escape(entry.getUrl()) + '|' + escape(entry.getTitle());
	}
	var cookie = 'breadCrumb=' + breadCrumbValue + '; path=' + this._context + ';';
	/* and set it */
	this._document.cookie = cookie;
}

/*
 * see we if we can find the breadcrumb cookie returning false if no
 * if we do find it, read in it's entries, with a sanity check to make sure we have at least one
 * @return true if found and read correctly
 */
BreadCrumb.prototype.read = function(){
	/* get all the cookies */
	var cookies = this._document.cookie;
	if (cookies == "") return false;

	/* isolate the value of the breadcrumb cookie if present */
	var breadCrumbStart = cookies.indexOf('breadCrumb=');
	if (breadCrumbStart == -1) return false;   
	breadCrumbStart += 'breadCrumb'.length + 1;  
	var breadCrumbEnd = cookies.indexOf(';', breadCrumbStart);
	if (breadCrumbEnd == -1) breadCrumbEnd = cookies.length;
	var breadCrumbValue = cookies.substring(breadCrumbStart, breadCrumbEnd);

	/* decode the value into our breadcrumb */
	var a = breadCrumbValue.split('&');    
	var entries = new Array();

	for(var i=0; i < a.length; i++){
		var parts = a[i].split('|');
		/* sanity check to make sure this is valid */
		if(parts.length == 2){
			/* create the new entry for this */
			entries[i] = new BreadCrumbEntry(unescape(parts[0]), unescape(parts[1]));
		}
	}

	if(entries.length == 0){
		return false;
	}

	this._entries = entries;
	return true;
}

/*
 * get rid of the breadcrumb cookie
 */
BreadCrumb.prototype.destroy = function(){
	this._document.cookie = 'breadCrumb=; expires=Fri, 02-Jan-1970 00:00:00 GMT; path=' +
		this._context + ';';
}


