/*
 * The default style, which will be returned if the cookie is not set.
 * Done as such so that we can change the default style to Christmas, or
 * whatever we want, without going through and dealing with the Javascript
 * itself.
 */

var defaultStyle = "pressingon";

/*
 * getImages function.  They work like a normal image rollover swap, but
 * are used when a new style has been selected.
 */

function getImages (title)
{
	if (document.getElementById)
	{
		var imageArray = document.getElementsByTagName("img");
		if (title != "null")
		{
			for (i = 0; i < imageArray.length; i++)
			{
				//only do this for images that should be swapped.
				if (imageArray[i].className == "swappedImage")
				{
					//parse its source
					var href = imageArray[i].src;
					//everything before the style title
					var hrefFirstPart = href.substring(0, href.indexOf("styles/")+6);
					//everything after the style title - the file name
					var hrefLastPart = href.substring(href.lastIndexOf("/") + 1, href.length);
					//put together the new file name, with the new style title included
					var newHref = hrefFirstPart + "/" + title + "/" + hrefLastPart;
					//now set the image's source to the new file name.
					imageArray[i].src = newHref;
				}
			}
		}
		else
		{
			for (i = 0; i < imageArray.length; i++)
			{
				//only do this for images that should be swapped.
				if (imageArray[i].className == "swappedImage")
				{
					//parse its source
					var href = imageArray[i].src;
					//everything before the style title
					var hrefFirstPart = href.substring(0, href.indexOf("styles/")+6);
					//everything after the style title - the file name
					var hrefLastPart = href.substring(href.lastIndexOf("/") + 1, href.length);
					//put together the new file name, with the new style title included
					var newHref = hrefFirstPart + "/pressingon/" + hrefLastPart;
					//now set the image's source to the new file name.
					imageArray[i].src = newHref;
				}
			}
		}
	}
}

/*
 * setActiveStyleSheet basically does what it says.  Gets all of the
 * associated sheets with the document, then sets the active one according to the
 * value of the variable title.
 */

function setActiveStyleSheet(title)
{
	if (document.getElementsByTagName)
	{
		var i, a, main;
		for(i=0; (a = document.getElementsByTagName("link")[i]); i++)
		{
			if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("href"))
			{
			//if the title is null, don't disable.  Silly IE.
				if (!(title == "null" || title==null))
					a.disabled = true;
				var titleToCompare = a.getAttribute("href");
				titleToCompare = titleToCompare.substring(titleToCompare.indexOf("styles/")+7,
titleToCompare.lastIndexOf("/"));
				if(titleToCompare == title) a.disabled = false;
	      	}
		}
		//now swap the images out
		getImages(title);
	}
}

function getActiveStyleSheet()
{
	if (document.getElementsByTagName)
	{
		var i, a;
		for(i=0; (a = document.getElementsByTagName("link")[i]); i++)
		{
			if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("href") && !a.disabled)
			{
				var titleToReturn = a.getAttribute("href");
				titleToReturn = titleToReturn.substring(titleToReturn.indexOf("styles/")+7,
titleToReturn.lastIndexOf("/"));
				return titleToReturn;
			}
		}
		return defaultStyle;
	}
}

function getPreferredStyleSheet()
{
	if (document.getElementsByTagName)
	{
		var i, a;
		for(i=0; (a = document.getElementsByTagName("link")[i]); i++)
		{
			if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("rel").indexOf("alt") == -1 && a.getAttribute("title"))
				return a.getAttribute("title");
		}
		return defaultStyle;
	}
}

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 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;
}

window.onload = function(e)
{
	if (document.getElementById)
	{
		var cookie = readCookie("style");
		var title = cookie ? cookie : getPreferredStyleSheet();
		setActiveStyleSheet(title);
	}
}

window.onunload = function(e)
{
	if (document.getElementById)
	{
		var title = getActiveStyleSheet();
		createCookie("style", title, 365);
	}
}

/* toggleMenu is used to collapse or show/hide the menus. */

function toggleMenu (divID)
{
	if (document.getElementById)
	{
		currentDiv = document.getElementById (divID);
		if (getActiveStyleSheet() == "topmenus")
		{
			// all submenus should hide
			var allDivs = document.getElementsByTagName ("div");
			for (var x = 0; x < allDivs.length; x++)
			{
				if (allDivs[x].className == "submenu")
				{
					allDivs[x].style.visibility = "hidden";
					allDivs[x].style.display = "none";
				}
			}
			//now make the appropriate change
			if (currentDiv.style.visibility == "visible")
			{
				currentDiv.style.visibility = "hidden";
				currentDiv.style.display = "none";
			}
			else
			{
				currentDiv.style.visibility = "visible";
				currentDiv.style.display = "block";
			}
		}
		else
		{
			// all submenus should be visible
			var allDivs = document.getElementsByTagName ("div");
			for (var x = 0; x < allDivs.length; x++)
			{
				if (allDivs[x].className == "submenu")
				{
					allDivs[x].style.visibility = "visible";
				}
			}
 			if (currentDiv.style.display == "block")
 			{
  				currentDiv.style.display = "none";
 			}
			else
			{
		  		currentDiv.style.display = "block";
 			}
		}
	}
}
