﻿function dk_dsg_msd_sp_dialog() {
    this.url = '';
    this.targetElement = null;
    this.dialogClass = '';
    this.ShowTitleBar = true;
    this.title = 'Salling';
    this.showTitleCloseButton = true;
    this.modal = true;
    this.height = "auto";
    this.width = "auto";
    this.minHeight = "auto";
    this.maxHeight = null;
    this.draggable = true;
    this.resizable = true;
    //Horzontal Postions:[Left, Center, Right]
    //Vertical Positions:[Top,  Center, Bottom]
    this.position = ['center', 'center'];  //['right','top'];
    this.zIndex = 3999;
    this.buttons = null;

    /*-see Buttons example below
    this.buttons = [{
    width:'100px',
    text: "Ok",
    click: function () {
    $(this).dialog("close");
    }
    },
    {
    width:'100px',
    text: 'Cancel',
    click: function () {
    alert('i am being clicked');
    }
    }];
    --End buttons example----*/

    //Not supported
    //this.overlay = null;// { backgroundColor: "#000", opacity: 0.5 };

    this.InitDialogOptions = function () {

        var dlgOptions = $.ui.dialog.prototype.options;

        if (this.dialogClass != '') {
            dlgOptions.dialogClass = this.dialogClass;
        }

        dlgOptions.title = this.title;
        dlgOptions.draggable = this.draggable;
        dlgOptions.resizable = this.resizable;
        dlgOptions.modal = this.modal;
        dlgOptions.height = this.height;
        dlgOptions.width = this.width;
        dlgOptions.minHeight = this.minHeight;

        if (this.maxHeight != null) {
            dlgOptions.maxHeight = this.maxHeight;
        }

        if (this.position != null) {
            dlgOptions.position = this.position;
        }

        if (this.buttons != null) {
            dlgOptions.buttons = this.buttons;
        }

        return dlgOptions;
    }

    this.showDialog = function () {

        var dlgoptions = this.InitDialogOptions();

        if (this.targetElement != null) {
            showTargetElementAsDialog(this.targetElement, dlgoptions);
        }
        else if (this.url != '') {
            showUrlinFramedDialog(this.url, dlgoptions);
        }

        this.SetOpenedDialogProperties();
    }

    this.HideDefaultCloseButton = function () {

        if (this.showTitleCloseButton != null && this.showTitleCloseButton == false) {
            //hide close button.
            //$("ui-dialog").parent().children().children('.ui-dialog-titlebar-close').hide();
            $(".ui-dialog-titlebar-close").hide();
        }
    }

    this.replaceCloseByImage = function () {
        //$(".ui-dialog-titlebar-close").html('<img src="/Style Library/Customer Core Styles/Styles/Images/close.gif" />');
        //$(".ui-dialog-titlebar-close").css({ 'padding-left': '50px' });
    }

    this.hideDialogTitleBar = function () {

        if (this.ShowTitleBar != null && this.ShowTitleBar == false) {
            $(".ui-dialog-titlebar").hide();
        }

    }

    this.hideDialog = function () {
        $('.ui-dialog').remove();
        // $(".ui-dialog").dialog("close");
    }

    this.changeTitlebarClass = function () {
        $('.ui-dialog-titlebar').addClass("openingHoursTitlebar");
        // $(".ui-dialog").dialog("close");
    }

    this.SetOpenedDialogProperties = function () {

        this.HideDefaultCloseButton();
        this.hideDialogTitleBar();
    }

    function showTargetElementAsDialog(targetElement, dlgOptions) {
        //Fix: [.parent().appendTo(jQuery("form:first"));] Fix for Jquery Validation Plugin because it works only if html is inside the form not inside the body bedefault jquery ui dialog insert html div inside body
        $('#' + targetElement.id).dialog(dlgOptions).parent().appendTo(jQuery("form:first"));
    }

    function showUrlinFramedDialog(url, dlgOptions) {

        // $("#dialog:ui-dialog").dialog("destroy");
        $('#divframe-dialog').remove();
        var dialogFrame = $('<div id="divframe-dialog" title="Basic modal dialog"> <div id="frameDiv"> <iframe id="dialog-frame" width="100%" height="100%" scrolling="no" frameborder="0"></iframe> </div> </div>');

        $('body').append(dialogFrame);
        $("#dialog-frame").attr("src", url);
        $("#divframe-dialog").dialog(dlgOptions);
    }

    $(document).ready(function () {
        if (parent != null && parent.location != window.location) {
            var height = $('.dsg_sp_mainContainer').height();
            height = height > 400 ? height : 400;
            parent.document.getElementById("dialog-frame").height = (height + 25) + "px";
            parent.document.getElementById("dialog-frame").width = ($('.dsg_sp_mainContainer').width() + 25) + "px";


            /*===Fix for dialog position in case of frame===*/
            var screenWidth = window.screen.width;
            var dialogwidth = $(parent.document).find('.ui-dialog').width();
            var dlgtop = $(parent.document).find('.ui-dialog').position().top;

            if ((screenWidth - dialogwidth) > 0) {
                var left = (screenWidth - dialogwidth) / 2;
                $(parent.document).find('.ui-dialog').offset({ top: dlgtop, left: left });
            }
            /*===End Fix for dialog position in case of frame===*/
        }
    });
}

$(document).ready(function () { window.dialog = new dk_dsg_msd_sp_dialog(); });


function setFoetexTop() {
    $('div.ui-dialog').each(function () {
        if ($(this).css('display') != 'none') {
            if ($(this).find('.popupTopContainer').length <= 0) {
                $('<div class="popupTopContainer"><div class="popupTopLeft">&nbsp;</div><div class="popupTopRight">&nbsp;</div></div>').prependTo(this);
            }
        }
    });

    $('.popupTopRight').each(function () {
        var oWidth = $(this).parent().width();
        $(this).css('width', (oWidth - 10) + 'px');
    });
}

