/*
 * history.js
 */

function setCookie( name, value ) {
	var d = new Date();
	d.setDate( d.getDate() + 7 ); // one week into the future.

	document.cookie = name + "=" + value + "; expires=" + d.toGMTString() + "; path=/";
}


function getCookie(name) {
	var na = name + '=';

	var out = null;

	var c = document.cookie;

	if ( c ) {
		var ca = c.split( ';' );
	}

	for ( var i in ca ) {
		var v = ca[i];

		var n = v.charAt(0) == ' ' ? v.substring(1, v.length) : v;

		if ( n.indexOf(na) == 0 ) {
			out = n.substring( na.length, n.length );

			break ;
		}
	}

	return out;
}


function historyAppend( name, item ) {
	var h = getHistory( name );

	var i = 0;

	// Remove any duplicates.
	// (Deleting items from an array is kind of odd in Javascript)
	while ( i < h.length ) {
		if ( h[i] == item ) {
			h.splice( i, 1 );
		} else {
			i++;
		}
	}

	var size = h.length;

	if ( size >= 10 ) {
		// Remove the oldest entries when array contain > 10 entires.
		for ( var i = size; i >= 10; i-- ) {
			h.shift();
		}
	}
	h.push( item );

	setCookie( name, h.toString() );
}


function getHistory( name ) {
	var h = new Array();
	var c = getCookie( name );

	if ( c ) {
		h = c.split( ',' );
	} 


	return h;
}
