/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
 var items2;
    var accordion;
    var calendarioXML;
    var historia=new Array();
    var htmlIndex='inicio';
    window.addEvent('load',function(){
        cargarCalendario();
        cargarAcordeonConMT12();
//        htmlIndex=$('contentAjax').get('html');
//        historia[0]=location.href;
    });

    function cargarCalendario()
    {
        var arrayFechas=new Array();
        calendarioXML=leerXML('javascripts/CALENDARIO.xml');
                    var fechas=calendarioXML.getElementsByTagName('fecha');
                    for (var i = 0; i < fechas.length; i++)
                    {
                        arrayFechas.include(fechas[i].getAttribute('dia')+' '+fechas[i].getAttribute('mes')+' '+fechas[i].getAttribute('anio')+' ');
                    }
        var myCal = new Calendar({ date: 'd/m/Y' },{
            direction: 0,
            tweak: { x: -20, y: 26 },
            draggable:false,
            blocked:arrayFechas,
            offset:1
//            classes:['dashboard']
        });
    }

    function cargarAcordeonConMT12()
    {
        //GENERO EL MENU
                var menuXML=leerXML('javascripts/MENU.xml');
                    var items=menuXML.getElementsByTagName('item');
                var numEnlace=0;
                for (var i = 0; i < items.length; i++)
                {
                    //GENERO EL TOGGLER
                        var toggle=new Element('div',{
                            'class':'toggler',
                            'html':items[i].getAttribute('titulo'),
                            'tabIndex':i
                        });
                        toggle.store('enlace',items[i].getAttribute('enlace'));
                        toggle.store('toggler',i);
                        toggle.injectInside($('accordion'));
                    //COMPRUEBO SI TIENE HIJOS
                        items2=items[i].getElementsByTagName('item2');
                        //alert(items2.length);
                        var tieneHijos=false;
                        //if(items2.length>0)
                        //{
                            //DEFINO LA SECCION DEL CONTENIDO DE ESTE MENU
                                var element=new Element('div',{
                                    'id':'element_'+i,
                                    'class':'element'
                                }).injectInside($('accordion'));
                            //GENERO TANTOS ITEMS2 COMO HAYA

                                for (var h = 0; h < items2.length; h++)
                                {
                                    var enlace=new Element('div',{
                                        'class':'enlace',
                                        'html':items2[h].getAttribute('titulo')
                                    });
                                    enlace.store('pos',numEnlace++);
                                    enlace.store('enlace',items2[h].getAttribute('enlace'));
                                    enlace.store('titulo',items2[h].getAttribute('titulo'));
                                    enlace.store('toggler',i);
                                    enlace.store('posEnlace',h);
                                    enlace.injectInside(element);
                                    tieneHijos=true;
                                }
                        //}
                    //ESPECIFICO SI EL TOGGLE TIENE HIJOS
                        if(tieneHijos)
                        {
                            toggle.store('hijos','true');
                        }
                        else
                        {
                            toggle.store('hijos','false');
                        }

                }

            myAccordion = new Accordion($('accordion'), 'div.toggler', 'div.element', {
                    opacity: false,
                    initialDisplayFx:true,
//                    alwaysHide:true,
//                    display:-1,
                    onActive: function(toggler, element){
                            toggler.setStyle('color', '#ffffff');
                            toggler.setStyle('background-color', '#94c03d');
                    },
                    onBackground: function(toggler, element){
                            toggler.setStyle('color', '#444343');
                            toggler.setStyle('background-color', '#dddddd');
                    },
                    onComplete:function(){

                    }

            });
            mostrarMenuActivo(location.href);

        //AÃ‘ADO LOS EVENTOS DEL MENU
            $$('.enlace').each(function(enlace){
                enlace.addEvent('click',function(){
//                        cargarLink(enlace.retrieve('enlace'));
//                        var enlace_=enlace.retrieve('enlace');
//                                    historia[historia.length]=enlace_;
//                        desactivarEnlaces();
//                        enlace.removeClass('enlace');
//                        enlace.addClass('enlaceON');
                          window.open(enlace.retrieve('enlace')+'?'+enlace.retrieve('toggler')+'&'+enlace.retrieve('posEnlace'), '_self');
                });
            })

            $$('.toggler').each(function(toggler){
                if(toggler.retrieve('hijos')=='false')
                {
                    toggler.addEvent('click',function(){
//                        new Request.HTML({
//                            url: toggler.retrieve('enlace'),
//                            onSuccess: function(html){
//                                    $('contentAjax').empty();
//                                    $('contentAjax').adopt(html);
//                                    var enlace_=toggler.retrieve('enlace');
//                                    historia[historia.length]=enlace_;
//                            },
//                             Our request will most likely succeed, but just in case, we'll add an
//                             onFailure method which will let the user know what happened.
//                            onFailure: function(response){
//                                    $('contentAjax').empty();
//                                    $('contentAjax').set('html','No se encuentra el archivo especificado. '+response);
//                            }
//
//                        }).send();
                          window.open(toggler.retrieve('enlace')+'?'+toggler.retrieve('toggler'), '_self');
                    });
                    
                }
            });
    }
    function mostrarMenuActivo(url)
    {
        var arrayUrl=url.split('?');
        if(arrayUrl.length>=2)
        {
            var datos=arrayUrl[1].split('&');
            if(datos.length==2)
            {
                //MUESTRO EL ACCORDION CORRESPONDIENTE
                    myAccordion.display(datos[0]);
                //OBTENGO EL TOGGLER SELECCIONADO
                if(Browser.Engine.trident)
                {
                    var zonaAccordion=document.getElementById('accordion');
                        var listaDivs=zonaAccordion.getElementsByTagName('div');
                        for (var i = 0; i < listaDivs.length; i++) {
                            if(listaDivs[i].className=='toggler' && listaDivs[i].tabIndex==datos[0])
                            {
                                var element=listaDivs[i+1];
                                    var enlace=element.getElementsByTagName('div')[datos[1]];
                                        enlace.className='enlaceON';

                            }
                        }

                }
                else
                {
                    var toggler=$(document.body).getElement('div[tabIndex='+datos[0]+']');
                    //OBTENGO EL ELEMENTO DEL TOGGLER
                        var element=toggler.getNext();
                        //OBTENGO LOS ENLACES DEL TOGGLER
                            var enlace=element.getElements('div[class^=enlace]')[datos[1]]
                                enlace.setProperty('class','enlaceON');
                }
                //OBTENGO EL HIJO SEGUN EL DATO[2]
                    //alert(next.length);
            }
            else
            {
                //MUESTRO EL ACCORDION CORRESPONDIENTE
                    myAccordion.display(datos[0]);
            }
            if($('formulario_registro')){addEventForm()};

        }
        else
        {
            myAccordion.display(-1);
        }
    }
    function desactivarEnlaces()
    {
        $$('.enlace').each(function(enlace){
           enlace.removeClass('enlaceON');
           enlace.removeClass('enlace');
           enlace.addClass('enlace');
        });
        $$('.enlaceON').each(function(enlace){
           enlace.removeClass('enlaceON');
           enlace.removeClass('enlace') ;
           enlace.addClass('enlace') ;
        });
    }
    function leerXML(url)
          {
                try //Internet Explorer
                {
                    xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
                }
                catch(e)
                {
                    try //Firefox, Mozilla, Opera, etc.
                    {
                        xmlDoc=document.implementation.createDocument("","",null);
                    }
                    catch(e)
                    {
                        sexy.alert(OpenLayers.i18n('ERROR LECTURA XML'));
                    }
                }
                try
                {
                    xmlDoc.async=false;
                    xmlDoc.load(url);
                    return(xmlDoc);
                }
                catch(e)
                {
                    alert(e.message)
                }
                return(null);
          }
    function cargarLink(enlace)
    {
//            new Request.HTML({
//                url: enlace,
//                onSuccess: function(html){
//                        $('contentAjax').empty();
//                        $('contentAjax').adopt(html);
//                                    historia[historia.length]=enlace;
//                },
//                // Our request will most likely succeed, but just in case, we'll add an
//                // onFailure method which will let the user know what happened.
//                onFailure: function(response){
//                        $('contentAjax').empty();
//                        $('contentAjax').set('html','No se encuentra el archivo especificado. '+response);
//
//                }
//
//            }).send();
    }
    function addEventForm()
    {
//        $('go').addEvent('click',function(e){
//            e.stop();
//            $('formulario_registro').set('send',{
//               onComplete:function(response){
//                   $(document).set('html',response);
//                   if($('formulario_registro')){addEventForm()};
//               }
//            });
//            $('formulario_registro').send();
//        });
    }

    function obtenerTextoSegunDia(dia,mes,anio)
    {
        var info='';
                    calendarioXML=leerXML('javascripts/CALENDARIO.xml');
                    var fechas=calendarioXML.getElementsByTagName('fecha');
                    for (var i = 0; i < fechas.length; i++)
                    {
                        if(fechas[i].getAttribute('dia')==dia )
                        {
                           info=fechas[i].getAttribute('info');
                        }
                    }
        return info;
    }

    function back()
    {
//        if(historia.length-2==0 || historia.length-3==0)
//        {
//            $('contentAjax').empty();
//            $('contentAjax').set('html',htmlIndex);
//        }
//        else
//        {
//            new Request.HTML({
//                    url: historia[historia.length-3],
//                    async:false,
//                    onSuccess: function(html){
//                            $('contentAjax').empty();
//                            $('contentAjax').adopt(html);
//                            historia.pop();
//                    },
//                    // Our request will most likely succeed, but just in case, we'll add an
//                    // onFailure method which will let the user know what happened.
//                    onFailure: function(response){
//                            $('contentAjax').empty();
//                            $('contentAjax').set('html','No ha sido posible retroceder. '+response);
//
//                    }
//
//                }).send();
//        }
    }


