// Register the namespace for the control.
Type.registerNamespace('Controls');

//-----------------------------------------------
//  PanelBarSect class.
//-----------------------------------------------
// Define the control properties.
Controls.PanelBarSect = function(panelBar, title, viewDetails, details){
    this._panelBar = panelBar;
    this._title = title;
    this._viewDetails = viewDetails;
    this._details = details;
    
    this._interval = null;
    this._heightBeg = 0;
    this._heightEnd = 0;
    this._frameRate = 10;
    
    return this;
}
// Create the prototype for the control.
Controls.PanelBarSect.prototype = {
    actDetails : function(e){
        if(this._panelBar == null || this._details == null) return;
        if(this._details.style.display != "block")
            this.showDetails();
        else
            this.hiddenDetails();
    },
    showDetails : function(){
        //  Hiddens current section.
        if(this._panelBar._currSect != null && this._panelBar._currSect != this)
            this._panelBar._currSect.hiddenDetails();

        this._panelBar._currSect = this;
            
        //  Hiddens view details.
        if(this._viewDetails != null)
            this._viewDetails.style.visibility = "hidden";
        
        //  Shows details.
        this._heightBeg = 0;
        this._details.style.overflow = "hidden";
        this._details.style.display = "block";
        this._heightEnd = this._details.offsetHeight;
        this._details.style.height = "1px";
        this._frameRate = Math.floor(this._heightEnd / this._panelBar._speed);
        
        this.stop();
        var currSect = this;
        this._interval = setInterval(function(){
                if (currSect._heightEnd <= currSect._heightBeg){
                    currSect._details.style.overflow = "";
                    currSect._details.style.height = "";
                    currSect.stop();
                }else{
                    currSect._heightBeg += currSect._frameRate;
                    currSect._details.style.height = currSect._heightBeg + "px";
                }
            }, 10);        
    },
    hiddenDetails : function(){
        //  Hiddens details.
        this._details.style.overflow = "hidden";
        this._heightBeg = this._details.offsetHeight;
        this._heightEnd = 0;
        this._frameRate = Math.floor(this._heightBeg / this._panelBar._speed);
                
        this.stop();
        var currSect = this;
        this._interval = setInterval(function(){
                if (currSect._heightBeg <= currSect._heightEnd){
                    currSect._details.style.display = "none";
                    currSect._details.style.overflow = "";
                    currSect._details.style.height = "";                    
                    currSect.stop();
                    
                    //  Shows view details.
                    if(currSect._viewDetails != null)
                        currSect._viewDetails.style.visibility = "visible";
                }else{
                    currSect._heightBeg -= currSect._frameRate;
                    currSect._details.style.height = (0 < currSect._heightBeg ? currSect._heightBeg + "px" : "1px");
                }
            }, 10);
    },
    stop : function(){
        if(this._interval != null){
            clearInterval(this._interval);
            this._interval = null;
        }
    }
}

//-----------------------------------------------
//  PanelBar class.
//-----------------------------------------------
// Define the control properties.
Controls.PanelBar = function(element) { 
    Controls.PanelBar.initializeBase(this, [element]);

    //  CssClasses.
    this._speed = 0;
    this._cssSection = "";
    this._cssTitle = "";
    this._cssView = "";
    this._cssDetails = "";    
    
    //  Sections.
    this._sections = [];
    this._currSect = null;    
    
    return this;
}
// Create the prototype for the control.
Controls.PanelBar.prototype = {
    initialize : function() {
        Controls.PanelBar.callBaseMethod(this, 'initialize');        
        if(this._speed === 0 || this._cssSection === "" || this._cssDetails == "") return;

        //  Gets panelBar's sections.
        var divs = this.get_element().getElementsByTagName("DIV");
        for(i = 0; i < divs.length; ++i){
            if(Sys.UI.DomElement.containsCssClass(divs[i], this._cssSection)){
                //  Looks for title, view details, details.
                var title = null, view = null, details = null;
                
                var childs = divs[i].getElementsByTagName("*");
                for(child in childs){
                    if(childs[child].className === undefined) continue;
                    //  Title.
                    if(this._cssTitle !== "" && Sys.UI.DomElement.containsCssClass(childs[child], this._cssTitle))
                        title = childs[child];
                    //  View details.
                    if(this._cssView !== "" && Sys.UI.DomElement.containsCssClass(childs[child], this._cssView))
                        view = childs[child];
                    //  Details.
                    if(Sys.UI.DomElement.containsCssClass(childs[child], this._cssDetails))
                        details = childs[child];                    
                }
                
                if(details != null){
                    var pbSect = new Controls.PanelBarSect(this, title, view, details);
                    this._sections.push(pbSect);
                    
                    //  Adds title handler.
                    if(title != null) $addHandler(title, "click", Function.createDelegate(pbSect, pbSect.actDetails));
                    //  Adds view details handler.
                    if(view != null) $addHandler(view, "click", Function.createDelegate(pbSect, pbSect.actDetails));                    
                }
            }
        }
    },    
    dispose : function() {
        $clearHandlers(this.get_element());        
        Controls.PanelBar.callBaseMethod(this, 'dispose');
        
        //  Childs dispose.
        for(i = 0; i < this._sections.length; ++i){
            if(this._sections[i]._title != null)
                $clearHandlers(this._sections[i]._title);
            if(this._sections[i]._viewDetails != null)
                $clearHandlers(this._sections[i]._viewDetails);
        }
    },
    // Properties.
    get_speed : function(){
        return this._speed;
    },
    set_speed : function(value){
        if (this._speed !== value) {
            this._speed = value;
            this.raisePropertyChanged('speed');
        }
    },    
    get_cssSection : function(){
        return this._cssSection;
    },
    set_cssSection : function(value){
        if (this._cssSection !== value) {
            this._cssSection = value;
            this.raisePropertyChanged('cssSection');
        }
    },
    get_cssTitle : function(){
        return this._cssTitle;
    },
    set_cssTitle : function(value){
        if (this._cssTitle !== value) {
            this._cssTitle = value;
            this.raisePropertyChanged('cssTitle');
        }
    },    
    get_cssView : function(){
        return this._cssView;
    },
    set_cssView : function(value){
        if (this._cssView !== value) {
            this._cssView = value;
            this.raisePropertyChanged('cssView');
        }
    },
    get_cssDetails : function(){
        return this._cssDetails;
    },
    set_cssDetails : function(value){
        if (this._cssDetails !== value) {
            this._cssDetails = value;
            this.raisePropertyChanged('cssDetails');
        }
    }
}

// Register the class as a type that inherits from Sys.UI.Control.
Controls.PanelBar.registerClass('Controls.PanelBar', Sys.UI.Control);

// Optional descriptor for JSON serialization.
Controls.PanelBar.descriptor = {
    properties: [   {name: 'speed', type: Number},
                    {name: 'cssSection', type: String},
                    {name: 'cssTitle', type: String},
                    {name: 'cssView', type: String},
                    {name: 'cssDetails', type: String}]
}

// 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();