// Content: js navigation
Navigation.prototype.HEIGHT_CLOSED = 42;
Navigation.prototype.BOTTOM_MARGIN = 10;
Navigation.prototype.WIN_IE50 = (navigator.userAgent.toLowerCase().indexOf("msie 5.0") > 0); 
Navigation.prototype.WIN_IE5PLUS = ((navigator.userAgent.toLowerCase().indexOf("msie") > 0) && (navigator.userAgent.toLowerCase().indexOf("msie 5.0") < 0)); 
Navigation.prototype.WIN_IE60 = (navigator.userAgent.toLowerCase().indexOf("msie 6.0") > 0);

function Navigation (container, bottomImage, HTML, extFrame) {
   this.navigation = document.getElementById(container);
   this.navigation.innerHTML = HTML;
   this.closing = -1;
   this.opening = -1;
   this.openImage = document.getElementById(bottomImage);
   this.logoImage = document.getElementById('navlogo');
   this.allItems = this.navigation.getElementsByTagName("li");
   this.initAllItems();
   this.subLists = this.navigation.getElementsByTagName('ul')[0].getElementsByTagName('ul');
   //this.targetHeight = this.getHeight() + this.HEIGHT_CLOSED + this.BOTTOM_MARGIN;
   this.targetHeight = this.getHeight() + this.BOTTOM_MARGIN;
   this.isOpen = false;
   if (!document.all || this.WIN_IE50) this.initExtFrame(); 
   /* iframe behind navigation for IE 5.5+ to hide selectboxes */
   if (document.all && !this.WIN_IE50) {
      this.selectHider = this.createHider();
   }
   if (this.WIN_IE50) {
      this.selectBoxes = document.getElementsByTagName('select');
   }
   this.navigation.onmouseover = createContextFunction(this, "open");
   this.navigation.onmouseout = createContextFunction(this, "close");
   /* IE initialization for navigation entries */
   if (document.all) {
      this.initIE();
   }
   this.placeItems();
   if (document.getElementById('sitemap')) {
//    generateSitemap();
      if (this.WIN_IE50) window.resizeBy(0, 1);
   }
   this.setFullHeight();

}
Navigation.prototype.initIE = function () {
   for (var i=0; i < this.allItems.length; i++) {
      var node = this.allItems[i];
      node.onmouseover = function () {
         this.className += "over";
      }
      node.onmouseout = function () {
         this.className = this.className.replace("over", "");
      }
   }
}
Navigation.prototype.initExtFrame = function () {
   this.extFrame = document.getElementById('extFrame');
   if (this.extFrame) this.extFrame.style.position = 'relative'; /* ppkpatch: give iframe position: relative so top can be used instead of marginTop */
   if (this.extFrame) this.extFrame.startPos = calculateTop(this.extFrame);
}
Navigation.prototype.initAllItems = function () {
   for (var i = 0; i < this.allItems.length; i++) {
      if (this.allItems[i].className.indexOf('submenu') != -1) {
         this.allItems[i].submenu = this.allItems[i].getElementsByTagName('ul')[0];
         this.allItems[i].subItem = this.allItems[i].submenu.getElementsByTagName('li')[0];
      } else {
         this.allItems[i].submenu = null;
         this.allItems[i].subItem = null;
      }
   }
}
Navigation.prototype.open = function (e) {
   if (!isSafari()) {
      if (e) if (e.originalTarget.innerHTML == "Home") return;
      if (e) if (e.originalTarget.id == "navigation") return;
   }
   if (e) var dest = e.relatedTarget; else var dest = window.event.toElement;
   if (dest) if (dest.parentNode.id == 'home' || dest.parentNode.id == 'top' || dest.parentNode.id == 'ls_top' || dest.parentNode.id == 'maincontainer') return;
   //alert(dest.parentNode.id);
   //this.allItems[0].getElementsByTagName('a')[0].focus();
   //this.allItems[0].getElementsByTagName('a')[0].blur();
   if (this.WIN_IE50) {
      this.slideOpen()
   } else {
      if (this.closing > 0) {
         clearTimeout(this.closing);
         this.closing = -1;
      }
      if (this.opening < 0 && !this.isOpen) {
         this.opening = setTimeout(createContextFunction(this, "slideOpen"), 300);
      }
   }
}
Navigation.prototype.slideOpen = function () {
   this.openImage.style.display = 'block';
   this.logoImage.style.display = 'block';
   this.navigation.style.height = this.targetHeight + "px";
   if (this.selectHider) this.selectHider.style.display = 'block';
   if (this.selectBoxes) updateSelectBoxes(this.selectBoxes, 'hidden');
   if (this.extFrame && !this.isOpen) {
      this.extFrame.overLap = (this.HEIGHT_CLOSED + this.targetHeight - this.extFrame.startPos);
      this.extFrame.style.top = this.extFrame.overLap + "px"; /* ppkpatch: marginTop -> top */
   }
   if (!this.WIN_IE50) clearTimeout(this.opening);
   this.opening = -1;
   this.isOpen = true;
}
Navigation.prototype.close = function (e) {
   if (e) var dest = e.relatedTarget; else var dest = window.event.toElement;
   if (!isChild(this.navigation, dest)) {
      if (this.WIN_IE50) {
         this.slideClose();
      } else {    
         if (this.opening > 0) {
            clearTimeout(this.opening);
            this.opening = -1;
         }
         if (this.closing < 0 && this.isOpen) {
            this.closing = setTimeout(createContextFunction(this, "slideClose"), 300);
         }
      }
   }
   if (e) e.cancelBubble = true; else window.event.cancelBubble = true;
}
Navigation.prototype.slideClose = function () {
   this.openImage.style.display = 'none';
   this.logoImage.style.display = 'none';
   this.navigation.style.height = this.HEIGHT_CLOSED + "px";
   if (this.selectHider) this.selectHider.style.display = 'none';
   if (this.selectBoxes) updateSelectBoxes(this.selectBoxes, 'visible');
   if (this.extFrame) {
      this.extFrame.style.top = "0"; /* ppkpatch: marginTop -> top */
   }
   if (!this.WIN_IE50) clearTimeout(this.closing);
   this.closing = -1;
   this.isOpen = false;
}
Navigation.prototype.getHeight = function () {
   var targetHeight = 0;
   var height3rdLevel = 0;
   this.show();
   for (var i = 0; i < this.subLists.length; i++) {
      if (this.subLists[i].offsetHeight > targetHeight) targetHeight = this.subLists[i].offsetHeight;
      height3rdLevel = get3rdLevelHeight(this.subLists[i]);
      if (height3rdLevel > targetHeight) targetHeight = height3rdLevel;
   }
   this.hide();
   return targetHeight;
}
Navigation.prototype.show = function () {
   for (var i = 0; i < this.allItems.length; i++) {
      this.allItems[i].persistentClassName = this.allItems[i].className;
      this.allItems[i].className +="over";
      this.allItems[i].className +=" show";
   }
}
Navigation.prototype.hide = function () {
   for (var i = 0; i < this.allItems.length; i++) {
      this.allItems[i].className = this.allItems[i].persistentClassName;
   }
}
Navigation.prototype.setFullHeight = function () {
   for (var i = 0; i < this.subLists.length; i++) {
      //this.subLists[i].style.height = this.getHeight()+"px";
   }
}
Navigation.prototype.createHider = function () {
   var iframe = document.createElement('div');
   
   iframe.style.height = this.getHeight() + "px";
   iframe.style.width = this.navigation.offsetWidth + "px";
   
   this.navigation.appendChild(iframe);
   return (iframe);
}

Navigation.prototype.placeItems = function () {
   var mainItems = new Array();
   
   for (var i = 0; i < this.allItems.length; i++) {
      if (this.allItems[i].parentNode.className == 'level1') mainItems[mainItems.length] = this.allItems[i];
   }
   if (document.body.offsetWidth < 988) {
      mainItems[mainItems.length - 1].className += 'left';
      mainItems[mainItems.length - 1].id = 'lastleft';
      mainItems[mainItems.length - 2].className += 'left';
   }
   // force width of right-side nav items to display properly in ie
   if ((this.WIN_IE50) || (this.WIN_IE5PLUS)) {
      for (var i = 0; i < this.allItems.length; i++) {
         if (this.allItems[i].className == 'submenuleft') { 
            this.menu_item = this.allItems[i];
            this.checkWidth(this.menu_item);
         }
      }
   }
}
Navigation.prototype.checkWidth = function(menu_item) {
   this.menu_item = menu_item;
   this.wid = this.menu_item.offsetWidth;
   this.oddOrEven(this.wid);
   if (!this.WIN_IE60) {
      this.newWid += 15; //add right padding back on for ie6.0
   }
   this.menu_item.style.width = this.newWid;
}
Navigation.prototype.oddOrEven = function(wid) {
   this.wid = wid - 15; //remove right padding
   this.oe = this.wid % 2;
   if ((this.oe == 1) && (this.menu_item.id != "lastleft")) { this.wid++; }
   else if ((this.oe != 1) && (this.menu_item.id == "lastleft")) { this.wid++; }
   this.newWid = this.wid;
}
function get3rdLevelHeight(ul) {
   var height = 0;
   var maxHeight = 0;
   var currentYPos = 0;
   var secondLevelCounter = 0;
   var entries = ul.getElementsByTagName('li');
   
   for (var i = 0; i < entries.length; i++) {    
      if (entries[i].parentNode.className == 'level2') {
         if (entries[i].className == 'submenuover') {
            currentYPos = entries[i].offsetTop;
            height = currentYPos + entries[i].getElementsByTagName('ul')[0].offsetHeight;
         }
         if (height > maxHeight) maxHeight = height;
      }
   }
   return maxHeight;
}
function updateSelectBoxes(boxes, visibility) {
   for (var i = 0; i < boxes.length; i++) {
      boxes[i].style.visibility = visibility;
   }
}



function loadNav() {
   if (document.getElementById('navlogo')) {
      new Navigation('navigation', 'navopen', document.getElementById('navigation').innerHTML);
   }
   else {
      /* Prevent double loading, since older jsps, still load the navigation with a setTimeout */
      if (!navFrame && document.getElementById('navigation')) {
         navFrame = new RPCFrame(window);
         navFrame.setLocation('', initNavigation);
         try {
            clearInterval(n);
            } catch (e) {
         }
      }  
      else {
         return false;
      }
   }  
}
function initNavigation () {
   new Navigation('navigation', 'navopen', navFrame.getHTMLById('navcontent').innerHTML);
}

function isChild(ancestor, candidate) {
   if (!ancestor || !ancestor.parentNode || !candidate) return false;
   while (candidate && candidate != ancestor.parentNode) {
      if (candidate == ancestor) return true;
      try {
         candidate = candidate.parentNode;
      } catch (c) {return false}
   }
}
function createContextFunction(context, method, method2) {
   return (function(x){
      method = (method == "post") ? method2 : method;
      eval("context."+method+"(x)");
      return false;
   });
}
function setCookie(inName, inValue, inNumberOfDays)
{
   var expDate = new Date ();
   var cookieString;
   
   expDate.setTime (expDate.getTime() + (86400000 * inNumberOfDays));
   var gmtExpDate = expDate.toGMTString();
   
   cookieString = inName + "=" + inValue;
   if (inNumberOfDays != 0)
      cookieString += ";expires=" + gmtExpDate; 
   
   cookieString += ";path=/"; 
   document.cookie = cookieString;
}

//this function gets the value of a cookie given it's name
function getCookie (inName)   {
   var dCookie = document.cookie; 
   var cName = inName + "=";
   var cLen = dCookie.length;
   var cBegin = 0;
   while (cBegin < cLen)   {
      var vBegin = cBegin + cName.length;
      if (dCookie.substring(cBegin, vBegin) == cName) { 
         var vEnd = dCookie.indexOf (";", vBegin);
          if (vEnd == -1) 
            vEnd = cLen;
         return unescape(dCookie.substring(vBegin, vEnd));
      }
      cBegin = dCookie.indexOf(" ", cBegin) + 1;
      if (cBegin == 0)
          break;
   }
   return null;
}

//this function will delete a cookie by setting the expiration date in the past
function deleteCookie(inName) {
   document.cookie = inName + "=; expires=Thu, 01-Jan-70 00:00:01 GMT";
}

function isSafari() {
   return (/safari/i).test(navigator.userAgent);
}
window.onload = function() {
   loadNav();
}