﻿function redimensionar_menu() {

    // Calculate total width of list items
    var lis = $('#menu_baqueira ul li a.superior');

    //Medida del separador, multiplicaremos por el numero de elementos - 1.
    var separador = 2;
    separador = separador * (lis.length - 1);
    var menuwidth = 940; //Medida del menu
    var actualWidth = menuwidth - separador;
    var totalLIWidth = 0;



    lis.each(function() {
        totalLIWidth += $(this).find('div:first').width();
    });

    // 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 = (requiredPadding * lis.length * 2) + totalLIWidth - actualWidth;

    // Apply padding to list items
    var isFirst = true;
    lis.each(function() {

        var w_elemento_actual;
        if (isFirst) {
            $(this).find('div:first').css('padding-left', requiredPadding - roundingErrorFix + 'px')
                .css('padding-right', requiredPadding - roundingErrorFix + 'px');
            isFirst = false;
        }
        else {
            $(this).find('div:first').css('padding-left', requiredPadding + 'px')
                .css('padding-right', requiredPadding + 'px')
        }
        //Igualar medida elementos superior a medida del dropdown
        var ddmenu = $(this).parent().find('ul li');
        w_elemento_actual = $(this).parent().width() - 2;
        $(this).parent().find('ul').css('width', w_elemento_actual + 'px');
        if (ddmenu.length > 0) {
            ddmenu.each(function() {
                $(this).css('width', w_elemento_actual + 'px');
                //Igualar medida menu 3º nivel(left)
                var ddmenu3 = $(this).find('ul li');
                if (ddmenu3.length > 0) {
                    $(this).find('ul').css('left', w_elemento_actual + 'px');
                }
            });
        }
    });

}

//Mascara Textbox
$(document).ready(function() {
   var match = 'input.reemplazar_textbox[@type=text]';
   $(match).focus(function() {
       this.valuedefault = this.valuedefault || this.value;
       if (this.value == this.valuedefault)
           this.value = '';
       $(this).css('color', '#5b5b5b');
   });
   $(match).blur(function() {
       if (this.value.length == 0 || this.value == this.valuedefault)
           $(this).css('color', '#5b5b5b');
       if (this.valuedefault && this.value.length == 0)
           this.value = this.valuedefault;
   });
});

//IMAGENES RELACIONADAS
function CargarImagen(imagen, descripcion, imagen_a) {
    $('#imagen_producto').fadeOut("slow", function() {
        $('#imagen_producto').attr("src", imagen);
        $('#imagen_producto').fadeIn("slow");
        $('#imagen_producto').attr("alt", descripcion);
        $('#imagen_producto').attr("title", descripcion);
        $('#enlace_ampliar').attr("href", imagen_a);
    });
}

function CargarImagenTam(imagen, descripcion, imagen_a, anchura, altura) {
    $('#imagen_producto').fadeOut("slow", function() {
        $('#imagen_producto').attr("src", imagen);
        $('#imagen_producto').attr("width", anchura);
        $('#imagen_producto').attr("height", altura);
        $('#imagen_producto').fadeIn("slow");
        $('#imagen_producto').attr("alt", descripcion);
        $('#imagen_producto').attr("title", descripcion);
        $('#enlace_ampliar').attr("href", imagen_a);
    });
}

function reservarApartamento(id_apartamento) {
    var res = controles_apartamentos_listado_contenidos.reservarApartamento(id_apartamento);
    if (res != null && res.value != null) {
        return true;
    }
    else {
        return false;
    }
}

function reservarApartamentoDestacado(id_apartamento) {
    var res = controles_apartamentos_apartamentos_destacados_home.reservarApartamento(id_apartamento);
    if (res != null && res.value != null) {
        return true;
    }
    else {
        return false;
    }
}

$.maxZIndex = $.fn.maxZIndex = function(opt) {
    /// <summary>
    /// Returns the max zOrder in the document (no parameter)
    /// Sets max zOrder by passing a non-zero number
    /// which gets added to the highest zOrder.
    /// </summary>    
    /// <param name="opt" type="object">
    /// inc: increment value, 
    /// group: selector for zIndex elements to find max for
    /// </param>
    /// <returns type="jQuery" />
    var def = { inc: 10, group: "*" };
    $.extend(def, opt);
    var zmax = 0;
    $(def.group).each(function() {
        var cur = parseInt($(this).css('z-index'));
        zmax = cur > zmax ? cur : zmax;
    });
    if (!this.jquery)
        return zmax;

    return this.each(function() {
        zmax += def.inc;
        $(this).css("z-index", zmax);
    });
}

//Imagenes Carrousel
function roundImage() {
    $('.rounded_carrusel').one('load', function() {
        var img = $(this);
        var img_width = img.width();
        var img_height = img.height();

        // build wrapper
        var wrapper = $('<div class="rounded_wrapper"></div>');
        wrapper.width(img_width);
        wrapper.height(img_height);

        // move CSS properties from img to wrapper
        wrapper.css('float', img.css('float'));
        img.css('float', 'none')

        wrapper.css('margin-right', img.css('margin-right'));
        img.css('margin-right', '0')

        wrapper.css('margin-left', img.css('margin-left'));
        img.css('margin-left', '0')

        wrapper.css('margin-bottom', img.css('margin-bottom'));
        img.css('margin-bottom', '0')

        wrapper.css('margin-top', img.css('margin-top'));
        img.css('margin-top', '0')

        wrapper.css('display', 'block');
        img.css('display', 'block')

        // IE6 fix (when image height or width is odd)
        if ($.browser.msie && $.browser.version == '6.0') {
            if (img_width % 2 != 0) {
                wrapper.addClass('ie6_width')
            }
            if (img_height % 2 != 0) {
                wrapper.addClass('ie6_height')
            }
        }

        // wrap image
        img.wrap(wrapper);

        // add rounded corners
        img.after('<div class="tl"></div>');
        //img.after('<div class="tr"></div>');
        img.after('<div class="bl"></div>');
        //img.after('<div class="br"></div>');
    }).each(function() {
        $(this).trigger("load");
    });
}
