Type.registerNamespace("DL.UI");
var _dropDownCollection = new Array();
// Constructor
DL.UI.DropDownControl = function(element,content,decor,cssDown,cssHover,cssRest) {

    DL.UI.DropDownControl.initializeBase(this, [$get(element)]);

    if(typeof(content) === "undefined"){
        throw Error.argumentNull('DropDownControl: content id is required');
    }
    this.isDown = false;
    this._contentPanel = $get(content);
    if(typeof(decor) != "undefined")
        this._decorPanel = $get(decor);        
    this.set_elementsVisible(false);
    this._cssDown = cssDown;
    this._cssHover = cssHover;
    this._cssRest = cssRest;

    this._clickDelegate = null;
    this._mouseoverDelegate = null;
    this._mouseoutDelegate = null;
    this._mousedownDelegate = null;
    this._mouseupDelegate = null;
    this._keydownDelegate = null;
    this._bodyClickDelegate = null;
    this._bodyResizeDelegate = null; 
    
    this._initDelegate = Function.createDelegate(this,this.initialize);

    Sys.Application.add_init(this._initDelegate);   
    
    _dropDownCollection.push(this);
}
DL.UI.DropDownControl.prototype = {    
    set_clip: function(element,top,left,bottom,right){
        element.style.clip = "rect("+top+","+left+","+bottom+","+right+")"; 
    },

    set_elementopacity : function(element, value) {       
        if (!element) {throw error.argumentnull('element');}
        
        if (element.filters) {
            var filters = element.filters;
            var createfilter = true;
            if (filters.length !== 0) {
                var alphafilter = filters['dximagetransform.microsoft.alpha'];
                if (alphafilter) {
                    createfilter = false;
                    alphafilter.opacity = value * 100;
                }
            }
            if (createfilter) {
                element.style.filter = 'progid:dximagetransform.microsoft.alpha(opacity=' + (value * 100) + ')';
            }
        }
        else {
            element.style.opacity = value;
        }
    },
    
    set_elementsVisible:function(visible){
        var visibility = visible ? "visible" : "hidden";

        if(this._contentPanel)
        {
            this._contentPanel.style.zIndex = "1000";
            this._contentPanel.style.visibility = visibility;
        }
        if(this._decorPanel)
        {
            this._decorPanel.style.zIndex = "999";
            this._decorPanel.style.visibility = visibility;
        }
        //TODO: set_clip
    },
    
        
    //events handlers
    _hidePanel:function(eventElement){
        if(this._contentPanel===null){throw Error.argumentNull('HidePanel: _contentPanel');}
        if(this._cssRest != null)
        {
            var element = this.get_element();
            element.className = this._cssRest;
        }
        
        this.set_elementsVisible(false);
        this.isVisible = false;
    },
        
    _showPanel:function(eventElement){
        if(this._contentPanel===null){throw Error.argumentNull('ShowPanel: _contentPanel');}

        for(var i = 0; i < _dropDownCollection.length; i++)
        {
            if(_dropDownCollection[i] != this)
                _dropDownCollection[i]._hidePanel();
        }
        
        if(this.isVisible)
        {
            this._hidePanel();
            return;
        }

        var element = this.get_element();
        if(this._cssDown != null)
            element.className = this._cssDown;
        var bounds = Sys.UI.DomElement.getBounds(element);
        
        this.set_elementsVisible(true);
        if(this._decorPanel)
        {
            var contentBounds = Sys.UI.DomElement.getBounds(this._contentPanel)
            this._decorPanel.style.height = contentBounds.height + "px";
            this._decorPanel.style.width = contentBounds.width + "px";
        }
                
        eventElement.stopPropagation();
        this.isVisible = true;
    },
    
    _onKeyDown:function(eventElement){
        if(eventElement.charCode === Sys.UI.Key.enter){
            this._showPanel(eventElement);
        }
        else{
            this._hidePanel(eventElement);
        }                           
    },

    _onMouseOver:function(eventElement){
        if(this._cssHover != null && !this.isVisible)
        {
            var element = this.get_element();
            element.className = this._cssHover;
        }
    },
    _onMouseOut:function(eventElement){
        if(this._cssRest != null && !this.isVisible)
        {
            var element = this.get_element();
            element.className = this._cssRest;
        }
    },

    dispose: function() {
        var element = this.get_element();
        $clearHandlers(element);
        if(this._bodyClickDelegate) {
            $removeHandler(document.body,"click",this._bodyClickDelegate);
        }
        if(this._bodyResizeDelegate) {
            $removeHandler(window,"resize",this._bodyResizeDelegate);
        }
        DL.UI.DropDownControl.callBaseMethod(this, 'dispose');
    },

    initialize: function() {
        var element = this.get_element();
        if(this._cssRest != null)
            element.className = this._cssRest;
            
        this._clickDelegate = Function.createDelegate(this,this._showPanel);
        this._mouseoverDelegate = Function.createDelegate(this,this._onMouseOver);
        this._mouseoutDelegate = Function.createDelegate(this,this._onMouseOut);
        this._keydownDelegate = Function.createDelegate(this,this._onKeyDown);
        this._bodyClickDelegate = Function.createDelegate(this,this._hidePanel);
        this._bodyResizeDelegate = Function.createDelegate(this,this._hidePanel);
        
        $addHandlers(element,{keypress:this._keydownDelegate,click:this._clickDelegate,mouseover:this._mouseoverDelegate,mouseout:this._mouseoutDelegate});
        $addHandlers(document.body,{click:this._bodyClickDelegate});
        $addHandlers(window,{resize:this._bodyResizeDelegate});
        DL.UI.DropDownControl.callBaseMethod(this, 'initialize');
    }
}
DL.UI.DropDownControl.registerClass('DL.UI.DropDownControl', Sys.UI.Control);

// Since this script is not loaded by System.Web.Handlers.ScriptResourceHandler
// invoke Sys.Application.notifyScriptLoaded to notify ScriptManager 
// that this is the end of the script.
if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();
