function showhide( label, sh ) {
  //figure out if we're showing or hiding
  var show = 1;
  if ( document.getElementById( sh ).style.display == "" ) { show = 0; }

  var labeltext;

  //empty the label
  while ( label.hasChildNodes() ) {
    if ( label.firstChild.nodeName == "B" ) {
      labeltext = label.firstChild.firstChild.nodeValue;
    }
    label.removeChild( label.firstChild );
  }

  //make the image
  var img = document.createElement( 'img' );
  img.className = "sli";
  img.setAttribute( "class", "sli" );

  if ( show ) {
    //build the minus image
    img.setAttribute( "src", "images/20/nav/minus.png" );
    img.setAttribute( "alt", "-" );
  } else {
    //build the plus image
    img.setAttribute( "src", "images/20/nav/plus.png" );
    img.setAttribute( "alt", "+" );
  }

  //make the label
  label.appendChild( img );
  var b = document.createElement( 'b' );
  b.appendChild( document.createTextNode( labeltext ) );
  label.appendChild( b );

  //perform the show/hide
  if ( show ) {
    document.getElementById( sh ).style.display = "";
  } else {
    document.getElementById( sh ).style.display = "none";
  }
}

function mousewhere( e ) {
  if ( !e ) { e = window.event; }
  //document.getElementById( 'x' ).replaceChild( document.createTextNode( ( e.clientX / window.screen.availWidth ) * 360 ), document.getElementById( 'x' ).firstChild );
  //document.getElementById( 'y' ).replaceChild( document.createTextNode( ( e.clientY / window.screen.availHeight ) * 360 ), document.getElementById( 'y' ).firstChild );

  var H = ( e.clientX / window.screen.availWidth ) * 360;
  var S = e.clientY / window.screen.availHeight;
  var V = 0.5;

  var Hi = ( H / 60 ) % 6;
  Hi = Math.floor( Hi );
  var f = ( H / 60 ) - Hi;
  var p = V * ( 1 - S );
  var q = V * ( 1 - ( f * S ) );
  var t = V * ( 1 - ( 1 - f ) * S );

  var R = 128;
  var G = 128;
  var B = 128;

  if ( Hi == 0 ) {
    R = V;
    G = t;
    B = p;
  } else if ( Hi == 1 ) {
    R = q;
    G = V;
    B = p;
  } else if ( Hi == 2 ) {
    R = p;
    G = V;
    B = t;
  } else if ( Hi == 3 ) {
    R = p;
    G = q;
    B = V;
  } else if ( Hi == 4 ) {
    R = t;
    G = p;
    B = V;
  } else if ( Hi == 5 ) {
    R = V;
    G = p;
    B = q;
  }

  document.getElementsByTagName( 'body' )[0].style.backgroundColor = "rgb( " + Math.floor(255*R) + ", " + Math.floor(255*G) + ", " + Math.floor(255*B) + " )";
  //document.getElementById( 'debug' ).replaceChild( document.createTextNode( (255*R) + ", " + (255*G) + ", " + (255*B) + "::" + H + ", " + S + ", " + V + "::" + Hi + " " ), document.getElementById( 'debug' ).firstChild );
}

function doHSVtoRGB() {
  window.onmousemove = mousewhere;
  document.getElementsByTagName( 'body' )[0].onmousemove = mousewhere;
  SetCookie( "20_bg", "move" );
}

function findcolor() {
  doHSVtoRGB();
  window.onmousedown = pickcolor;
  document.getElementsByTagName( 'body' )[0].onmousedown = pickcolor;
}

function pickcolor() {
  window.onmousemove = function() {};
  document.getElementsByTagName( 'body' )[0].onmousemove = function() {};
  window.onmousedown = function() {};
  document.getElementsByTagName( 'body' )[0].onmousedown = function() {};
  SetCookie( "20_bg", document.getElementsByTagName('body')[0].style.backgroundColor );
}

function randBackground() {
  window.onmousemove = function() {};
  document.getElementsByTagName( 'body' )[0].onmousemove = function() {};
  window.onmousedown = function() {};
  document.getElementsByTagName( 'body' )[0].onmousedown = function() {};
  document.getElementsByTagName( 'body' )[0].style.backgroundColor = "rgb( " + Math.floor(255*Math.random()) + ", " + Math.floor(255*Math.random()) + ", " + Math.floor(255*Math.random()) + " )";
}

function randRandBackground() {
  randBackground();
  SetCookie( "20_bg", "random" );
}

function randSaveBackground() {
  randBackground();
  SetCookie( "20_bg", document.getElementsByTagName('body')[0].style.backgroundColor );
}

function biggertext() {
  var fs;
  if ( !document.getElementsByTagName( 'body' )[0].style.fontSize ) {
    fs = 0.6;
  } else {
    fs = parseFloat( document.getElementsByTagName( 'body' )[0].style.fontSize );
  }
  fs += 0.2;
  document.getElementsByTagName( 'body' )[0].style.fontSize = fs + "em";  
}

function smallertext() {
  var fs;
  if ( !document.getElementsByTagName( 'body' )[0].style.fontSize ) {
    fs = 0.6;
  } else {
    fs = parseFloat( document.getElementsByTagName( 'body' )[0].style.fontSize );
  }
  fs -= 0.2;
  document.getElementsByTagName( 'body' )[0].style.fontSize = fs + "em";  
}

/*
 * from A List Apart
 */
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;
         SetCookie( "20_style", title );
       }
     }
   }
}


// cookies.js
// You can use this code for your McNuggets!
// Derived from the Bill Dortch code at http://www.hidaho.com/cookies/cookie.txt

var today = new Date();
//expires in a year....
var expiry = new Date(today.getTime() + 365 * 24 * 60 * 60 * 1000);

function getCookieVal (offset) {
	var endstr = document.cookie.indexOf (";", offset);
	if (endstr == -1) { endstr = document.cookie.length; }
	return unescape(document.cookie.substring(offset, endstr));
}

function GetCookie (name) {
	var arg = name + "=";
	var alen = arg.length;
	var clen = document.cookie.length;
	var i = 0;
	while (i < clen) {
		var j = i + alen;
		if (document.cookie.substring(i, j) == arg) {
			return getCookieVal (j);
			}
		i = document.cookie.indexOf(" ", i) + 1;
		if (i == 0) break; 
		}
	return null;
}

function DeleteCookie (name,path,domain) {
	if (GetCookie(name)) {
		document.cookie = name + "=" +
		((path) ? "; path=" + path : "") +
		((domain) ? "; domain=" + domain : "") +
		"; expires=Thu, 01-Jan-70 00:00:01 GMT";
		}
}

function SetCookie (name,value,expires,path,domain,secure) {

  document.cookie = name + "=" + escape (value) +
    ((expires) ? "; expires=" + expires.toGMTString() : "; expires=" + new Date(2037, 01, 01, 01, 01, 01).toGMTString()) +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    ((secure) ? "; secure" : "");
}

//end of cookies.js

/*
 * prepare the stylesheet before the page is loaded to avoid the style
 * suddenly changing (hopefully)
 * these are duplicated in the onload function in case they don't
 * work.
 */
function makeItSo() {
  try { 
    if ( GetCookie( '20_style' ) != null ) {
      setActiveStyleSheet( GetCookie( '20_style' ) );
    }
   } catch ( eee ) {}

  try {
    if ( GetCookie( "20_bg" ) != null ) {
      if ( GetCookie( "20_bg" ) == "random" ) {
        //random background
        randRandBackground();
      } else if ( GetCookie( "20_bg" ) == "move" ) {
        //moving
        doHSVtoRGB();
      } else {
        document.getElementsByTagName( 'body' )[0].style.backgroundColor = GetCookie( "20_bg" );
      }
    }
  } catch ( eee ) {}
}

//call it
makeItSo();

function compressNav() {
  //find the spans in the nav div
  var spans = document.getElementById( 'nav' ).getElementsByTagName( 'span' );
  //get the ul's out of each expand span
  for ( i = 0; i < spans.length; i++ ) {
    if ( spans[i].className == "expand" ) {
      var uls = spans[i].parentNode.getElementsByTagName( 'ul' );
      //now set the display style attribute to 'none'
      for ( j = 0; j < uls.length; j++ ) {
        uls[j].style.display = "none";
      }
    }
  }
}

function navtoggle( li ) {
  var status = li.getElementsByTagName('ul')[0].style.display;
  if ( status == "block" ) {
    li.getElementsByTagName('ul')[0].style.display = "none";
    li.style.backgroundImage = "url( images/20/nav/plus.png )";
  } else {
    li.getElementsByTagName('ul')[0].style.display = "block";
    li.style.backgroundImage = "url( images/20/nav/minus.png )";
  }
}

/* These lines will create the style rules required for the nav system.
   These will accomplish the same thing as the compressNav function, but these
   can be run before the nav/page finishes loading */
if ( document.styleSheets && document.styleSheets[0] && document.styleSheets[0].addRule ) {
  document.styleSheets[0].addRule( "div#nav ul li ul", "display: none;" );
  document.styleSheets[0].addRule( "div#nav ul li ul li ul", "display: none;" );
} else if ( document.styleSheets && document.styleSheets[0] && document.styleSheets[0].insertRule ) {
  document.styleSheets[0].insertRule( "div#nav ul li ul { display: none; }", document.styleSheets[0].cssRules.length );
  document.styleSheets[0].insertRule( "div#nav ul li ul li ul { display: none; }", document.styleSheets[0].cssRules.length );
}

window.onload = function() {
  //doHSVtoRGB();
  makeItSo();
  compressNav();
}

