﻿// (c) Copyright Microsoft Corporation.
// This source is subject to the Microsoft Permissive License.
// See http://www.microsoft.com/resources/sharedsource/licensingbasics/sharedsourcelicenses.mspx.
// All other rights reserved.


/// <reference name="MicrosoftAjax.debug.js" />
/// <reference name="MicrosoftAjaxTimer.debug.js" />
/// <reference name="MicrosoftAjaxWebForms.debug.js" />
/// <reference path="../ExtenderBase/BaseScripts.js" />
/// <reference path="../Common/Common.js" />
/// <reference path="../DynamicPopulate/DynamicPopulateBehavior.js" />
/// <reference path="../Compat/Timer/Timer.js" />
/// <reference path="../Animation/Animations.js" />
/// <reference path="../Animation/AnimationBehavior.js" />
/// <reference path="../PopupExtender/PopupBehavior.js" />
/// <reference path="../HoverExtender/HoverBehavior.js" />


Type.registerNamespace('Controtex.Controls.Ajax');

Controtex.Controls.Ajax.BasicDropDownBehavior = function(element) {
    Controtex.Controls.Ajax.BasicDropDownBehavior.initializeBase(this, [element]);
    this._dropDownControl = null;
    this._dropElement = null;
    this._hoverCssClass = 'hover',
    this._isOpen = false;
    this._isOver = false;
    this._wasClicked = null;
    this._dropElementHoverBehavior = null;
    this._dropPopupPopupBehavior = null;
    this._onShowJson = null;
    this._onHideJson = null;
    
    this._dropDownControl$delegates = {
        click : Function.createDelegate(this, this._dropDownControl_onclick),
        contextmenu : Function.createDelegate(this, this._dropDownControl_oncontextmenu)
    }
    this._dropElement$delegates = {
        click : Function.createDelegate(this, this._dropElement_onclick),
        contextmenu : Function.createDelegate(this, this._dropElement_oncontextmenu)
    }
    this._document$delegates = {
        click : Function.createDelegate(this, this._document_onclick),
        contextmenu : Function.createDelegate(this, this._document_oncontextmenu)
    }
    this._dropElementHoverBehavior$delegates = {
        hover : Function.createDelegate(this, this._dropElementHoverBehavior_onhover),
        unhover : Function.createDelegate(this, this._dropElementHoverBehavior_onunhover)
    }
}
Controtex.Controls.Ajax.BasicDropDownBehavior.prototype = {
    
    
    initialize : function() {
        Controtex.Controls.Ajax.BasicDropDownBehavior.callBaseMethod(this, 'initialize');
        var elt = this._dropElement = this.get_element();        
        var parent = this._dropElement.parentNode;
        
        //
        // _dropDownControl
        //
        if (this._dropDownControl == null) {
            $common.createElementFromTemplate({
                parent : parent,
                nameTable : this,
                name : "_dropDownControl",
                nodeName : "div",
                visible : false,
                cssClasses : this._dropDownControl ? null : ["ajax__basicDropDown_panel"],
                properties : { 
                    __GENERATED : true 
                }
            });
        }
        $addHandlers(this._dropDownControl, this._dropDownControl$delegates);
            
        $addHandlers(elt, this._dropElement$delegates);
        
        //
        // _dropPopupPopupBehavior
        //
        this._dropPopupPopupBehavior = $create(AjaxControlToolkit.PopupBehavior, { 
            positioningMode : AjaxControlToolkit.PositioningMode.BottomRight, 
            parentElement : elt, 
            y : -1 
        }, null, null, this._dropDownControl);
        
        // Create the animations (if they were set before initialize was called)
        if (this._onShowJson) {
            this._dropPopupPopupBehavior.set_onShow(this._onShowJson);
        }
        if (this._onHideJson) {
            this._dropPopupPopupBehavior.set_onHide(this._onHideJson);
        }
        
        
        //
        // _dropElementHoverBehavior
        //
        this._dropElementHoverBehavior = $create(AjaxControlToolkit.HoverBehavior, { 
            hoverElement : elt
        }, this._dropElementHoverBehavior$delegates, null, this._dropElement);

        //
        // wire events
        //
        $addHandlers(document, this._document$delegates);
    },
    
    dispose : function() {
        var elt = this.get_element();
        if (this._isOpen) {
            this.hide();
            this.unhover();
            this._isOpen = false;
        }
        $common.removeHandlers(document, this._document$delegates);
        this._onShowJson = null;
        this._onHideJson = null;
        if (this._dropPopupPopupBehavior) {
            this._dropPopupPopupBehavior.dispose();
            this._dropPopupPopupBehavior = null;
        }
        if (this._dropElementHoverBehavior) {
            this._dropElementHoverBehavior.dispose();
            this._dropElementHoverBehavior = null;
        }
        if (this._dropElement) {
            $common.removeHandlers(this._dropElement, this._dropElement$delegates);
            this._dropElement = null;
        }
        if (this._dropDownControl) {
            $common.removeHandlers(this._dropDownControl, this._dropDownControl$delegates);
            if (this._dropDownControl.__GENERATED) {
                $common.removeElement(this._dropDownControl);
            }
            this._dropDownControl = null;
        }
        Controtex.Controls.Ajax.BasicDropDownBehavior.callBaseMethod(this, 'dispose');
    },    

    hover : function() {
        var elt = this.get_element();
        if (!this._isOver) {
            this._isOver = true;
            this.raiseHoverOver(Sys.EventArgs.Empty);
            
            Sys.UI.DomElement.addCssClass(elt, this._hoverCssClass);
        }
    },
    
    unhover : function() {
        var elt = this.get_element();
        if (this._isOver || !this._isOpen) {
            this._isOver = false;
            if (!this._isOpen)
                Sys.UI.DomElement.removeCssClass(elt, this._hoverCssClass);
            
            this.raiseHoverOut(Sys.EventArgs.Empty);
        }
    },
    
    show : function() {
        if (!this._isOpen) {
            this.hover();

            var eventArgs = new Sys.CancelEventArgs();
            this.raiseShowing(eventArgs);
            this.raisePopup(eventArgs);
            if (eventArgs.get_cancel()) {
                return;
            }
            
            this._isOpen = true;
            this.populate();
            if (!this._dynamicPopulateBehavior || (this._dynamicPopulateBehavior._populated && this._cacheDynamicResults)) {
                this._showPopup();
            }
        }
    },
    
    _showPopup : function() {
        /// <summary>
        /// Show the drop down's popup
        /// </summary>
        /// <returns />
        
        this._dropPopupPopupBehavior.show();
        this.raiseShown(Sys.EventArgs.Empty);
    },
    
    hide : function() {
        if (this._isOpen) {
            var eventArgs = new Sys.CancelEventArgs();
            this.raiseHiding(eventArgs);
            if (eventArgs.get_cancel()) {
                return;
            }
        
            this._isOpen = false;
            this._dropPopupPopupBehavior.hide();
            
            this.raiseHidden(Sys.EventArgs.Empty);
        }
    },
    
    _dropElementHoverBehavior_onhover : function(sender, e) {
        this.hover();
    },
    _dropElementHoverBehavior_onunhover : function(sender, e) {
        this.unhover();
    },
    _dropElement_onclick : function(e) {
        if(e.target.tagName != "A") {
            if(!this._isOpen) {
                this.show();
            } else {
                this.hide();
            }
            this._wasClicked = true;
        }
    },
    _dropElement_oncontextmenu : function(e) {
        if(e.target.tagName != "A") {
            this._wasClicked = true;
            e.preventDefault();
            this.show();
        }
    },
    _dropFrame_onclick : function(e) {
        if(!this._isOpen) {
            this.show();
        } else {
            this.hide();
        }
        this._wasClicked = true;
    },
    _dropFrame_oncontextmenu : function(e) {
        this._wasClicked = true;
        e.preventDefault();
        this.show();
    },
    _dropDownControl_onclick : function(e) {
        //this._wasClicked = true;
    },
    _dropDownControl_oncontextmenu : function(e) {
        this._wasClicked = true;
        e.preventDefault();
    },
    _document_onclick : function(e) {        
        if(this._wasClicked) {
            this._wasClicked = false;
        } else if(this._isOpen) {
            this.hide();
            this.unhover();
        }
    },
    _document_oncontextmenu : function(e) {
        if(this._wasClicked) {
            this._wasClicked = false;
        } else if(this._isOpen) {
            this.hide();
            this.unhover();
        }
    },
    
    _onPopulated : function(sender, eventArgs) {
        /// <summary>
        /// Handler for DynamicPopulate behavior's Populated event
        /// </summary>
        /// <param name="sender" type="Object">
        /// DynamicPopulate behavior
        /// </param>
        /// <param name="eventArgs" type="Sys.EventArgs" mayBeNull="false">
        /// Event args
        /// </param>
        Controtex.Controls.Ajax.BasicDropDownBehavior.callBaseMethod(this, '_onPopulated', [sender, eventArgs]);
        
        if (this._isOpen) {
            this._showPopup();
        }
    },
    
    get_onShow : function() {
        /// <value type="String" mayBeNull="true">
        /// Generic OnShow Animation's JSON definition
        /// </value>
        return this._dropPopupPopupBehavior ? this._dropPopupPopupBehavior.get_onShow() : this._onShowJson;
    },
    set_onShow : function(value) {
        if (this._dropPopupPopupBehavior) {
            this._dropPopupPopupBehavior.set_onShow(value)
        } else {
            this._onShowJson = value;
        }
        this.raisePropertyChanged('onShow');
    },
    get_onShowBehavior : function() {
        /// <value type="Controtex.Controls.Ajax.Animation.GenericAnimationBehavior">
        /// Generic OnShow Animation's behavior
        /// </value>
        return this._dropPopupPopupBehavior ? this._dropPopupPopupBehavior.get_onShowBehavior() : null;
    },
    onShow : function() {
        /// <summary>
        /// Play the OnShow animation
        /// </summary>
        /// <returns />
        if (this._dropPopupPopupBehavior) {
            this._dropPopupPopupBehavior.onShow();
        }
    },
        
    get_onHide : function() {
        /// <value type="String" mayBeNull="true">
        /// Generic OnHide Animation's JSON definition
        /// </value>
        return this._dropPopupPopupBehavior ? this._dropPopupPopupBehavior.get_onHide() : this._onHideJson;
    },
    set_onHide : function(value) {
        if (this._dropPopupPopupBehavior) {
            this._dropPopupPopupBehavior.set_onHide(value)
        } else {
            this._onHideJson = value;
        }
        this.raisePropertyChanged('onHide');
    },
    get_onHideBehavior : function() {
        /// <value type="Controtex.Controls.Ajax.Animation.GenericAnimationBehavior">
        /// Generic OnHide Animation's behavior
        /// </value>
        return this._dropPopupPopupBehavior ? this._dropPopupPopupBehavior.get_onHideBehavior() : null;
    },
    onHide : function() {
        /// <summary>
        /// Play the OnHide animation
        /// </summary>
        /// <returns />
        if (this._dropPopupPopupBehavior) {
            this._dropPopupPopupBehavior.onHide();
        }
    },
    
    get_dropDownControl : function() {
        return this._dropDownControl;
    },
    set_dropDownControl : function(value) {        
        if (this._dropDownControl != value) {
            this._dropDownControl = value;
            this.raisePropertyChanged("BasicDropDownControl");
        }
    },
    
    get_isOver : function() {
        return this._isOver;
    },
    
    get_isOpen : function() {
        return this._isOpen;
    },
    
    add_showing : function(handler) {
        /// <summary>
        /// Add an event handler for the showing event
        /// </summary>
        /// <param name="handler" type="Function" mayBeNull="false">
        /// Event handler
        /// </param>
        /// <returns />
        this.get_events().addHandler('showing', handler);
    },
    remove_showing : function(handler) {
        /// <summary>
        /// Remove an event handler from the showing event
        /// </summary>
        /// <param name="handler" type="Function" mayBeNull="false">
        /// Event handler
        /// </param>
        /// <returns />
        this.get_events().removeHandler('showing', handler);
    },
    raiseShowing : function(eventArgs) {
        /// <summary>
        /// Raise the showing event
        /// </summary>
        /// <param name="eventArgs" type="Sys.CancelEventArgs" mayBeNull="false">
        /// Event arguments for the showing event
        /// </param>
        /// <returns />
        
        var handler = this.get_events().getHandler('showing');
        if (handler) {
            handler(this, eventArgs);
        }
    },

    add_shown : function(handler) {
        /// <summary>
        /// Add an event handler for the shown event
        /// </summary>
        /// <param name="handler" type="Function" mayBeNull="false">
        /// Event handler
        /// </param>
        /// <returns />
        this.get_events().addHandler('shown', handler);
    },
    remove_shown : function(handler) {
        /// <summary>
        /// Remove an event handler from the shown event
        /// </summary>
        /// <param name="handler" type="Function" mayBeNull="false">
        /// Event handler
        /// </param>
        /// <returns />
        this.get_events().removeHandler('shown', handler);
    },
    raiseShown : function(eventArgs) {
        /// <summary>
        /// Raise the shown event
        /// </summary>
        /// <param name="eventArgs" type="Sys.EventArgs" mayBeNull="false">
        /// Event arguments for the shown event
        /// </param>
        /// <returns />
        
        var handler = this.get_events().getHandler('shown');
        if (handler) {
            handler(this, eventArgs);
        }
    },
    
    add_popup : function(handler) {
        /// <summary>
        /// Add an event handler for the popup event
        /// </summary>
        /// <param name="handler" type="Function" mayBeNull="false">
        /// Event handler
        /// </param>
        /// <returns />
        /// <obsolete>Use the shown event instead</obsolete>
        this.get_events().addHandler('popup', handler);
    },
    remove_popup : function(handler) {
        /// <summary>
        /// Remove an event handler from the popup event
        /// </summary>
        /// <param name="handler" type="Function" mayBeNull="false">
        /// Event handler
        /// </param>
        /// <returns />
        /// <obsolete>Use the shown event instead</obsolete>
        this.get_events().removeHandler('popup', handler);
    },
    raisePopup : function(eventArgs) {
        /// <summary>
        /// Raise the popup event
        /// </summary>
        /// <param name="eventArgs" type="Sys.EventArgs" mayBeNull="false">
        /// Event arguments for the popup event
        /// </param>
        /// <returns />
        /// <obsolete>Use the shown event instead</obsolete>
        
        var handler = this.get_events().getHandler('popup');
        if (handler) {
            handler(this, eventArgs);
        }
    },
    
    add_hiding : function(handler) {
        /// <summary>
        /// Add an event handler for the hiding event
        /// </summary>
        /// <param name="handler" type="Function" mayBeNull="false">
        /// Event handler
        /// </param>
        /// <returns />
        this.get_events().addHandler('hiding', handler);
    },
    remove_hiding : function(handler) {
        /// <summary>
        /// Remove an event handler from the hiding event
        /// </summary>
        /// <param name="handler" type="Function" mayBeNull="false">
        /// Event handler
        /// </param>
        /// <returns />
        this.get_events().removeHandler('hiding', handler);
    },
    raiseHiding : function(eventArgs) {
        /// <summary>
        /// Raise the hiding event
        /// </summary>
        /// <param name="eventArgs" type="Sys.CancelEventArgs" mayBeNull="false">
        /// Event arguments for the hiding event
        /// </param>
        /// <returns />
        
        var handler = this.get_events().getHandler('hiding');
        if (handler) {
            handler(this, eventArgs);
        }
    },    
    
    add_hidden : function(handler) {
        /// <summary>
        /// Add an event handler for the hidden event
        /// </summary>
        /// <param name="handler" type="Function" mayBeNull="false">
        /// Event handler
        /// </param>
        /// <returns />
        this.get_events().addHandler('hidden', handler);
    },
    remove_hidden : function(handler) {
        /// <summary>
        /// Remove an event handler from the hidden event
        /// </summary>
        /// <param name="handler" type="Function" mayBeNull="false">
        /// Event handler
        /// </param>
        /// <returns />
        this.get_events().removeHandler('hidden', handler);
    },
    raiseHidden : function(eventArgs) {
        /// <summary>
        /// Raise the hidden event
        /// </summary>
        /// <param name="eventArgs" type="Sys.EventArgs" mayBeNull="false">
        /// Event arguments for the hidden event
        /// </param>
        /// <returns />
        
        var handler = this.get_events().getHandler('hidden');
        if (handler) {
            handler(this, eventArgs);
        }
    },

    add_hoverOver : function(handler) {
        /// <summary>
        /// Add an event handler for the hoverOver event
        /// </summary>
        /// <param name="handler" type="Function" mayBeNull="false">
        /// Event handler
        /// </param>
        /// <returns />
        this.get_events().addHandler('hoverOver', handler);
    },
    remove_hoverOver : function(handler) {
        /// <summary>
        /// Remove an event handler from the hoverOver event
        /// </summary>
        /// <param name="handler" type="Function" mayBeNull="false">
        /// Event handler
        /// </param>
        /// <returns />
        this.get_events().removeHandler('hoverOver', handler);
    },
    raiseHoverOver : function(eventArgs) {
        /// <summary>
        /// Raise the hoverOver event
        /// </summary>
        /// <param name="eventArgs" type="Sys.EventArgs" mayBeNull="false">
        /// Event arguments for the hoverOver event
        /// </param>
        /// <returns />
        
        var handler = this.get_events().getHandler('hoverOver');
        if (handler) {
            handler(this, eventArgs);
        }
    },
    
    add_hoverOut : function(handler) {
        /// <summary>
        /// Add an event handler for the hoverOut event
        /// </summary>
        /// <param name="handler" type="Function" mayBeNull="false">
        /// Event handler
        /// </param>
        /// <returns />
        this.get_events().addHandler('hoverOut', handler);
    },
    remove_hoverOut : function(handler) {
        /// <summary>
        /// Remove an event handler from the hoverOut event
        /// </summary>
        /// <param name="handler" type="Function" mayBeNull="false">
        /// Event handler
        /// </param>
        /// <returns />
        this.get_events().removeHandler('hoverOut', handler);
    },
    raiseHoverOut : function(eventArgs) {
        /// <summary>
        /// Raise the hoverOut event
        /// </summary>
        /// <param name="eventArgs" type="Sys.EventArgs" mayBeNull="false">
        /// Event arguments for the hoverOut event
        /// </param>
        /// <returns />
        
        var handler = this.get_events().getHandler('hoverOut');
        if (handler) {
            handler(this, eventArgs);
        }
    }
}
Controtex.Controls.Ajax.BasicDropDownBehavior.registerClass('Controtex.Controls.Ajax.BasicDropDownBehavior', AjaxControlToolkit.DynamicPopulateBehaviorBase);

if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();