// JavaScript Document
window.onload = makeDoubleDelegate(window.onload, inicio);

function cambio(idCapa, idCapa2){
  document.getElementById(idCapa).style.display = "none";
  document.getElementById(idCapa2).style.display = "block";
}

function makeDoubleDelegate(function1, function2) {
return function() {
if (function1)
function1();
if (function2)
function2();
}
}

function inicio()
{
    /*if (window.location.href=='http://www.caatvalencia.es/') {
    open('http://www.caatvalencia.es/mail/concierto_2010_popup.html','miVentana','width=700,height=700','toolbar=no,directories=no,menubar=no,status=no');
    };   
    if (window.location.href=='http://www.caatvalencia.es/Default.aspx') {
    open('http://www.caatvalencia.es/mail/concierto_2010_popup.html','miVentana','width=700,height=700','toolbar=no,directories=no,menubar=no,status=no');
    };*/
    
    /*if (window.location.href=='http://www.caatvalencia.es/') {
    open('http://www.caatvalencia.es/mail/vino_2010_popup.html','miVentana','width=660,height=530','toolbar=no,directories=no,menubar=no,status=no');
    };   
    if (window.location.href=='http://www.caatvalencia.es/Default.aspx') {
    open('http://www.caatvalencia.es/mail/vino_2010_popup.html','miVentana','width=660,height=530','toolbar=no,directories=no,menubar=no,status=no');
    };  */
    
    rollup('menu_pub', true, true, false);
    rollup('menu_priv', true, true, false);
    rollup('menu_publico', true, true, false); 
    rollup('enlaces_basicos', false, true, false); 
    rollup('enlaces_relacionados', false, true, false);
    rollup('enlaces_favoritos', false, true, false);
    
    var objcapacentral = document.getElementById('contenido');
    var objcapaizq = document.getElementById('navegacion');
    var objcapader = document.getElementById('utilidades');
      
    if (objcapacentral.offsetHeight > objcapaizq.offsetHeight && objcapacentral.offsetHeight > objcapader.offsetHeight)
    {
        objcapacentral.style.height = '';
        objcapaizq.style.height = objcapacentral.offsetHeight + 'px';
        objcapader.style.height = objcapaizq.style.height;
    }
    else
    {    
        objcapaizq.style.height = (Math.max(Math.max(objcapacentral.offsetHeight, objcapaizq.offsetHeight), objcapader.offsetHeight)) + 'px';
        objcapader.style.height = objcapaizq.style.height;
        //objcapacentral.style.height = objcapader.style.height;
    };

   
}

function rollout(objMenuitem, objEvent, strMenu, bMultiple, bEstatico)
{
    var iKeyCode;
    
    var objcapacentral = document.getElementById('contenido');
    var objcapaizq = document.getElementById('navegacion');
    var objcapader = document.getElementById('utilidades');
    
    objcapaizq.style.height = '';
    objcapader.style.height = '';
    
    if (bEstatico)
        return false
    // Check if from a keyboard - non IE, but
    // irrelevant as tab doesn't trigger the 
    // keypress event in IE
    
    if (objEvent && objEvent.type == 'keypress')
    {
        if (objEvent.keyCode)
            iKeyCode = objEvent.keyCode;
        else if (objEvent.which)
            iKeyCode = objEvent.which;

        // If it's not the enter key or space key, 
        // pass control back to the browser
        if (iKeyCode != 13 && iKeyCode != 32)
            return true;
    }

    // Work out what we need to do      
   if (objMenuitem.parentNode.nextSibling.style.display == 'block')    
        var strDisplay = 'none'
    else
        var strDisplay = 'block';
    
    // Close any nested lists that are open
    var objMenu = document.getElementById(strMenu);
    var objNested = objMenu.getElementsByTagName('ul');

    if (!bMultiple)
        for (var i=0; i<objNested.length; i++)
            if (objNested[i].style.display == 'block')
                objNested[i].style.display = 'none';

    objMenuitem.parentNode.nextSibling.style.display = strDisplay;
    
    // Stop the browser requesting the link
  
    objcapaizq.style.height = (Math.max(Math.max(objcapacentral.offsetHeight, objcapaizq.offsetHeight), objcapader.offsetHeight)) + 'px';
    objcapader.style.height = objcapaizq.style.height;
    if (objcapacentral.offsetHeight == (Math.max(Math.max(objcapacentral.offsetHeight, objcapaizq.offsetHeight), objcapader.offsetHeight))) 
    {objcapacentral.style.height = '';};
    //else
    //{objcapacentral.style.height = objcapaizq.style.height;};
    
    
    return false;
}

function rollup(strMenu, bColapsado, bMultiple, bEstatico)
{	
    var bRollup, objLinks, objNode, objAnchor;

    // Check we're working with a DOM compliant browser
    if (document.getElementById && document.createElement)
    {
        var strLocation = window.location;

        var objMenu = document.getElementById(strMenu);

        if (objMenu == null) return false;
        
        var objNested = objMenu.getElementsByTagName('ul');

        // Hide each of the nested unordered list
        for (var i=0; i<objNested.length; i++)
        {
            // Only hide, if the current location is not found in the list
          
            bRollup = true;
            objLinks = objNested[i].getElementsByTagName('a');

            for (var j=0; j<objLinks.length; j++)
            {
                if (objLinks[j].href == strLocation)
                {
                    bRollup = false;

                    // Added by John Hunter
                    // remove link for current page
                    objNode = objLinks[j];
                    strContent = objNode.firstChild.data;
                    //var objCurrentPage = document.createElement('strong');
                    var objCurrentPage = document.createElement('div');
                    objCurrentPage.appendChild(document.createTextNode(strContent));
                    objNode.parentNode.replaceChild(objCurrentPage, objNode);

                    // Add an id so we can keep the parents open
                    objCurrentPage.id = 'jsKeepOpen';
                }
            }
          
            bRollup = bColapsado
            if (bRollup == true)
                objNested[i].style.display = 'none';
            else
                objNested[i].style.display = 'block';

            // Place the top-level text in an anchor tag
            objNode = objNested[i].parentNode.firstChild;            
            
            objPs = objNested[i].parentNode.getElementsByTagName('p');

            if(objPs.length > 0)
            {
                // si tenemos un p hacemos cosas nuevas para que el enlace abarque todo el <p>                           
                objSpans = objPs[0].parentNode.getElementsByTagName('span');
                if(objSpans.length > 0)
                {
                    strContent = objSpans[0].firstChild.data;
                    objspan = document.createElement('span');
                    objspan.appendChild(document.createTextNode(strContent));
                }
                strContent = objPs[0].firstChild.data;
                objp = document.createElement('p');
                objp.appendChild(document.createTextNode(strContent));
                objp.appendChild(document.createElement('br'));
                objp.appendChild(objspan);
                
                objAnchor = document.createElement('a');
                objAnchor.href = '#';                
                objAnchor.onclick = function(event){return rollout(this, event, strMenu, bMultiple, bEstatico);}
                objAnchor.onkeypress = function(event){return rollout(this, event, strMenu, bMultiple, bEstatico);}
                objAnchor.appendChild(objp);
                objNode.replaceChild(objAnchor, objNode.firstChild);
            }
            else
            {
                strContent = objNode.firstChild.data;

                objAnchor = document.createElement('a');
                objAnchor.href = '#';              
                objAnchor.onclick = function(event){return rollout(this, event, strMenu, bMultiple, bEstatico);}
                objAnchor.onkeypress = function(event){return rollout(this, event, strMenu, bMultiple, bEstatico);}
                objAnchor.appendChild(document.createTextNode(strContent));
                objNode.replaceChild(objAnchor, objNode.firstChild);
            };

            
        }

        // Keep any parent menus for the current item
        
        if (document.getElementById('jsKeepOpen'))
        {
            var objKeepOpen = document.getElementById('jsKeepOpen');
          
            objKeepOpen = objKeepOpen.parentNode;
            objKeepOpen.style.display = 'block';
            objKeepOpen = objKeepOpen.parentNode;
            objKeepOpen.style.display = 'block';
/*            
            while (objKeepOpen.id != strMenu)
            {                
                objKeepOpen = objKeepOpen.parentNode;
                objKeepOpen.style.display = 'block';                
            }
*/            
        }    
    }
}
