var Tabs = Class.create({
	
	initialize: function()
	{
		
		/* grab ul.tabs */
		$$('#tabs .linksModule ul').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);
					
					/* apply onclick event to <a> (show/hides content) */
					Event.observe(link, "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) {
								$(item.rel).show();
								$(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
				}
				
			});
			
			/* 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');
				} 
				
			});
			
		});
		
	}

});

Event.observe(window,"load",function(){ var Tab = new Tabs(); } );