var Tabs = Class.create({
    innerTabs: [],
    
    initialize: function()
    {
        
        /* grab ul.tabs */
        $$('.tabs').each( function(tl,index){
            
            tl.id = ( tl.id ? tl.id : 'tablist_'+index ); // apply ID to <ul> if one doesn't exist
                
            /* find links within ul.tabs */
            
            target_content_array = []; // collects targets
            
            $$('#'+tl.id+' li a').each(function(link){ 
                
                if ( $PROTO(link.href.replace(/^(.*)#/,'')) ) { // make sure target exists
                
                    link.rel = link.href.replace(/^(.*)#/, ''); // set rel="" to target id from #anchor
                    link.href = 'javascript:;'; // overwrite href anchor 
                    target_content_array.push(link.rel);
                    this.innerTabs.push(link);
                    
                    /* apply onclick event to <a> (show/hides content) */
                    link.observe('mouseup', function() {
                        window.clearInterval(window.tabInterval);    
                    });
                    link.observe('click', function() {
                    
                        target = this.rel;
                        
                        /* find sibling links */
                        $$('#' + $(this).up('ul').id + ' li a[rel]').each(function(item){
                        
                            /* show target content / add CSS class */
                            if (item.rel == target) {
                                Effect.Appear($(item.rel));
                                $PROTO(item).up('li').addClassName('selectedTab');
                                
                            /* hide sibling content / remove CSS class */
                            }
                            else {
                                $PROTO(item.rel).hide();
                                $PROTO(item).up('li').removeClassName('selectedTab');
                            }
                        });
                        
                    });
                    
                }else{
                    $PROTO(link).up('li').addClassName('disabledTab'); // apply disabled class if content not found
                }
                
            }, this);
            
            /* select #anchor if one is found, if not select first target in list */
            target = ( target_content_array.indexOf(location.href.replace(/^(.*)#/,''))!=-1 ? location.href.replace(/^(.*)#/,'') : $$('#'+tl.id+' li a').first().rel );
            
            /* show/hide target and siblings */
            $$('#'+tl.id+' li a[rel]').each(function(link,index){

                if( target==link.rel ){ 
                    $PROTO(target).show();
                    $PROTO(link).up('li').addClassName('selectedTab'); 
                }else{ 
                    $PROTO(link.rel).hide();
                    $PROTO(link).up('li').removeClassName('selectedTab');
                } 
                
            });
            
        }, this);
        
        window.currentTab = 0;
        window.tabInterval = window.setInterval(function() {    
            if(window.currentTab >= window.Tab.innerTabs.length - 1)
                window.currentTab = 0;
            else
                window.currentTab++;    
                
            if(!!window.Tab.innerTabs[window.currentTab])
                window.Tab.innerTabs[window.currentTab].onclick();
        }, 5000);
        
    }

});

Event.observe(window,"load",function(){ window.Tab = new Tabs(); } );
