window.addEvent('domready', function() 
{
	if ($('tabs-block')) {
	
		var tabsMenu = new Element('ul', {'class':'tab-menu', 'id': 'tab-menu'});
 		tabsMenu.inject($('tabs-block'), 'before');
					
		// setup tabs
		var original = $('tabs-block');
		var originalHandles = original.getElements('h4');
		
		// for each handle, re-arrange
		originalHandles.each(function(item, index){
			var daddy = item.getParent();
			item.inject(daddy, 'before');	
		});
	
		// and then split
		var tCount = 0;
		original.getChildren().each(function(el, index) 
		{
			if (el.get('tag') == 'h4') {
				
				// counter
				tCount++;
				var tabName = el.get('text').replace(" ", "_");
		
				// make new handle
				var handle = new Element('li', 
				{
					'rel': 'tabbySet'+tCount,
					'class': 'tabbyHandle'+tCount,
					'id': 'tab' + tabName,
					'events': 
					{
				        'click': function(e){
							if (e)
						        e.stop();
								
				        	// handles
					        tabsMenu.getElements('li').each(function(li)
							{
						    	li.removeClass('active');
					        });
						
					        handle.addClass('active');
			        	
			        		// tabs
			          		original.getElements('.tabbyContent').each(function(tab)
							{
					         	// tab.fade('out');
			     		     	tab.setStyle('display','none');
			         		 });
				 
		        			original.getElements('.'+handle.get('rel')).each(function(tab)
							{
		      			    	// tab.fade('in');
		     			     	tab.setStyle('display','block');          	
		       				});	  
		      			  }//click
			    	}// el events
				}).adopt(new Element('a', {href:'#'}));

				// give it the final span
				handle.getElement('a').adopt(new Element('span', {'html': el.get('html')}));
		

				// throw it in:
				handle.inject(tabsMenu, 'bottom');	
		
				el.destroy();
			
			} else {
				el.addClass('tabbySet'+tCount);	
				el.addClass('tabbyContent');
			}
			
		});
	

  	// init handles
    tabsMenu.getElements('li').each(function(li)
	{
      li.removeClass('active');
    });
	
    tabsMenu.getElement('li').addClass('active');

		// init tab content
	  original.getElements('.tabbyContent').each(function(tab)
	  {
	  	// tab.fade('out');
	  	tab.setStyle('display','none');
	  });
	  
	  original.getElements('.tabbySet1').each(function(tab)
	  {
	  	// tab.fade('in');
	  	tab.setStyle('display','block');
	  });
	  
	  //finally, ask if a tab has been selected via the query string
	  var tab = parseQueryString("tab");
	  
	  if (tab != "")
	  {
	  	//yes, find the tab 
		var liTag = $$("li#tab" + tab);
	  	
		//and if found, use javascript to fire the click event
		if ((liTag) && (liTag.length > 0))
			liTag[0].fireEvent("click");
		//else
		//	alert("Cannot find tab #" + tab); 
	  }
	  	
	}// end ifs
 
});
 
/**
* read Query string value
*/
function parseQueryString(key, default_)	
{
	if (default_ == null)
		default_=""; 
	
 		key = key.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	
	var regex = new RegExp("[\\?&]"+key+"=([^&#]*)");
	var qs = regex.exec(window.location.href);
	if(qs == null)
	    return default_;
	else
		return qs[1];
}



	

