////////////////////////////////////////////////////////////////////////////////
// Javascript for Switching Frames
////////////////////////////////////////////////////////////////////////////////

// Not ideal, but list any file from any other style with the replacement image here.
// Going from  "oldfilename" : "tothisfilename"
var PageStyles = {
	"contrast": {
		"plus.gif" : "plus_highcontrast.gif",
		"minus.gif" : "minus_highcontrast.gif"
	},
	"default": {
		"plus_highcontrast.gif" : "plus.gif",
		"minus_highcontrast.gif" : "minus_highcontrast.gif"
	}
};

// Sets the active stylesheet to the alternate whose name equals title.  Checks
// for a frameset, and if it finds one loops through the frames, otherwise just
// sets the document
function ondoc() {
  var cookie = readCookie("style");
  var title = "default";
  if (cookie == 'contrast' || cookie == 'default') {
	  title = cookie;
  } else {
	var preferred = getPreferredStyleSheet();
	if (preferred != null) {
	    title = preferred;
	}
  }
  setActiveStyleSheet(title);
}

function setActiveStyleSheet(title)
{
  if (top.frames.length > 0)
  {
    var i, j, a;
    for (i = 0; i < top.frames.length; ++i)
    {
      if (top.frames[i].name != "HEAD") {
      setActiveStyleSheetForDoc(top.frames[i].document, title);
      swapImagesInFrame(top.frames[i].document, title);
      }
    }
  }
  else
  {
    setActiveStyleSheetForDoc(document, title);
  }
  createCookie("style", title, 365);
}

function swapImagesInFrame(doc, title) {
	if (PageStyles[title] != undefined)
	{
		var imgs = doc.getElementsByTagName("img");
		for (var i = 0; i < imgs.length; ++i)
		{
			var currentFile = imgs[i].getAttribute("src");
			if (currentFile.indexOf("/") != -1) currentFile = currentFile.substring(currentFile.lastIndexOf("/")+1);
			if (PageStyles[title][currentFile] != undefined)
			{
				imgs[i].src = PageStyles[title][currentFile];
			}
		}
	}
}

// Set's the stylesheet for document to the one named by title.
function setActiveStyleSheetForDoc (doc, title)
{
	var elements = doc.getElementsByTagName("link");
	for (var i = 0; i < elements.length; ++i)
	{
		var sheet = elements[i];
		if ((sheet.getAttribute("rel").indexOf("style") != -1) && sheet.getAttribute("title"))
		{
			sheet.disabled = (sheet.getAttribute("title") != title);
		}
	}
}

function getActiveStyleSheet() {
	var elements = document.getElementsByTagName("link");
	for (var i = 0; i < elements.length; ++i)
	{
		var sheet = elements[i];
		if ((sheet.getAttribute("rel").indexOf("style") != -1) && sheet.getAttribute("title") && !sheet.disabled)
		{
			return sheet.getAttribute("title");
		}
	}
	return null;
}

function getPreferredStyleSheet() {
	var stylesheets = document.getElementsByTagName("link");
	for(var i = 0; i < stylesheets.length; i++)
	{
		var sheet = stylesheets[i];
		if ((sheet.getAttribute("rel").indexOf("style") != -1) && (sheet.getAttribute("rel").indexOf("alt") == -1) && sheet.getAttribute("title"))
		{
			return sheet.getAttribute("title");
		}
  	}
	return null;
}

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

function setDocumentStyle (e)
{
  var cookie = readCookie("style");
  var title = cookie ? cookie : getPreferredStyleSheet();
  setActiveStyleSheet(title);
}

