$(document).ready(function() {

    //select all the a tag with name equal to modal
    $('a[name=modal]').click(function(e) {
        //Cancel the link behavior
        e.preventDefault();
        //Get the A tag
        var id = $(this).attr('href');
        var jpg = $(this).attr('rel');
        var nombre = $(this).attr('alt');

        //$('#myDiv').html("<div style='width:'" + myWidth + "'px'>Lorem ipsum</div>");
        $('.window').append("<div class='popimg'> <a href=" + $(this).attr('rel') + " width='auto' class='zoom'> <img src='" + $(this).attr('rel') + "'> </a> </div>");
        $('.popimg').append("<div class='titulo'>" + nombre + "</div>");


        var tam = $(".popimg img").attr("width");

		if (tam < 800) {
            $(".popimg img").css("width", tam);
        } else {
            $(".popimg img").css("width", 800);
        }

        var tamalto = $(".popimg img").attr('height');

        //relacion alto / ancho
        if (tam / tamalto > 0.8 && tam > 699) {
            $(".popimg img").css("width", 600);
        }

        if (tam / tamalto < 0.799 && tam > 699) {
            $(".popimg img").css("width", 500);
        }

        //aplicar zoom si la imagen es > 800
        if (tam > 799) { //599
            $('a.zoom').easyZoom();
        }

        //$('a.zoom').easyZoom();
        //Get the screen height and width
        var maskHeight = $(document).height();
        var maskWidth = $(window).width();

        //Set height and width to mask to fill up the whole screen
        $('#mask').css({
            'width': maskWidth,
            'height': maskHeight
        });

        //transition effect        
        $('#mask').fadeIn(1000);
        $('#mask').fadeTo("slow", 0.8);

        //Get the window height and width
        var winH = $(window).height();
        var winW = $(window).width();

        //Set the popup window to center
        $(id).css('top', winH / 2 - $(id).height() / 1.5);
        $(id).css('left', winW / 2 - $(id).width() / 1.5);

        //transition effect
        $(id).fadeIn(2000);

    });

    //if close button is clicked
    $('.window .close').click(function(e) {
        //Cancel the link behavior
        e.preventDefault();
        $('#mask, .window').hide();
        $('.popimg').remove();
    });

    //if mask is clicked
    $('#mask').click(function() {
        $(this).hide();
        $('.popimg').remove();
        $('.window').hide();

    });

});
