TabControl = {
    tabs: [],
    currentTab: null,
    
    register: function(tab) {
        tab.deactivate();
        
        /*if (!this.tabs.length)
            tab.activate();
        */  
        if(tab.isdefault)
            tab.activate();
            
        this.tabs.push(tab);    
    },
    
    notify: function(tab) {
        cTab = this.currentTab;
        if (cTab)
            cTab.deactivate();
        
        this.currentTab = tab;
    }
};

Tab = Class.create({
    tab: null,
    tabContent: null,
    controller: null,
    isdefault: false,
    
    initialize: function(tab, tabContent) {
        this.tab = $(tab);
        this.tabContent = $(tabContent);
       
        if (arguments[2] && arguments[2] == true)
            this.isdefault = true;
             
        TabControl.register(this);
        
        Event.observe(this.tab, 'click', this.activate.bindAsEventListener(this));
    
    },
    
    activate: function() {
        TabControl.notify(this);
        this.tab.setStyle({'background-image':'url(/lib/gomiata/tab-on.jpg)'});
        this.tab.removeClassName('tab-off');
        this.tab.addClassName('tab-on');
        this.tabContent.show();
    },
    
    deactivate: function() {
        this.tab.setStyle({'background-image':'url(/lib/gomiata/tab-off.jpg)'});
        this.tab.removeClassName('tab-on')
        this.tab.addClassName('tab-off');
        this.tabContent.hide();
    }
});

document.observe("dom:loaded", 
    function() {
        if ($('additional-info')) {
            new Tab('additional-info','additional-information-content');
        }
        if ($('product-reviews')) {
            new Tab('product-reviews','product-reviews-content');        
        }
        if ($('similar-items')) {
            new Tab('similar-items','similar-items-content');        
        }
        if ($('manufacturer-information')) {
            new Tab('manufacturer-information','manufacturer-information-content');        
        }
        if ($('returns-and-rebates')) {
            new Tab('returns-and-rebates','returns-and-rebates-content');        
        }
        if ($('overview')) {
            new Tab('overview','overview-content');        
        }
        if ($('specifications')) {
            new Tab('specifications','specifications-content');        
        }
        if ($('customer-reviews')) {
            new Tab('customer-reviews','customer-reviews-content');        
        }
        if ($('accessories')) {
            new Tab('accessories','accessories-content');        
        }
        if ($('research')) {
            new Tab('research','research-content');        
        }
        if ($('installation-and-setup')) {
            new Tab('installation-and-setup','installation-and-setup-content');        
        }
        if ($('shipping-and-terms')) {
            new Tab('shipping-and-terms','shipping-and-terms-content');        
        }
        if ($('product-information')) {
            new Tab('product-information','product-information-content', true);        
        }
        if ($('warranty')) {
            new Tab('warranty','warranty-content');
        }
        if ($('additional-specs')) {
            new Tab('additional-specs','additional-specs-content');
        }
    }
);

