﻿Sys.Application.add_load(initPopupsPisos);

function initPopupsPisos() {
    //$('body').bind('keydown', function(e) { if (e.keyCode == 27) { hideBigImage(); } });
    
    $(".fotoMapaSituacion").bind("click", function(e) { setBigImage(this) });
    $(".fotoProducto").bind("click", function(e) { setBigImage(this) });
    $("#cerrarBigImage").bind("click", function(e) { hideBigImage(); e.preventDefault(); });
    $("#bigImageBackground").bind("click", function(e) { hideBigImage(); e.preventDefault(); });

    $(".imgBigProducto").bind("click", function(e) { hideBigImage(); e.preventDefault(); });

    // Se ha acabado de cargar la imagen real, comprobamos el tamaño y la mostramos
    $(".imgBigProducto").load(function() {
        checkBigFotoSize();
        showBigImage();
    })
}
    

// si la foto grande es mas alta o ancha que la pantalla la restringimos mediante thumbnail
function checkBigFotoSize() {
    orig_w = parseFloat($(".imgBigProducto").width());
    orig_h = parseFloat($(".imgBigProducto").height());

    if ((orig_w > parseFloat($(window).width()) - 100) || (orig_h > parseFloat($(window).height()) - 100)) {
        new_width = parseInt((parseFloat($(window).height()) - 100) * orig_w / orig_h);
        if (new_width > parseFloat($(window).width()) - 100) {
            new_width = parseInt($(window).width()) - 100;
        }
        $(".imgBigProducto").css("width", parseInt(new_width));
    }
}

function setBigImage(img) {

    if (img.src.indexOf('thumbnailer.ashx') > 0) {
        $(".imgBigProducto").attr("src", img.src + "&thumb=no");
    } else {
        $(".imgBigProducto").attr("src", img.src);
    }
   
}

function showBigImage(img) {
    //$("body").scrollTop(0);
    //$("body").css("overflow", "hidden");
    //$("#bigImageBackground").css("top", $("body").scrollTop());
    $("#bigImageBackground").css("width", parseInt($(window).width()));
    $("#bigImageBackground").css("height", parseInt($(window).height() + $("body").scrollTop()));
    $("#bigImageBackground").show();
    $("#bigImageBackground").css("visibility", "visible");

    $(".imgBigProducto").css("top", ($("#bigImageBackground").height() / 2) - ($(".imgBigProducto").height() / 2));
    $(".imgBigProducto").css("left", ($("#bigImageBackground").width() / 2) - ($(".imgBigProducto").width() / 2));
    $(".imgBigProducto").fadeIn(500);

    $("#cerrarBigImage").css("top", $(".imgBigProducto").position().top - $("#cerrarBigImage").height());
    $("#cerrarBigImage").css("left", $(".imgBigProducto").position().left + $(".imgBigProducto").width() - $("#cerrarBigImage").width() - 10);

    $("#cerrarBigImage").fadeIn(500);
}

function hideBigImage() {
    $(".imgBigProducto").fadeOut(500, function(e) { $("#bigImageBackground").hide(); $("#bigImageBackground").css("visibility", "hidden"); })
    $("#cerrarBigImage").fadeOut(500, showScrollbar);
    
}
function showScrollbar() {
    //$("body").css("overflow", "auto");
}
