
var request = {
    al_completar:'',
    ctrlAJAX:'divppal',

    get_htm:function(strurl,salida,pbody,oncomplet){

        informa.work(true);

        ctrlAJAX = salida;
        if (oncomplet==undefined){oncomplet=''}
        request.al_completar=oncomplet;

        var myRequest = new Request.HTML({
            url: strurl,
            method: 'post',
            data: pbody,
            onSuccess:this.response_srv,
            onFailure:this.response_srv_err
        });
        myRequest.send();
    },

    response_srv:function(responseTree, responseElements, responseHTML, responseJavaScript){
        informa.work(false);

        switch (responseHTML.split('_')[0])
        {
            case 'MSG':
                alert(responseHTML.substr(4));
                break;
            case 'POP':
                informa.aviso(responseHTML.substr(6),responseHTML.split('_')[1]);
                break;
            case 'PAG':
                pagina.aviso(responseHTML.substr(4),'cabecera');
                break;
            case 'REDIR':
                window.location = responseHTML.substr(6);
                break;
            case 'WIN':
                //alert(responseHTML.substr(4));
                window.open(responseHTML.substr(4));
                break;
            default:
                if (ctrlAJAX != '') {$(ctrlAJAX).innerHTML = responseHTML;}
                break;
        }

        if (request.al_completar.length >0) {
            paco=request.al_completar;
            request.al_completar='';
            eval(paco);
        }
    },

    response_srv_err:function(instance){
        informa.aviso(instance.responseText,2);
    }
}


var pagina={

    open: function(donde){
        $('ddoverlay').setOpacity(0.4);
        $('ddoverlay').setStyle('display','block');

        $('pop_pagina').setStyles({
            top:$(donde).getTop()+$(donde).getHeight(),
            display:'block',
            height:0
        })
        lefti = $(donde).getLeft() + (850/2);
        lefti-=(($('dv_pagina').getWidth()+40)/2);
        $('pop_pagina').setStyles({
            left:lefti,
            width:$('dv_pagina').getWidth()+40
        })

        pagina.redim();
    },

    redim:function(){
        $('pop_pagina').set('tween', {duration: 300,onComplete:function(){}});
        $('pop_pagina').tween('height',$('dv_pagina').getHeight()+30);
    },

    close:function(){
        $('ddoverlay').setStyle('display','none');
        $('pop_pagina').set('tween', {duration: 300,onComplete:function(){$('pop_pagina').setStyle('display','none')}});
        $('pop_pagina').tween('height',0);
    },
    aviso:function(txt,donde,boton){
        if (donde==undefined){donde='cabecera'}
        $('dv_pagina').empty();
        tex = new Element('div',{
                'class':'box_info',
                'html':txt,
                'styles': {
                        'margin-top': '30px',
                        'margin-bottom': '10px'
                    }
            });
        $('dv_pagina').adopt(tex);
        if (boton==undefined){
            bot = new Element('div',{
                    'class':'btn_gr',
                    'text':'Cerrar',
                    'events': {
                            'click': function(){
                                pagina.close();
                            }
                        }
                });
        }
        else
        {
            bot=boton;
        }
        
        $('dv_pagina').adopt(bot);
        pagina.open(donde);
    },

    cabecera:function(estado){
        cabeza=$('cabecera');
        if (estado==0){
            $('menu_inicio').setStyle('display','none');
            $('mn_alumno').setStyle('display','block');
            cabeza.set('tween', {duration: 300});
            cabeza.tween('height',80,50);
            $('link_ppal').setStyle('display','block');
            $('foto').setStyle('display','none');
            $$('.titulo, .subtitulo').set('tween', {duration: 300, onComplete:function(){
                        $$('.titulo, .subtitulo').setStyle('text-align','right')}
                        });
            $$('.titulo, .subtitulo').tween('margin-left',0,500);

        }
        else
        {
            $$('.titulo, .subtitulo').setStyle('text-align','left')
            $('menu_inicio').setStyle('display','block');
            $('mn_alumno').setStyle('display','none');
            $('mn_alumno').set('html','');
            $('link_ppal').setStyle('display','none');
            cabeza.set('tween', {duration: 300});
            cabeza.tween('height',50,80);
            $$('.titulo, .subtitulo').set('tween', {duration: 300});
            $$('.titulo, .subtitulo').tween('margin-left',500,0);
            $('foto').setStyle('display','block');
        }
    }

}

var informa = {
    ef:0,
    icon:['icon_wait','icon_info','icon_error'],

    start:function(){
        if ($('dv_wait')){
            ef = new Fx.Morph($('dv_wait'), {duration: 200, link:'cancel'});//,onChainComplete:function(){alert(informa);informa.efect_end();}})
            $('dv_wait').addEvent('click', function(e){
                        informa.roller(false);
                    });
        }
    },

    work:function(si){
        if ($('dv_wait')){
	        if (si==true)
	        {
		        document.body.style.cursor = "wait";
		        if ($('imgwork')) {$('imgwork').setStyle('display','block')};
	        }
	        else
	        {
		        document.body.style.cursor = "default";
		        if ($('imgwork')) {$('imgwork').setStyle('display','none')};
	        }
	        this.roller(si,'Espere por favor...');
	    }
    },
    aviso: function(tx,tipo){
        //ef.cancel;
        if ($('dv_wait')){
            window.scrollTo(0,0);
            el=$('dv_wait');
            el.set('html', tx);

            desde=el.getStyle('marginTop').toInt();

            ef.start({
                'margin-top':[desde,0], 
                'backgroundColor':['#ccc','#eee']
            });
            informa.icono(tipo);
		$('dv_wait').setStyle('zIndex',400);
        }
        else
        {alert(tx)}
    },
    icono:function(cual){
        el=$('dv_wait');
        for (x=0;x<informa.icon.length;x++){
            el.removeClass(informa.icon[x]);
        }
        el.addClass(informa.icon[cual]);
    },
    roller: function(visible,tx){
        //ef.cancel;
	    if ($('dv_wait')){
	            el = $('dv_wait');

	            informa.icono(0);

	            if (visible==true) {
			        if (tx!=null){el.set('html', tx);}
			        desde=el.getStyle('marginTop').toInt();
	                ef.start({
	                    'marginTop':[desde, 0],
	                    'backgroundColor':['#ccc','#eee']
	                });
	            }
	            else
	            {
			$('dv_wait').setStyle('zIndex',0);
			hasta=-el.getSize().y;
	                ef.start({
		        	'marginTop':[0,hasta],
        		        'backgroundColor': ['#eee','#ccc']
			    });
		    }
		}
    },
    efect_end:function(){
        el = $('dv_wait');
        if (el.getStyle('marginTop').toInt()<-1){
            el.setStyle('zIndex',0);
            el.set('html', '');
        }
        else
        {
            $('dv_wait').setStyle('zIndex',400);
        }
    }
}

var menu_pop = {
    al_complete_pop:null,
    start:function(boton,popi,top_mod,left_mod,onclic,al_complete_pop,relative){
        if (top_mod==undefined){top_mod=0}
        if (left_mod==undefined){left_mod=0}
        if (al_complete_pop!=undefined){this.al_complete_pop=al_complete_pop}
        if (relative==undefined){relative='mn_alumno'}

        informa.start();
        eventi='mouseenter';
        if (onclic==true){eventi='click';}
        $(boton).removeEvents(eventi);

        $(boton).addEvent(eventi, function(e){
	        e = new Event(e).stop();
	        jeje=$(boton);
            $(popi).setStyles({
                top:jeje.getCoordinates(relative).top+10 + (jeje.getHeight())-top_mod,
                left:jeje.getCoordinates(relative).left+15-left_mod
            });

	        menu_pop.open(popi);
        });

        $(boton).removeEvents('mouseleave');
        $(boton).addEvent('mouseleave', function(e){
            e = new Event(e).stop();
	        menu_pop.close(e,popi);
        });

        $(popi).removeEvents('mouseleave');
        $(popi).addEvent('mouseleave', function(e){
            e = new Event(e).stop();
	        menu_pop.close(e,popi);
        });
    },

    open:function(popi){
        $(popi).setStyles({
            height:0,
            display:'block'
        })

        menu_pop.redim(popi);
        if (this.al_complete_pop!=undefined){
            eval(menu_pop.al_complete_pop);
        }
    },

    close:function(e,popi){
        if ($(popi).getStyle('display')=='none') {return}
        desde = e.target || window.event.srcElement;
        hasta = e.relatedTarget || e.event.toElement

        if (hasta){
            switch (hasta.className){
                case 'pop_menu_pop':
                    break;
                case 'pop_contenido_pop':
                    break;
                default:
		    if (desde.getParent('.pop_menu_pop')==null){
                    	$(popi).set('tween', {duration: 300,onComplete:function(){$(popi).setStyle('display','none')}});
                    	$(popi).tween('height',0);
		    }
            }
        }
    },
    cierra:function(popi){
        if ($(popi).getStyle('display')=='none') {return}
        $(popi).set('tween', {duration: 300,onComplete:function(){$(popi).setStyle('display','none')}});
        $(popi).tween('height',0);
    },
    redim:function(popi){
        $(popi).set('tween', {duration: 300,link:'cancel'});
        $(popi).tween('height',$(popi).getFirst().getHeight());
    }
}


var solapas={

    start:function(grupo,inicial){
        if ($$('.div_grupo_solapas').length > 0) {
            solis='.div_solapa_grupo'
	        if (grupo!=undefined){
	            solis='#'+grupo+' '+solis;
	        }

            elems = $$(solis);
            elems.each(function(el, i){
	            el.addEvent('click', function(e){
                    solapas.clic(el);
	            });
	            el.addEvent('mouseenter', function(e){
	                el.setStyle('background-color','#f5f5f5');
	            });
	            el.addEvent('mouseleave', function(e){
	                el.setStyle('background-color','#fff');
	            });
	        });
	        if (inicial!=undefined){
                solapas.clic(elems[inicial]);
	        }
        }
     },

    clic:function(cual){
        grupo = cual.getParent().id;
        soles=$$('#' + grupo + ' .div_solapa_grupo');
        soles.removeClass('solapa_on');
        cual.addClass('solapa_on');

        try{
            //el contenido es de la clase del id de las solapas
            //$$('#' + grupo + ' .div_solapa_grupo');
            if ($$('.'+grupo).length > 0) {
                $$('.' + grupo).setStyle('display','none');
                for (x=0;x<soles.length;x+=1){
                    if (soles[x]==cual){
                        $$('.'+grupo)[x].setStyle('display','block');
                        break;
                    }
                }
            }
        }
        catch(err){}
    }
}


var func = {

    atras:function(){
        window.history.back();
    },
    toggle_ver:function(cual){
        if ($(cual).getStyle('display') == 'block'){
            $(cual).setStyle('display','none');
        }
        else
        {
            $(cual).setStyle('display','block');
        }
    },
    link_win:function(url){
        window.open(url);
    },
    seltab:function (cual){

        $$('.TabCellSelLeft')[0].className = 'TabCellLeft';
        $$('.TabCellSel')[0].className = 'TabCell';
        $$('.TabCellSelRight')[0].className = 'TabCellRight';
        $$('.pn_sel')[0].className = 'pn_nosel';

        $('sl_' + cual + '_1').className = 'TabCellSelLeft';
        $('sl_' + cual + '_2').className = 'TabCellSel';
        $('sl_' + cual + '_3').className = 'TabCellSelRight';
        $('sol_' + cual).className = 'pn_sel';

    },

    tree_start:function(){
        $$('.tree_padre').each(function(el, i){
	        el.addEvent('click', function(e){
	            func.toggle_ver(el.getNext());
	        });
	    })
	    $$('.tree_hijo,.tree_hijo_last').each(function(el, i){
		    el.addEvent('mouseenter', function(e){
			    el.set('morph', {duration: 300, link:'cancel'});
			    el.morph({color:'#000',backgroundColor:'#eee'});
		    });

		    el.addEvent('mouseleave', function(e){
			    el.set('morph', {duration: 300, link:'cancel'});
			    el.morph({color:'#333',backgroundColor:'#fff'});
		    });
        })
    },
    sesion_on:function(){
        func.keep_alive.periodical(300000);
    },
    keep_alive:function(){
        rq_alive = new Request.HTML({
            url: '/alumno/func.aspx?op=0',
            link:'ignore'
        });
        rq_alive.send();
    }

}

var paginas={

    aviso:function(txt,donde,info,delay){
        ppal = new Element('div',{
                'styles': {
                        'width':'300px'
                    }
            });
        tex = new Element('div',{
                'class':'box_info',
                'html':txt
            });
            
        bot = new Element('div',{
                'class':'btn_gr',
                'text':'Cerrar',
                'styles': {
                        'margin-left':'100px'
                    },
                'events': {
                        'click': function(){
                            pagina_actual.close();
                        }
                    }
            });

        btns = new Element('div',{
                'styles': {
                        'border-top':'1px solid #ccc',
                        'padding':'5px'
                    }
            });

        btns.adopt(bot);
        ppal.adopt(tex,btns);

        pagi=new pagina_pop('info',donde);
        
        $('info_pop_texto').adopt(ppal);
        if (info!=true) {$('info_pop_texto').adopt(btns);}
        pagina_actual.presenta();
        if (delay){(function(){ pagina_actual.close(); }).delay(5000);}
    },
    /* pagina para presentar información (objeto adopt) y ejecutar una accion. is_control es si es un control existente que tiene que restablecer luego*/
    seleccion: function (adopt, txt, fn, is_control,tipo_bots) {
        if (tipo_bots==undefined){tipo_bots=0}
        obj_fuente=undefined;
        if (is_control==true){obj_fuente=$(adopt).getParent();}

        ppal = new Element('div', {
            'class': 'pop_div_seleccion'
        });
        if (txt!=''){
            tex = new Element('div', {
                'class': 'box_info pop_txt_dialogo',
                'html': txt
            });
            ppal.adopt(tex);
        }
        dv_control = new Element('div', {
            'class': 'pop_div_control'
        });
        dv_control.adopt(adopt);
        ppal.adopt(dv_control);

        btns = new Element('div', {
            'class': 'pop_botones'
        });
        if (tipo_bots!=0){
            bot_acepta = new Element('div', {
                'class': 'btn_gr pop_bot_acepta',
                'text': 'Aceptar',
                'events': {
                    'click': function () {
                        pagina_actual.close();
                        eval(fn);
                    }
                }
            });
            btns.adopt(bot_acepta);
        }
        bot_cancel = new Element('div', {
            'class': 'btn_gr pop_bot_cancel',
            'text': 'Cerrar',
            'events': {
                'click': function () {
                    pagina_actual.close();
                }
            }
        });

        btns.adopt(bot_cancel);
        ppal.adopt(btns);

        pagi = new pagina_pop('getsel', '', obj_fuente);

        $('getsel_pop_texto').adopt(ppal).adopt(btns);
        pagina_actual.presenta();
        //$('pop_txtbox').focus();
    },
    help:function(idhelp,donde){
        new pagina_pop('help',donde);
        strafter='pagina_actual.presenta();try{help.start()}catch(err){}';
        request.get_htm('/alumno/func.aspx','help_pop_texto','op=48&num='+idhelp,strafter);
    }
}


