
var base_title	= 'Citi at Work - ';	// <- strip this off of the title, if specified
var cookie_name	= 'citiatwork_recent';	// <- name of the cookie
var max_items	= 10;					// <- max items to store in the cookie
var days		= 1;					// - number of days to store the cookie

/* create the expires date: */
var date = new Date();
date.setTime(date.getTime() + (days*60*60*24*1000));
var expires = date.toGMTString();

/* read and set the cookie: */
var cookie_items = get_cookie_items();
set_recentlist_cookie(cookie_items);


/* ---- parse any existing cookies ----*/
function get_cookie_items() {
	var cookie = get_recentlist_cookie(cookie_name);
	var recent_items = new Array();
	if(cookie == null) {
		return recent_items;
	}
	// split the cookie into the title/url pairs:
	var sets = cookie.split('||');
	
	// split the title/urls pairs:
	for(var i = 0; i < sets.length; i++) {
		var pair = sets[i].split('==');
		recent_items[recent_items.length] = pair;
	}
	return recent_items;
}

/* ---- set the cookie ----*/
function set_recentlist_cookie(cookie_items) {
	// prep the cookie value:
	var value_set = prepare_current_value();
	if(value_set == null) {
		return false;
	}
	var value = '';
	
	// we'll remove any duplicates found, and we'll also keep the total items
	// under the max_items limit:
	var seen = {};
	var start = cookie_items.length >= max_items ? cookie_items.length - max_items : 0;
	for(var i = start; i < cookie_items.length; i++) {
		if(value_set[0] != cookie_items[i][0]) {
			if(seen[cookie_items[i][0]] == null) {
				seen[cookie_items[i][0]] = 1;
				if(value) {
					value += '||';
				}
				value += cookie_items[i][0] + '==' + cookie_items[i][1];
			}
		}
	}
	if(value) {
		value += '||';
	}
	value += value_set[0] + '==' + value_set[1];
	// now build and write the cookie:
	value = escape(value);
	document.cookie = cookie_name + '=' + value + '; expires=' + expires + '; path=/';
}

/* ---- get the raw cookie ----*/
function get_recentlist_cookie(name) {
	var use_name = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0; i < ca.length; i++) {
		var c = ca[i].replace(/^\s+/, '');
		if(c.indexOf(use_name) == 0) {
			return unescape(c.substring(use_name.length,c.length));
		}
	}
	return null;
}

/* ---- prepare the current value of the cookie ----*/
function prepare_current_value() {
	// we'll use the current page's title as a descriptor:
	var title = document.getElementsByTagName('TITLE').item(0).innerHTML;
	// we're also ignoring the base index page, so items listed must have a
	// title that is prefixed with the 'base_title'
	if(title == null || !title.match(base_title)) {
		return null;
	}
	title = title.replace(base_title, '');

	var url = window.location.href;
	if(url == null) {
		return false;
	}
	url = url.replace(window.location.protocol + '//' + window.location.host, '');
	url = url.replace(/\bcompany=[\w\d]+/g, '');
	url = url.replace(/\?$/g, '');
	url = url.replace(/\?/g, '~');
	url = url.replace(/=/g, '!');
	url = url.replace(/\//g, ';');
	return [title,url];
}

/* ---- draw the items stored in the cookie to the webpage ----*/
function draw_recent_items(count) {
	if(count == null || !count) {
		count = 3;
	}
	var max = cookie_items.length < count ? cookie_items.length : count;
	var min = cookie_items.length - max;
	var store = new Array();
	for(var i = cookie_items.length - 1; i >= min; i--) {
		store[store.length] = cookie_items[i];
	}
	store = store.sort(sortNames);
	for(var i = 0; i < store.length; i++) {
		var title = unescape(store[i][0]);
		if(title.match('&')) {
			title = title.replace(/&lt;/g, '<');
			title = title.replace(/&gt;/g, '>');
		}
		var url = store[i][1];
		url = url.replace(/~/g, '?');
		url = url.replace(/!/g, '=');
		url = url.replace(/;/g, '/');
		document.write('<a href="' + unescape(url) + '">' + title + '</a><br>');
	}
}

function has_recent_items() {
	var has = cookie_items.length > 0 ? 1 : 0;
	return has;
}

/* ---- custom sort for the two-dimensional array ----*/
function sortNames(a,b) {
	return a[0] == b[0] ? 0 : a[0] < b[0] ? -1 : 1;
}

/**
 * toggleDisclosures - does some cool stuff eh?
 */
function toggleDisclosures() {
	var $disclosure = $('#disclosure');
	if( $disclosure.length > 0 ) {
		if( $disclosure.height() < 50 ) {
			$disclosure.animate({'height': '134px'});
			$('#disclosure a:first').html('[&ndash;] <span>Less</span>');
		}
		else {
			$disclosure.animate({'height': '40px'});
			$('#disclosure a:first').html('[+] <span>More</span>');
		}
	}
	
	return false;
}

