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 ( $(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.onmouseup = function() {
						window.clearInterval(window.tabInterval);	
					}
					link.onclick = 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));
								$(item).up('li').addClassName('selectedTab');
								
							/* hide sibling content / remove CSS class */
							}
							else {
								$(item.rel).hide();
								$(item).up('li').removeClassName('selectedTab');
							}
						});
						
					};
					
				}else{
					$(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 ){ 
					$(target).show();
					$(link).up('li').addClassName('selectedTab'); 
				}else{ 
					$(link.rel).hide();
					$(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++;	
				
			window.Tab.innerTabs[window.currentTab].onclick();
		}, 3000);
		
	}

});

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