﻿$(document).ready(function () {
    $('a.dialog-box-button').live('click', function (event) {
        event.preventDefault();
        if ($('#dialog-box-overlay').length > 0) {
            $('#dialog-box-overlay').hide().remove();
        }
        $('#dialog-box-parent-container').fadeOut("slow", function () {
            if ($('#dialog-box-parent-container').length > 0) {
                $('#dialog-box-parent-container').remove();
            }
        });
        if ($.browser.msie && $.browser.version.substr(0, 1) < 7) {
            $('#dialog-box-parent-container').hide();
            if ($('#dialog-box-parent-container').length > 0) {
                $('#dialog-box-parent-container').remove();
            }
        }
        return false;
    });
    
    //event.preventDefault();
    //show_dialog('شكراً لزيارتكم موقع جريدة الوطن الإلكترونية <br/> هل تريد أن يكون موقع الوطن صفحتك الرئيسية؟', '', 'opacity-00', 'confirm');
    
    
    $('#popup-popper').click(function (event) {
        event.preventDefault();
        show_dialog('Message', 'asdasd', 'opacity-00', 'warn');
        return false;
    });
    $(window).resize(function () {
        if (!$('#dialog-box-parent-container').is(':hidden') && $('#dialog-box-parent-container').length > 0) {
            show_dialog();
        }
    });
    $(window).scroll(function () {
        if ($.browser.msie && $.browser.version.substr(0, 1) < 7) {
            if (!$('#dialog-box-parent-container').is(':hidden') && $('#dialog-box-parent-container').length > 0) {
                show_dialog();
            }
        }
    });
});

function show_dialog(message, title, classOverlay, dialogType) {
    var docHeight = $(document).height();
    var winWidth = $(window).width();
    $('#dialog-box-overlay').css({
        height: docHeight,
        width: winWidth
    }).show(); //puts overlay over the whole document, not just viewable area.
    if ($('#dialog-box-parent-container').length == 0) {
        var dialog_box_html = "<div id=\"dialog-box-parent-container\">";
        //dialog_box_html += "<div id=\"dialog-box-transparent-background\" class=\"opacity-70 box-round-all-2px\"></div>";
        //dialog_box_html += "<div id=\"dialog-box-header\" class=\"box-round-top-4px\">";
        //dialog_box_html += "<h2 class=\"box-round-top-4px " + dialogType + "\"><span class=\"dialog-box-header-content\">" + title + "</span></h2>";
        //dialog_box_html += "</div>";
        dialog_box_html += "<div id=\"dialog-box-content\" class=\"box-round-bottom-4px\">";
        dialog_box_html += "<div id=\"dialog-box-text\">" + message + "</div>";
        dialog_box_html += "<div class=\"dialog-box-buttonpane\">";
        dialog_box_html += "<a href=\"#\" class=\"dialog-box-button box-round-default\">Okay</a>";
        dialog_box_html += "</div>";
        dialog_box_html += "</div>";
        dialog_box_html += "</div>";
        $("body").append(dialog_box_html);
    }
    var dialog_box_html = "<div id=\"dialog-box-overlay\" class=\"" + classOverlay + "\"></div>";
    $("body").append(dialog_box_html);
    var maskHeight = $(document).height();
    var maskWidth = $(window).width();
    $('#dialog-box-overlay').css({
        height: maskHeight,
        width: maskWidth
    }).show();
    if ($.browser.msie && $.browser.version.substr(0, 1) > 6) {
        $('#dialog-box-transparent-background').corner("round 10px");
    }
    $('#dialog-box-parent-container').center().fadeIn("fast");
    $('#dialog-box-text').html(message);
}
$.fn.center = function (absolute) {
    return this.each(function () {
        var t = jQuery(this);
        pos = absolute ? 'absolute' : 'fixed';
        topPos = '50%';
        if ($.browser.msie && $.browser.version.substr(0, 1) < 7) {
            pos = 'absolute';
            topPos = jQuery(window).height() / 2 - t.height() / 2 + jQuery(window).scrollTop();
        }
        t.css({
            position: pos,
            left: '50%',
            top: topPos,
            zIndex: '9999'
        }).css({
            marginLeft: '-' + (t.outerWidth() / 2) + 'px',
            marginTop: '-' + (t.outerHeight() / 2) + 'px'
        });
        if (absolute) {
            t.css({
                marginTop: parseInt(t.css('marginTop'), 10) + jQuery(window).scrollTop(),
                marginLeft: parseInt(t.css('marginLeft'), 10) + jQuery(window).scrollLeft()
            });
        }
    });
};
