/*
 * menuExpandable.js - implements an expandable menu based on a HTML list
 * Author: Dave Lindquist (http://www.gazingus.org)
 */

if (!document.getElementById)
    document.getElementById = function() { return null; }

function initializeMenu(menuId, actuatorId) {
    var menu = document.getElementById(menuId);
    var actuator = document.getElementById(actuatorId);

    if (menu == null || actuator == null) return;

    //if (window.opera) return; // I'm too tired

    actuator.onclick = function() {
        var display = menu.style.display;
        this.style.backgroundImage =
            (display == "block") ? "url(../img/right.gif)" : "url(../img/down.gif)";
        menu.style.display = (display == "block") ? "none" : "block";

        return false;
    }
}

function setActiveStyleSheet(title) {
  var i, a, main;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
      a.disabled = true;
      if(a.getAttribute("title") == title) a.disabled = false;
    }
  }
}

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

function getPreferredStyleSheet() {
  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 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;
}

window.onload = function() {
  var standard = document.getElementById("standard");
	var large = document.getElementById("large");
	var cookie = readCookie("style");
	var title = cookie ? cookie : getPreferredStyleSheet();
	title == "large" ? large.className =  "dim" : standard.className = "dim";
	initializeMenu("regions", "regionActuator");
		
	standard.onmouseover = function() {
	window.status = "standard text size";
	return true;
	}

	standard.onmouseout = function() {
	window.status = "";
	return true;
	}
	
	standard.onclick = function() {
	this.className="dim"
	large.className="";
	setActiveStyleSheet("standard");
	return false;
	}
	
	large.onmouseover = function() {
	window.status = "larger text size";
	return true;
	}

	large.onmouseout = function() {
	window.status = "";
	return true;
	}
	
	large.onclick = function() {
	this.className="dim"
	standard.className="";
	setActiveStyleSheet("large");
	return false;
	}	

  var cookie = readCookie("style");
  var title = cookie ? cookie : getPreferredStyleSheet();
  setActiveStyleSheet(title);
	
}

window.onunload = function(e) {
  var title = getActiveStyleSheet();
  createCookie("style", title, 365);
}

var cookie = readCookie("style");
var title = cookie ? cookie : getPreferredStyleSheet();
setActiveStyleSheet(title);