﻿////////////////////////////////////////////////////
// Author   : Tien Do
//--------------------------------------------------

/* create namespace for IAv2 Screen */
IAv2 = {};
IAv2.Screen = {};
IAv2.Screen.Default = {
    width: 'auto'
    , height: 'auto'
    , maxHeight: 480
    , maxWidth: 800
    , minHeight: 30
    , minWidth: 200
}

/* Register a pop-up */
//container     : div element which contains content of pop-up
//openButton    : id of HTML element used to open pop-up
//closeButton    : id of HTML element used to close pop-up
//title         : Specifies the title of the dialog. The title can also be specified by the title attribute on the dialog source element.
//width         : The width of the dialog, in pixels.
//height        : The height of the dialog, in pixels.
//resizable     : If set to true, the dialog will be resizeable.
//draggable     : If set to true, the dialog will be draggable will be draggable by the titlebar.
//modal         : If set to true, the dialog will have modal behavior; other items on the page will be disabled (i.e. cannot be interacted with). Modal dialogs create an overlay below the dialog but above other page elements.
//closeOnEscape : Specifies whether the dialog should close when it has focus and the user presses the esacpe (ESC) key.
//maxHeight     : The maximum height to which the dialog can be resized, in pixels.
//maxWidth      : The maximum width to which the dialog can be resized, in pixels.
//minHeight     : The minimum height to which the dialog can be resized, in pixels.
//minWidth      : The minimum width to which the dialog can be resized, in pixels.

IAv2.Screen.RegisterPopup = function(obj) {
    if (!('container' in obj)) {
        return;
    }

    // Fixed bug placing pop-up inside an UpdatePanel -> duplicated
    // Fixing by removing pop-up if existed
    var originalId = obj.container.replace("#", "");
    if ($("div[id='" + originalId + "']").length > 1) {
        var existed = $("div[aria-labelledby='ui-dialog-title-" + originalId + "']:last");
        if (existed && (existed.length > 0)) {
            existed.remove();
            var o = $("div[id='" + originalId + "']:last");
            o.remove();
        }
    }
    var originalWidth = $(obj.container).width();

    $(obj.container).dialog({
        autoOpen: false
        , width: ('width' in obj) ? obj.width : IAv2.Screen.Default.width
        , height: ('height' in obj) ? obj.height : IAv2.Screen.Default.height
        , title: ('title' in obj) ? obj.title : this.title
        , resizable: ('resizable' in obj) ? obj.resizable : false
        , draggable: ('draggable' in obj) ? obj.draggable : true
        , modal: ('modal' in obj) ? obj.modal : true
        //, closeOnEscape: ('closeOnEscape' in obj) ? obj.closeOnEscape : true
        , closeOnEscape: false
        , maxHeight: ('maxHeight' in obj) ? obj.maxHeight : IAv2.Screen.Default.maxHeight
        , maxWidth: ('maxWidth' in obj) ? obj.maxWidth : IAv2.Screen.Default.maxWidth
        , minHeight: ('minHeight' in obj) ? obj.minHeight : IAv2.Screen.Default.minHeight
        , minWidth: ('minWidth' in obj) ? obj.minWidth : IAv2.Screen.Default.minWidth
        , open: function(type, data) {
            $(this).parent().appendTo("form");
            $(".ui-dialog-titlebar-close").hide();
        }
        , beforeclose: function(event, ui) {
            $('#ui-datepicker-div').css('display', 'none');
        }
    });

    //    if (jQuery.browser.msie) {
    //        if (jQuery.browser.version == "7.0") {
    //            //$(obj.container).dialog('option', 'width', $(obj.container).parent().width() + 18);
    //            $(obj.container).dialog('option', 'width', originalWidth + 28);
    //        }
    //    }

    if ($.browser.msie && $.browser.version == 6) {
        $(".ui-dialog-container").css({ overflow: 'hidden' });
    }

    // append button event handle to open pop-up
    if ('openButton' in obj) {
        $(obj.openButton).click(function() {
            $(obj.container).dialog('open');
        });
    }

    // append button event handle to close pop-up
    if ('closeButton' in obj) {
        $(obj.closeButton).click(function() {
            $(obj.container).dialog('close');
        });
    }
}

/* Open a pop-up */
IAv2.Screen.ShowPopup = function(containerId) {
    //var x = document.body.clientWidth / 2;
//    var x = $(containerId).dialog('option', 'position');
//    var y = document.body.clientHeight / 2;
//    alert(x);
//    //alert(y);
    //$(containerId).dialog('option', 'position', [x, y]); //dlg.dialog('option', 'position', 'center');
    $(containerId).dialog("open");
    return false;
}

/* Close a pop-up */
IAv2.Screen.ClosePopup = function(containerId) {
    $(containerId).dialog("close");
}

/* Destroy a pop-up */
IAv2.Screen.DestroyPopup = function(containerId) {
    $(containerId).dialog('destroy');
}

/* Changes pop-up title */
IAv2.Screen.ChangePopupTitle = function(containerId, title) {
    $(containerId).dialog('option', "title", title);
}

/* Changes pop-up width */
IAv2.Screen.ChangePopupWidth = function(containerId, width) {
    $(containerId).dialog('option', "width", width);
}

/* enables/disables a single button */
IAv2.Screen.EnableButton = function(buttonId, state) {
    if (state === true) {
        $(buttonId).removeAttr('disabled');
    } else {
        $(buttonId).attr("disabled", "disabled");
    }
}

/* enables a list buttons contained in array */
//IAv2.Screen.EnableButtons = function(buttonArray) {
//    for (var i = 0; i < buttonArray.length; i++) {
//        IAv2.Screen.EnableButton(buttonArray[i]);
//    }
//}

IAv2.Screen.DisableButton = function(buttonId) {
    $(buttonId).attr("disabled", "disabled");
}

IAv2.Screen.RegisterPopupForm = function(obj) {
if (!('container' in obj)) {
    return;
}

// Fixed bug placing pop-up inside an UpdatePanel -> duplicated
// Fixing by removing pop-up if existed
var originalId = obj.container.replace("#", "");
if ($("div[id='" + originalId + "']").length > 1) {
    var existed = $("div[aria-labelledby='ui-dialog-title-" + originalId + "']:last");
    if (existed && (existed.length > 0)) {
        existed.remove();
        var o = $("div[id='" + originalId + "']:last");
        o.remove();
    }
}
var originalWidth = $(obj.container).width();

$(obj.container).dialog({
        autoOpen: false
        , width: ('width' in obj) ? obj.width : IAv2.Screen.Default.width
        , height: ('height' in obj) ? obj.height : IAv2.Screen.Default.height
        , title: ('title' in obj) ? obj.title : this.title
        , resizable: ('resizable' in obj) ? obj.resizable : false
        , draggable: ('draggable' in obj) ? obj.draggable : false
        , modal: ('modal' in obj) ? obj.modal : true
        , closeOnEscape: ('closeOnEscape' in obj) ? obj.closeOnEscape : true
        , maxHeight: ('maxHeight' in obj) ? obj.maxHeight : IAv2.Screen.Default.maxHeight
        , maxWidth: ('maxWidth' in obj) ? obj.maxWidth : IAv2.Screen.Default.maxWidth
        , minHeight: ('minHeight' in obj) ? obj.minHeight : IAv2.Screen.Default.minHeight
        , minWidth: ('minWidth' in obj) ? obj.minWidth : IAv2.Screen.Default.minWidth
        , open: function(type, data) {
            $(this).parent().appendTo("form");
        }
        //,beforeclose: function(event, ui){alert('dialog box is closed')}
    });

    // append button event handle to open pop-up
    if ('openButton' in obj) {
        $(obj.openButton).click(function() {
            $(obj.container).dialog('open');
        });
    }

    // append button event handle to close pop-up
    if ('closeButton' in obj) {
        $(obj.closeButton).click(function() {
            $(obj.container).dialog('close');
        });
    }
    if (jQuery.browser.msie) {
        if (jQuery.browser.version == "7.0") {
            //$(obj.container).dialog('option', 'width', $(obj.container).parent().width() + 18);
            $(obj.container).dialog('option', 'width', originalWidth + 28);
        }
    }
}

IAv2.Screen.HideModalWindow = function(modalWindowId) {
    var modal = $find(modalWindowId);
    alert(modal);
//    modal.hide();
}
