var Tabs = Class.create({
	selectedTab: null,	

	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);
					
					/* apply onclick event to <a> (show/hides content) */
					Event.observe(link, "click", function(event, link){
						if(this.selectedTab) {
							this.selectedTab.up('li').removeClassName('selectedTab');
							if(this.selectedTab == link) {
								Effect.BlindUp($(this.selectedTab.rel));
								link = null;
							} else {
								Effect.BlindUp($(this.selectedTab.rel), {
									afterFinish: function() {
										Effect.BlindDown($(link.rel));
										$(link).up('li').addClassName('selectedTab');
									}
								});
							}
						} else {
							Effect.BlindDown($(link.rel));
							$(link).up('li').addClassName('selectedTab');
						}
						this.selectedTab = link;


						//target = this.rel;
						///* find sibling links */
						//$$('#' + $(this).up('ul').id + ' li a.selectedTab[rel]').each(function(item){
						//	/* show target content / add CSS class */
						//	if (item.rel == target) {
						//		Effect.BlindDown($(link.rel));
						//		$(link).up('li').addClassName('selectedTab');
						//	/* hide sibling content / remove CSS class */
						//	} else {
						//		$(item.rel).hide();	
						//		$(item).up('li').removeClassName('selectedTab');
						//	}
						//	if (item.rel != target) {
						//		Effect.BlindUp($(link.rel));
						//		$(link).up('li').removeClassName('selectedTab');
						//	}
						//});
					}.bindAsEventListener(this, link));
					
				}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(/^(.*)#/,'') : null ); //$$('#'+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);
		
	}

});

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