﻿Type.registerNamespace("OmniWeb");

OmniWeb.ExpanderImage = function(element) {
    OmniWeb.ExpanderImage.initializeBase(this, [element]);

    /* Properties */
    this._Collapsed = null;
    this._CollapseImageUrl = null;
    this._CollapseHoverUrl = null;
    this._ExpandImageUrl = null;
    this._ExpandHoverUrl = null;
    this._OnCollapse = null;
    this._OnExpand = null;
    this._TargetId = null;
    this._StateId = null;

    /* Handlers */
    this._clickHandler = null;
    this._mouseOverHandler = null;
    this._mouseOutHandler = null;
}

OmniWeb.ExpanderImage.prototype =
{
    initialize: function() {
        OmniWeb.ExpanderImage.callBaseMethod(this, 'initialize');

        this._clickHandler = Function.createDelegate(this, this._onClick);
        this._mouseoverHandler = Function.createDelegate(this, this._onMouseover);
        this._mouseoutHandler = Function.createDelegate(this, this._onMouseout);

        var cntrl = this.get_element();
        $addHandler(cntrl, "click", this._clickHandler);
        $addHandler(cntrl, "mouseover", this._mouseoverHandler);
        $addHandler(cntrl, "mouseout", this._mouseoutHandler);

        this._setDisplay();
    },

    dispose: function() {
        $clearHandlers(this.get_element());

        if (this._clickHandler) delete this._clickHandler;
        if (this._mouseoverHandler) delete this._mouseoverHandler;
        if (this._mouseoutHandler) delete this._mouseoutHandler;

        OmniWeb.ExpanderImage.callBaseMethod(this, 'dispose');
    },

    set_Collapsed: function(value) {
        this._Collapsed = value;
    },
    get_Collapsed: function() {
        return this._Collapsed;
    },

    set_CollapseImageUrl: function(value) {
        this._CollapseImageUrl = value;
    },
    get_CollapseImageUrl: function() {
        return this._CollapseImageUrl;
    },

    set_CollapseHoverUrl: function(value) {
        this._CollapseHoverUrl = value;
    },
    get_CollapseHoverUrl: function() {
        return this._CollapseHoverUrl;
    },

    set_ExpandImageUrl: function(value) {
        this._ExpandImageUrl = value;
    },
    get_ExpandImageUrl: function() {
        return this._ExpandImageUrl;
    },

    set_ExpandHoverUrl: function(value) {
        this._ExpandHoverUrl = value;
    },
    get_ExpandHoverUrl: function() {
        return this._ExpandHoverUrl;
    },

    set_OnCollapse: function(value) {
        this._OnCollapse = value;
    },
    get_OnCollapse: function() {
        return this._OnCollapse;
    },

    set_OnExpand: function(value) {
        this._OnExpand = value;
    },
    get_OnExpand: function() {
        return this._OnExpand;
    },

    set_TargetId: function(value) {
        this._TargetId = value;
    },
    get_TargetId: function() {
        return this._TargetId;
    },

    set_StateId: function(value) {
        this._StateId = value;
    },
    get_StateId: function() {
        return this._StateId;
    },

    collapse: function() {
        this._Collapsed = true;
        this._setDisplay();
        if (this._OnCollapse) {
            eval(this._OnCollapse);
        }
    },
    expand: function() {
        this._Collapsed = false;
        this._setDisplay();
        if (this._OnExpand) {
            eval(this._OnExpand);
        }
    },
    toggle: function() {
        if (this._Collapsed) {
            this.expand();
        } else {
            this.collapse();
        }
    },

    /* Setup Display */

    _setDisplay: function() {
        var panel = null;
        var state = null;
        var cntrl = this.get_element();
        if (this._TargetId) {
            panel = $get(this._TargetId);
        }
        if (this._StateId && this._StateId != "") {
            state = $get(this._StateId);
        }
        if (this._Collapsed) {
            cntrl.src = this._ExpandImageUrl;
            if (state) state.value = "1";
            if (panel) panel.style.display = "none";
        } else {
            cntrl.src = this._CollapseImageUrl;
            if (state) state.value = "0";
            if (panel) {
                if (panel.style.removeAttribute) {
                    panel.style.removeAttribute("display");
                } else {
                    panel.style.removeProperty("display");
                }
            }
        }
    },

    /* UI Event Handlers */

    _onClick: function(event) {
        this.toggle();
        this._onMouseover();
        event.stopPropagation();
    },
    _onMouseover: function() {
        if (this._Collapsed) {
            this.get_element().src = this._ExpandHoverUrl;
        } else {
            this.get_element().src = this._CollapseHoverUrl;
        }
    },
    _onMouseout: function() {
        if (this._Collapsed) {
            this.get_element().src = this._ExpandImageUrl;
        } else {
            this.get_element().src = this._CollapseImageUrl;
        }
    }

}

OmniWeb.ExpanderImage.registerClass('OmniWeb.ExpanderImage', Sys.UI.Behavior);

if (typeof (Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();

if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();