// JavaScript Document

// global variables
var nav_visible = true;
var bg_light = true;
var bg_light_val = '#ffffff';
var bg_dark_val = '#404040';

function toggle_nav() {
	if (nav_visible) {
		hideDiv("main_nav");
		moveDiv("content", -190);
		changeContents("toggle_nav_verb", "show");
//		showDiv("toggle_bg");
//		if (!bg_light) { changeBodyBackground(bg_dark_val); }
	} else {
		showDiv("main_nav");
		moveDiv("content", 190);
		changeContents("toggle_nav_verb", "hide");
//		hideDiv("toggle_bg");
//		changeBodyBackground(bg_light_val);
	}
	nav_visible = !nav_visible;
}

function toggle_bg() {
	if (bg_light) {
		changeBodyBackground(bg_dark_val);
		changeContents("toggle_bg_color", "light");
	} else {
		changeBodyBackground(bg_light_val);
		changeContents("toggle_bg_color", "dark");
	}
	bg_light = !bg_light;
}

function changeBackground(divID_string, color) {
	var myReference = getRefToDiv(divID_string);
	if( myReference.style ) { myReference = myReference.style; }
	myReference.bgColor = color;
	myReference.background = color;
	myReference.backgroundColor = color;
}

function changeBodyBackground(color) {
	if( document.documentElement && document.documentElement.style ) {
		document.documentElement.style.backgroundColor = color; }
	if( document.body && document.body.style ) {
		document.body.style.backgroundColor = color; }
	document.bgColor = color;
}

function changeContents(divID_string, string) {
	var myReference = getRefToDiv(divID_string);
	if( typeof( myReference.innerHTML ) != 'undefined' ) {
	  //used by all current browsers
	  myReference.innerHTML = string;
	} else if( myReference.document && myReference.document != window.document ) {
	  //used by layers browsers
	  myReference.document.open();
	  myReference.document.write(string);
	  myReference.document.close();
	}
}

function moveDiv(divID_as_a_string, x_shift, y_shift) {
	var myReference = getRefToDiv(divID_as_a_string);
	var noPx = document.childNodes ? 'px' : 0;
	if( myReference.style ) { myReference = myReference.style; }
	myReference.left = ( parseInt(myReference.left) + x_shift ) + noPx;
	myReference.top = ( parseInt(myReference.top) + y_shift ) + noPx;
}

// this function needs a timing aspect, among other things.
// do not use in its current state.
/*
function moveDivGradual(divID_string, xs, ys) {
	var f = 5;
	var xc = 0;
	var yc = 0;
	while (xs != 0 && ys != 0) {
		if (Math.abs(xs) < f && xs != 0) { xc = xs / Math.abs(xs); } else {
			xc = xs / f;
		}
		if (Math.abs(ys) < f && xs != 0) { yc = ys / Math.abs(ys); } else {
			yc = ys / f;	
		}
		moveDiv(divID_string, xc, yc);
		xs += xc;
	}
}
*/

function getRefToDiv(divID,oDoc) {
  if( document.getElementById ) {
    return document.getElementById(divID); }
  if( document.all ) {
    return document.all[divID]; }
  if( !oDoc ) { oDoc = document; }
  if( document.layers ) {
    if( oDoc.layers[divID] ) { return oDoc.layers[divID]; } else {
      //repeatedly run through all child layers
      for( var x = 0, y; !y && x < oDoc.layers.length; x++ ) {
        //on success, return that layer, else return nothing
        y = getRefToDiv(divID,oDoc.layers[x].document); }
    return y; } }
  return false;
}

function hideDiv(divID_as_a_string) {
  //get a reference as above ...
  myReference = getRefToDiv(divID_as_a_string);
  if( !myReference ) {
    window.alert('This feature is not supported by this browser');
    return; //don't go any further
  }
  //now we have a reference to it
  if( myReference.style ) {
    //DOM & proprietary DOM
    myReference.style.visibility = 'hidden';
  } else {
    //layers syntax
    myReference.visibility = 'hide';
  }
}

function showDiv(divID_as_a_string) {
  //get a reference as above ...
  myReference = getRefToDiv(divID_as_a_string);
  if( !myReference ) {
    window.alert('This feature is not supported by this browser');
    return; //don't go any further
  }
  //now we have a reference to it
  if( myReference.style ) {
    //DOM & proprietary DOM
    myReference.style.visibility = 'visible';
  } else {
    //layers syntax
    myReference.visibility = 'show';
  }
}
