//
// TabBar
//
FVWebComponent.TabBar = function(id, config) {
    this.listTabBarItem = null;
    this.itemSelected = null;
    this.init(id, config);
}

FVWebComponent.TabBar.prototype = new FVWebComponent.ComponentBase;

FVWebComponent.TabBar.prototype.init = function (id, config) {
    FVWebComponent.ComponentBase.prototype.init.call(this, id);
    
    // Find all TabItem elements in the TabBar.
    var listItemElement = YAHOO.util.Dom.getElementsByClassName('fvTabBarItem', 'div', this.containerObj);
    
    // Look for all the tab bar items inside.
    this.listTabBarItem = new Array();
    
    for (var i = 0; i < listItemElement.length ; i++) {
        var itemElement = listItemElement[i];
        
        // Create a new component for each tab bar item.
        //var newItemComponent;
        //newItemComponent = new FVWebComponent.TabBarItem();
        //newItemComponent.init(itemElement.id);
        
        // Set up handlers for tab bar items.
        
        YAHOO.util.Event.addListener(itemElement, "click", this.handleTabBarItemEvent, {target:itemElement,tabbar:this});
        YAHOO.util.Event.addListener(itemElement, "mouseover", this.handleTabBarItemEvent, {target:itemElement,tabbar:this});
        YAHOO.util.Event.addListener(itemElement, "mouseout", this.handleTabBarItemEvent, {target:itemElement,tabbar:this});
    }
    
    FVWebComponent.addEventHandler(this, this.handleFVWebComponentEvent);
}

FVWebComponent.TabBar.prototype.handleFVWebComponentEvent = function (type, args, me) {
    //alert("TabBar (" + me.id + ") is handling event: " + args[0].toString());
}

FVWebComponent.TabBar.prototype.handleTabBarItemEvent = function (evt, obj) {

    var tabbar = obj.tabbar;
    var tabbaritem = obj.target;

    if (FVWebComponent.debugLevel >= 10) {
        alert("TabBar (" + tabbar.id + ") is handling event (" + evt.type + ") from TabBarItem (" + tabbaritem.id + ")");
    }
    
    if (evt.type == "click") {

        
        // Select new item.
        tabbar.selectTab(tabbaritem.id);
        
        // Fire the select event.
        FVWebComponent.fireEvent("TABBAR_SELECT", tabbar.id, tabbaritem.id, null);
        
    } else if (evt.type == "mouseover") {
        if (tabbaritem != tabbar.itemSelected) {
            if (! YAHOO.util.Dom.hasClass(tabbaritem, "fvTabBarItemHover")) {
                YAHOO.util.Dom.addClass(tabbaritem, "fvTabBarItemHover");
            }
        }
        
    } else if (evt.type == "mouseout") {
        if (tabbaritem != tabbar.itemSelected) {
            if (YAHOO.util.Dom.hasClass(tabbaritem, "fvTabBarItemHover")) {
                YAHOO.util.Dom.removeClass(tabbaritem, "fvTabBarItemHover");
            }
        }    
    }
    
}

FVWebComponent.TabBar.prototype.selectTab = function (tabItemId) {
    var tabbarItem = document.getElementById(tabItemId);
    if (tabbarItem != null) {
        // Unselect old item.
        if (this.itemSelected != null) {
            if (YAHOO.util.Dom.hasClass(this.itemSelected, "fvTabBarItemSelected")) {
                YAHOO.util.Dom.removeClass(this.itemSelected, "fvTabBarItemSelected");
            }
        }
            
        this.itemSelected = tabbarItem;
        YAHOO.util.Dom.addClass(this.itemSelected, "fvTabBarItemSelected");
        if (YAHOO.util.Dom.hasClass(this.itemSelected, "fvTabBarItemHover")) {
            YAHOO.util.Dom.removeClass(this.itemSelected, "fvTabBarItemHover");
        }
    }
}