// global variables
var _d = document;
var _Mtimeout = null;
var _MopenID = null;
var _Ntimeout = null;

// bootstrap; define functions that need to be called
nk_workaround_hideall=false; // called on when menu is about to be displayed
nk_workaround_showall=false;// called on when menu is about to get hidden
hideallselects = false;
showallselects = false;

/*
 fixed classnames
*/
var CLASS_TEXT_MOVER = "nav_item_MO";
var CLASS_TEXT_MOUT = "nav_item";
var CLASS_TEXT_NOCHANGE = "nav_item_current";


/*
 * class menu
 */ 

  // constructor
  function Menu(_MposID, _Mxpos, _Mypos, _Mwidth, _Mbtn, _Mspan) {
    // internal properties from _$GlobalMenuSettings
    for(propname in _$GlobalMenuSettings) {this[propname] = _$GlobalMenuSettings[propname];}

    // internal properties	
    this._MposID = _MposID;
    this._Mxpos = _Mxpos;
    this._Mypos = _Mypos;
    this._Mwidth = _Mwidth;

    this._Mbtn = _Mbtn;
    this._Mspan = _Mspan; /* #nk 28.5.2004 related text span id*/

    this._MrefID = _MposID + 'menuID';
    this._Mbtnsrc = null;
    this._Mposobj = null;
    this._Mopen = false;
    this._Mbtnlnk = _MposID + "btnlnk";
    this._Mbtnlnkobj = null; 
    this._MbtnAddWidth = 14; 
    this._MitemOff = null; 
    this.items = new Array();      

    // external methods	
    this.addItem = addItem;
    this.switchItem = switchItem;    
    this.writeMenu = writeMenu;
    this.show = show;
    this.hide = hide;    
        
    // internal methods  
    this.prototypeElement = prototypeElement;  
    this.getCoordinate = getCoordinate;            
  }
  
  // method addItem
  function addItem(_Mitem) {  	
    this.items[this.items.length] = _Mitem; 
    
    return true; 	
  }
  
  // switchItem 
  function switchItem() {
    var _MitemOn = parseInt((parseInt(this._Mbtnlnkobj.style.top) - parseInt(this.style.top)) / (parseInt(this._Mbtnlnkobj.style.height) + 2));	
    if(this._MitemOff != _MitemOn) {
    if(this._MitemOff != null) this.items[this._MitemOff].changeStyle();
    
    this.items[_MitemOn].changeStyle();
      
    this._MitemOff = _MitemOn;
    }
    return false; 
  }
  
  // method writeMenu
  function writeMenu() {  
    var _ISobj = new ItemStyle(_$GlobalItemSettings);	  	 
    
    _d.write('<div id="' + this._MrefID + '" class="' + this.clsLayer + '" onmouseover="showMenu(\'' + this._MrefID + '\');" onmouseout="hideMenu(\'' + this._MrefID + '\');"' + ' style="position: absolute; top: 200px;" >');  

    /* padding on the top of the menu*/
//    _d.write('<tr><td><img src="/icons/ecblank.gif" height="10" width="1"><!-- padding --></td></tr>'); 
    _d.write('<img src="/icons/ecblank.gif" height="2" width="1">');

    _d.write('<table border="0" cellspacing="0" cellpadding="0">');    


    // menu items      
    for(var i=0;i<this.items.length;i++) {
      this.items[i].getItem(_ISobj, this._Mwidth, i, this.items.length, this._MrefID);	
    }       

    _d.write('</table>');    
    _d.write('</div>');         
    
    // hidden btn      
    _d.write('<div id="' + this._Mbtnlnk + '" style="position: absolute; top: 0px; left: 0px; z-index: 10; visibility: hidden; width: 1px; height: 28px; overflow:hidden;"><a href="#" target="" onmouseover="showMenu(\'' + this._MrefID + '\'); return this.parentNode._menu.switchItem();" onmousemove="return this.parentNode._menu.switchItem();" onmouseout="hideMenu(\'' + this._MrefID + '\');" onfocus="this.blur();" style="background-color: transparent; display: block; empty-cells: show; width: 100%; height: 100%; font-size: 999px;">&nbsp;</a></div>');
       
    this.prototypeElement();
    
    return true;
  }    
  
  // method show
  function show() {

	if (nk_workaround_hideall) nk_workaround_hideall();

    _btnobj = getHTMLElement(this._Mbtn);       

/*
 debug_print (_btnobj.src +", (" + this._Mbtn + ") -> " + this._Mbtnsrc, false); 
 debug_print (this._Mbtnlnkobj.style.visibility + ":" + this.style.visibility, false); 
*/

    MM_swapImage(this._Mbtn,'',this._Mbtnsrc,1);    
	setCSSStyle (this._Mspan, CLASS_TEXT_MOVER); // set mouseover text span class


    this.style.left = this.getCoordinate("Left");
    this.style.top = this.getCoordinate("Top");  

    this._Mbtnlnkobj.style.left = this.style.left;
    this._Mbtnlnkobj.style.top = this.style.top;

    this._Mbtnlnkobj.style.width = this._Mwidth + this._MbtnAddWidth; 

if (this.items[0]) {
    this._Mbtnlnkobj.firstChild.href = this.items[0]._Ilink;
    this._Mbtnlnkobj.firstChild.target =  this.items[0]._Itarget;
  } // allow possibility of manu without any links (not configurated yet)
      
    this._Mbtnlnkobj.style.visibility = 'visible';    
    this.style.visibility = 'visible';
    
    this._Mopen = true;
    
    return true;     
  }
  
  // method hide
  function hide() {
    MM_swapImgRestore();	
	setCSSStyle (this._Mspan, CLASS_TEXT_MOUT); // restore text span class

    if(this._MitemOff != null) this.items[this._MitemOff].changeStyle();
    
    this._Mbtnlnkobj.style.visibility = 'hidden';            
    this.style.visibility = 'hidden';
        
    this._Mopen = false;
    this._MitemOff = null; 

/* do not move to the top-left corner; leave it were it is
    this.style.left = '0px';
    this.style.top = '0px';
    
    this._Mbtnlnkobj.style.left = '0px';
    this._Mbtnlnkobj.style.top = '0px';
    this._Mbtnlnkobj.style.width = '1';    
*/
	if (nk_workaround_showall) nk_workaround_showall();

    return true;     
  }        
     
  // method prototypeElement
  function prototypeElement() {
    _mobj = getHTMLElement(this._MrefID);
    
    _btnobj = getHTMLElement(this._Mbtn);

	_src = _btnobj.src;

	while (_src.indexOf("_l.gif")>-1) {
		_src = _src.replace(/_l\.gif/,"_h.gif");
	}

    this._Mbtnsrc = _src;
    
    this._Mposobj = getHTMLElement(this._MposID);
    this._Mbtnlnkobj = getHTMLElement(this._Mbtnlnk);    

    for(_Mprop in this) {_mobj[_Mprop] = this[_Mprop];}    
    
    _btnobj.parentNode._menu = _mobj.id;    
    this._Mbtnlnkobj._menu = _mobj;
    
    return true;
  }  
          
  // method getCoordinate
  function getCoordinate(axis) {     	
    var _pos = 0;
    var _objpos = this._Mposobj;
    
    while (_objpos != null) {
      _pos += _objpos["offset" + axis];
      _objpos = _objpos.offsetParent;
    }

    if(axis == 'Left') return parseInt(_pos + this._Mxpos) + 'px';
    if(axis == 'Top') return parseInt(_pos + this._Mypos) + 'px';    
    
    return parseInt(_pos) + 'px';
  }     

  



/*
 * class ItemStyle
 */ 

  // constructor
  function ItemStyle(_ISsettings) {
    // internal properties from _$GlobalItemSettings
    for(propname in _ISsettings) {this[propname] = _ISsettings[propname];}
  }  
  
  
  
         
   
/*
 * class item
 */ 

  // constructor
  function Item(_Ilabel, _Ilink, _Itarget) {      	
    // internal properties	
    this._Ilabel = _Ilabel;
    this._Ilink = _Ilink;
    this._Itarget = _Itarget;
    this._IleftIDsuffix = 'left';
    this._IrightIDsuffix = 'right';
    this._IrefL = null;    
    this._IrefR = null;
    this._Ilow = true; 
    this._Istyle = null;    
    
    // external methods
    this.getItem = getItem;
    this.changeStyle = changeStyle;	
  }
  
  // method getItem
  function getItem(_Istyle, _Iwidth, _Inumber, _Icount, _Mid) {
    this._Istyle = _Istyle;
    
    this._IrefL = _Mid + _Inumber + this._IleftIDsuffix;	  	  	
    this._IrefR = _Mid + _Inumber + this._IrightIDsuffix;
        
    // item	
    _d.write('<tr>'); 
         
    _d.write('<td id="' + this._IrefL + '" class="' + this._Istyle.clsLinkLeftOff + '" onmouseover="setLnk(\'' + _Mid + '\', \'' + _Inumber + '\');"><div class="' + this._Istyle.clsSpacerLeft + '"><img src="/icons/ecblank.gif" height="1" width="13"></div></td>');                 
    
    _d.write('<td onmouseover="setLnk(\'' + _Mid + '\', \'' + _Inumber + '\');"><div class="' + this._Istyle.clsSpacerV + '">&nbsp;</div></td>');        
            
    _d.write('<td nowrap width="' + _Iwidth + '" id="' + this._IrefR + '" class="' + this._Istyle.clsLinkRightOff + '" style="width: ' + _Iwidth + 'px" onmouseover="setLnk(\'' + _Mid + '\', \'' + _Inumber + '\');">' + this._Ilabel + '</td>');        
         
    _d.write('</tr>');          
        
    // divider
    if((_Inumber+1) != _Icount) {    	
      _d.write('<tr>'); 
      _d.write('<td colspan="3"><div class="' + this._Istyle.clsSpacerH + '">&nbsp;</div></td>');   
      _d.write('</tr>');              
    }
     
    return true; 
  } 
  
  // method changeStyle
  function changeStyle() {
    var _IobjL = getHTMLElement(this._IrefL);
    var _IobjR = getHTMLElement(this._IrefR); 
    
    if(this._Ilow) {
      _IobjL.className = this._Istyle.clsLinkLeftOn;
      _IobjR.className = this._Istyle.clsLinkRightOn; 

      this._Ilow = false;     
    } else {
      _IobjL.className = this._Istyle.clsLinkLeftOff;    	
      _IobjR.className = this._Istyle.clsLinkRightOff;
      
      this._Ilow = true;
    }
    
    return true;
  } 
  
  
  
/*
 * class Navigation
 */ 

  // constructor
  function Navigation(_NclsLl, _NclsLh, _NclsRl, _NclsRh) {
    // properties
    this._NclsLl = _NclsLl;
    this._NclsLh = _NclsLh;
    this._NclsRl = _NclsRl;
    this._NclsRh = _NclsRh;
    this._NrefL = null;
    this._NrefR = null;  
    this._NndID = null;  
   
    // external methods
    this.switchStyle = switchStyle;
    
    // internal methods
    this.getRef = getRef;
    this.itemON = itemON;
    this.itemOFF = itemOFF;    
    
  }
  
  // function switchStyle
  function switchStyle(_nID) {
    if(this._NndID == _nID) return true;	
    if(_nID == null) return this.itemOFF();
        
    if(this._NndID != null) this.itemOFF();    
    this._NndID = _nID;
    
    this.getRef();
    
    return this.itemON();    
  }   
  
  // getRef
  function getRef() {
    var _Nbase = getHTMLElement(this._NndID);
    var counter = 0;
    
    for(i=0;i<_Nbase.childNodes.length;i++) {
      if(_Nbase.childNodes[i].nodeName.toLowerCase() == 'td') {
      	   counter++;
          if(counter == 1) {this._NrefL = _Nbase.childNodes[i];}
          if(counter == 3) {this._NrefR = _Nbase.childNodes[i]; i = 1000;}
      }	  	
    }
    
    return true;     
  }
  
  // itemON
  function itemON() {
    if(this._NrefL) this._NrefL.className = this._NclsLh;	
    if(this._NrefR) this._NrefR.className = this._NclsRh; 
    
    return true;   
  }
  
  // itemOFF
  function itemOFF() {
    if(this._NrefL) this._NrefL.className = this._NclsLl;	
    if(this._NrefR) this._NrefR.className = this._NclsRl;   	
    
    this._NndID = null;
    
    return true;
  }

  

  
/*
 * different functions
 */ 
 
 // createMenu
 function createMenu() {   
   for(var i=0;i<menu.length;i++) {
     menu[i].writeMenu();
   }   
   
   return true;	
 }
 
 // hideMenu
 function hideMenu(_menu) {   
   if(arguments.length == 1)
     _Mtimeout = setTimeout('execHideMenu("' + _menu + '")', 99);
   else
     execHideMenu(_menu);     
     
   return false;	
 }
 
 // execHideMenu
 function execHideMenu(_menu) {  
   _Mobj = getHTMLElement(_menu);
   _Mobj.hide();
  
   _MopenID = null;
  
   return false;	
 }
  
 // showMenu
 function showMenu(_menu) { 
   if(arguments.length == 2 && _MopenID != null) {
     _Mobj = getHTMLElement(_MopenID);	
     _Mobj.hide();
     _MopenID = null;
   }
   
   _Mobj = getHTMLElement(_menu);
   
   clearTimeout(_Mtimeout);    

   if(!_Mobj._Mopen) _Mobj.show();
    
   _MopenID = _Mobj.id;
      
   return false;	
 }  
 
  // getHTMLElement
  function getHTMLElement(_element) {      	
    if(_d.getElementById) return _d.getElementById(_element);
    if(_d.all) return _d.all[_element];
    
    return false;
  } 
  
  // setLnk
  function setLnk(_menu, _lnkNr) {
    _Mobj = getHTMLElement(_menu);
    
    _Mobj._Mbtnlnkobj.style.top = (parseInt(_Mobj.style.top) + (parseInt(_Mobj._Mbtnlnkobj.style.height) + 2) * parseInt(_lnkNr)) + 'px';

    if (_Mobj.items[_lnkNr]) {
      _Mobj._Mbtnlnkobj.firstChild.href = _Mobj.items[_lnkNr]._Ilink;
      _Mobj._Mbtnlnkobj.firstChild.target =  _Mobj.items[_lnkNr]._Itarget;
    }
    return false;  	
  }      
  
  // function changeItem
  function changeItem(_node) {    
    if(_node == null) {
      if(_Ntimeout != null) clearTimeout(_Ntimeout);	
      _Ntimeout = setTimeout('execChangeItem()', 99);    
    } else {
      if(_Ntimeout != null) clearTimeout(_Ntimeout);	
      _$N.switchStyle(_node);
    }
    
    return false;	
  }
  
  // function execChangeItem
  function execChangeItem() {
    _$N.switchStyle(null);
    
    return false;
  }     
  
  // content navigation object
  var _$N = new Navigation('cnavLl', 'cnavLh', 'cnavRl', 'cnavRh');   



/* debug code */
function debug_print (txt, init) {
if (getHTMLElement("debug_printout")) {
		if (init) {
			getHTMLElement("debug_printout").innerHTML = txt;
		} else {
			getHTMLElement("debug_printout").innerHTML = getHTMLElement("debug_printout").innerHTML + "<br>" + txt;
		}
	}
}


/* 
* called on mouseover/mouseout for top navigation buttons
* eventually enable setting class parameter of the element 
*/
function setCSSStyle (objid, className) {
   /* set style */
   var obj = getHTMLElement(objid);
   if (obj!=null && !(obj.className==CLASS_TEXT_NOCHANGE)) obj.className = className; 
}