﻿function redimensionar_menu() {

    // Calculate total width of list items
    var lis = $('#menu_interior li a').delay(3000);

    //Medida del separador, multiplicaremos por el numero de elementos - 1.
    var separador = 2;
    separador = separador * (lis.length - 1);
    //    var menuwidth = $('#menu_interior').width(); //Medida del menu
    var menuwidth = 939; //Medida del menu
    var actualWidth = menuwidth - separador;
    var totalLIWidth = 0;
    lis.each(function() {
        totalLIWidth += $(this).outerWidth();
    });

    // Work out how much padding we need
    var requiredPadding = Math.round(((actualWidth - totalLIWidth) / lis.length) / 2);

    // To account for rounding errors, the error is going to be forced into the first tab.
    var roundingErrorFix = Math.abs(((requiredPadding * lis.length * 2) + totalLIWidth - actualWidth) / 2);
    
//    alert(totalLIWidth + (requiredPadding * 2 * lis.length) + (roundingErrorFix*2));
    while (totalLIWidth + (requiredPadding * 2 * lis.length) + (roundingErrorFix*2) > actualWidth) {
        requiredPadding -= 0.5;
    }

    // Apply padding to list items
    var isFirst = true;
    lis.each(function() {

        var w_elemento_actual;
        if (isFirst) {
            $(this).css('padding-left', requiredPadding + roundingErrorFix + 'px')
                .css('padding-right', requiredPadding + roundingErrorFix + 'px');
            isFirst = false;
        }
        else {
            $(this).css('padding-left', requiredPadding + 'px')
                .css('padding-right', requiredPadding + 'px')
        }
    });
    
    /*Volvemos a comprobar las medidas*/
    totalLIWidth = 0;
    lis.each(function() {
        totalLIWidth += $(this).width();
    });
    
    while (totalLIWidth + (requiredPadding * 2 * lis.length) + (roundingErrorFix * 2) > actualWidth) {
        requiredPadding -= 0.5;
    }

    var isFirst = true;
    lis.each(function() {

        var w_elemento_actual;
        if (isFirst) {
            $(this).css('padding-left', requiredPadding + roundingErrorFix + 'px')
                .css('padding-right', requiredPadding + roundingErrorFix + 'px');
            isFirst = false;
        }
        else {
            $(this).css('padding-left', requiredPadding + 'px')
                .css('padding-right', requiredPadding + 'px')
        }
    });

}

function mover(elemento) {
    var lft = 0;
    var lis = $('#menu_interior li a');
    var aux = 0;
    var anchura = 0;
    
    if (elemento != 0) {
        anchura = $('#item_menu_' + elemento).outerWidth(); ;

        lis.each(function() {
            if (aux < elemento - 1) {
                lft += $(this).outerWidth() + 2;
            }
            aux++;
        });

        if (elemento == 1) {
            lft = 0;
        }
    }
    
    $("#slider_menu_interior").stop().animate({ backgroundColor: '#3392ce', left: lft, width: anchura });
    
}

//Compara si las 2 Password son iguales en el formulario de registro
function comprobarPassword(pass1, pass2, div_error) {
    if (document.getElementById(pass1).value != document.getElementById(pass2).value) {
        document.getElementById(div_error).className = 'display_block';
        return false;
    }
    else {
        document.getElementById(div_error).className = 'display_none';
        return true;
    }
}

//Validacion Resgistro Password/email
function comprobarRegistro(pass1, pass2, div_pass_error, email1, email2, div_email_error) {
    var valido = true;
    //Comprobamos si los email son diferentes
    if (document.getElementById(email1).value !="" && document.getElementById(email2).value!="") {
        if (document.getElementById(email1).value != document.getElementById(email2).value) {
            $('#' + div_email_error).removeClass("display_none");
            $('#' + div_email_error).addClass("display_block");
            valido = false;
        }
        else {
            $('#' + div_email_error).removeClass("display_block");
            $('#' + div_email_error).addClass("display_none");
        }
    }

    //PASSWORD - Comprobamos si los password son diferentes
    if (document.getElementById(pass1).value != "" && document.getElementById(pass2).value != "") {
        if (document.getElementById(pass1).value != document.getElementById(pass2).value) {
            $('#' + div_pass_error).removeClass("display_none");
            $('#' + div_pass_error).addClass("display_block");
            valido = false;
        }
        else {
            $('#' + div_pass_error).removeClass("display_block");
            $('#' + div_pass_error).addClass("display_none");
        }
    }
    return valido;
}

//Validacion Perfil Password/email
function comprobarPerfil(pass1, pass2, div_pass_error, email1, email2, div_email_error) {
    var valido = true;
    //Comprobamos si los email son diferentes
    if (document.getElementById(email1).value != document.getElementById(email2).value) {
        $('#' + div_email_error).removeClass("display_none");
        $('#' + div_email_error).addClass("display_block");
        valido= false;
    }
    else {
        $('#' + div_email_error).removeClass("display_block");
        $('#' + div_email_error).addClass("display_none");
    }
    
    //PASSWORD - Comprobamos si han rellenado algun campo de password. si es asi comprobamos si son iguales.
    if (document.getElementById(pass1).value != "" || document.getElementById(pass2).value != "") {
        if (document.getElementById(pass1).value != document.getElementById(pass2).value) {
            $('#' + div_pass_error).removeClass("display_none");
            $('#' + div_pass_error).addClass("display_block");
            valido=false;
        }
        else {
            $('#' + div_pass_error).removeClass("display_block");
            $('#' + div_pass_error).addClass("display_none");
        }
    }

    return valido;
}

//Compara si el checkbox esta checkado en el formulario de registro
function comprobarCheckbox(chkbox, div_error) {
    if ($('#' + chkbox).attr('checked')) {
        document.getElementById(div_error).className = 'display_none';
        return true;
    }
    else {
        document.getElementById(div_error).className = 'display_block';
        return false;
    }
}

function cambiarTabProfesores(elemento) {
    var lft = 0;
    var fadeOut;
    var fadeIn;
    switch(elemento) {
        case 1:
            lft = 74;
            fadeOut = "div_archivo";
            fadeIn = "div_tema";
            break
        case 2:
            lft = 137;
            fadeOut = "div_tema";
            fadeIn = "div_archivo";
           break
        default:
            lft = 74;
    }

    //Mover flecha
    $("#div_cuadro_compartir_flecha").stop().animate({ left: lft });
    $("#" + fadeOut).fadeOut('slow', function(){$("#" + fadeIn).fadeIn('slow')});
}
