﻿jQuery.extend({
    rnd: function () {
        today = new Date();
        jran = today.getTime();

        ia = 9301;
        ic = 49297;
        im = 233280;
        jran = (jran * ia + ic) % im;
        return jran / (im * 1.0);
    },
    rand: function (number) {
        return Math.ceil($.rnd() * number);
    },
    showDialog: function (url, width, height, id) {
        if ($("#" + id).length == 0) {
            $('body').append('<div id="' + id + '" class="popup_block"></div>');
        }
        if ($("#" + id + "_popupFrame").length == 0) {
            $("#" + id).append("<iframe id='" + id + "_popupFrame' allowtransparency=true frameborder=0 width='" + width + "' height='" + height + "' scrolling=auto></iframe>")
        }
        var myRand = $.rand(50000);
        if (url.indexOf("?") > -1)
            url += "&";
        else
            url += "?";
        url += "rand=" + myRand;
        $("#" + id + "_popupFrame").attr("src", url);
        $('#' + id).fadeIn().css({ 'width': Number(width) });
        var dh = $('body').height();
        var popMargTop = $('body').scrollTop() + (screen.availHeight / 2) - (($('#' + id).height() + 160) / 2);
        var popMargLeft = (screen.availWidth / 2) - (($('#' + id).width() + 80) / 2);
        $('#' + id).css({
            'top': popMargTop,
            'left': popMargLeft
        });
        if ($("#fade").length == 0) $('body').append('<div id="fade"></div>');
        $('#fade').css({ 'filter': 'alpha(opacity=80)', 'height': dh }).fadeIn();

        return false;
    },
    hideDialogOfParent: function (id) {
        window.parent.$('#fade').fadeOut(function () {
            window.parent.$('#fade').remove();
        });
        window.parent.$('#' + id + "_popupFrame").fadeOut(function () {
            window.parent.$('#' + id + "_popupFrame").remove();
        });
        window.parent.$('#' + id).fadeOut(function () {
            window.parent.$('#' + id).remove();
        });

        return false;
    },
    showMessage: function (message, width) {
        $("#divMessage").fadeIn().css({ 'width': Number(width) });
        $("#divMessageText").attr("innerHTML", message);
        var popMargTop = $('body').scrollTop() + (screen.availHeight / 2) - (($("#divMessage").height() + 80));
        var popMargLeft = (screen.availWidth / 2) - (($("#divMessage").width() + 80) / 2);
        var dh = $('#divMaster').height();
        alert(popMargTop);
        $("#divMessage").css({
            'top': popMargTop,
            'left': popMargLeft
        });
        if ($("#fade").length == 0) $('body').append('<div id="fade"></div>');
        $('#fade').css({ 'filter': 'alpha(opacity=80)', 'height': dh }).fadeIn();

        return false;
    },
    hideMessage: function () {
        $('#fade , #divMessage').fadeOut(function () {
            $('#fade').remove();  //fade them both out
        });
        return false;
    }
});
