
/*\
 | Copyright (C) 2009-2010 Link Digital
 | http://www.linkdigital.com.au/
 | All rights reserved.
 | Unauthorised copying, distribution or derivation is prohibited.
\*/


/****************************** Helper Functions ******************************/

Array.min	= function (array)	{ return Math.min.apply (Math, array); };
Array.max	= function (array)	{ return Math.max.apply (Math, array); };

/****************************** General Utility *******************************/

// Set all <a> tags with rel="ext" to open in new window
function a_init_ext (element)
{
	if (!(element = $ (element)))	return;

	element.observe ("click", function (event) { window.open (this.readAttribute ("href")); event.stop (); });
}

// Set an input text box to clear its default text on focus
function input_init_srch (element)
{
	if (!(element = $ (element)))	return;

	var temp		= $F (element);

	element.setValue (element.readAttribute ("title"));
	element.defaultValue	= $F (element);

	element.observe ("focus", function ()
	{
		if ($F (this) === this.defaultValue)
		{
			this.clear ();
			this.removeClassName ("dflt");
		}
	});

	element.observe ("blur",  function ()
	{
		if (!this.present ())
		{
			this.setValue (this.defaultValue);
			this.addClassName ("dflt");
		}
	});

	if (temp)
		element.setValue (temp);
	else	element.addClassName ("dflt");
}

// Set an input password box to show plaintext until focus
function input_init_pass (element)
{
	if (!(element = $ (element)))	return;
	element.removeClassName ("lc_pass");
	element.setValue ("");

	//var newelem = element.clone (); 		// IE won't let us just change the type attribute, OR clone the element >:(
	var newelem = new Element ("input",
	{
		name:		"-1",
		type:		"text",
		value:		"Password",
		title:		"Password",
		tabindex:	parseInt (element.getAttribute ("tabindex")) + 1,
		className:	element.className
	});

	element.twin = newelem;
	newelem.twin = element;

	newelem.observe ("focus", function () { this.replace (this.twin); this.twin.activate (); });	// this.twin.focus doesn't work right in IE for some reason
	element.observe ("blur",  function () { if (!this.present ()) this.replace (this.twin); });

	element.focus ();
	element.blur ();
}


/********************************** Cookies ***********************************/

// 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);
	}
};


/******************************** Load Things *********************************/

// Load everything
document.observe ("dom:loaded", function ()
{
	$$ ("a[rel~=ext]").each 		(a_init_ext);		// Anchor OnClicks

	$$ ("input.lc_srch").each		(input_init_srch);	// Search field niceness
	$$ ("input.lc_pass").each		(input_init_pass);	// Password field niceness

	$$ (".lc_hide").invoke			('hide');		// Hide stuff that can't be put in <noscript> tags.
	$$ (".lc_show").invoke			('show');		// Show stuff that is only of use to people with scripts enabled.
});

