$(document).ready(function() {

    //select all the a tag with name equal to modal
    $('a[rel=modalWindow]').live("click", function(e) {
        //Cancel the link behavior
        e.preventDefault();

        //Get the value of the name attribute from the clicked A tag or set to default content.
        var id = $(this).attr('name');
        if (id == ""){
            id = "testcontent.php";
        }

        //Load popup window content
        $("#dialog").load(id);
        
        //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});
        $('iframe').hide();
        //transition effect
        $('#mask').fadeIn(1000);
        $('#mask').fadeTo("fast",0.8);

        //Get the window height and widths
        var winH = $(window).height();
        var winW = $(window).width();

        //Set the dialog width and height
        $("#dialog").css("height", "300px");
        $("#dialog").css("width", "600px");
        
        //Set the popup window to center
        $("#dialog").css('top',  (winH/2-$("#dialog").height()/2)+$(window).scrollTop());
        $("#dialog").css('left', winW/2-$("#dialog").width()/2);        

        //Scroll to top of page in case user is below pop-up display area.
        //$('html, body').animate({scrollTop:0}, 'fast');

        //transition effect
        $("#dialog").fadeIn(2000);

    });

    //if close button is clicked
    $('a.close').live("click",function (e) {
        //Cancel the link behavior
        e.preventDefault();
        $('#mask').hide();
        $('.window').hide();
        $('iframe').show();
    });

    //if mask is clicked
    $('#mask').click(function () {
        $(this).hide();
        $('.window').hide();
        $('iframe').show();
    });

});
