
// Open link in a new tab/window
function a_onclick_ext ()
{
	return !window.open (this.href);
}

// Set the font size of the elements listed in ids (can be an array or a string)
// Uses cookies for persistance
function a_onclick_fsize (ids, size, unit)
{
	if (typeof (ids) == "string")
		ids = [ids];

	for (c = 0, x = ids.length; x--; )
	{
		CookieJar.set ("fontEl_" + c++, ids[x], 90);

		if (el = document.getElementById (ids[x]))
			el.style.fontSize = size + unit;
	}

	CookieJar.set ("fontSize", size, 90);
	CookieJar.set ("fontUnit", unit, 90);
	CookieJar.set ("fontNum",  c,    90);

	return false;
}

// Image mouseover swaps
function a_onmouseover_img ()
{
	if (!this.targetElement) this.targetElement		= document.getElementById (this.rev);
	if ( this.targetElement) this.targetElement.src	= this.href;//.replace (/\.([^.]+)$/, '.L.$1');
	return false;
}

// Handle horrid Dreamweaver dropdowns ... *shudder*
function a_onmouseover_omo ()
{
	clearTimeout (goBlank);
	hideall ();
	MM_showHideLayers (this.rev, '', 'show');
}

function a_onmouseout_omo ()
{
	goBlank = window.setTimeout ("MM_showHideLayers ('" + this.rev + "', '', 'hide');", 1200);
}

// Check the REL attribute of <a> elements and assign an onclick (etc) event
function init_as ()
{
	var els = document.getElementsByTagName ("a");

	if (els) for (var x = els.length; x--; )
	{
		var r = els[x].getAttribute ("rel");

		if (r) switch (r.toLowerCase ())
		{
			case "omo":
				var div__x 		= document.getElementById (els[x].rev);
				div__x.rev 		= els[x].rev;
				els[x].onmouseover	=
				div__x.onmouseover	= a_onmouseover_omo;
				els[x].onmouseout	=
				div__x.onmouseout	= a_onmouseout_omo;
										break;
			case "img": els[x].onmouseover	= a_onmouseover_img;	// We also want them to open links in a new window, so don't break here

			case "ext": els[x].onclick	= a_onclick_ext;	break;
		}
	}
}


// Load persistant font sizes set with a_onclick_fsize
function onload_fsizes ()
{
	fontElem	= [];
	fontNum		= CookieJar.get ("fontNum");
	fontSize	= CookieJar.get ("fontSize");
	fontUnit	= CookieJar.get ("fontUnit");

	if (fontNum != null && fontSize != null && fontUnit != null)
	{
		for (x = parseInt (fontNum); x--; )
			if ((e = CookieJar.get ("fontEl_" + x)) != null)
				fontElem.push (e);

		a_onclick_fsize (fontElem, fontSize, fontUnit);
	}
}


// Double-tap prevention
function form_onsubmit_nodbltap ()
{
	if (!this.submitted)
		return this.submitted = true
	return false;
}


// Set up pretty niceness in login form
function form_onload_login (form)
{
	var lu		= form.login_username;
	var lp		= form.login_password;
	var lt		= form.login_passtext;

	if (lu)
	{
		lu.onfocus	= function () { if (this.value == this.defaultValue)	this.value = ""; };
		lu.onblur	= function () { if (this.value == "")			this.value = this.defaultValue; };
	}

	if (lt)
	{
		lp.value		= "";

		lp.style.display	= "none";
		lt.style.display	= "inline";

		lt.onfocus		= function ()
		{
			lp.style.display	= "inline";
			lt.style.display	= "none";
			lp.focus ();
		}

		lp.onblur		= function ()
		{
			if (!this.value)
			{
				lp.style.display	= "none";
				lt.style.display	= "inline";
			}
		}
	}
	else
	{
		lt		=  lp.cloneNode (false)
		lt.id		= "login_passtext";
		lt.name		= "login_passtext";

		lt.onfocus	= function ()
		{
			this.parentNode.replaceChild (lp, this)
			lp.focus ();
		};

		lp.onblur	= function ()
		{
			if (!this.value)
				this.parentNode.replaceChild (lt, this);
		};

		lp.value	= "";


		lt.setAttribute ("type", "text");
		lp.parentNode.replaceChild (lt, lp);
	}
}


// Run any initialisaton functions we need to on forms
function init_forms ()
{
	for (var x = document.forms.length; x--; ) switch (document.forms[x].name)
	{
		case "login":	form_onload_login (document.forms[x]);				break;
		//default:	document.forms[x].onsubmit	= form_onsubmit_nodbltap;	break
	}
}

//DomLoaded.load (onload_fsizes);
DomLoaded.load (init_as);
DomLoaded.load (init_forms);
