var slidedown_menuObj;   // Reference to the menu div 
var currentZIndex = 1000; 
var liIndex = 0; 
var visibleMenus = new Array(); 
var activeMenuItem = false; 
var timeBeforeAutoHide = 1200; // Microseconds from mouse leaves menu to auto hide. 
var slidedown_menu_arrow = ''; 

var MSIE = navigator.userAgent.indexOf('MSIE')>=0?true:false; 
var isFirefox = navigator.userAgent.toLowerCase().indexOf('firefox')>=0?true:false; 
var navigatorVersion = navigator.appVersion.replace(/.*?MSIE ([0-9]\.[0-9]).*/g,'$1')/1; 
var menuBlockArray = new Array(); 
var menuParentOffsetLeft = false;    


// {{{ getStyle() 
/** 
* Return specific style attribute for an element 
* 
* @param Object el = Reference to HTML element 
* @param String property = Css property 
* @private 
*/        
function getStyle(el,property) 
{        

  if (document.defaultView && document.defaultView.getComputedStyle) { 

     var retVal = null;              
     var comp = document.defaultView.getComputedStyle(el, ''); 
     if (comp){ 
        retVal = comp[property]; 

        if(!retVal){ 
           var comp = document.defaultView.getComputedStyle(el, null); 
           retVal = comp.getPropertyCSSValue(property); 
        }          
     }    

     if(retVal==null)retVal=''; 

     return el.style[property] || retVal; 
  } 
  if (document.documentElement.currentStyle && MSIE){    
     var value = el.currentStyle ? el.currentStyle[property] : null; 
     return ( el.style[property] || value ); 

  } 
  return el.style[property];              
} 

function getTopPos(inputObj) 
{ 
var origInputObj = inputObj;

 var returnValue = inputObj.offsetTop; 

//alert (origInputObj.id+' top: '+returnValue+' parent:'+inputObj.offsetParent.id);
 if(inputObj.tagName=='LI' && inputObj.parentNode.className=='menuBlock1'){    
    var aTag = inputObj.getElementsByTagName('A')[0]; 
    if(aTag)returnValue += aTag.parentNode.offsetHeight; 
 } 
 var topOfMenuReached = false; 
 while((inputObj = inputObj.offsetParent) != null){ 
	//alert (inputObj.parentNode.id);
    if(inputObj.parentNode.id=='slidedown_menu')topOfMenuReached=true; 
    if(topOfMenuReached && !inputObj.className.match(/menuBlock/gi) || (!MSIE && origInputObj.parentNode.className=='menuBlock1')){ 
       var style = getStyle(inputObj,'position'); 
       if(style=='absolute' || style=='relative'){                
          return returnValue;            
       } 
    } 

    returnValue += inputObj.offsetTop;
    //alert(returnValue);
 } 

 return returnValue; 
} 

function getLeftPos(inputObj) 
{ 
 var returnValue = inputObj.offsetLeft; 

 var topOfMenuReached = false; 
 while((inputObj = inputObj.offsetParent) != null){ 
   if(inputObj.parentNode.id=='slidedown_menu')topOfMenuReached=true; 
    if(topOfMenuReached && !inputObj.className.match(/menuBlock/gi)){ 
       var style = getStyle(inputObj,'position'); 
       if(style=='absolute' || style=='relative')return returnValue; 
    } 

    returnValue += inputObj.offsetLeft; 
 } 
 return returnValue; 
} 



function showHideSub() 
{ 

  var attr = this.parentNode.getAttribute('currentDepth'); 
  if(navigator.userAgent.indexOf('Opera')>=0){ 
     attr = this.parentNode.currentDepth; 
  } 

  this.className = 'currentDepth' + attr + 'over'; 

  if(activeMenuItem && activeMenuItem!=this){ 
     activeMenuItem.className=activeMenuItem.className.replace(/over/,''); 
  } 
  activeMenuItem = this; 

  var numericIdThis = this.id.replace(/[^0-9]/g,''); 
  var exceptionArray = new Array(); 
  // Showing sub item of this LI 
  var sub = document.getElementById('subOf' + numericIdThis); 
  if(sub){ 
     visibleMenus.push(sub); 
     sub.style.display=''; 
     sub.parentNode.className = sub.parentNode.className + 'over'; 
     exceptionArray[sub.id] = true; 
  }    

  // Showing parent items of this one 

  var parent = this.parentNode; 
  while(parent && parent.id && parent.tagName=='UL'){ 
     visibleMenus.push(parent); 
     exceptionArray[parent.id] = true; 
     parent.style.display=''; 

     var li = document.getElementById('slidedownmenu_listItem' + parent.id.replace(/[^0-9]/g,'')); 
     if(li.className.indexOf('over')<0)li.className = li.className + 'over'; 
     parent = li.parentNode; 

  } 


  hideMenuItems(exceptionArray); 



} 

function hideMenuItems(exceptionArray) 
{ 
  /* 
  Hiding visible menu items 
  */ 
  var newVisibleMenuArray = new Array(); 
  for(var no=0;no<visibleMenus.length;no++){ 
     if(visibleMenus[no].className!='menuBlock1' && visibleMenus[no].id){ 
        if(!exceptionArray[visibleMenus[no].id]){ 
           var el = visibleMenus[no].getElementsByTagName('A')[0]; 
           visibleMenus[no].style.display = 'none';
            var li = document.getElementById('slidedownmenu_listItem' + visibleMenus[no].id.replace(/[^0-9]/g,'')); 
           if(li.className.indexOf('over')>0)li.className = li.className.replace(/over/,''); 
        }else{              
           newVisibleMenuArray.push(visibleMenus[no]); 
        } 
     } 
  }        
  visibleMenus = newVisibleMenuArray;        
} 



var menuActive = true; 
var hideTimer = 0; 
function mouseOverMenu() 
{ 
  menuActive = true;        
} 

function mouseOutMenu() 
{ 
  menuActive = false; 
  timerAutoHide();    
} 

function timerAutoHide() 
{ 
  if(menuActive){ 
     hideTimer = 0; 
     return; 
  } 

  if(hideTimer<timeBeforeAutoHide){ 
     hideTimer+=100; 
     setTimeout('timerAutoHide()',99); 
  }else{ 
     hideTimer = 0; 
     autohideMenuItems();    
  } 
} 

function autohideMenuItems() 
{ 
  if(!menuActive){ 
     hideMenuItems(new Array());    
     if(activeMenuItem)activeMenuItem.className=activeMenuItem.className.replace(/over/,'');        
  } 
} 


function initSubMenus(inputObj,initOffsetLeft,currentDepth) 
{ 
//caundo se inicia todos los submenus se dibujan y se colocan en posicion absoluta
  //alert (inputObj.id+' offset left '+initOffsetLeft+', currdepth '+currentDepth+' probable width:'+inputObj.offsetWidth); 
	var probable_width=inputObj.offsetWidth; //width del ul
  var subUl = inputObj.getElementsByTagName('UL'); 
//alert (subUL);
  if(subUl.length>0){ 
     var ul = subUl[0]; 
//alert (ul);
     ul.id = 'subOf' + inputObj.id.replace(/[^0-9]/g,''); 
//alert (ul.id);
     ul.setAttribute('currentDepth' ,currentDepth); 
     ul.currentDepth = currentDepth; 
     ul.className='menuBlock' + currentDepth; 
     ul.onmouseover = mouseOverMenu; 
     ul.onmouseout = mouseOutMenu; 
     currentZIndex+=1; 
     ul.style.zIndex = currentZIndex; 
     menuBlockArray.push(ul); 
     ul = slidedown_menuObj.appendChild(ul); 
	//alert (inputObj.id);
     var topPos = getTopPos(inputObj); 
     var leftPos = getLeftPos(inputObj)/1 + initOffsetLeft/1; 
        
	  ul.style.position = 'absolute'; 
	//alert ('colocando '+ul.id+' en posl '+leftPos);
	if ( MSIE){
		var width_ventana=document.body.offsetWidth;
	} else {   
		var width_ventana=window.innerWidth;
	}
	
	var tamano=leftPos+225;
	var mblock=0;
	if ((tamano > width_ventana) && initOffsetLeft==0) {
		//alert ('w '+width_ventana+' t:'+tamano+' L:'+leftPos+' d:'+currentDepth+' i:'+initOffsetLeft+' prob width:'+probable_width);
		//leftPos=(leftPos+((width_ventana-leftPos)/3))-225;
		leftPos=(leftPos+probable_width)-225-17;//el 17 es del padding definido en el css
	}
	if (initOffsetLeft>0) {
		//para los nivles 3 en adelante
		if (tamano >  width_ventana){
			mblock=1;
			
			//alert('io:'+initOffsetLeft+' gl:'+getLeftPos(inputObj)+' v:'+width_ventana);
			//leftPos=getLeftPos(inputObj)+initOffsetLeft/3;
			leftPos=getLeftPos(inputObj)-225;//el tamaño del menublock en el css
		}
	}
     ul.style.left = leftPos + 'px'; 
     ul.style.top = topPos + 'px'; 
     var li = ul.getElementsByTagName('LI')[0]; 
     while(li){ 
        if(li.tagName=='LI'){    
           li.className='currentDepth' + currentDepth;                
           li.id = 'slidedownmenu_listItem' + liIndex; 
           liIndex++;              
           var uls = li.getElementsByTagName('UL'); 
           li.onmouseover = showHideSub; 

           if(uls.length>0){ 
              var offsetToFunction = li.getElementsByTagName('A')[0].offsetWidth+2; 
              if(navigatorVersion<6 && MSIE) offsetToFunction+=15;   // MSIE 5.x fix 
              initSubMenus(li,offsetToFunction,(currentDepth+1)); 
           }    
           if(MSIE){ 
              var a = li.getElementsByTagName('A')[0]; 
              a.style.width=li.offsetWidth+'px'; 
              a.style.display='block'; 
           }                
        } 
        li = li.nextSibling; 
     } 
	ul.style.display = 'none';
	//ul.style.display = 'block';//ver como los dibuja desde el principio
	//if (mblock) {
	//	ul.style.display = 'block';
	//} else {
	//	ul.style.display = 'none';
	//}
      
    

     if(!document.all){ 
        //slidedown_menuObj.appendChild(ul); 
     } 
  }    
} 


function resizeMenu() 
{ 
  var offsetParent = getLeftPos(slidedown_menuObj); 

  for(var no=0;no<menuBlockArray.length;no++){ 
     var leftPos = menuBlockArray[no].style.left.replace('px','')/1; 
     menuBlockArray[no].style.left = leftPos + offsetParent - menuParentOffsetLeft + 'px'; 
  } 
  menuParentOffsetLeft = offsetParent; 
} 

/* 
Initializing menu 
*/ 
function initSlideDownMenu() 
{ 
  slidedown_menuObj = document.getElementById('slidedown_menu'); 


  var aTags = slidedown_menuObj.getElementsByTagName('A'); 
  for(var no=0;no<aTags.length;no++){          

     var subUl = aTags[no].parentNode.getElementsByTagName('UL'); 
     if(subUl.length>0 && aTags[no].parentNode.parentNode.parentNode.id != 'slidedown_menu'){ 
        //var img = document.createElement('IMG'); 
        //img.src = slidedown_menu_arrow; 
        //aTags[no].appendChild(img);              

     } 

  } 

  var mainMenu = slidedown_menuObj.getElementsByTagName('UL')[0]; 
  mainMenu.className='menuBlock1'; 
  mainMenu.style.zIndex = currentZIndex; 
  mainMenu.setAttribute('currentDepth' ,1); 
  mainMenu.currentDepth = '1'; 
  mainMenu.onmouseover = mouseOverMenu; 
  mainMenu.onmouseout = mouseOutMenu;        

  var mainMenuItemsArray = new Array(); 
  var mainMenuItem = mainMenu.getElementsByTagName('LI')[0]; 
  mainMenu.style.height = mainMenuItem.offsetHeight + 2 + 'px'; 
  while(mainMenuItem){ 

     mainMenuItem.className='currentDepth1'; 
     mainMenuItem.id = 'slidedownmenu_listItem' + liIndex; 
     mainMenuItem.onmouseover = showHideSub; 
     liIndex++;              
     if(mainMenuItem.tagName=='LI'){ 
        mainMenuItem.style.cssText = 'float:left;';    
        mainMenuItem.style.styleFloat = 'left'; 
        mainMenuItemsArray[mainMenuItemsArray.length] = mainMenuItem; 
        initSubMenus(mainMenuItem,0,2); 
     }          

     mainMenuItem = mainMenuItem.nextSibling; 

  } 

  for(var no=0;no<mainMenuItemsArray.length;no++){ 
     initSubMenus(mainMenuItemsArray[no],0,2);          
  } 

  menuParentOffsetLeft = getLeftPos(slidedown_menuObj);    
  window.onresize = resizeMenu;    
  slidedown_menuObj.style.visibility = 'visible';    
} 

