﻿//var popupVisible = false;

var dialogLocation;
var initializedDialog;

function showDialog(url, title, refreshTarget) {

    // Remove any tabs from the titlebar
    $(".ui-dialog-titlebar ul").remove();

    // Set the loading html
    var domElement = $('<p style="width:200px; height:100px;">Loading...</p>');
    $('#dialogContent').contents().remove();
    $('#dialogContent').append(domElement);

    if (initializedDialog == true) {
        $('#dialogContent').dialog({ title: title,
                                    close: function (event, ui) {
                                    if (refreshTarget != undefined) {
                                        RefreshModule(refreshTarget);
                                    }
            } });
        $('#dialogContent').dialog('open');
    } else {
        $('#dialogContent').dialog({
            width: 'auto',
            resizable: false,
            title: title,
            close: function (event, ui) {
                if (refreshTarget != undefined) {
                    RefreshModule(refreshTarget);
                }
            }
        });
        initializedDialog = true;
    }
    

    //offsetDialog();

    $.ajaxSetup({ cache: false });

    $.get(url, {
    //content: t
}, function (txt) {
    //alert(txt);
    dialogLocation = url;
    var domElement = $(txt);
    $('#dialogContent').contents().remove();
    $('#dialogContent').append(domElement);
    //offsetDialog();
    InitializeContent();
    $('#dialogContent').dialog('option', 'position', 'center'); 

});
}

function centerDialog() {
    alert('center dialog');
    $('#dialogContent').dialog('option', 'position', 'center');
}

function refreshDialog() {
    $.ajaxSetup({ cache: false });

    $.get(dialogLocation, {}, function (txt) {
        var domElement = $(txt);
        $('#dialogContent').contents().remove();
        $('#dialogContent').append(domElement);
        //offsetDialog();
        InitializeContent();
    });
}

function showClientDialog(title, jquery) {
    $('#dialogContent').contents().remove();
    
    // Update this html with a new template, this is the one i used for solo at the moment
    var domElement = $('\
    <div class="boxArea">\
        <div class="boxAreaHeader">\
            <h2>'
                + title + '\
            </h2>\
        </div>\
        <div class="boxAreaContent">\
            <div class="boxContent">'
                 + jquery.html() + '\
                <div class="clear"></div>\
            </div>\
        </div>\
        <div class="boxAreaFooter"></div>\
    </div>\
    ');

    $('#dialogContent').append(domElement);
    //offsetDialog();
    $('#dialogContent').dialog({ modal: true });
    //$('#dialogContent').dialog('option', 'position', 'center');
   
}

function hideDialog() {
    $('#dialogContent').dialog('close')
}

// Only call offset after the content has been added to the DOM
/*function offsetDialog() {
    var popupWidth = $('#dialogContent').width();
    var popupHeight = $('#dialogContent').height();

    $("#dialogPanel").css({
        "margin-top": 0 - popupHeight / 2,
        "margin-left": 0 - popupWidth / 2
    });
}*/

// Setup Close Event Handlers
//$(document).ready(function () {

    //Close Button
//    $("#dialogPanelClose").click(function() {
//        hideDialog();
//    });

//    //Click Background
//    $("#backgroundPopup").click(function() {
//        hideDialog();
//    });

    //Escape Button
//    $(document).keypress(function(e) {
//        if (e.keyCode == 27 && popupVisible == true) {
//            hideDialog();
//        }
//    });

//});
