﻿//<![CDATA[
//var OnTabMouseOverCallBack = function( e, tabCtrl, tab ) {
//    return true;
//};
//var OnTabMouseOutCallBack = function( e, tabCtrl, tab ) {
//    return true;
//};
//var OnTabSelectionChangedCallBack = function( e, tabCtrl, oldTab, newTab ) {
//    return true;
//};
    if( typeof( window.$ ) == "undefined" ){
        window.$ = function(){
            var elements = [];
            for (var i = 0; i < arguments.length; i++) {
                var element = arguments[i];
                if (typeof element == 'string') {
                    element = exId(element);
                }
                if (arguments.length == 1) { return element; }
                elements.push(element);
            }
            return elements;
        }
    }
    if( typeof( Function.prototype.bind ) == "undefined" ){
        Function.prototype.bind = function( o ) {
            var method = this
                ,   temp = function() { return method.apply( o, arguments ); };
            return temp;
        };
    };
        
    TabCtrl = {
        Tabs                            : null
    ,   OnTabMouseOverCallBack          : null
    ,   OnTabMouseOutCallBack           : null
    ,   OnTabSelectionChangedCallBack   : null
    ,   selectedIndex                   : -1
    ,   selectedName                    : ""
    ,   selectedTab                     : null
    
    ,   Create  : function( params ){
            this.Tabs = new Array();
            if( params ){
                this.OnTabMouseOverCallBack = 
                    typeof(params["OnTabMouseOverCallBack"]) == "undefined" 
                    ? null : params["OnTabMouseOverCallBack"];
                this.OnTabMouseOutCallBack = 
                    typeof(params["OnTabMouseOutCallBack"]) == "undefined" 
                    ? null : params["OnTabMouseOutCallBack"];
                this.OnTabSelectionChangedCallBack = 
                    typeof(params["OnTabSelectionChangedCallBack"]) == "undefined" 
                    ? null : params["OnTabSelectionChangedCallBack"];
            }
            var selectedTab = Cookie.Read( "selectedTab" );
            if( isNaN( selectedTab ) && selectedTab.length > 0 ) {
                this.selectedName = selectedTab;
            }
            else{
                this.selectedIndex = selectedTab ? selectedTab : 0;
            } 
            return this;
        }          
    
    ,   Destroy:function(){
            this.RemoveAll();
            this.OnTabMouseOverCallBack = null;
            this.OnTabMouseOutCallBack  = null;
            this.OnTabSelectionChangedCallBack = null;
        }
    ,   AddTab:function( params ){
            var tab = new Tab({
                             "tabButton":params["tabButton"]          
                        ,    "tabPanel":params["tabPanel"]          
                        ,    "index":this.Tabs.length                  
                        ,    "name":params["name"]                
                        ,    "tabButtonSelectedClassName":params["tabButtonSelectedClassName"]                   
                        ,    "tabButtonMouseOverClassName":params["tabButtonMouseOverClassName"]                        
                        ,    "tabButtonEnabledClassName":params["tabButtonEnabledClassName"]                       
                        ,    "tabButtonDisabledClassName":params["tabButtonDisabledClassName"]                       
                        ,    "tabPanelVisibleClassName":params["tabPanelVisibleClassName"]                       
                        ,    "tabPanelHiddenClassName":params["tabPanelHiddenClassName"]                        
                        ,    "selected":params["selected"]                       
                        ,    "disabled":params["disabled"]                        
                        ,    "OnMouseOverCallBack":this.OnTabMouseOverCallBack && this.OnMouseOver ? this.OnMouseOver.bind( this ) : null
                        ,    "OnMouseOutCallBack":this.OnTabMouseOutCallBack && this.OnMouseOut ? this.OnMouseOut.bind( this ) : null
                        ,    "OnClickCallBack":this.OnClick ? this.OnClick.bind( this ) : null
                        });
            
            this.Tabs.push( tab );
            
            if( (this.selectedName == tab.name)
            ||  (this.Tabs.length == this.selectedIndex
            ||   tab.selected) ){
                this.SelectTab( tab );
                this.selectedTab = tab;
            }            
            
            return tab;
        }
        
    ,   RemoveAt:function( index ){
            var count = this.Tabs.length;
            if( index < count ){
                var tab = this.Tabs[ index ];
                if( tab ){
                    tab.CleanUp();
                    tab = null;
                    return true;
                }
            }
            return false;
        }
            
    ,   RemoveTab:function( button, panel, name ){    
            var count = this.Tabs.length;
            for(var i = 0; i < count; i++ ){
                var tab = this.Tabs[ i ];
                if( tab ){
                    if( (tab.tabButton == button || ( null == button ) )
                    &&  (tab.tabPanel == panel || ( null == panel ) )
                    &&  (tab.name == name || ( null == name ) ) ){
                    
                        tab.CleanUp();
                        tab = null;
                        Tabs.splice( i, 1 );
                        return true;
                    }
                }
            }
            return false;
        }
        
    ,   RemoveAll:function() {
            var count = this.Tabs.length;
            for(var i = 0; i < count; i++ ){
                var tab = this.Tabs.pop();
                if( tab ){
                    tab.Destroy();
                    tab = null;
                }
            }
        }
        
    ,   SelectTab:function( selection ){   
            var selectedTab = null;
            if( selection ){
                if( !isNaN( selection ) && this.Tabs.length < selection ){
                    selectedTab = this.Tabs[selection];
                }else if( typeof (selection) == "string" ){
                    selectedTab = $(selection);
                }else {
                    selectedTab = selection;
                }            
            }
            if( selectedTab ){
                this.selectedName = selectedTab.name;
                this.OnClick( null, selectedTab );
                this.selectedTab = selectedTab;
            }
        }
         
    ,   OnMouseOver:function(){
            if( this.OnTabMouseOverCallBack ){
                return this.OnTabMouseOverCallBack.apply( this, arguments );
            }
            return false;
        }
    ,   OnMouseOut:function(){
            if( this.OnTabMouseOutCallBack ){
                return this.OnTabMouseOutCallBack.apply( this, arguments );
            }
            return false;
        }
    ,   OnClick:function( e, newTab ){
            var previousIndex = this.selectedTab ? this.selectedTab.index : -1;
            var tmp = false;
            if( !newTab.disabled ){
                if( this.OnTabSelectionChangedCallBack ){
                    tmp = this.OnTabSelectionChangedCallBack.call( this, e, this, this.selectedTab, newTab );
                }
                else{
                    if( this.selectedTab ) this.selectedTab.Deselect();
                    tmp = newTab.Select();
                    if( tmp ){
                        this.selectedTab = newTab;
                        Cookie.Create( "selectedTab", newTab.name && newTab.name.length > 0 ? newTab.name : newTab.index, 1 );
                    }
                }
            }
            if( e )e.cancelBubbel = !tmp;
            return tmp;
        }
};

Tab = function( params ) {
            this.tabButton = null;
            this.tabPanel = null;
            this.index = -1;
            this.name = "";
            this.tabButtonClassName = "";
            this.tabButtonSelectedClassName = "";
            this.tabButtonMouseOverClassName = "";
            this.tabButtonEnabledClassName = "";
            this.tabButtonDisabledClassName = "";
            this.tabPanelClassName = "";
            this.tabPanelVisibleClassName = "";
            this.tabPanelHiddenClassName = "";
            this.selected = false;
            this.disabled = false;
            this.OnMouseOverCallBack = null;
            this.OnMouseOutCallBack = null;
            this.OnClickCallBack = null;
            this.Create( params );   
};
Tab.prototype.Create = function( params ){
    if( params ){
        var tmp = typeof(params["tabButton"]) == "undefined" ? null : params["tabButton"];
        this.tabButton = ( typeof(tmp) == "string" ) ? $( tmp ) : tmp;  
        tmp = typeof(params["tabPanel"]) == "undefined" ? null : params["tabPanel"];               
        this.tabPanel = ( typeof(tmp) == "string" ) ? $( tmp ) : tmp; 
        tmp = typeof(params["index"]) == "undefined" ? 0 : params["index"];                    
        this.index = tmp;   
        tmp = typeof(params["name"]) == "undefined" ? "" : params["name"];                    
        this.name = tmp;
        tmp = typeof(params["tabButtonSelectedClassName"]) == "undefined" ? "" : params["tabButtonSelectedClassName"];                        
        this.tabButtonSelectedClassName = tmp;  
        tmp = typeof(params["tabButtonMouseOverClassName"]) == "undefined" ? "" : params["tabButtonMouseOverClassName"];                        
        this.tabButtonMouseOverClassName = tmp; 
        tmp = typeof(params["tabButtonEnabledClassName"]) == "undefined" ? "" : params["tabButtonEnabledClassName"];                        
        this.tabButtonEnabledClassName = tmp; 
        tmp = typeof(params["tabButtonDisabledClassName"]) == "undefined" ? "" : params["tabButtonDisabledClassName"];                        
        this.tabButtonDisabledClassName = tmp;  
        tmp = typeof(params["tabPanelVisibleClassName"]) == "undefined" ? "" : params["tabPanelVisibleClassName"];                        
        this.tabPanelVisibleClassName = tmp;    
        tmp = typeof(params["tabPanelHiddenClassName"]) == "undefined" ? "" : params["tabPanelHiddenClassName"];                        
        this.tabPanelHiddenClassName = tmp;     
        tmp = typeof(params["selected"]) == "undefined" ? false : params["selected"];                        
        this.selected = tmp;                    
        tmp = typeof(params["disabled"]) == "undefined" ? false : params["disabled"];                        
        this.disabled = tmp;                    
        tmp = typeof(params["OnMouseOverCallBack"]) == "undefined" ? null : params["OnMouseOverCallBack"];                        
        this.OnMouseOverCallBack = tmp;        
        tmp = typeof(params["OnMouseOutCallBack"]) == "undefined" ? null : params["OnMouseOutCallBack"];                        
        this.OnMouseOutCallBack  = tmp;         
        tmp = typeof(params["OnClickCallBack"]) == "undefined" ? null : params["OnClickCallBack"];                        
        this.OnClickCallBack  = tmp;
        
        if( this.tabButton && this.tabButton.className ){
            this.tabButtonClassName = trim( this.tabButton.className.
                                                replace( this.tabButtonSelectedClassName, "" ).
                                                replace( this.tabButtonMouseOverClassName, "" ).
                                                replace( this.tabButtonEnabledClassName, "" ).
                                                replace( this.tabButtonDisabledClassName, "" ) );
        }
        if( this.tabPanel && this.tabPanel.className ){
            this.tabPanelClassName = trim( this.tabPanel.className.
                                                replace( this.tabPanelVisibleClassName, "" ).
                                                replace( this.tabPanelHiddenClassName, "" ) );
        }              
    }
    
    if( this.tabButton ){
        this.tabButton.onmouseover  = this.OnMouseOverCallBack && this.OnMouseOver ? this.OnMouseOver.bind( this ) : null;
        this.tabButton.onmouseout = this.OnMouseOutCallBack && this.OnMouseOut ? this.OnMouseOut.bind( this ) : null;
        this.tabButton.onclick = this.OnClickCallBack && this.OnClick ? this.OnClick.bind( this ) : null;
    }
    return this; 
};
Tab.prototype.Destroy = function(){
    if( this.tabButton ){
        this.tabButton.onmouseover  = null;
        this.tabButton.onmouseout = null;
        this.tabButton.onclick = null;
    }
    
    this.OnMouseOverCallBack = null;
    this.OnMouseOutCallBack = null;
    this.OnClickCallBack = null;
};
Tab.prototype.Select = function(){
    
    this.selected = true;
    this.tabButton.className = this.tabButtonClassName;
    this.tabButton.className += " " + (this.disabled ? this.tabButtonDisabledClassName : this.tabButtonEnabledClassName );
    this.tabButton.className += " " + this.tabButtonSelectedClassName;
    
    this.tabPanel.className = this.tabPanelClassName;
    this.tabPanel.className += " " + this.tabPanelVisibleClassName;
    
    return true;
};      
Tab.prototype.Deselect = function(){
    
    this.selected = false;
    this.tabButton.className = this.tabButtonClassName;
    this.tabButton.className += " " + (this.disabled ? this.tabButtonDisabledClassName : this.tabButtonEnabledClassName );
    
    this.tabPanel.className = this.tabPanelClassName;
    this.tabPanel.className += " " + this.tabPanelHiddenClassName;
    
    return true;
};
Tab.prototype.OnMouseOver = function( e ){
            e = e ? e : window.event;
            e.cancelBubble = this.disabled; 
            if( !this.disabled && this.tabButton ){
                if( this.OnMouseOverCallBack ){
                    var rc = this.OnMouseOverCallBack( e, this );
                    e.cancelBubble = !rc;
                    return rc;
                }else {
                    return false;
                }
            }
        }
        
Tab.prototype.OnMouseOut = function( e ){
    e = e ? e : window.event;
    e.cancelBubble = this.disabled; 
    if( !this.disabled && this.tabButton ){
        if( this.OnMouseOutCallBack ){
            var rc = this.OnMouseOutCallBack( e, this );
            e.cancelBubble = !rc;
            return rc;
        }else {
            return false;
        }
    }
};
Tab.prototype.OnClick = function( e ){
    e = e ? e : window.event;
    e.cancelBubbel = this.disabled; 
    if( !this.disabled && this.tabButton ){
        if( this.OnClickCallBack ){
            var rc = this.OnClickCallBack( e, this );
            e.cancelBubble = !rc;
            return rc;
        }else {
            return false;
        }
    }
};    
 
//]]>    

