﻿//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//  JQuery Dialog Related Functions
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
var TimeOutModal;

//========================================================
//  Set up simple dialog defaults
//========================================================
$.extend($.modal.defaults, {
    closeHTML: "<a class='modalCloseImg simplemodal-close'>Close <span class='icon-img close-diag'>&nbsp;</span></a>"
});

//========================================================
//  ShowDialog
//========================================================
function ShowDialog(url, minWidth, minHeight, callback) {

    //clear existing timeouts
    window.clearTimeout(TimeOutModal);
    
    //check if callback function exists
    if (callback == undefined) callback = function() { };

    //add a timestamp to the call, to prevent caching
    var currentTime = new Date();
    var timestamp = { ts: currentTime.getTime() };

    $.get(url, timestamp, function(data) {

        // if this operation requires user to be logged in
        if (data == "login") {
            Login();
            return;
        }

        //TODO: Joey Temp
        swap_flash('.has_flash');
        
        $.modal(data, {
            overlayClose: false,
            minWidth: minWidth,
            minHeight: minHeight,
            onShow: callback,
            onClose: function() {
                //TODO: Joey Temp
                swap_flash('.has_flash');
                CloseDialog();
            }
        });
    });
}

//========================================================
//  Close the SimpleDialog instance
//========================================================
function CloseDialog(timeout) {

    if (typeof (timeout) == 'number') {

        //close the dialog after 'timeout' milliseconds
        TimeOutModal = window.setTimeout(
            function() { $.modal.close(); }
            , timeout
        )
    }
    else {
        //close the modal right away
        $.modal.close();
    }
}

//========================================================
//  ResizeDialog
//========================================================
function ResizeDialog(minWidth, minHeight) {

    var dim = $.modal.impl.getDimensions();

    var dataWidth = $.modal.impl.d.data.outerWidth(true);
    var dataHeight = $.modal.impl.d.data.outerHeight(true);

    $.modal.impl.d.container.css({ height: dataHeight, width: dataWidth }); // reset  dimensions

    $.modal.impl.setContainerDimensions(false);
    $.modal.impl.setPosition();


    
//    if (minWidth == null || minWidth == 'undefined') {
//        minWidth = 0;
//    }

//    if (minHeight == null || minHeight == 'undefined') {
//        minHeight = 0;
//    }

//    var data = $('#simplemodal-data');
//    $.modal.close();

//    //TODO: Joey Temp
//    swap_flash('.has_flash');

//    $.modal(data, {
//        overlayClose: true,
//        minWidth: minWidth,
//        minHeight: minHeight,
//        onClose: function() {
//            //TODO: Joey Temp
//            swap_flash('.has_flash');
//            CloseDialog();
//        }
//    });
}
