/*
* Tab
* @author Gennadiy Ukhanov
* @version 0.0.1
* @build 1 (22/08/2011 16:20 AM)
*/

(function() {

    var $=jQuery;
    var Tab = (function() {

        /**
         * Constructor
         */
        bfm.ui.Tab = function() {
            this.tabs = new Array();
        }

        bfm.ui.Tab.prototype = {

            /**
             * @public
             * @param {_el} Tab selector
             */
            create : function(_el) {
                
                this.list = $( " > li:has(a[href])", _el );
                this.anchors = this.list.map(function() {
                        return $( "a", this )[ 0 ];
                });

                this.tabs = this.list;
                this.anchors.bind('click', $.proxy(this, '_showTab'));

                this._hideAllTabs(0);

            },

            _hideAllTabs : function(index) {
                $($(this.list).removeClass('b-tabs-nav__item_state_selected'));
                for(var i=0; i<this.anchors.length; i++) {
                    $($(this.anchors[i]).attr('href')).css('display', 'none')
                }
                if(index!=undefined){
                    $($(this.anchors[index]).attr('href')).css('display', 'block')
                    $($(this.list)[index]).addClass('b-tabs-nav__item_state_selected')
                }
            },

            _showTab : function(e) {
                this._hideAllTabs();

                $(e.currentTarget).parent().addClass('b-tabs-nav__item_state_selected')
                $($(e.currentTarget).attr('href')).show();
                
                return false;
            }

           
        }
    })(Tab);
})();
