/*  
	Title : Cookie Crumbs
	Author : Tom Coote
	Wedsite : http://www.tomcoote.co.uk
*/

 /**
	* @argument iNumberOfCrumbs - How many crumbs do you want to show on the page, ie the last 5 page views!
	*
	*/
function CookieCrumbs(iNumberOfCrumbs) {
    
	var that = {}; 

	/* Private Variables */
	var sCookieCrumbsSeparator = '|',	// The value used to separate each 'link & title' pair in the cookie
		sLinkTitleSeparator = '::';		// The value used to separate link from title for each pair
		/* NOTE: don't use the above values in your page titles */


    /* Private Functions */

	function getCookie(name) {
		var start = document.cookie.indexOf(name + "=");
		var len = start + name.length + 1;
		if ((!start) && (name != document.cookie.substring(0, name.length))) {
			return null;
		}

		if (start == -1) {
			return null;
		}

		var end = document.cookie.indexOf( ";", len );

		if (end == -1) {
			end = document.cookie.length;
		}

		return unescape(document.cookie.substring(len, end));
		
	}

	function setCookie(name, value, expires, path, domain, secure) {
		var today = new Date();
		today.setTime(today.getTime());

		if (expires) {
			expires = expires * 1000 * 60 * 60 * 24;
		}

		var expires_date = new Date(today.getTime() + (expires));
		
		document.cookie = name+"="+escape(value) +
			((expires) ? ";expires="+expires_date.toGMTString() : "") + 
			((path) ? ";path=" + path : "") +
			((domain) ? ";domain=" + domain : "") +
			((secure) ? ";secure" : "");

		return getCookie(name);
	}

	function deleteCookie(name, path, domain) {
		if (getCookie(name)) {
			document.cookie = name + "=" +
			((path) ? ";path=" + path : "") +
			((domain) ? ";domain=" + domain : "") +
			";expires=Thu, 01-Jan-1970 00:00:01 GMT";
		}
	}

	function setLocation(iPos) {
		var sLocation = formatLastPage(document.location.toString());
		var prevLoc = '';
		if (sLocation !== null) {
			if (sLocation.indexOf('?') !== -1) {
				return sLocation + '&ccp=' + iPos;
			}
			else {
				return sLocation + '?ccp=' + iPos;
			}
		}
	}

	function sliceCookie(Cookie) {
		var sLocation = document.location.toString(),
			iPos = -1, sCookies = '', sCrumbTrail = '', i = 0;

		if (sLocation.indexOf('?ccp=') !== -1) {
			sLocation = sLocation.slice(sLocation.indexOf('?ccp=')+5, sLocation.length); // will always be at the end so can use length
			iPos = parseInt(sLocation,10);
		}

		if (sLocation.indexOf('&ccp=') !== -1) {
			sLocation = sLocation.slice(sLocation.indexOf('&ccp=')+5, sLocation.length);
			iPos = parseInt(sLocation,10);
		}
		
		if (iPos === -1) {
			return Cookie;
		}
		else {
			sCookies = Cookie.split(sCookieCrumbsSeparator);

			if (sCookies.length-1 === iPos) {
				return Cookie;
			}

			for (i=0; i < iPos; i++) {
				sCrumbTrail += sCookieCrumbsSeparator + sCookies[i]; 
			}
		}

		sCrumbTrail = (sCrumbTrail === '') ? '||' : sCrumbTrail;
		return setCookie('CookieCrumbs', sCrumbTrail.replace(sCookieCrumbsSeparator,''), 1);
	}

	function formatLastPage(sLastPage) {

		if (sLastPage.indexOf('?ccp=') !== -1) {
			sLastPage = sLastPage.replace(sLastPage.slice(sLastPage.indexOf('?ccp='), sLastPage.length),'');
		}

		if (sLastPage.indexOf('&ccp=') !== -1) {
			sLastPage = sLastPage.replace(sLastPage.slice(sLastPage.indexOf('&ccp='), sLastPage.length),'');
		}

		return sLastPage;
	}

	function canUpdateExistingCookie(sLastPage) {
		if (document.referrer.toString() === document.location.toString()) {
			return false;
		}

		if (document.referrer.toString() === '') {
			return false;
		}

		if (formatLastPage(sLastPage) === document.location.toString()) {
			return false;
		}

		if (formatLastPage(sLastPage) === formatLastPage(document.location.toString())) {
			return false;
		}

		return true;
	}


	/* Public Functions */
	function Left(str, n){
		if (n <= 0)
			return "";
		else if (n > String(str).length)
			return str;
		else
			return String(str).substring(0,n);
	}
	
	function Right(str, n){
		if (n <= 0)
		   return "";
		else if (n > String(str).length)
		   return str;
		else {
		   var iLen = String(str).length;
		   return String(str).substring(iLen, iLen - n);
		}
	}
	
	
		
	that.GetCrumbs = function() {
		try
		{
			var PageTitleTemp = document.getElementsByTagName('title')[0].innerHTML;
			var ActualPageTitle = Right(PageTitleTemp, (PageTitleTemp.length - (PageTitleTemp.lastIndexOf('-')+2)));
			
			var Cookie = getCookie('CookieCrumbs'),
				sCrumbTrail = '',
				sPageTitle = ActualPageTitle || '',
				sCookies = '', sTitle = '', sLink = '', i = 0;
			
			if (document.referrer.toString() === '') {
				Cookie = setCookie('CookieCrumbs', sCookieCrumbsSeparator + setLocation(0) + sLinkTitleSeparator + sPageTitle, 1);
			}
			else if (Cookie !== null) {
				Cookie = sliceCookie(Cookie);	
				sCookies = Cookie.split(sCookieCrumbsSeparator);
				sLastPage = sCookies[sCookies.length-1].split(sLinkTitleSeparator)[0];
				if (canUpdateExistingCookie(sLastPage) && sLastPage !== ActualPageTitle) {
					Cookie = setCookie('CookieCrumbs', Cookie + sCookieCrumbsSeparator + setLocation(sCookies.length) + sLinkTitleSeparator + sPageTitle, 1);
				}
			}
			else {
				Cookie = setCookie('CookieCrumbs', sCookieCrumbsSeparator + setLocation(0) + sLinkTitleSeparator + sPageTitle, 1);
			}

			if (Cookie === null) { return ''; } // for browsers that have cookies blocked

			sCookies = Cookie.split(sCookieCrumbsSeparator);
			if (iNumberOfCrumbs === -1) {
				iNumberOfCrumbs = sCookies.length;
			}
			else {
				iNumberOfCrumbs = (sCookies.length < iNumberOfCrumbs) ? sCookies.length : iNumberOfCrumbs;
			}
			var PrevLink = '';
			for (i=sCookies.length-1; i > sCookies.length-1-iNumberOfCrumbs; i--) {
			
				if (sCookies[i] !== '') {
					var sLinkTitle = sCookies[i].split(sLinkTitleSeparator);
					if (sLinkTitle[0] !== "undefined" && sLinkTitle[1] !== "undefined" && PrevLink !== sLinkTitle[1]) {
						PrevLink = sLinkTitle[1];
						sCrumbTrail = '<img src="/images/crumbs.png" border="0" class="imgFIX" alt="Bread Crumbs" width="30" height="15" style="vertical-align:bottom;" \/><a href=\'' + sLinkTitle[0] + '\'>' + sLinkTitle[1] + '</a> ' + sCrumbTrail;
					}
				}
			}

			return sCrumbTrail;
		}
		catch(err) {
			//return err;  // For debugging
			return '';
		}
	};

	that.DeleteCookieTrail = function() {
		deleteCookie('CookieCrumbs');
	};

    return that;
}
