var navTimerID = null ;
var headerTimerID = null ;
var footerTimerID = null ;
var navIframeReady = false;
var headerIframeReady = false;
var footerIframeReady = false;
var delay = 2 ;

function loadNavigation(){
   var thisPageNavObj = document.getElementById ( "nav" );
   var navFramObj = window.frames["Navigation"];
   var iframeDoc = null;
   var navObj = null;
   if ( navFramObj != null ) {
      iframeDoc = navFramObj.document;
   }
   if ( iframeDoc != null ) {
      navObj = iframeDoc.getElementById("nav");
   }
   if ( navObj != null ) {
      var tmpNavHTML = navObj.innerHTML;
      var currentURL = window.location.href;
      var liObj = navObj.getElementsByTagName ( "LI" );
      var pos = currentURL.lastIndexOf ( "/" );
      currentURL = currentURL.substring ( pos + 1);      
    //  pos = currentURL.lastIndexOf ( "?" );
      
    //  if ( pos != -1 )
    //     currentURL = currentURL.substring (0, pos);  
            
      var tmpCurrentURL = currentURL.toLowerCase();         
      
      for ( i = 0; i < liObj.length; i++ ) {
         var liHTML = liObj[i].innerHTML;
         liHTML = liHTML.toLowerCase();
         if ( liHTML.indexOf ( tmpCurrentURL) != -1 ){
            liObj[i].className = "Current";
            var subID = (liObj[i].childNodes[0].innerHTML).toLowerCase();            
            while ( subID.indexOf ( " " ) != -1 )
               subID = subID.replace ( " ", "");               
            var subItemsObj = iframeDoc.getElementById( subID  ); 
            
            if ( subItemsObj != null ) {
               subItemsObj.style.display = "block";               
            }
         } else
            liObj[i].className = "";
         var childNodes = liObj[i].childNodes;
         for ( j = 0 ; j < childNodes.length; j++ ) {
            if ( childNodes[j].tagName == "DIV" ) {
               var divChildNodes = childNodes[j].childNodes;               
               for ( k = 0; k < divChildNodes.length; k++ ) {
                  var aHref = divChildNodes[k].href;
                  if ( typeof ( aHref ) != "undefined" ) {
                     pos = aHref.lastIndexOf ( "/" );
                     aHref = aHref.substring ( pos + 1);      
                     aHref = aHref.toLowerCase();
                     //alert (tmpCurrentURL);
                     //alert ( aHref );                   
                    // if ( aHref.indexOf ( tmpCurrentURL) != -1 ){
                     if ( aHref == tmpCurrentURL){
                        divChildNodes[k].style.color = "blue";
                        liObj[i].className = "Current";                        
                        //liObj[i].style.paddingBottom = "5px";
                        childNodes[j].style.display = "block";
                     }
                  }
               }
            }
         }
      }    
      tmpNavHTML = navObj.innerHTML;
      // these two lines for the product links, because it's absolute url
      //tmpNavHTML = tmpNavHTML.replace (  "<LI><A href=\"" + currentURL + "\">", "<LI class=\"Current\"><A href=\"" + currentURL + "\">" );
      //tmpNavHTML = tmpNavHTML.replace (  "<li><a href=\"" + currentURL + "\">", "<li class=\"Current\"><a href=\"" + currentURL + "\">" );
      //tmpNavHTML = tmpNavHTML.replace (  "<LI><A style=\"PADDING-BOTTOM:18px\" href=\"" + currentURL + "\">", "<LI class=\"Current\"><A style=\"PADDING-BOTTOM:5px\" href=\"" + currentURL + "\">" );
      //tmpNavHTML = tmpNavHTML.replace (  "<li><a style=\"padding-bottom:18px\" href=\"" + currentURL + "\">", "<li class=\"Current\"><a style=\"padding-bottom:5px\" href=\"" + currentURL + "\">" );

      //var pos = currentURL.lastIndexOf ( "/" );
      //currentURL = currentURL.substring ( pos + 1);
    
      // these two lines for other links, because it's relative url     
      //tmpNavHTML = tmpNavHTML.replace (  "<LI><A href=\"" + currentURL + "\">", "<LI class=\"Current\"><A href=\"" + currentURL + "\">" );
      //tmpNavHTML = tmpNavHTML.replace (  "<li><a href=\"" + currentURL + "\">", "<li class=\"Current\"><a href=\"" + currentURL + "\">" );
      //tmpNavHTML = tmpNavHTML.replace (  "<LI><A style=\"PADDING-BOTTOM: 18px\" href=\"" + currentURL + "\">", "<LI class=\"Current\"><A style=\"PADDING-BOTTOM:5px\" href=\"" + currentURL + "\">" );
      //tmpNavHTML = tmpNavHTML.replace (  "<li><a style=\"padding-bottom: 18px\" href=\"" + currentURL + "\">", "<li class=\"Current\"><a style=\"padding-bottom:5px\" href=\"" + currentURL + "\">" );

      thisPageNavObj.innerHTML = tmpNavHTML;
      navIframeReady = true;
   }
   if ( !navIframeReady ) {
       navTimerID = self.setTimeout ( "loadNavigation()", delay ) ;
   } else {
      clearTimeout ( navTimerID ) ;
   }
}
function loadHeader(){
   var thisPageHeaderObj = document.getElementById ( "header" );
   var headerFramObj = window.frames["HeaderHTML"];
   var headerIframeDoc = null;
   var headerObj = null;
   if ( headerFramObj != null ) {
      headerIframeDoc = headerFramObj.document;
   }
   if ( headerIframeDoc != null ) {
      headerObj = headerIframeDoc.getElementById("header");
   }
   if ( headerObj != null ) {
      thisPageHeaderObj.innerHTML = headerObj.innerHTML;
      headerIframeReady = true;
   }
   if ( !headerIframeReady ) {
       headerTimerID = self.setTimeout ( "loadHeader()", delay ) ;
   } else {
      clearTimeout ( headerTimerID ) ;
   }
}
function loadFooter(){
   var thisPageFooterObj = document.getElementById ( "footer" );
   var footerFramObj = window.frames["FooterHTML"];
   var footerIframeDoc = null;
   var footerObj = null;
   
   if ( thisPageFooterObj != null ) {    
    if ( footerFramObj != null ) {
        footerIframeDoc = footerFramObj.document;
    }
    if ( footerIframeDoc != null ) {
        footerObj = footerIframeDoc.getElementById("footer");
    }   
    if ( footerObj != null ) {
        footerIframeReady = true;
    }
   }
   if ( !footerIframeReady ) {
       footerTimerID = self.setTimeout ( "loadFooter()", delay ) ;
   } else {
      thisPageFooterObj.innerHTML = footerObj.innerHTML;
      clearTimeout ( footerTimerID ) ;
   }
}
loadHeader();
loadNavigation();
loadFooter();
