/*
copyright 2004-2005 john daharsh
http://www.daharsh.net
copyright has been granted for use in the 
Colorado Council on the Arts Website
April 1, 2005

If you wish to use this script or derivative scripts on your
website, contact John Daharsh at john@daharsh.net
*/

/* set some global crud */
document.cw = 800;
document.ie = (navigator.appVersion.indexOf("MSIE")!=-1)
document.ie5 = (navigator.appVersion.indexOf("MSIE 5.")!=-1)
document.w = 0
document.h = 0
document.subNavChildName = "Sub";
document.defaultFileExtention = ".htm";
document.defaultFileName = "index";
document.subpagesInFolders = true;
document.navItems = new Array(0); // this will hold all the primary navigation items
document.subNavItems = new Array(0); // this will hold all the sub-navigation items
document.mouseX = 0;
document.mouseY = 0;
document.navHideDelay=300; // adjust the timer for hiding page elements
document.globalNavReference; // hold nav timeout stuff here
document.pageCont == "wrapper"; // the name of the container that will be driven by this file

function writeDebug(alertStr){
  if(document.getElementById("debug")){
   document.getElementById("debug").innerHTML+="<br>"+alertStr+"<br>------------------------------------------";
  }  
}

/************************/
/*     track mouse      */
/************************/
/* monitor mouse movement */
/*document.onmousemove = function(e){

 if(navigator.appVersion.indexOf("MSIE")==-1){
  document.mouseX = e.clientX;
  document.mouseY = e.clientY;
 } else {
  document.mouseX = window.event.clientX;
  document.mouseY = (document.body.scrollTop + window.event.clientY);
 }
}*/

/*
 check to see if this collection is part of an object,
 if it is, return its parent's name
*/
function getContainerParent(coll){
 var re = /.co.coll$/; // test to see if this collection is part of an object
 if(re.test(coll.name)){
  return coll.name.replace(re,"");
 } else {
  return "";
 }
}

/*
 redraw all nav in a collection, 
 then check to see if turning all 
 elements off affected a parent
*/
function handleNavParent(coll){
 // check to see if this collection is part of an object.
 // if this collection is part of an object and the collFlag is false, hide the object,
 // set the parent's flag to false
 
 var collFlag = refreshNavPeers(coll);

 var parentItem = getContainerParent(coll)
 if(parentItem!=""){
  var parentObj = eval(parentItem); 
  var re = /\[[\d+]\]$/; // strip off the [] from the end of the array name
  var parentColl = eval(parentItem.replace(re,""));
  if(!collFlag){
   parentObj.co.show(false);
   document.getElementById(parentObj.n).f=false;
   // parentObj.refresh();
   handleNavParent(parentColl)
  }
 }
}

/*
 redraw all nav within a collection(shows/hides etc.)
*/
function refreshNavPeers(coll){
 var collFlag = false;
 
 // loop over objects to check for active objects
 for(var i=0;i<coll.length;i++){
  if(coll[i].co!=null){ // is there a child object?
   if(coll[i].co.coll.length>0){
    // test the child object's contents to see if any are active
    if(refreshNavPeers(coll[i].co.coll)){ 
     document.getElementById(coll[i].n).f=true; 
    }
   }
   coll[i].co.show(document.getElementById(coll[i].n).f)
  }  
  if(document.getElementById(coll[i].n).f){
   collFlag=true;  
  }
  coll[i].refresh();
 }
 return collFlag;
}

/*
 reset all nav rollover images
*/
function resetNavImages(){
 for(var i=0;i<document.navItems.length;i++){
  document.navItems[i].resetImage();
 }
}

/*
 my wireless mouse was causing onmouseout events to
 fire almost as soon as I rolled over something 
 *on my secondary monitor*
 that had a mouseout event, this checks to make sure that
 it doesn't fire a false onmouseout event
*/
/*
function confirmMouseOut(e){
 var eventFROM = document.ie?event.fromElement:e.currentTarget
 var eventTO = document.ie?event.toElement:e.relatedTarget
 var objFROM = eventFROM?(eventFROM.id==''?eventFROM.name:eventFROM.id):'none'
 var objTO = eventTO?(eventTO.id==''?eventTO.name:eventTO.id):'none'
 return !(objTO=='none'||objTO==objFROM)
}
*/


/*
  this tests for the existence of a div and its
  style property before making any attempt to access
  any style settings (prevents errors - just use wisely)
*/
function fastStyle(obj,css,setting){
 if(document.getElementById(obj)){
  if(document.getElementById(obj).style){
   eval("document.getElementById('"+obj+"').style."+css+"='"+setting+"'")
  }
 }
 return css;
}

/* 
 assign all properties and 
 functionality to nav object 
*/
function styleHandler(obj){
 this.exist = false;
 this.imgFlag=false; //is this an image?
 
 if(document.getElementById(obj)){
  this.exist = true; 
 } else if(document.images[obj]){
  this.exist = true;
  this.imgFlag = true;
 }

 this.n = obj;
 document.getElementById(obj).objId=0; // this will reference the poistion in the virtual list of styleHandler objects
 document.getElementById(obj).f=false;
 this.f = document.getElementById(obj).f; // generic flag, can be used by other objects to indicate display
 this.p = ""; // use to reference "parent" (object that causes this object to activate)
 this.cn = ""; // use to reference a "child"
 this.co = null; // instance of a child object
 this.parentColl = ""; // name of collection that this is a part of (if grouped)

 var re = eval("/"+document.subNavChildName+"$/");
 if(re.test(this.n)){ //is this object a child object?
  var parentObj = this.n.replace(re,"") // store a reference to the parent object
  if(document.getElementById(parentObj)){ // test for the existence of the parent
   this.p = parentObj; 
  }
 }
  
 if(document.getElementById(obj+document.subNavChildName)){
  this.cn = obj+document.subNavChildName
  this.co = new styleHandler(this.cn); // if a child layer exists, create a new object
 }

 this.coll = new Array(0); // hold an array of whatever -- perhaps child nav elements?
 this.cvf = false; // do any of my elements have a child that's visible?
 this.c = ""; // settable foreground color
 this.cX = ""; // settable foreground rollover color
 this.b = ""; // settable background color
 this.bX = ""; // settable background rollover color
 this.i = ""; // settable "off" image
 this.iX = ""; // settable "on" image
 this.content = this.exist?document.getElementById(obj).innerHTML:"";
 this.contentX = "" // in case there is alternate content on mouseover
 this.href=""; // does this fire an onclick to a link?
 
 this.s = this.exist?document.getElementById(obj).style:"";
 this.contentH = this.exist?document.getElementById(obj).offsetHeight:"";
 this.contentW = this.exist?document.getElementById(obj).offsetWidth:"";
 this.w = this.exist?this.s.width.replace("px","")*1:0;
 this.h = this.exist?this.s.height.replace("px","")*1:0;
 this.x = this.exist?this.s.left.replace("px","")*1:0;
 this.y = this.exist?this.s.top.replace("px","")*1:0;
 this.v = this.exist?(this.s.visibility==null||this.s.visibility==""?true:(this.s.visibility=="visible")):false;
 this.currC = this.exist?this.s.color:""; // grab current color
 this.currB = this.exist?this.s.backgroundColor:""; // grab current backround color
 this.currI = this.exist?this.s.backgroundImage:""; // grab current backround image
 this.currContent = this.exist?this.innerHTML:""; // grab current content
  
 // functions
 this.modC = function(c){if(this.exist){this.s.color=c;this.currC = c;}}
 this.modI = function(i){if(this.exist){if(this.imgFlag){document.images[this.n].src=i}else{this.s.backgroundImage="url("+i+" )";}this.currI = i;}}
 this.modB = function(b){if(this.exist){this.s.backgroundColor=b;this.currB = b;}}
 this.modX = function(x){if(this.exist){this.s.left=x;this.x=x;}}
 this.modY = function(y){if(this.exist){this.s.top=y;this.y=y;}}
 this.modW = function(w){if(this.exist){if(w>0){this.s.width=w;this.w=w;}}}
 this.modH = function(h){if(this.exist){if(h>0){this.s.height=h;this.h=h;}}}
 this.modContent = function(content){if(this.exist){document.getElementById(obj).innerHTML=content;this.currContent=content;}}
 this.show = function(v){if(this.exist){this.s.visibility=(v?"visible":"hidden");this.v=v;}}
 this.setParentStatus=function(f){if(this.exist&&this.p!=""){if(document.getElementById(this.p)){document.getElementById(this.p).f=f}else if(document.getElementById(obj).parentNode){document.getElementById(obj).parentNode.f=f}}}
 this.refresh = function(){
  if(this.exist){ 
   if(!this.imgFlag){
    this.modC(document.getElementById(obj).f?this.cX:this.c);
    this.modB(document.getElementById(obj).f?this.bX:this.b);
   }
   this.modI(document.getElementById(obj).f?this.iX:this.i);
   if(this.contentX!=""){this.modContent(document.getElementById(obj).f?this.contentX:this.content);}
  }
 } 
 this.toggleF = function(){document.getElementById(obj).f=!document.getElementById(obj).f;this.f = document.getElementById(obj).f;this.refresh();}
 this.modF = function(f){document.getElementById(obj).f=f;this.f = document.getElementById(obj).f;this.refresh();}
 return this;
}

/*
 this will grab the parent nav obj
 and dissect it and individual nav items
 and shove those into the global collection
*/
function navInit(obj,coll){
 if(document.getElementById(obj)){
  var thisColl = new Array();
  thisColl.name = coll; // give the array a name so you can check it later
  
  // before traversing the nodes, select only element nodes
  var navObj = new Array();
  for(var t=0;t<document.getElementById(obj).childNodes.length;t++){
   if(document.getElementById(obj).childNodes[t].nodeType==1 && document.getElementById(obj).childNodes[t].nodeName != "BR" && document.getElementById(obj).childNodes[t].nodeName != "br"&&(document.getElementById(obj).childNodes[t].id!=null)){ // element == nodeType 1
    navObj[navObj.length] = document.getElementById(obj).childNodes[t]
   }
  }
  // build collection of navItems
  for(var i=0;i<navObj.length;i++){
    var navId=(navObj[i].id==null||navObj[i].id==""||navObj[i].id=="undefined")?navObj[i].name:navObj[i].id
    thisColl[i] = new styleHandler(navId);
    thisColl[i].parentColl = coll;
    document.getElementById(navId).objId=i
    // recurse any children
    if(thisColl[i].co!=null){
     thisColl[i].co.coll = navInit(navId+document.subNavChildName,coll+"["+document.getElementById(navId).objId+"].co.coll")
    }
    // add event handlers for nav items
    navObj[i].onmouseover = function(){ 
     clearTimeout(document.globalNavReference);
     var eventSender = this.id;
     document.getElementById(eventSender).f=true;
     refreshNavPeers(eval(coll)); 
    }
    navObj[i].onmouseout = function(){
     var eventSender = this.id;
     document.getElementById(eventSender).f=false;
     document.globalNavReference = setTimeout("handleNavParent("+coll+")",document.navHideDelay)
    }  
    
    navObj[i].onclick = function(){
     var eventSender = this.id;
     var thisLink = "";
     
     //look for an anchor tag in the layer
     if(document.getElementById(eventSender).childNodes){
      for(var c=0;c<document.getElementById(eventSender).childNodes.length;c++){
       if(document.getElementById(eventSender).childNodes[c].nodeType==1&&(document.getElementById(eventSender).childNodes[c].nodeName=="a"||document.getElementById(eventSender).childNodes[c].nodeName=="A")){
        thisLink = document.getElementById(eventSender).childNodes[c].href
       }
      }
     }
     
     if(thisColl[document.getElementById(eventSender).objId].href==""){
      if(thisLink==""){ // if there was an anchor with a link, use that
       if(document.subpagesInFolders){
         var parentItem = getContainerParent(thisColl)
         if(parentItem!=""){
          thisLink = eval(parentItem).n+"/"+eventSender+document.defaultFileExtention
         } else {
          thisLink = eventSender+"/"+document.defaultFileName+document.defaultFileExtention        
         }
       } else {
        thisLink = eventSender+document.defaultFileExtention
       }
      }
     } else {
      thisLink = thisColl[document.getElementById(eventSender).objId].href
     }
     window.location=thisLink;
    }
  }
  return thisColl
 } else {
  setTimeout("navInit('"+coll+"','"+obj+"')",100); // keep trying until object is loaded
 }
}

/*

 the icing on the cake, by which I mean that
 this allows nav to be styled after loading.
 Originally, this always recursed, I have 
 updated it so there is a boolean indicating 
 if recursion is desired.

 coll           array to iterate over
 prop           property of navigation object to be modified
 val            value to set prop
 f              boolean indicating whether or not to recurse

*/
function styleNav(coll,prop,val,f){
 for(var i=0;i<coll.length;i++){
  // hit all children too
  if(coll[i].co!=null&&f){
   styleNav(coll[i].co.coll,prop,val,f);
  }
  if(eval(coll.name+"["+i+"]."+prop)!=null){
   eval(coll.name+"["+i+"]."+prop+"='"+val+"'")
  }
  coll[i].refresh();
 }
}

/*ALL the following is only necessary when centering the content on the page */

/* 
  this repositions the contents of 
  document.pageCont when it loads or reloads  
*/
function adjustDisplay(){
 // every time page is resized, 
 // these variables need adjusted
 document.w = (document.ie?document.body.clientWidth:window.innerWidth);
 document.h =(document.ie?document.body.clientHeight:window.innerHeight);

 // move the content so it's centered
 
 // in Firefox, the vertical scrollbar changes the width of the window.innerWidth on pages with longer content
 // causing the repositioned menu to shift right.
 // i'm going to make a fast hack for now, hopefully make better at some point
 if(window.navigator.product=="Gecko"&&(document.body.offsetHeight>document.h-10)){
 	fastStyle(document.pageCont,"left",(document.w>document.cw?((document.w/2)-(document.cw/2)-11)+"px":"0px"));
 } else {
	 fastStyle(document.pageCont,"left",(document.w>document.cw?((document.w/2)-(document.cw/2)-1)+"px":"0px"));
 }
 
 return true;
}

/*
  let everything get into place 
  before showing the page for the first time
*/
function revealPage(){
 adjustDisplay();
 fastStyle(document.pageCont,"visibility","visible");
 return true;
}

/* keep at bottom of file */
//window.onresize = adjustDisplay 
//window.onload = revealPage
