$(document).ready(init);

function init()
{
	// stylesheet switcher
	$(".styleswitch").click(function()
		{
			switchStylestyle(this.getAttribute("rel"));
			return false;
		}
	);
	
	// search box
	$("#nav-search-input").focus(function()
		{
			if (this.value=='search') this.value='';
		}).blur(function()
		{
			if (this.value=='') this.value='search';
		}
	);
	$(".date-picker").focus(function()
		{
			if (this.value=='dd/mm/yyyy') this.value='';
		}).blur(function()
		{
			if (this.value=='') this.value='dd/mm/yyyy';
		}
	);

	// make submit rollovers work on IE
	if ($.browser == "msie") {
		//$('button.submit').hover(function() // doesn't work because button is a shortcut to input type="button"
		$('.submit, .date-picker, .search').hover(function() 
		{
			$(this).css({'background-position':'0 -30'});
		}, function()
		{
			$(this).css({'background-position':'0 0'});
		});
	}
	
	var c = readCookie('style');
	if (c) switchStylestyle(c);
}

function switchStylestyle(styleName)
{
	//changed selector to work with jquery 1.3.2 (was [@rel=*style*])
	$("link[rel^=style]").each(function(i) {
			if (this.getAttribute("title")) {
				this.disabled = true;
				if (this.getAttribute("title") == styleName) this.disabled = false;
			}
		}
	);
	createCookie("style", styleName, 365);
}



// DOM element creator for jQuery and Prototype by Michael Geary
// http://mg.to/topics/programming/javascript/jquery
// Inspired by MochiKit.DOM by Bob Ippolito
// Free beer and free speech. Enjoy!
// Tweaked by Kelvin Luck [http://mg.to/2006/02/27/easy-dom-creation-for-jquery-and-prototype#comment-168]

$.defineTag = function( tag ) {
	$[tag.toUpperCase()] = function() {
		return $._createNode( tag, arguments );
	}
};

(function() {
	var tags = [
		'a', 'br', 'button', 'canvas', 'div', 'fieldset', 'form',
		'h1', 'h2', 'h3', 'hr', 'img', 'input', 'label', 'legend',
		'li', 'ol', 'optgroup', 'option', 'p', 'pre', 'select',
		'span', 'strong', 'table', 'tbody', 'td', 'textarea',
		'tfoot', 'th', 'thead', 'tr', 'tt', 'ul' ];
	for( var i = tags.length - 1;  i >= 0;  i-- ) {
		$.defineTag( tags[i] );
	}
})();

$.NBSP = '\u00a0';

$._createNode = function( tag, args ) {
	var fix = { 'class':'className', 'Class':'className' };
	var e;
	try {
		if(typeof(args[0]) == 'string') {
			var newArgs = [{}];
			for (i=0; i<args.length; i++) newArgs.push(args[i]);
			args = newArgs;
		}
		var attrs = args[0] || {};
		e = document.createElement( tag );
		for( var attr in attrs ) {
			var a = fix[attr] || attr;
			e[a] = attrs[attr];
		}
		for( var i = 1;  i < args.length;  i++ ) {
			var arg = args[i];
			if( arg == null ) continue;
			if( arg.constructor != Array ) append( arg );
			else for( var j = 0;  j < arg.length;  j++ )
				append( arg[j] );
		}
	}
	catch( ex ) {
		alert( 'Cannot create <' + tag + '> element:\n' +
			args.toSource() + '\n' + args );
		e = null;
	}
	
	function append( arg ) {
		if( arg == null ) return;
		var c = arg.constructor;
		switch( typeof arg ) {
			case 'number': arg = '' + arg;  // fall through
			case 'string': arg = document.createTextNode( arg );
		}
		e.appendChild( arg );
	}
	
	return e;
};

$.TEXT = function(s) {
    return document.createTextNode(s);
}; 
// /DOM element creator



// cookie functions http://www.quirksmode.org/js/cookies.html
function createCookie(name,value,days)
{
	if (days)
	{
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}
function readCookie(name)
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++)
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}
function eraseCookie(name)
{
	createCookie(name,"",-1);
}
// /cookie functions
