/*  Chintimini Wildlife Rehabilitation Center web site
  by Mathieu Federspiel, Sep 2005.  Updated March 2007.
  Creates the left navigation with + and - images a la windows explorer.
  Modification of free code example from:
  The JavaScript Source!! http://javascript.internet.com
*/

var temp, temp2, cookieArray, cookieArray2, cookieCount;
var hh, hpage, hminus, hplus;
var tpage, tminus, tplus;
// Set the path to the images used.
hh="Images/";
hpage = hh + "page.png";
hminus = hh + "minus.png";
hplus = hh + "plus.png";
// Set the accessibility text for the images. These are used as the title attribute on the span tag.
tpage = "the following is a navigation item";
tminus = "click here to collapse this navigation section";
tplus = "click here to expand this navigation section";

function initiate(){
 cookieCount=0;

 if(document.cookie){
  cookieArray=document.cookie.split(";");
  cookieArray2=new Array();
  for(i in cookieArray){
   cookieArray2[cookieArray[i].split("=")[0].replace(/ /g,"")]=cookieArray[i].split("=")[1].replace(/ /g,"");
  }
 }

 cookieArray=(document.cookie.indexOf("state=")>=0)?cookieArray2["state"].split(","):new Array();
 temp=document.getElementById("containerul");

 for(var o=0;o<temp.getElementsByTagName("li").length;o++){
  
  if(temp.getElementsByTagName("li")[o].getElementsByTagName("ul").length>0){ // for a plus or minus
   temp2= document.createElement("span");
   temp2.className= "symbols";
   temp2.style.backgroundImage= (cookieArray.length>0)?((cookieArray[cookieCount]=="true")?"url("+hminus+")":"url("+hplus+")"):"url("+hplus+")";
   temp2.title= (cookieArray.length>0)?((cookieArray[cookieCount]=="true")?tminus:tplus):tplus;
   temp2.onclick=function(){
    showhide(this.parentNode);
    writeCookie();
   }

   temp.getElementsByTagName("li")[o].insertBefore(temp2,temp.getElementsByTagName("li")[o].firstChild)
   temp.getElementsByTagName("li")[o].getElementsByTagName("ul")[0].style.display = "none";
   if(cookieArray[cookieCount]=="true"){
    showhide(temp.getElementsByTagName("li")[o]);
   }

   cookieCount++;
  }
  else{ // for a final page

   temp2= document.createElement("span");
   temp2.className= "symbols";
   temp2.style.backgroundImage= "url("+hpage+")";
   temp2.title= tpage;
   temp.getElementsByTagName("li")[o].insertBefore(temp2,temp.getElementsByTagName("li")[o].firstChild);

  }
 }
 return true;
}

function showhide(el){
  el.getElementsByTagName("ul")[0].style.display=(el.getElementsByTagName("ul")[0].style.display=="block")?"none":"block";
  el.getElementsByTagName("span")[0].style.backgroundImage=(el.getElementsByTagName("ul")[0].style.display=="block")?"url("+hminus+")":"url("+hplus+")";
  el.getElementsByTagName("span")[0].title=(el.getElementsByTagName("ul")[0].style.display=="block")?tminus:tplus;
}

// Runs through the menu and puts the "states" of each nested list into an array, the array is then joined together and assigned to a cookie.
function writeCookie(){

 cookieArray=new Array()
 for(var q=0;q<temp.getElementsByTagName("li").length;q++){

  if(temp.getElementsByTagName("li")[q].childNodes.length>0){
   if(temp.getElementsByTagName("li")[q].childNodes[0].nodeName=="SPAN" && temp.getElementsByTagName("li")[q].getElementsByTagName("ul").length>0){

    cookieArray[cookieArray.length]=(temp.getElementsByTagName("li")[q].getElementsByTagName("ul")[0].style.display=="block");

   }
  }
 }

 document.cookie="state="+cookieArray.join(",")+";expires="+new Date(new Date().getTime() + 365*24*60*60*1000).toGMTString();
}
