// based on pirex post-tabs wordpress plugin 
// http://pirex.com.br/wordpress-plugins/post-tabs/


// DIpostTabs
// requires MooTool 
//Create tabs from dive with the style 'postTabs_divs'
//this function creates the a ul containing the tabs which is placed above the first div
//tabs name is pulled for the first <b> contents if found else the title is 'Tab 0' etc
//
// updateSize() changes the size of each 'postTabs_divs' to fit the container div
//
// call setupTabsNav() to start



function DIpostTabs_show(x){		
		$$('.postTabs_divs').each(function(ele){
			ele.setStyle('display','none');
		});
		$$('.postTabs li').each(function(ele){
			ele.removeClass('postTabs_curr');
		});
		
		$$('.postTabs_divs')[x].setStyle('display','block');
		$$('.postTabs li')[x].addClass('postTabs_curr');
}


function DIupdateSize(){
	divTop = 0;
	
	el = $$('.postTabs_divs')[0]; 
	divpos = el.getPosition(el.getParent());
	divTop = divpos.y+21;
	parentpos = el.getParent().getSize();
	newHeight = (parentpos.y-divTop);
	
	$$('.postTabs_divs').each(function(el){
		el.setStyle('height',newHeight);
	});
}


function DIsetupTabsNav(){
	if($$('.postTabs_divs').length > 0){
		
		var ul = new Element('ul', {
		    'class': 'postTabs'
		    });
		
		var x = 0 ;
		$$('.postTabs_divs').each(function(el){

			var tempx = x;
			var newli = new Element('li');
			
			if(el.getFirst('b')){
				title = el.getFirst('b').get('text');
			}else{
				title = "Tab " + tempx;
			}
			
			var newa = new Element('a', {
			    'html': title,
			    'events': {
				    'click': function(){
						DIpostTabs_show(tempx);
					}
			    }
			});
			
			newa.inject(newli);
			newli.inject(ul);
			
			x++;
		});
		
		ul.inject($$('.postTabs_divs')[0],'before');
		DIupdateSize();
		$$('.postTabs_divs').each(function(el){el.setStyle('display','none')});
		$$('.postTabs_divs')[0].setStyle('display','block');
		$$('.postTabs li')[0].addClass('postTabs_curr');
		
	}
}
