﻿// (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.

Type.registerNamespace('AjaxControlToolkit');

AjaxControlToolkit.DropDownBehavior = function AjaxControlToolkit$DropDownBehavior(element) {
    AjaxControlToolkit.DropDownBehavior.initializeBase(this, [element]);
    this._dropDownControl = null;
    this._dropDownControlID = null;
    this._highlightBorderColor = "#2353B2";
    this._highlightBackgroundColor = "#FFF3DB";
    this._dropArrowBackgroundColor = "#C6E1FF";
    this._dropArrowImageUrl = null;
    this._dropArrowWidth = "16px";
    this._oldBackgroundColor = null;
    this._dropFrame = null;
    this._dropArrow = null;
    this._dropArrowImage = null;
    this._dropWrapper = null;
    this._isOpen = false;
    this._isOver = false;
    this._wasClicked = null;
    this._dropWrapperHoverBehavior = null;
    this._dropFramePopupBehavior = null;
    this._dropPopupPopupBehavior = null;
    this._cacheDynamicResults = true;
    this._dynamicContextKey = null;
    this._dynamicServicePath = null;
    this._dynamicServiceMethod = null;
    this._dynamicPopulateBehavior = null;
    this._dynamicPopulated = false;
    this._dropWrapper_onhover$delegate = Function.createDelegate(this, this._dropWrapper_onhover);
    this._dropWrapper_onunhover$delegate = Function.createDelegate(this, this._dropWrapper_onunhover);
    this._dropWrapper_onclick$delegate = Function.createDelegate(this, this._dropWrapper_onclick);
    this._dropWrapper_oncontext$delegate = Function.createDelegate(this, this._dropWrapper_oncontext);
    this._dropFrame_onclick$delegate = Function.createDelegate(this, this._dropFrame_onclick);
    this._dropFrame_oncontext$delegate = Function.createDelegate(this, this._dropFrame_oncontext);
    this._dropPopup_onclick$delegate = Function.createDelegate(this, this._dropPopup_onclick);
    this._dropPopup_oncontext$delegate = Function.createDelegate(this, this._dropPopup_oncontext);
    this._document_onclick$delegate = Function.createDelegate(this, this._document_onclick);
    this._document_oncontext$delegate = Function.createDelegate(this, this._document_oncontext);
    this._dynamicPopulate_onpopulated$delegate = Function.createDelegate(this, this._dynamicPopulate_onpopulated);
}
AjaxControlToolkit.DropDownBehavior.prototype = {

    add_popup : function(handler) {
        this.get_events().addHandler("popup", handler);
    },
    remove_popup : function(handler) {
        this.get_events().removeHandler("popup", handler);
    },
    raisePopup : function(args) {
        var eh = this.get_events().getHandler("popup");
        if (eh) {
            eh(this, args || Sys.EventArgs.Empty);
        }
    },

    add_populating : function(handler) {
        this.get_events().addHandler("populating", handler);
    },
    remove_populating : function(handler) {
        this.get_events().removeHandler("populating", handler);
    },
    raisePopulating : function() {
        var eh = this.get_events().getHandler("populating");
        if (eh) {
            eh(this, Sys.EventArgs.Empty);
        }
    },

    add_populated : function(handler) {
        this.get_events().addHandler("populated", handler);
    },
    remove_populated : function(handler) {
        this.get_events().removeHandler("populated", handler);
    },
    raisePopulated : function() {
        var eh = this.get_events().getHandler("populated");
        if (eh) {
            eh(this, Sys.EventArgs.Empty);
        }
    },

    get_dropDownControl : function() {
        return this._dropDownControl;
    },
    set_dropDownControl : function(value) {        
        if (this._dropDownControl != value) {
            this._dropDownControl = value;
            this.raisePropertyChanged("dropDownControl");
        }
    },
    
    get_highlightBorderColor : function() {
        return this._highlightBorderColor;
    },
    set_highlightBorderColor : function(value) {

        if (this._highlightBorderColor != value) {
            this._highlightBorderColor = value;
            if(this.get_isInitialized()) {
                this._dropFrame.style.borderColor = value;
            }
            this.raisePropertyChanged("highlightBorderColor");
        }
    },
    
    get_highlightBackgroundColor : function() {
        return this._highlightBackgroundColor; 
    },
    set_highlightBackgroundColor : function(value) {

        if (this._highlightBackgroundColor != value) {
            this._highlightBackgroundColor = value;
            if(this.get_isInitialized() && this._isOpen) {
                this._dropWrapper.style.backgroundColor = value;
            }
            this.raisePropertyChanged("highlightBackgroundColor");
        }
    },
    
    get_dropArrowBackgroundColor : function() {
        return this._dropArrowBackgroundColor;
    },
    set_dropArrowBackgroundColor : function(value) {

        if (this._dropArrowBackgroundColor != value) {
            this._dropArrowBackgroundColor = value;
            if(this.get_isInitialized()) {
                this._dropArrow.style.backgroundColor = value;
            }
            this.raisePropertyChanged("dropArrowBackgroundColor");
        }
    },
    
    get_dropArrowImageUrl : function() {
        return this._dropArrowImageUrl;
    },
    set_dropArrowImageUrl : function(value) {
        if (this._dropArrowImageUrl != value) {
            this._dropArrowImageUrl = value;
            if(this.get_isInitialized()) {
                if (this._dropArrow.className) {
                    this._dropArrow.className = "";
                    this._dropArrowWrapper.style.display = 'block';
                }
                this._dropArrowImage.src = value;
            }
            this.raisePropertyChanged("dropArrowImageUrl");
        }
    },
    
    get_dropArrowWidth : function() {
        return this._dropArrowWidth;
    },
    set_dropArrowWidth : function(value) {

        if (this._dropArrowWidth != value) {
            this._dropArrowWidth = value;
            if(this.get_isInitialized()) {
                this._dropArrow.style.width = value;
            }
            this.raisePropertyChanged("dropArrowWidth");
        }
    },
    
    get_cacheDynamicResults : function() {
        return this._cacheDynamicResults;
    },
    set_cacheDynamicResults : function(value) {
        if (this._cacheDynamicResults != value) {
            this._cacheDynamicResults = value;
            this.raisePropertyChanged("cacheDynamicResults");
        }
    },

    get_dynamicContextKey : function() {
        return this._dynamicContextKey;
    },
    set_dynamicContextKey : function(value) {
        if (this._dynamicContextKey != value) {
            this._dynamicContextKey = value;
            this.raisePropertyChanged('dynamicContextKey');
        }
    },

    get_dynamicServicePath : function() {
        return this._dynamicServicePath;
    },
    set_dynamicServicePath : function(value) {
        if (this._dynamicServicePath != value) {
            this._dynamicServicePath = value;
            this.raisePropertyChanged('dynamicServicePath');
        }
    },

    get_dynamicServiceMethod : function() {
        return this._dynamicServiceMethod;
    },
    set_dynamicServiceMethod : function(value) {
        if (this._dynamicServiceMethod != value) {
            this._dynamicServiceMethod = value;
            this.raisePropertyChanged('dynamicServiceMethod');
        }
    },
    
    get_isOver : function() {
        return this._isOver;
    },
    
    get_isOpen : function() {
        return this._isOpen;
    },
    
    initialize : function() {
        AjaxControlToolkit.DropDownBehavior.callBaseMethod(this, 'initialize');        
        var elt = this.get_element();        
        //
        // dropDownControl
        //
        var dropDownControl = this._dropDownControl;
        if (dropDownControl == null) {
            dropDownControl = this._dropDownControl = document.createElement("div");
            Sys.UI.DomElement.addCssClass(dropDownControl, "ajax__dropdown_panel");
            Sys.UI.DomElement.setVisible(dropDownControl, false);
            elt.parentNode.appendChild(dropDownControl);
        }
        $addHandlers(dropDownControl, {
            click:this._dropPopup_onclick$delegate,
            contextmenu:this._dropPopup_oncontext$delegate
        });        
        //
        // dropFrame
        //
        var dropFrame = this._dropFrame = document.createElement('div');
        Sys.UI.DomElement.addCssClass(dropFrame, "ajax__dropdown_frame");
        Sys.UI.DomElement.setVisible(dropFrame, false);
        $common.setStyle(dropFrame, {
            borderColor:this._highlightBorderColor
        });
        $addHandlers(dropFrame, {
            click:this._dropFrame_onclick$delegate,
            contextmenu:this._dropFrame_oncontext$delegate
        });
        elt.parentNode.appendChild(dropFrame);
        //
        // dropArrow
        //
        var dropArrow = this._dropArrow = document.createElement('div');
        Sys.UI.DomElement.addCssClass(dropArrow, "ajax__dropdown_arrow");
        $common.setStyle(dropArrow, {
            width:this._dropArrowWidth,
            backgroundColor:this._dropArrowBackgroundColor
        });
        if (!this._dropArrowImageUrl) {
            Sys.UI.DomElement.addCssClass(dropArrow, "ajax__dropdown_arrow_image");
        }
        dropFrame.appendChild(dropArrow);
        //
        // dropArrowWrapper
        //
        var dropArrowWrapper = document.createElement('div');
        Sys.UI.DomElement.addCssClass(dropArrowWrapper, "ajax__dropdown_arrow_wrapper");
        if (!this._dropArrowImageUrl) {
            Sys.UI.DomElement.setVisible(dropArrowWrapper, false);
        }
        dropArrow.appendChild(dropArrowWrapper);
        //
        // dropArrowImage
        //
        var dropArrowImage = this._dropArrowImage = document.createElement('img');
        dropArrowImage.src = this._dropArrowImageUrl;
        dropArrowWrapper.appendChild(dropArrowImage);
        //
        // dropWrapper
        //
        var dropWrapper = this._dropWrapper = document.createElement('span');
        $common.setStyle(dropWrapper, {
            cursor:"default"
        });
        $addHandlers(dropWrapper, {
            click:this._dropWrapper_onclick$delegate,
            contextmenu:this._dropWrapper_oncontext$delegate
        });
        elt.parentNode.replaceChild(dropWrapper, elt);
        dropWrapper.appendChild(elt);
        //
        // behaviors
        //
        var dropFramePopupBehavior = this._dropFramePopupBehavior = $create(AjaxControlToolkit.PopupBehavior, { positioningMode : AjaxControlToolkit.PositioningMode.Absolute, parentElement : elt }, { }, null, dropFrame);
        var dropPopupPopupBehavior = this._dropPopupPopupBehavior = $create(AjaxControlToolkit.PopupBehavior, { positioningMode : AjaxControlToolkit.PositioningMode.BottomRight, parentElement : elt, y : -1 }, { }, null, dropDownControl);
        var dropWrapperHoverBehavior = this._dropWrapperHoverBehavior = $create(AjaxControlToolkit.HoverBehavior, { hoverElement : this._dropFrame }, { hover : this._dropWrapper_onhover$delegate, unhover : this._dropWrapper_onunhover$delegate }, null, dropWrapper);
        //
        // wire events
        //
        $addHandlers(document, {
            click:this._document_onclick$delegate,
            contextmenu:this._document_oncontext$delegate
        });
    },    
    
    dispose : function() {
        var elt = this.get_element();
        if (this._isOpen) {
            this.hide();
            this.unhover();
            this._isOpen = false;
        }
        $removeHandler(document, "click", this._document_onclick$delegate);
        $removeHandler(document, "contextmenu", this._document_oncontext$delegate);
        if (this._dynamicPopulateBehavior) {
            this._dynamicPopulateBehavior.dispose();
            this._dynamicPopulateBehavior = null;
        }
        if (this._dropPopupPopupBehavior) {
            this._dropPopupPopupBehavior.dispose();
            this._dropPopupPopupBehavior = null;
        }
        if (this._dropFramePopupBehavior) {
            this._dropFramePopupBehavior.dispose();
            this._dropFramePopupBehavior = null;
        }
        if (this._dropWrapperHoverBehavior) {
            this._dropWrapperHoverBehavior.dispose();
            this._dropWrapperHoverBehavior = null;
        }
        if (this._dropFrame) {
            $removeHandler(this._dropFrame, "click", this._dropFrame_onclick$delegate);
            $removeHandler(this._dropFrame, "contextmenu", this._dropFrame_oncontext$delegate);
            this._dropFrame.parentNode.removeChild(this._dropFrame);
            this._dropFrame = null;
            this._dropArrow = null;
            this._dropArrowImage = null;
        }
        if (this._dropWrapper) {
            $removeHandler(this._dropWrapper, "click", this._dropWrapper_onclick$delegate);
            $removeHandler(this._dropWrapper, "contextmenu", this._dropWrapper_oncontext$delegate);
            this._dropWrapper.removeChild(elt);
            this._dropWrapper.parentNode.replaceChild(elt, this._dropWrapper);        
            this._dropWrapper = null;
        }
        if (this._dropDownControl) {
            $removeHandler(this._dropDownControl, "click", this._dropPopup_onclick$delegate);
            $removeHandler(this._dropDownControl, "contextmenu", this._dropPopup_oncontext$delegate);
            this._dropDownControl = null;
        }
        AjaxControlToolkit.DropDownBehavior.callBaseMethod(this, 'dispose');
    },    

    hover : function() {
        var elt = this.get_element();
        if (!this._isOver) {
            this._isOver = true;
            var size = AjaxControlToolkit.CommonToolkitScripts.getSize(elt);
            AjaxControlToolkit.CommonToolkitScripts.setSize(this._dropFrame, size);            
            this._dropFramePopupBehavior.show();
            if (!this._oldBackgroundColor) {            
                this._oldBackgroundColor = AjaxControlToolkit.CommonToolkitScripts.getCurrentStyle(elt, 'backgroundColor');
            }
            elt.style.backgroundColor = this._highlightBackgroundColor;
        }
    },
    
    unhover : function() {
        var elt = this.get_element();
        if (this._isOver || !this._isOpen) {
            this._isOver = false;
            if (!this._isOpen) {        
                this._dropFramePopupBehavior.hide();
                if (this._oldBackgroundColor) {
                    elt.style.backgroundColor = this._oldBackgroundColor;
                    this._oldBackgroundColor = null;
                } else {
                    elt.style.backgroundColor = "transparent";
                }
            }
        }
    },
    
    show : function() {
        if (!this._isOpen) {
            this.hover();
            var args = new Sys.CancelEventArgs();
            this.raisePopup(args);
            if (!args.get_cancel()) {
                this._isOpen = true;
                this.populate();
                if (!this._dynamicPopulateBehavior || (this._dynamicPopulated && this._cacheDynamicResults)) {
                    this._dropPopupPopupBehavior.show();
                }
            }
        }
    },
    
    hide : function() {
        if (this._isOpen) {
            this._isOpen = false;
            this._dropPopupPopupBehavior.hide();
        }
    },

    populate : function(contextKeyOverride) {
        if (this._dynamicPopulated && this._cacheDynamicResults) {
            return;
        }
        if (this._dynamicPopulateBehavior && (this._dynamicPopulateBehavior.get_element() != this.get_element())) {
            this._dynamicPopulateBehavior.dispose();
            this._dynamicPopulateBehavior = null;
        }
        if (!this._dynamicPopulateBehavior && this._dynamicServiceMethod) {
            this._dynamicPopulateBehavior = $create(AjaxControlToolkit.DynamicPopulateBehavior,{"ContextKey":this._dynamicContextKey,"ServicePath":this._dynamicServicePath,"ServiceMethod":this._dynamicServiceMethod}, {"populated":this._dynamicPopulate_onpopulated$delegate}, null, this._dropDownControl);
        }
        if(this._dynamicPopulateBehavior) {
            this.raisePopulating();
            this._dynamicPopulateBehavior.populate(contextKeyOverride ? contextKeyOverride : this._dynamicContextKey);
        }
    },
    
    _dropWrapper_onhover : function(sender, e) {
        this.hover();
    },
    _dropWrapper_onunhover : function(sender, e) {
        this.unhover();
    },
    _dropWrapper_onclick : function(e) {
        if(e.target.tagName != "A") {
            if(!this._isOpen) {
                this.show();
            } else {
                this.hide();
            }
            this._wasClicked = true;
        }
    },
    _dropWrapper_oncontext : 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_oncontext : function(e) {
        this._wasClicked = true;
        e.preventDefault();
        this.show();
    },
    _dropPopup_onclick : function(e) {
        //this._wasClicked = true;
    },
    _dropPopup_oncontext : 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_oncontext : function(e) {
        if(this._wasClicked) {
            this._wasClicked = false;
        } else if(this._isOpen) {
            this.hide();
            this.unhover();
        }
    },
    _dynamicPopulate_onpopulated : function(sender, e) {
        this.raisePopulated();
        if (this._isOpen) {
            this._dynamicPopulated = true;
            this._dropPopupPopupBehavior.show();
        }
    }
}
AjaxControlToolkit.DropDownBehavior.registerClass('AjaxControlToolkit.DropDownBehavior', AjaxControlToolkit.BehaviorBase);
if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();