$(document).ready(function() {
    // Both popups below checked in case popupDialog.js is included more than once.

    // The standard popup dialog that various pages use for regular
    // user interaction.
    if ($('#popupOverlay').length === 0) {
        $('body').append("<div id='popupOverlay' class='popupOverlay'></div>");
        $('#popupOverlay').append("<div id='popupDialog'></div>");
        $('#popupDialog').append("<div id='popupContent'></div>");
    }

    // A special high-priority dialog that is used for session timeout.
    if ($('#sessionTimeoutOverlay').length === 0) {
        $('body').append("<div id='sessionTimeoutOverlay' class='popupOverlay'></div>");
        $('#sessionTimeoutOverlay').append("<div id='sessionTimeoutPopupDialog'></div>");
        $('#sessionTimeoutPopupDialog').append("<div id='sessionTimeoutPopupContent'></div>");
    }
});

$(window).ready(function() {
    /* --Standard jQuery Dialogs--
    These dialogs will only be init if the element exists was not
    initialized by any other code during the document.readys.
     */
    var init_dialog = $('#msgDialog');
    if (init_dialog.length && !init_dialog.hasClass('ui-dialog-content')) {
        init_dialog.dialog({
            autoOpen:  false,
            modal:     true,
            resizable: false,
            minWidth:  250,
            buttons: [{
                id: 'msgDialogCloseButton',
                class: 'cancelButton',
                title: gettext("Close"),
                click: function() {
                    closeMsg();
                }
            }]
        });
    }
});

function showMsg(msgTitle, msg) {
    $('#msgDialog')
        .dialog({title: msgTitle})
        .dialog('open');
    $('#msgDialogText').text(msg);
}

function closeMsg() {
    $('#msgDialog')
        .dialog('close')
        .dialog({title: ''});
    $('#msgDialogText').text();
}


/*
 * Hide a normal dialog window
 */
function hideDialog() {
    $('#popupDialog').hide();
    $('#popupOverlay').fadeOut();
    $('#popupContent').html('');
}

/*
 * Hide session timeout dialog window
 */
function hideSessionTimeoutDialog() {
    $('#sessionTimeoutPopupDialog').hide();
    $('#sessionTimeoutOverlay').fadeOut();
    $('#sessionTimeoutPopupContent').html('');
}

/*
 * Show a dialog 500 px wide
 * The height is automatically determined from the html content
 */
function showFixedWidthDialog(html_content) {
    showSetWidthDialog(500, html_content);
}

/*
 * Show the session timeout warning dialog.
 */
function showSessionTimeoutDialog(html_content) {
    return showSetWidthDialogInternal(
       $('#sessionTimeoutOverlay'),
       $('#sessionTimeoutPopupContent'),
       $('#sessionTimeoutPopupDialog'),
       400,
       html_content);
}

/*
 * Show a dialog set to width specified
 * The height is automatically determined from the html content
 */
function showSetWidthDialog(width, html_content) {
    return showSetWidthDialogInternal(
       $('#popupOverlay'),
       $('#popupContent'),
       $('#popupDialog'),
       width,
       html_content);
}

function setWidthHeightDialog(width, height, html_content)
{

     var popupOverlay =  $('#popupOverlay');
     var popupContent =  $('#popupContent');
     var popupDialog  =  $('#popupDialog');
     popupOverlay.fadeIn(100);
     popupContent.html(html_content);
     popupDialog.show();
     var forget_this_variable = popupContent.height();
     popupDialog.css('overflowY','auto');
     resizeWindowInternal(popupDialog, width, height)

}

function showSetWidthDialogInternal(popupOverlay, popupContent, popupDialog, width, html_content) {
    var height = Math.min(($('#popupContent').height() + 30), ($(window).height() - 200));
    popupOverlay.fadeIn(100);
    popupContent.html(html_content);
    popupDialog.show();

    // IE HACK !! In Internet Explorer - the popupContent height is wrong when first called.
    // Calling it twice fixes the issue - go figure!
    // (oh and this only happens for certain heights - just in case you thought there was a sensible reason)
    var forget_this_variable = popupContent.height();

    var height = Math.min((popupContent.height() + 30), ($(window).height() - 200));
    popupDialog.css('overflowY','auto');
    resizeWindowInternal(popupDialog, width, height)
}

/*
 * Shows a loading message
 */
function showLoadingDialog(loading_string) {
    $('#popupOverlay').fadeIn(100);
    var content = "<span>"+ loading_string +"<img width=30 src='/jquery/img/ajax-loader.gif'/></span>";
    $('#popupDialog').show();
    $('#popupContent').html(content);
    resizeWindow(250, 50);
}

/*
 * Resizes the popup window to the specified width and height and centers it on the screen.
 */
function resizeWindow(x_width, x_height)
{
    return resizeWindowInternal($('#popupDialog'), x_width, x_height);
}

function resizeWindowInternal(dialog, x_width, x_height)
{
    dialog.width(x_width);
    dialog.height(x_height);
    var marleft = - (x_width / 2) + 'px';
    var martop = - ((x_height / 2)+30) + 'px';

    dialog.css({
        'margin-left': marleft,
        'margin-top': martop
    });
}

/*

/*
 * Centers the dialog popup window
 */
function centerDialog()
{
    var marleft = - ($('#popupDialog').width()/2) + 'px';
    var martop = - ($('#popupDialog').height()/2) + 'px';

    $('#popupDialog').css({
        'margin-left': marleft,
        'margin-top': martop
    });
}

/*
 * Show confirmation dialog with 'Okay' and 'Cancel'
 * THIS CODE IS NOT YET LIVE. PLEASE DO NOT REMOVE FOR NOW.
 */
/*
function showApplyCancelDialog(action_string) {
    $('#popupOverlay').fadeIn(100);
    var content = "";
    content += "<h1>Confirm Action</h1>";
    content += "<p>"+action_string+"</p>";
    content += "<p>Are you sure you wish to proceed?</p>";
    content += "<input id='applyCancelDialog_apply' type='button' class='apply_button' value='Apply'>";
    content += "<a id='applyCancelDialog_cancel' type='button' class='cancel_button' href='#'>Cancel</a>";
    $('#popupDialog').show();
    $('#popupContent').html(content);
    resizeWindow(300, 130);
    $('#applyCancelDialog_apply').bind('click', function(){return true});
    $('#applyCancelDialog_cancel').bind('click', function(){return false});
}
*/
