/* MainNavLib.js // This defines the following functions:
  catchClick(e)  / Toggle(el)
  setMessage(e) / clearMessage(e)
  SetCookie(cName,cData) / GetCookieData(cName)
  SetNavCookie() / UpdateNav(){
  IsInArray(thisArray,thisElement)
  GetRootID() / GetOpenTopics()
*/

function Toggle(el){
/* Modified 12/16/05 by JN to handle whitespace in the generated code */
  // if el is the enclosing A instead of the Image, retarget the enclosed image (first element-node child)
  if (el.tagName=="A" && el.firstChild){
    var tmp=el.firstChild; // link's following element (ul)
    while (tmp.nodeType !=1 && tmp.nextSibling) {tmp=tmp.nextSibling;}
    if(tmp.nodeType==1 && tmp.tagName=="IMG"){
      el=tmp; 
    } else {
      return false;
    }
  }
  var a=el.parentNode; // link element this img belongs to
  var p=a.parentNode; // link's parent (should be li)
  // get the nextSibling, until it is a element node (nodeType 1)(skip text nodes)
  var s=a.nextSibling; // link's following element (ul)
  while (s.nodeType !=1 && s.nextSibling) { s=s.nextSibling;}
  
  var str="selected element= " + el.tagName +":" + el.className + "\n";
  str +="contined in= " + a.tagName +":" + a.className +"\n";
  str +="parent element= " + p.tagName +":" + p.className +"\n";
  str +="sibling element= " + s.tagName +":" + s.className +"\n";
  if(p.className="int"){
    // the parent li class=int : it has children contained in an adjacent ul
    str += "next sibling= " + s.tagName + ":" + s.className; +"(" + s.style.display + ")\n";
    // toggle the display of the ul, and the source of the +/- icon to match state
    if (s.style.display=="none" || s.style.display==""){
      s.style.display = "block";
      el.src=mn.minusIcon.src;
    } else {
      s.style.display = "none";
      el.src=mn.plusIcon.src;
    }
  }
//  alert(str);
}
function catchClick(e){
  if(!e) e=window.event;
  var el=(e.target) ? e.target : e.srcElement;
  // Intercept clicks images & A.nolink elements - stop bubbling & call Toggle()
  if(el.tagName=="IMG" || (el.tagName=="A" && el.className=="nolink")) {
    (e.stopPropagation) ? e.stopPropagation() : e.cancelBubble=true; // DOM L2:IE
    (e.preventDefault) ? e.preventDefault() : e.returnValue=false; // DOM L2:IE
//    alert("Toggle("+el.tagName+")");
    Toggle(el)
  }
}
function setMessage(e){
  if(!e) e=window.event;
  var el=(e.target) ? e.target : e.srcElement;
  // Intercept mouseover on images & A.nolink elements- stop bubbling & write message to status bar
  if(el.tagName=="IMG" || (el.tagName=="A" && el.className=="nolink")) {
    (e.stopPropagation) ? e.stopPropagation() : e.cancelBubble=true; // DOM L2:IE
    (e.preventDefault) ? e.preventDefault() : e.returnValue=true; // DOM L2:IE
    window.status="Hide/Show contents";
  }
}
function clearMessage(e){
  if(!e) e=window.event;
  var el=(e.target) ? e.target : e.srcElement;
  // Intercept mouseover on images & A.nolink elements- stop bubbling & clear status bar message
  if(el.tagName=="IMG" || (el.tagName=="A" && el.className=="nolink")) {
    (e.stopPropagation) ? e.stopPropagation() : e.cancelBubble=true; // DOM L2:IE
    (e.preventDefault) ? e.preventDefault() : e.returnValue=false; // DOM L2:IE
    window.status="";
  }
}

/* Persistent Nav State
  Cookie Functions      */
function GetCookieData(cName){
//function returns data for a cookie specifed by cName
  if (document.cookie != ""){
    cookieArray = document.cookie.split(";");
    for (i=0;i<cookieArray.length;i++){
      p= cookieArray[i].indexOf(cName+"=")
      if (p==0||p==1) return(cookieArray[i].split("=")[1])
    }
  }
  return ""
}
function SetCookie(cName,cData){
//writes a cookie to the browser of name specified
  var expiration = new Date();
  // set the expiration 20 minutes (600,000 milliseconds) in the future
  expiration.setTime(expiration.getTime() + (20*60*1000));
  document.cookie = cName + "=" + cData + ";expires=" + expiration ; return true;
}
function SetNavCookie(){
  SetCookie(GetRootID(),GetOpenTopics());
}
function UpdateNav(){
// Reset topic hide/show status based on cookie
/* Modified 12/27/05 by JN to handle whitespace in the generated code */
  var cData = GetCookieData(GetRootID());
  var a=""
  var icon=""
  var str=""
  // exit the function if there is no stored cookie data
  if (cData==""){return false;}
  var openTopicsArray=cData.split(","),topics=mn.getElementsByTagName("UL"),i
  for(i=0;i<topics.length;i++){
    if (topics[i].previousSibling){
      // a href element should precede the ul element - its first child element node should be the +/- image
      a=topics[i].previousSibling;
      while (a.nodeType !=1 && a.previousSibling) {a=a.previousSibling;}
      if (a.nodeType==1 && a.tagName=="A") {
        str+= "\n" + a.nodeType + ":" + a.tagName
        // iterate through the children of a to find the first element node
        icon=a.firstChild
  //      str+=icon.nodeType + ":" + icon.tagName + "\n"
        while (icon.nodeType !=1 && icon.nextSibling) {icon=icon.nextSibling;}
        str+= " - " + icon.nodeType + ":" + icon.tagName
        if (icon.nodeType!=1 || icon.tagName!="IMG") {icon="";}
        // icon is now the img element or ""
        str+= " - " + icon.tagName + " - " + (icon != "")
        if (icon != ""){
          if (IsInArray(openTopicsArray,topics[i].id)){
            topics[i].style.display="block";
            icon.src=mn.minusIcon.src
          } else {
            topics[i].style.display="none";
            icon.src=mn.plusIcon.src
          }
        }
      }
    }
  }
//  alert(str);
  return true;
}
function IsInArray(thisArray,thisElement){
  var i,str=thisElement+"\n";
  for (i=0;i<thisArray.length;i++){if (thisArray[i]==thisElement) return true;}
  return false;
}
function GetRootID(){
// returns the ID of the root nav Item for uniquely identifying a cookie
  return mn.getElementsByTagName("UL")[0].id;
}
function GetOpenTopics(){
 // returns a comma-delim list of UL elements that are open
  var topics=mn.getElementsByTagName("UL"),i,str="",dispVal;
  for(i=0;i<topics.length;i++){
    if (window.getComputedStyle) dispVal=getComputedStyle(topics[i],null).display
    else dispVal=topics[i].currentStyle.display;
    if(dispVal=="block"){
      if(str!="")str+=",";str+=topics[i].id;
    }
  }
  return str;
}
