﻿
//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;
    }
}

//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 redimensionar_menu() {

    // Calculate total width of list items
    //    var lis = $('#menu_interior li a.item_menu').delay(3000);
    var lis = $('#menu_interior li a[id^="item_menu"]');

    //Medida del separador, multiplicaremos por el numero de elementos - 1.
    var separador = 0;
    //separador = separador * (lis.length - 1);
    //    var menuwidth = $('#menu_interior').width(); //Medida del menu
    var menuwidth = 820; //Medida del menu (Son 940px -120px= que mide el elemento de Inicio que es fijo y no se tiene en cuenta en la redimension)
    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.2;
        }
//    while (totalLIWidth + (requiredPadding * 2 * lis.length) + (roundingErrorFix * 2) > actualWidth) {
//        var totalWidth = totalLIWidth + (requiredPadding * 2 * lis.length) + (roundingErrorFix * 2);
//        if (((totalWidth - actualWidth) - roundingErrorFix) >= 0) {
//            roundingErrorFix = (totalWidth - actualWidth) / 2;
//        }
//        else {
//            requiredPadding = (totalWidth - actualWidth) / (2 * lis.length);
//        }
//    }

    // 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) {
//        var totalWidth = totalLIWidth + (requiredPadding * 2 * lis.length) + (roundingErrorFix * 2);
//        if (((totalWidth - actualWidth) - roundingErrorFix) >= 0) {
//            roundingErrorFix = (totalWidth - actualWidth) / 2;
//        }
//        else {
//            requiredPadding = (totalWidth - actualWidth) / (2 * lis.length);
//        }
//    }

//    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')
//        }
//    });

}

//IMAGENES RELACIONADAS
function CargarImagen(imagen, descripcion, imagen_a) {
    $('#img_apartamento').fadeOut("slow", function () {
        $('#img_apartamento').attr("src", imagen);
        $('#img_apartamento').fadeIn("slow");
        $('#img_apartamento').attr("alt", descripcion);
        $('#img_apartamento').attr("title", descripcion);
        $('#enlace_ampliar').attr("href", imagen_a);
    });
}

function CargarImagenTam(imagen, descripcion, imagen_a, anchura, altura) {
    $('#img_apartamento').fadeOut("slow", function () {
        $('#img_apartamento').attr("width", anchura);
        $('#img_apartamento').attr("height", altura);
        $('#img_apartamento').attr("alt", descripcion);
        $('#img_apartamento').attr("title", descripcion);
        $('#enlace_ampliar').attr("rel", imagen_a);
        $('.div_imagen_apartamento').addClass("div_loading");
        $('#img_apartamento').attr("src", imagen)
        .load(function () {
            $(this).fadeIn("slow");
            $('.div_imagen_apartamento').removeClass("div_loading");
        });
    });
}

function CargarImagenTamGaleria(imagen, descripcion, imagen_a, anchura, altura) {
    $('#img_galeria').fadeOut("slow", function () {
        $('#img_galeria').attr("width", anchura);
        $('#img_galeria').attr("height", altura);
        $('#img_galeria').attr("alt", descripcion);
        $('#img_galeria').attr("title", descripcion);
        $('#enlace_ampliar').attr("rel", imagen_a);
        $('.div_imagen_galeria').addClass("div_loading");
        $('#img_galeria').attr("src", imagen)
        .load(function () {
            $(this).fadeIn("slow");
            $('.div_imagen_galeria').removeClass("div_loading");
        });
    });
}
