﻿/// <reference name="MicrosoftAjax.debug.js" />
/// <reference name="MicrosoftAjaxTimer.debug.js" />
/// <reference name="MicrosoftAjaxWebForms.debug.js" />
/// <reference name="AjaxControlToolkit.ExtenderBase.BaseScripts.js" assembly="AjaxControlToolkit" />
/// <reference name="AjaxControlToolkit.Common.Common.js" assembly="AjaxControlToolkit" />
/// <reference name="AjaxControlToolkit.Compat.DragDrop.DragDropScript.js" assembly="AjaxControlToolkit" />


Type.registerNamespace('Infocaster.Web.Ajax');

//TODO: Expose events OnShown and OnHidden
Infocaster.Web.Ajax.SimplePopupBehavior = function(element) {
    Infocaster.Web.Ajax.SimplePopupBehavior.initializeBase(this, [element]);
    this._visible = false;
    this._boundedElements = new Array();
    this._mouseSpacing = 3;
    this._mouseGlue = true,
    // Generic animation behaviors that automatically build animations from JSON descriptions
    this._shown = new AjaxControlToolkit.Animation.GenericAnimationBehavior(element);
    this._hidden = new AjaxControlToolkit.Animation.GenericAnimationBehavior(element);
}
Infocaster.Web.Ajax.SimplePopupBehavior.prototype = {
    initialize: function() {
        Infocaster.Web.Ajax.SimplePopupBehavior.callBaseMethod(this, 'initialize');
        var el = this.get_element();
        Sys.Debug.assert(el != null);
        //Call to init
        this.set_Visible(this.get_Visible());

        //Initialize the generic animation behaviors
        this._shown.initialize();
        this._hidden.initialize();
    },

    dispose: function() {
        Infocaster.Web.Ajax.SimplePopupBehavior.callBaseMethod(this, 'dispose');
    },
    //Basicly just shows the element. might include some animations in the future
    show: function() {
        this.set_Visible(true);
        this._onShown();
    },
    //Hides the popup
    hide: function() {
        this.set_Visible(false);
        this.onHidden();
    },
    get_Visible: function() {
        return this._visible;
    },
    set_Visible: function(val, args) {
        this._visible = val;
        var el = this.get_element();

        $common.setVisible(el, val);
        if (args != null || args != undefined) {
            window._event = args;
            el.style.position = "absolute";
        }
    },
    get_Location: function() {
        return $common.getLocation(this.get_element());
    },
    set_Location: function(value) {
        $common.setLocation(this.get_element(), value);
    },
    get_MouseSpacing: function() {
        return this._mouseSpacing;
    },
    set_MouseSpacing: function(value) {
        this._mouseSpacing = value;
    },
    get_MouseGlue: function() {
        return this._mouseGlue;
    },
    set_MouseGlue: function(val) {
        this._mouseGlue = val;
    },
    // Type get_dragDataType()
    get_dragDataType: function() { return "SimplePopup"; },
    // Object getDragData(Context)
    getDragData: function() { return this; },
    // DragMode get_dragMode()
    get_dragMode: function() { return AjaxControlToolkit.DragMode.Move; },
    // void onDragStart()
    onDragStart: function() { },
    // void onDrag()
    onDrag: function() { },
    // void onDragEnd(Cancelled)
    onDragEnd: function() { },
    //Binds a specific element to the popup as a mouseover
    bind: function(target, show) {
        if (!Array.contains(this._boundedElements, target)) {
            Array.add(this._boundedElements, target);

            var local = this;
            $addHandler(target, "mouseover", function(args) {
                local.set_Visible(true, args);
                local._onShown();
            });
            if (this.get_MouseGlue()) {
                $addHandler(target, "mousemove", function(args) {
                    var el = local.get_element();
                    var spacing = local.get_MouseSpacing();
                    var size = $common.getSize(el);
                    var docBounds = $common.getBounds(window.document.documentElement);                    
                    var x = args.clientX + spacing;
                    //Reposition x if nessesary
                    if (x + size.width > docBounds.width)
                        x = args.clientX - size.width - spacing;
                    var y = args.clientY + spacing;
                    if (y + size.height > docBounds.height)
                        y = args.clientY - size.height - spacing;
                    //next, apply the scrollposition
                    var scrollPosition = local.getScrollPositions();
                    Sys.UI.DomElement.setLocation(el, x + scrollPosition.x, y + scrollPosition.y);
                });
            }
            $addHandler(target, "mouseout", function(args) {
                //$common.setVisible(local.get_element(), false);
                local._onHidden();
            });

            if (show)
                this.show();
        }
    },
    _onShown: function(sender, args) {
        this._hidden.quit();
        this._shown.play();
    },

    _onHidden: function(sender, args) {
        this._shown.quit();
        this._hidden.play();
    },
    get_OnShown: function() {
        return this._shown.get_json();
    },

    set_OnShown: function(value) {
        this._shown.set_json(value);
        this.raisePropertyChanged('OnShown');
    },

    get_OnShownBehavior: function() {
        return this._shown;
    },

    get_OnHidden: function() {
        return this._hidden.get_json();
    },

    set_OnHidden: function(value) {
        this._hidden.set_json(value);
        this.raisePropertyChanged('OnHidden');
    },

    get_OnHiddenBehavior: function() {
        return this._hidden;
    },
    getScrollPositions: function() {
        var scrollLeftPosition = 0;
        var scrollTopPosition = 0;

        if ((document.documentElement != null) && (document.documentElement.scrollLeft != null))
            scrollLeftPosition = document.documentElement.scrollLeft;
        else if ((document.body != null) && (document.body.scrollLeft != null))
            scrollLeftPosition = document.body.scrollLeft;
        else if (window.pageXOffset != null)
            scrollLeftPosition = window.pageXOffset;
        else if (window.scrollX != null)
            scrollLeftPosition = window.scrollX;

        scrollLeftPosition = parseInt(scrollLeftPosition);
        if ((isNaN(scrollLeftPosition) == true) || (scrollLeftPosition < 0))
            scrollLeftPosition = 0;

        if ((document.documentElement != null) && (document.documentElement.scrollTop != null))
            scrollTopPosition = document.documentElement.scrollTop;
        else if ((document.body != null) && (document.body.scrollTop != null))
            scrollTopPosition = document.body.scrollTop;
        else if (window.pageYOffset != null)
            scrollTopPosition = window.pageYOffset;
        else if (window.scrollY != null)
            scrollTopPosition = window.scrollY;

        scrollTopPosition = parseInt(scrollTopPosition);
        if ((isNaN(scrollTopPosition) == true) || (scrollTopPosition < 0))
            scrollTopPosition = 0;

        return new Sys.UI.Point(scrollLeftPosition, scrollTopPosition);
    }

}
Infocaster.Web.Ajax.SimplePopupBehavior.registerClass('Infocaster.Web.Ajax.SimplePopupBehavior',
AjaxControlToolkit.BehaviorBase,
AjaxControlToolkit.IDragSource);

if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();