﻿// README
//
// There are two steps to adding a property:
//
// 1. Create a member variable to store your property
// 2. Add the get_ and set_ accessors for your property.
//
// Remember that both are case sensitive!
//

Type.registerNamespace('Igloo.ObjectModel.Controls');

Igloo.ObjectModel.Controls.PageDialogBehavior = function(element) {

    Igloo.ObjectModel.Controls.PageDialogBehavior.initializeBase(this, [element]);

    this._closeElement = null;
    this._shadowBehavior = null;
    this._shadowResourceUrl = null;
    this._revealElement = null;
    this._revealElementId = null;
    this._showAnimation = null;    
    this._ghostElement = null;
    this._closeHandler = null;
    this._openWithWaitMessage = false;
    this._showUnpinned = false;
    this._showModal = false;
    this._centerDialog = true; 
    this._waitMessage = null;
    this._clickHandler = null;
    this._shown = false;
}

Igloo.ObjectModel.Controls.PageDialogBehavior.prototype = {

    initialize: function() {
        Igloo.ObjectModel.Controls.PageDialogBehavior.callBaseMethod(this, 'initialize');

        /*
        // read shadow object
        var rgb = Sys.UI.Behavior.getBehaviorsByType(this._element, Igloo.ObjectModel.Controls.SoftShadowExtenderBehavior);
        if(rgb.length == 0) {
        this._shadowBehavior = rgb[0];
        }
        */

        this._element.style.display = "none";
        this._shadowBehavior = $create(Igloo.ObjectModel.Controls.SoftShadowExtenderBehavior, { ShadowResourceUrl: this._shadowResourceUrl }, null, null, this._element);

        this._closeHandler = Function.createDelegate(this, this.onCloseButton);
        $addHandler(this._closeElement, "click", this._closeHandler);

        this._clickHandler = Function.createDelegate(this, this.clickAnywhere);
        $addHandler(document, "click", this._clickHandler);


    },

    dispose: function() {

        this._shadowBehavior.dispose();
        $removeHandler(this._closeElement, "click", this._closeHandler);
        $removeHandler(document, "click", this._clickHandler);
        Igloo.ObjectModel.Controls.PageDialogBehavior.callBaseMethod(this, 'dispose');
    },

    get_RevealElement: function() {
        var elByID = this._revealElementId ? $get(this._revealElementId) : null;
        return elByID ? elByID : this._revealElement;
    },

    set_RevealElement: function(value) {
        if(value && value.id && value.id != '') {
            this._revealElementId = value.id;
        } else {
            this._revealElementId = null;
        }

        this._revealElement = value;
    },

    get_CloseElement: function() {
        return this._closeElement;
    },

    set_CloseElement: function(value) {
        this._closeElement = value;
    },

    get_ShadowResourceUrl: function() {
        return this._shadowResourceUrl;
    },

    set_ShadowResourceUrl: function(value) {
        this._shadowResourceUrl = value;
    },

    get_OpenWithWaitMessage: function() {
        return this._openWithWaitMessage;
    },

    set_OpenWithWaitMessage: function(value) {
        this._openWithWaitMessage = value;
    },

    get_ShowUnpinned: function() {
        return this._showUnpinned;
    },

    set_ShowUnpinned: function(value) {
        this._showUnpinned = value;
    },

    get_ShowModal: function() {
        return this._showModal;
    },

    set_ShowModal: function(value) {
        this._showModal = value;
    },

    get_CenterDialog: function() {
        return this._centerDialog;
    },

    set_CenterDialog: function(value) {
        this._centerDialog = value;
    },

    setAbsoluteLocation: function(el, point) {
        var locParent = $common.getLocation(el.offsetParent);
        $common.setLocation(el, { x: point.x - locParent.x, y: point.y - locParent.y });
    },

    setAbsoluteBounds: function(el, bounds) {
        var locParent = $common.getLocation(el.offsetParent);
        $common.setLocation(el, { x: bounds.x - locParent.x, y: bounds.y - locParent.y });
        $common.setSize(el, bounds);
    },

    show: function(elReveal) {

        if(!this._shown) {

            var element = this.get_element();
            if(!element) return;

            if(elReveal) {
                this.set_RevealElement(elReveal);
            }

            if(!this.get_RevealElement()) {
                this.set_RevealElement(document.body)
            }

            element.style.visibility = "hidden";
            element.style.display = "block";

            if(this._centerDialog) {
                // center the dialog        
                var clientBounds = $common.getClientBounds();
                var x = (document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft);
                var y = (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);


                x += Math.floor((clientBounds.width - element.offsetWidth) / 2);
                y += Math.floor((clientBounds.height - element.offsetHeight) / 2);

                this.setAbsoluteLocation(element, { x: x, y: y });
            }



            if(this._openWithWaitMessage && !this._waitMessage) {
                this.showWaitMessage();
            }


            //build the animation
            if(this._ghostElement == null) {
                this._ghostElement = document.createElement("div");
                this._ghostElement.style.border = "solid 1px #808080";
                document.body.appendChild(this._ghostElement);
            }

            this._ghostElement.style.display = "block";


            var boundsSrc = $common.getBounds(this.get_RevealElement());
            $common.setBounds(this._ghostElement, boundsSrc);
            var boundsDst = $common.getBounds(element);


            // move ghost                
            var aniMove = new AjaxControlToolkit.Animation.MoveAnimation(this._ghostElement, 0, 0, boundsDst.x - boundsSrc.x, boundsDst.y - boundsSrc.y, true, "px");
            var aniResize = new AjaxControlToolkit.Animation.ResizeAnimation(this._ghostElement, 0, 0, boundsDst.width, boundsDst.height, "px");
            this._showAnimation = new AjaxControlToolkit.Animation.ParallelAnimation(this._ghostElement, 0.4, 15, [aniResize, aniMove]);
            this._showAnimation.get_events().addHandler("ended", Function.createDelegate(this, this.onOpenAnimationEnd));
            this._showAnimation.play();


        }

    },

    showWaitMessage: function() {

        var element = this.get_element();
        if(!element) return;

        if(this._waitMessage) {
            this.removeWaitMessage();
        }

        this._waitMessage = document.createElement("div");
        this._waitMessage.style.position = "absolute";
        this._waitMessage.style.backgroundColor = $common.getInheritedBackgroundColor(element);
        var elContent = $get("dlgContent", element);
        if(elContent == null) {
            elContent = element.childNodes[element.childNodes.length - 1];
        }
        element.appendChild(this._waitMessage);
        var cntBounds = $common.getBounds(elContent);
        this.setAbsoluteBounds(this._waitMessage, cntBounds);


        // insedt centered table
        var tblMsg = document.createElement("table");
        this._waitMessage.appendChild(tblMsg);

        var tbody = document.createElement("tbody");
        tblMsg.appendChild(tbody);

        var tr = document.createElement("tr");
        tbody.appendChild(tr);

        var td = document.createElement("td");
        tr.appendChild(td);
        td.style.verticalAlign = "middle";
        td.style.textAlign = "center";
        td.style.height = cntBounds.height.toString() + "px";
        td.style.width = cntBounds.width.toString() + "px";
        td.style.fontWeight = "700";
        td.style.fontSize = "120%";

        var img = document.createElement("img");
        img.src = Pythagoras.StandardResources.WaitIco;
        img.style.verticalAlign = "middle";

        td.appendChild(img);
        td.appendChild(document.createTextNode("  Prosím, čekejte ..."));

    },

    removeWaitMessage: function() {

        if(this._waitMessage) {
            this._waitMessage.parentNode.removeChild(this._waitMessage);
            this._waitMessage = null;
        }

        this._shadowBehavior.layoutItems();

    },

    layoutDialog: function() {
        this._shadowBehavior.layoutItems();
    },

    onCloseButton: function(ev) {

        ev.stopPropagation();
        ev.preventDefault();
        this.close();

    },

    close: function() {
        if(this._shown) {
            if(this._showModal) {
                Pythagoras.Modal.UnModal();
            }
            this._shown = false;
            this.get_element().style.display = "none";
        }
    },

    onOpenAnimationEnd: function() {
        this._element.style.display = "block";
        this._ghostElement.style.display = "none";
        this._shown = true;
        this._shadowBehavior.layoutItems();
        this._element.style.visibility = "visible";

        if(this._showModal) {
            Pythagoras.Modal.GoModal();
        }
    },

    clickAnywhere: function(e) {

        if(this._shown && this._showUnpinned) {
            var element = this.get_element();
            if(!AjaxControlToolkit.DomUtility.isDescendantOrSelf(element, e.target)) {
                this.close();
            }
        }

    },

    get_IsVisible: function() {
        return this._shown;
    }
}

Igloo.ObjectModel.Controls.PageDialogBehavior.registerClass('Igloo.ObjectModel.Controls.PageDialogBehavior', AjaxControlToolkit.BehaviorBase);

//if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();
if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();