﻿function CreateDialogDiv() {

    if (document.getElementById("divDialog") == undefined) {
        divDialog = document.createElement('div');
        divDialog.id = 'divDialog';
        document.body.appendChild(divDialog);

        $(divDialog).dialog({
            closeOnEscape: true,
            autoOpen: false,
            width: 850,
            height: 530,
            title: 'Kladionica',
            modal: true,
            show: 'slide',
            draggable: true,
            buttons: {
                "Close": function () {
                    $(this).dialog("close");
                }
            }
        });
    }
}

function CreateDialogDivSize(width,height) {

    if (document.getElementById("divDialog") == undefined) {
        divDialog = document.createElement('div');
        divDialog.id = 'divDialog';
        document.body.appendChild(divDialog);

        $(divDialog).dialog({
            closeOnEscape: true,
            autoOpen: false,
            width: width ,
            height: height,
            title: 'Kladionica',
            modal: true,
            show: 'slide',
            draggable: true,
            buttons: {
                "Close": function() {
                    $(this).dialog("close");
                }
            }
        });
    }
}

//metoda sa fiksnom velicinom prozora koja se upotrebljava samo za proveru tiketa
function JQMessage(message) {
   
    CreateDialogDiv();

    var divTable = "<table cellpadding='8' height='99%' border='0' width='99%'>";
    divTable += "<tr>";
    divTable += "<td width='80%' valign='top' class='proveraTiketaJQ'>" + message + "</td>";
    
    divTable += '</tr>';
    divTable += '</table>';

    $('#divDialog').html(divTable);
    $('#divDialog').dialog('open');
   
}

function JQMessageSize(message,width,height,type) {

    var icon = "";

    if (type == 0) icon = "";
    if (type == 1) icon = "<img src='../img/messageIcons/information.png'>";
    if (type == 2) icon = "<img src='../img/messageIcons/success.png'>";
    if (type == 3) icon = "<img src='../img/messageIcons/error.png'>";
    if (type == 4) icon = "<img src='../img/messageIcons/warning.png'>";
   
    CreateDialogDivSize(width,height);
    var divTable = "<table cellpadding='8' height='99%' border='0' width='99%'>";
    divTable += "<tr>";
    divTable += "<td width='80%' valign='top' class='proveraTiketaJQ'>" + message + "</td><td width='20%' valign='top'>" + icon + "</td>";
    divTable += '</tr>';
    divTable += '</table>';

    $('#divDialog').html(divTable);
    $('#divDialog').dialog('open');
    
}


$.fn.centerInClient = function (options) {
    /// <summary>Centers the selected items in the browser window. Takes into account scroll position.
    /// Ideally the selected set should only match a single element.
    /// </summary>    
    /// <param name="fn" type="Function">Optional function called when centering is complete. Passed DOM element as parameter</param>    
    /// <param name="forceAbsolute" type="Boolean">if true forces the element to be removed from the document flow 
    ///  and attached to the body element to ensure proper absolute positioning. 
    /// Be aware that this may cause ID hierachy for CSS styles to be affected.
    /// </param>
    /// <returns type="jQuery" />
    var opt = { forceAbsolute: false,
        container: window,    // selector of element to center in
        completeHandler: null
    };
    $.extend(opt, options);

    return this.each(function (i) {
        var el = $(this);
        var jWin = $(opt.container);
        var isWin = opt.container == window;

        // force to the top of document to ENSURE that 
        // document absolute positioning is available
        if (opt.forceAbsolute) {
            if (isWin)
                el.remove().appendTo("body");
            else
                el.remove().appendTo(jWin.get(0));
        }

        // have to make absolute
        el.css("position", "absolute");

        // height is off a bit so fudge it
        var heightFudge = isWin ? 2.0 : 1.8;

        var x = (isWin ? jWin.width() : jWin.outerWidth()) / 2 - el.outerWidth() / 2;
        var y = (isWin ? jWin.height() : jWin.outerHeight()) / heightFudge - el.outerHeight() / 2;

        el.css("left", x + jWin.scrollLeft());
        el.css("top", y + jWin.scrollTop());

        // if specified make callback and pass element
        if (opt.completeHandler)
            opt.completeHandler(this);
    });
}


