
if (typeof (String.prototype.ltrim)	=== "undefined")	{ String.prototype.ltrim = function (chars) { return this.replace (new RegExp ("^[" + (chars || "\\s") + "]+",  "g"), "");	}; }
if (typeof (String.prototype.rtrim)	=== "undefined")	{ String.prototype.rtrim = function (chars) { return this.replace (new RegExp ( "[" + (chars || "\\s") + "]+$", "g"), "");	}; }
if (typeof (String.prototype.trim)	=== "undefined")	{ String.prototype.trim  = function (chars) { return this.ltrim (chars).rtrim (chars);					}; }

if (typeof (Array.prototype.indexOf)	=== "undefined")
{
	Array.prototype.indexOf	= function (v, b, s)
	{
		for (var x = +b || 0, l = this.length; x < l; x++)
			if (this[x] === v || s && this[x] == v)
				return x;
		return -1;
	};
}


// Data Rotator - this version can only handle one DataRotator object per document!
var DataRotator =
{
	pres:	[],	// Image Preloads
	imgs:	[],	// Image URIs
	lnks:	[],	// Link URIs
	dats:	[],	// Date Texts
	ttls:	[],	// Title Texts
	txts:	[],	// Descriptive Texts (often contains HTML)

	lmns:		// DOM elements that the above attach to.
	{
		img:	null,
		lnk:	null,
		dat:	null,
		ttl:	null,
		txt:	null
	},

	ivl:	 null,	// Interval object (regular rotation)
	del:	 null,	// Delay object (short pause in rotation when someone clicks an arrow)
	pos:	    0,	// Position in rotation
	len:	    0,	// Length of rotation
	tck:	 7500,	// Interval tick (ms)
	tim:	30000,	// Delay time (ms)
	pau:	false,	// Is rotation paused?

	Init:	function (img, lnk, dat, ttl, txt)
	{
		if (img) DataRotator.lmns.img	= document.getElementById (img);
		if (lnk) DataRotator.lmns.lnk	= document.getElementById (lnk);
		if (dat) DataRotator.lmns.dat	= document.getElementById (dat);
		if (ttl) DataRotator.lmns.ttl	= document.getElementById (ttl);
		if (txt) DataRotator.lmns.txt	= document.getElementById (txt);

		var els = document.getElementsByTagName ("a");
		if (els) for (var x = els.length; --x; )
		{
			if (els[x].rel == "rot") switch (els[x].rev)
			{
				case "prev": els[x].onclick = DataRotator.Prev; break;
				case "paus": els[x].onclick = DataRotator.Paus; break;
				case "play": els[x].onclick = DataRotator.Play; break;
				case "next": els[x].onclick = DataRotator.Next; break;
			}
		}
	},

	Append:	function (img, lnk, dat, ttl, txt)
	{
		if (img)
		{
			DataRotator.pres.push (new Image ());
			DataRotator.pres[DataRotator.pres.length - 1].src = img;
		}

		DataRotator.imgs.push (img ? img : null);
		DataRotator.lnks.push (lnk ? lnk : null);
		DataRotator.dats.push (dat ? dat : null);
		DataRotator.ttls.push (ttl ? ttl : null);
		DataRotator.txts.push (txt ? txt : null);

		DataRotator.len++;
	},

	Rotate:	function (x)
	{
		DataRotator.pos				= (x + DataRotator.len) % DataRotator.len;

		if (DataRotator.lmns.img)
		{
			//DataRotator.lmns.img.src		= DataRotator.imgs[DataRotator.pos];
			//DataRotator.lmns.img.alt		= DataRotator.ttls[DataRotator.pos];
			DataRotator.lmns.img.style.backgroundImage	= "url(" + DataRotator.imgs[DataRotator.pos] + ")";
		}

		if (DataRotator.lmns.lnk)
			DataRotator.lmns.lnk.href		= DataRotator.lnks[DataRotator.pos];

		if (DataRotator.lmns.dat)
		{
			DataRotator.lmns.dat.innerText		=
			DataRotator.lmns.dat.textContent	= DataRotator.dats[DataRotator.pos];
		}

		if (DataRotator.lmns.ttl)
		{
			DataRotator.lmns.ttl.innerText		=
			DataRotator.lmns.ttl.textContent	= DataRotator.ttls[DataRotator.pos];
		}

		if (DataRotator.lmns.txt)
			DataRotator.lmns.txt.innerHTML		= DataRotator.txts[DataRotator.pos];

		return false;
	},

	Play:	function ()
	{
		DataRotator.ivl = setInterval (DataRotator.Inc, DataRotator.tck);
		DataRotator.pau = false;
		return false;
	},

	Paus:	function ()
	{
		clearInterval (DataRotator.ivl);	DataRotator.ivl = null;
		clearTimeout  (DataRotator.del);	DataRotator.del = null;

		DataRotator.pau = true;
		return false;
	},

	Clck:	function (fnc)
	{
		clearInterval (DataRotator.ivl);	DataRotator.ivl = null;
		clearTimeout  (DataRotator.del);	DataRotator.del = null;

		if (!DataRotator.pau) DataRotator.del = setTimeout (DataRotator.Play, DataRotator.tim);

		return fnc ();
	},

	Inc:	function () { return DataRotator.Rotate (++DataRotator.pos); },
	Dec:	function () { return DataRotator.Rotate (--DataRotator.pos); },

	Next:	function () { return DataRotator.Clck (DataRotator.Inc); },
	Prev:	function () { return DataRotator.Clck (DataRotator.Dec); }
};


// Make cookies easier to deal with
var CookieJar =
{
	set: function (name, value, days)
	{
		var expires = "";

		if (days)
		{
			var date = new Date ();
			date.setTime (date.getTime () + (days * 86400000));
			expires = "; expires=" + date.toGMTString ();
		}

		document.cookie = name + "=" + value + expires + "; path=/";
	},

	get: function (name)
	{
		var ca		= document.cookie.split (";");

		for (var i = ca.length; i--; )
		{
			var c = ca[i].trim ().split ("=");
			if (c[0] == name) return c[1];
		}

		return null;
	},

	clr: function (name)
	{
		this.set (name, "", -1);
	}
};


// Sourced from http://www.cherny.com/webdev/27/domloaded-updated-again
var DomLoaded =
{
	onload: [],

	loaded: function ()
	{
		if (arguments.callee.done) return;
		arguments.callee.done = true;
		for (var x = DomLoaded.onload.length; x--; )		//for (var x = 0; x < DomLoaded.onload.length; x++)
			DomLoaded.onload[x] ();
	},

	load: function (fireThis)
	{
		this.onload.unshift (fireThis);				//this.onload.push (fireThis);

		if (document.addEventListener)
			document.addEventListener ("DOMContentLoaded", DomLoaded.loaded, null);

		if (/KHTML|WebKit/i.test (navigator.userAgent))
		{
			var _timer = setInterval (function ()
			{
				if (/loaded|complete/.test (document.readyState))
				{
					clearInterval (_timer);
					delete _timer;
					DomLoaded.loaded ();
				}
			}, 10);
		}

/*@cc_on @*/
/*@if (@_win32)
		var proto = "src='javascript:void (0)'";
		if (location.protocol == "https:") proto = "src=//0";
		document.write ("<scr" + "ipt id=__ie_onload defer " + proto + "><\/scr" + "ipt>");
		document.getElementById ("__ie_onload").onreadystatechange = function ()
		{
			if (this.readyState == "complete")
			{
				DomLoaded.loaded ();
			}
		};
/*@end @*/

		window.onload = DomLoaded.loaded;
	}
};

