// JavaScript Document

    var w;                                        // anchura temporal
    var h;                                        // altura temporal
    var creado = false;                            // divs inicializadas
    var visible = false;                        // divs visibles
    var fondo;                                    // objeto fondo
    var marginTop = 10;

    var incW = 100;
    var incH = 30;

    var shadow;                                    // objeto sombra
    var offsetXShadow = 5;
    var offsetYShadow = 5;

    var papel;                                    // objeto papel
    var papelBorder = [1,1,1,1];
    var    wFinal;                                    // ancho del papel
    var    hFinal;                                    // alto del papel

    var contentpapel;


    var spreadFinished = false;
    var whatToDoAfterSpread = '';

    // -------------- obtención de dimensiones de la ventana del navegador

    function getBodySize() {
    var size = [0, 0];
        if (typeof window.innerWidth != 'undefined'){
            size = [window.innerWidth,window.innerHeight];
        } else if (typeof document.documentElement != 'undefined'&& typeof document.documentElement.clientWidth !='undefined' && document.documentElement.clientWidth != 0){
            size = [document.documentElement.clientWidth,document.documentElement.clientHeight];
        } else {
            size = [document.getElementsByTagName('body')[0].clientWidth,document.getElementsByTagName('body')[0].clientHeight];
       }
       return size;
    }


    // -------------- inicializador de las divs

    function initDivs(){
        fondo  = $('fondo');
        shadow = $('sombra');
        papel = $('papel');
        contentpapel = $('contentpapel');

        hideAll();

        w = 2;
        h = 2;
        wFinal = 2;
        hFinal = 2;

        fondo.setStyle({top:'0px', left:'0px'});
        shadow.setStyle({width:w+'px', height:h+'px', top:0+'px', left:0+'px'});
        papel.setStyle({width:w+'px', height:h+'px', top:0+'px', left:0+'px'});

        visible = false;

    }

    // -------------- oculta las divs de fondo, sombra y papel

    function hideAll(){
        if(fondo.visible())fondo.hide();
        if(shadow.visible())shadow.hide();
        if(papel.visible())papel.hide();
        visible = false;
    }


    // -------------- redimensiona el papel y ejecuta una funcion al acabar

    function resize(){
        if(arguments[0]){
            wFinal = arguments[0];
        }

        if(arguments[1]){
            hFinal = arguments[1];
        }

        if(!visible){
            fondo.show();
            shadow.show();
            papel.show();
        }

        /* getWidth() retorna la anchura con bordes incluidos
         * por lo que se le resta los bordes
         */
        w = papel.getWidth() - 2;
        h = papel.getHeight() - 2;

        deltaW = wFinal - w;
        deltaH = hFinal - h;

        masMenosW = (deltaW >= 0)?1:-1;
        masMenosH = (deltaH >= 0)?1:-1;

        bodyDim = getBodySize();

        if(deltaW != 0){
            if(Math.abs(deltaW) >= incW){
                w = w + (masMenosW*incW);
            }else{
                w = w + (masMenosW*Math.abs(deltaW));
            }

            x = Math.floor((bodyDim[0]/2) - (w/2));

            papel.setStyle({width: w+'px',left:x+'px'});
            shadow.setStyle({width: w+'px', left:(x+offsetXShadow)+'px'});
            setTimeout("resize()",10);

        }else{
            if(deltaH != 0){
                if(Math.abs(deltaH) >= incH){
                    h = h + (masMenosH*incH);
                }else {
                    h = h + (masMenosH*Math.abs(deltaH));
                }

                y = Math.floor((bodyDim[1]/2) - (h/2));
                if(y < marginTop) y = marginTop;

                papel.setStyle({height: h+'px', top:y+'px'});
                shadow.setStyle({height: h+'px', top:(y+offsetYShadow)+'px'});

                setTimeout("resize()",10);
            }else{

                eval(whatToDoAfterResize);

                clearTimeout();
            }
        }
    }

    // -------------- responders AJAX

    function showResponsePapel(originalRequest){
        contentpapel.update(originalRequest.responseText);
    }

    function showLoadingPapel(){
        contentpapel.update('<div class="loading"><img src="/img/loadingmini.gif" ></div>');
    }

    function showFailurePapel(){
        contentpapel.update('<div class="loading">Error</div>');
    }

    function showExceptionPapel(msg){
        contentpapel.update(msg);
    }

    // -------------- inicializa para mostrar resultado de inscripcion

    function resizeWindow(w,h){
        wFinal = w;
        hFinal = h;
        whatToDoAfterResize = ";";
        resize();
    }

    function showWindow(code){
        $('contentpapel').update('');

        switch (code){
            // se muestran normas de inscripcion
            case 'rules':
                    switch(arguments[1]){
                        case 0:
                                wFinal = 620;
                                hFinal = 400;
                                break;
                        case 1:
                                wFinal = 580;
                                hFinal = 400;
                                break;
                        case 2:
                                wFinal = 650;
                                hFinal = 400;
                                break;
                        case 3:
                                wFinal = 600;
                                hFinal = 530;
                                break;
                        case 4:
                                wFinal = 380;
                                hFinal = 570;
                                break;
                    }

                    whatToDoAfterResize = "ajaxShowMsg('"+ code +"',"+ arguments[1] +")";
                    break;

            // error de formulario
            case 'signup_validation_error':
                    wFinal = 380;
                    hFinal = 700;
                    whatToDoAfterResize = "ajaxShowMsg('"+ code +"')";
                    break;

        /**************************************
         * mensajes respecto a password reset *
         **************************************/

            case 'password_reset':
                    wFinal = 350;
                    hFinal = 225;
                    whatToDoAfterResize = "ajaxShowMsg('"+ code +"'," + arguments[1] +")";
                    break;

        /***********************************
         * mensajes respecto a comentarios *
         ***********************************/

            // se muestra formulario de comentario
            case 'comment_form':
                    wFinal = 293;
                    hFinal = 450;
                    whatToDoAfterResize = "ajaxShowMsg('"+ code +"','"+ arguments[1] +"','"+ arguments[2] +"')";
                    break;

            // procesamiento de comentario
            case 'send_comment':
                    wFinal = 350;
                    hFinal = 250;
                    whatToDoAfterResize = ";";
                    break;

        /*******************************
         * mensajes respecto a signups *
         *******************************/

        // se procesan signups panel administrador
            case 'signups':
                    wFinal = 350;
                    hFinal = 400;
                    whatToDoAfterResize = "ajaxShowMsg('"+ code +"')";
                    break;

            // se procesan signups rechazados panel administrador
            case 'signup_rejected':
                    wFinal = 350;
                    hFinal = 400;
                    whatToDoAfterResize = "ajaxShowMsg('"+ code +"','"+ arguments[1] +"')";
                    break;

        /*******************************
         * mensajes respecto a bio     *
         *******************************/

            // se muestra ayuda de formulario panel personal
            case 'hint_foto_principal':
                    hFinal = 350;
                    wFinal = 500;
                    whatToDoAfterResize = "ajaxShowMsg('"+ code +"')";
                    break;

            // se muestra ayuda de formulario panel personal
            case 'hint_foto_avatar':
                    hFinal = 200;
                    wFinal = 500;
                    whatToDoAfterResize = "ajaxShowMsg('"+ code +"')";
                    break;

        /*******************************
         * noticia                     *
         *******************************/
            case 'show_news':
                    hFinal=360;
                    wFinal = 470;
                    whatToDoAfterResize = "ajaxShowMsg('"+ code +"',"+arguments[1]+")";
                    break;
        /*******************************
         * borrar items                *
         *******************************/
            case 'delete_item':
                    hFinal=110;
                    wFinal = 300;
                    whatToDoAfterResize = "killConfirmed("+arguments[1]+",'"+arguments[2]+"',"+arguments[3]+")";
                    break;
        /*******************************
         * mensajes generales          *
         *******************************/

            // proceso concluido
            case 'signup_process_success':
            // acceso denegado
            case 'access_denied':
            // error de procesamiento
            case 'signup_process_error':
            default:
                    wFinal = 380;
                    hFinal = 340;
                    whatToDoAfterResize = "ajaxShowMsg('"+ code +"')";
                    break;

        }

        resize();
    }

    function showDescItem(type){
        var a_type = new Array("serie","video","story","news");
        var a_form = new Array("form_serie","form_video","form_story","form_news");
        var x;

        for(x = 0; x < a_type.length; x++){
            $(a_type[x]).hide();
            $(a_form[x]).hide();
        }

        $('form_'+type).show();
        $(type).show();
    }

    // -------------- muestra div

    function showHint(divId){
            $(divId).show();
    }

    // -------------- oculta div

    function hideHint(divId){
            $(divId).hide();
    }

    // -------------- oculta div

    function toggleDiv(divId){
            $(divId).toggle();
    }

    // -------------- cuenta chars

    function countChars(from,to){
        var num_chars = $(from).value.length;
        $(to).update(num_chars);
        return true;
    }

    function disableForm(form){
        Form.disable(form);
        return true;
    }

    function enableForm(form){
        Form.enable(form);
        return true;
    }

    function clearDiv(divId){
        $(divId).update('');
        return true;
    }
