var iframeUrl = "";

$(document).ready(function(){
//btn_jqm replacement
var closeModal = function(hash)
{
    var $modalWindow = $(hash.w);
    //$('#jqmContent').attr('src', 'blank.html');
    $modalWindow.fadeOut('1000', function()
    {
        hash.o.remove();
        //refresh parent
        if (hash.refreshAfterClose === 'true')
        {
            window.location.href = document.location.href;
        }
    });
};

var openInFrame = function(hash)
{
    var $trigger = $(hash.t);
    var $modalWindow = $(hash.w);
    var $modalContainer = $('iframe', $modalWindow);
    var newWidth = '60%', newHeight = '80%', newLeft = 0, newTop = 0;
    var myTitle = $trigger.attr('title');
    
    $modalContainer.html('').attr('src', iframeUrl);
    //$('#jqmTitleText').text(myTitle);
    
    hash.refreshAfterClose = false;

    // let's run through all possible values: 90%, nothing or a value in pixel
    if (newHeight != 0)
    {
        if (newHeight.indexOf('%') > -1)
        {
            newHeight = Math.floor(parseInt($(window).height()) * (parseInt(newHeight) / 100));
        }
        var newTop = Math.floor(parseInt($(window).height() - newHeight) / 2);
    }
    else
    {
        newHeight = $modalWindow.height();
    }
    if (newWidth != 0)
    {
        if (newWidth.indexOf('%') > -1)
        {
            newWidth = Math.floor(parseInt($(window).width() / 100) * parseInt(newWidth));
        }
        var newLeft = Math.floor(parseInt($(window).width() / 2) - parseInt(newWidth) / 2);

    }
    else
    {
        newWidth = $modalWindow.width();
    }

    // do the animation so that the windows stays on center of screen despite resizing
    $modalWindow.css({
        width: newWidth,
        height: newHeight,
        opacity: 0
    }).jqmShow().animate({
        width: newWidth,
        height: newHeight,
        top: newTop,
        left: newLeft,
        marginLeft: 0,
        opacity: 1
    }, 'slow');

}

$('#modalWindow').jqm({
    overlay: 70,
    modal: true,
    trigger: 'input.btn_jqm',
    target: '#jqmContent',
    onHide: closeModal,
    onShow: openInFrame
});

});