/*
(c) 2002 - now :: rlidstone

	not to be used for controlling aircraft... that would be silly.
*/

// config variables - these may be changed for your environment
var filter = /e-Ticket: /g;				// anything matched by this regex will be stripped from the title and replaced with...
var filterReplace = '';                 // ...this
var homeTitle = "Event Selection";
var homeLoc = "/Ticketing";

// script variables - you don't need to mess with these
var cName = "bctrace";
var uName = "bcURL";
var cDelim = "@";
var homeRegex = new RegExp('^https?:\/\/[^\/]+' + slashify(homeLoc) + '$', 'i');

if(homeRegex.test(location.href))	// if we're in the home folder
{
	snap(0);			// ... remove residue
}

var title = document.title.replace(filter, filterReplace);				// filter title
var loc = location.href.replace(/^https?:\/\/[^\/]+/i, '');				// remove protocol and domain

add(title, loc);


var titles = parseCookie(cName);

if(titles[0] != homeTitle)
{
	snap(0);			// ... remove residue
    add(homeTitle, homeLoc);
    add(title, loc);
    titles = parseCookie(cName);
}

var urls = parseCookie(uName);
var preceedingSection = "";

for(var i=0; i<titles.length-1; i++)
{
	if(titles[i] == title)	// && urls[i] == loc)
	{
		snap(i+1);
		break;
	}
	preceedingSection += '<a href="' + urls[i] + '">' + titles[i] + '</a>&nbsp;<span>&raquo;</span>&nbsp;'; 
}
document.write(preceedingSection + title);



// ------------- FUNCTIONS --------------------

function add(title, loc)
{
    addToCookie(cName, title);
    addToCookie(uName, loc);
}


function slashify(str)
{
	return str.replace(/([\/\.\!\$\(\)\*\+\:\=\\\[\]\{\}\^\|])/g, '\\$1');
}

function snap(idx)
{
	snapCookie(cName, idx);
	snapCookie(uName, idx);
}



function parseCookie(cookName)
{
	return getCookie(cookName).split(cDelim);
}


function addToCookie(cookName, cookVal)
{
	if(getCookie(cookName))
	{
		setCookie(cookName, getCookie(cookName) + ((cDelim) ? cDelim : "") + cookVal);
	}else{
		setCookie(cookName, cookVal);
	}
}


function setCookie(cookName, cookVal)
{
	document.cookie = cookName + "=" + cookVal + ";path=/";
}


function getCookie(cookName)
{
	var pref = cookName + "=";
	var pos = document.cookie.indexOf(pref) + pref.length;
	if(pos >= pref.length)
	{
		var end = document.cookie.indexOf(";", pos);
		if(end == -1) { end = document.cookie.length; }
		return document.cookie.substring(pos, end);
	}
	return "";
}


function deleteCookie(cookName) 
{
	if (getCookie(cookName)) 
	{
		document.cookie = cookName + "=" + "; expires=Thu, 01-Jan-70 00:00:01 GMT";
	}
}


function snapCookie(cookName, num)
{
	var aVal = parseCookie(cookName);
	var cVal = "";
	
	for(var i=0; i<num; i++)
	{
		cVal += (i > 0 ? cDelim : "") + aVal[i];
	}
	setCookie(cookName, cVal);
}