//
// Infrastructure
//

// Adapted from an article on
// PlanetPDF.com by Kas Thomas.
function isString(p)
{
	if (typeof p == 'string')
	{
		return true;
	}
	if (typeof p == 'object')
	{
		var stringstring = p.constructor.toString().match(/string/i);
		return (stringstring != null);
	}
	return false;
}


//
// Cookies
//

function cookies_enabled()
{
	var cookies = (navigator.cookieEnabled) ? true : false;
	if ((typeof(navigator.cookieEnabled) == "undefined") && !cookies)
	{
		var cookiedate = new Date();
		var cookiestring = 'X'+cookiedate.getTime()+'X';
		create_cookie('testcookie', cookiestring, 0/* 0 means "session" */)
		cookies = (read_cookie('testcookie') != '') ? true : false;
		if (cookies)
		{
			delete_cookie('testcookie');
		}
	}
	return cookies;
}

function create_cookie(pName, pValue, pPersistence/* seconds */)
{
	var expiry = '';
	if (pPersistence != 0)
	{
		var date = new Date();
		date.setTime(date.getTime()+(pPersistence*1000));
		expiry = '; expires='+date.toGMTString();
	}
	var domain = document.domain;
	if (domain.substr(0, 4).toLowerCase() == 'www.')
	{
		domain = domain.substr(4);
	}
	var colpos = domain.indexOf(':');
	if (colpos != -1)
	{
		domain = domain.substr(0, colpos);
	}
	document.cookie = pName+'='+pValue+expiry+'; path=/';
}

function read_cookie(pName)
{
	var findit = pName+'=';
	var cookies = document.cookie.split(';');
	for (var i = 0; i < cookies.length ; i++)
	{
		var c = cookies[i];
		while (c.charAt(0) == ' ')
		{
			c = c.substring(1, c.length);
		}
		if (c.indexOf(findit) == 0)
		{
			return c.substring(findit.length, c.length);
		}
	}
	return '';
}

function delete_cookie(pName)
{
	create_cookie(pName, '', -3600/* 1 hour ago */);
}

//
// Color scheme support
//

function colorscheme(pScheme)
{
	if (!pScheme)
	{
		var pScheme = '';
	}
//	$colorschemes = colorschemes();
	var colorschemes_count = colorschemes.length;

	var scheme_to_find = '';

	if (isString(pScheme) && (pScheme != ''))
	{
		scheme_to_find = pScheme;
	}
	else if (read_cookie('colorscheme') != '')
	{
		scheme_to_find = read_cookie('colorscheme');
	}

	if (scheme_to_find != '')
	{
		for (var schemeindex=0 ; schemeindex<colorschemes_count ; schemeindex++)
		{
			if (scheme_to_find == colorschemes[schemeindex])
			{
				return colorschemes[schemeindex];
			}
		}
	}

	return $colorschemes[0];
}

function enable_color_scheme()
{
	var element_colorselect = document.getElementById("colorselect");
	if (element_colorselect)
	{
		try
		{
			// The correct way
			element_colorselect.style.display = "inherit";
		}
		catch (e)
		{
			// For most IE versions (ugly but works)
			element_colorselect.style.display = "";
		}
	}
	var element_colorselectspan = document.getElementById("colorselectspan");
	if (element_colorselectspan)
	{
		element_colorselectspan.style.display = "inline";
	}
	var element_colorselectform = document.getElementById("colorselectform");
	if (element_colorselectform)
	{
		element_colorselectform.style.display = "inline";
	}
	var element_colorselectnone = document.getElementById("colorselectnone");
	if (element_colorselectnone)
	{
		element_colorselectnone.style.display = "none";
	}
}

function change_color_scheme(pScheme)
{
	if (!pScheme)
	{
		var pScheme = '';
	}
	var colorschemes_count = colorschemes.length;

	var scheme_to_find = '';

	if (isString(pScheme) && (pScheme != ''))
	{
		scheme_to_find = colorscheme(pScheme);
	}
	else // use color scheme selector form element
	{
		var element = document.getElementById("colorscheme");
		if (element)
		{
			var sScheme = "" + element.value;
			if (sScheme != "")
			{
				scheme_to_find = colorscheme(sScheme);
			}
		}
	}

	if (scheme_to_find == '')
	{
		scheme_to_find = colorschemes[0];
	}

	var element = document.getElementById("colorschemestyle");
	if (element)
	{
		var pos = element.href.indexOf("/colorscheme");
		if (pos != -1)
		{
			element.href = element.href.substring(0, pos) + "/colorscheme" + scheme_to_find + ".css";
		}
	}

	var elements = new Array();
	if (document.all)
	{
		elements = document.all;
	}
	else
	{
		elements = document.getElementsByTagName("img");
	}

	for (var i = 0 ; i < elements.length ; i++)
	{
		var classWords = " " + elements[i].className + " ";
		if (classWords.indexOf(" colorschemedimage ") >= 0)
		{
			if (elements[i].nodeName == "IMG")
			{
				var mmpos = elements[i].src.indexOf("--");
				if (mmpos >= 0)
				{
					var srcroot = elements[i].src.substr(0, mmpos);
					elements[i].src = srcroot + "--" + scheme_to_find + ".png";
				}
			}
		}
	}

	if (cookies_enabled())
	{
		create_cookie("colorscheme", scheme_to_find, 366*24*3600 /* seconds */);
	}
}

//
// Button Highlighting
//
// Useful information was found at:
//
//  http://www.quirksmode.org/dom/getstyles.html
//  http://www.robertnyman.com/2006/04/24/get-the-rendered-style-of-an-element/
//

function get_rendered_color(pElement)
{
	var color = null;
	if (pElement.currentStyle)
	{
		color = pElement.currentStyle['color'];
	}
	else if (window.getComputedStyle)
	{
		color = document.defaultView.getComputedStyle(pElement,null).getPropertyValue('color');
	}
	return color;
}

function get_rendered_background_color(pElement)
{
	var backgroundColor = null;
	if (pElement.currentStyle)
	{
		backgroundColor = pElement.currentStyle['backgroundColor'];
	}
	else if (window.getComputedStyle)
	{
		backgroundColor = document.defaultView.getComputedStyle(pElement,null).getPropertyValue('background-color');
	}
	return backgroundColor;
}

//
// End of file
//
