/*
 * @maxatwork: Переключалка вида - список/метро/карта
 */

if (!AtmsOffices) 
    var AtmsOffices = function()
    {
        
    }
    
AtmsOffices.Tabs = function(selectedClassName, hiddenClassName)
{
    this._tabs = [];
    this.HiddenClassName = hiddenClassName;
    this.SelectedClassName = selectedClassName;
    this._handlerOnClick = [];
}

AtmsOffices.Tabs.prototype = {
    AddTab: function( tabHandleSelector, tabBodySelector )
    {
        var tabId = this._tabs.length;
        
        var tabHandles = jQuery(tabHandleSelector);
        var tabBodies = jQuery(tabBodySelector);
        
        this._tabs[tabId] = {
            id : tabId,
            tabHandle : (tabHandles),
            tabBody : (tabBodies),
            clickHandlers : [],
            OnClick : function ( handler ) {
                this.clickHandlers[this.clickHandlers.length] = handler;
            }
        }
        
        var _this = this;
        
        this._tabs[tabId].tabHandle.click( function () { _this.TabClick( tabId ); } );
        
        return this._tabs[tabId];
    },
    
    TabClick : function ( tabId )
    {
        for( var i = 0; i < this._tabs.length; i++)
        {
            if ( i != tabId ) 
            {
                if (this._tabs[i].tabHandle) this._tabs[i].tabHandle.removeClass(this.SelectedClassName);
                //if (this._tabs[i].tabBody) this._tabs[i].tabBody.addClass(this.HiddenClassName);
                
                // Хак, но по другому в IE ничего работать не хочет =/
                if (this._tabs[i].tabBody) jQuery(this._tabs[i].tabBody).css('display', 'none');
            }
        }
        
        if (this._tabs[tabId].tabHandle) this._tabs[tabId].tabHandle.addClass(this.SelectedClassName);
        //if (this._tabs[tabId].tabBody) this._tabs[tabId].tabBody.removeClass(this.HiddenClassName);
        
        // Хак, но по другому в IE ничего работать не хочет =/
        if (this._tabs[tabId].tabBody) jQuery(this._tabs[tabId].tabBody).css( 'display', 'block');
        
        for( var i = 0; i < this._tabs[tabId].clickHandlers.length; i++)
            this._tabs[tabId].clickHandlers[i]( this._tabs[tabId] );
    }
}