$().ready(function() {
	$('.tabbed_blocks').each(function(){new TabStrip($(this));});
});

function TabStrip(jq) {
	var divs = $('div.inner',jq);
	divs.hide();
	
	var self = this;
	var currentTab = '';
	var inAction = false;
	var speed = 400;
	
	//insert a buton bar
	jq.prepend('<div class="tabbar"></div>');	
	var tabs = $('h2.tab',jq).appendTo('div.tabbar');
	var tabHeight = $('div.tabbar').height();

	//set relevant styles
	divs.css({
		position: 'absolute',
		width: '597px',
		top: tabHeight,
		left: 0
	});
	jq.css({position: 'relative'});
	

	$('h2.tab',jq).click(function(){
		self.showTab($(this).attr('id'));
		return false; 
	});
	this.showTab = function(name) {

		if(!inAction && currentTab != name) {
			inAction = true;
			var tabS = '#'+name;
			var divS = 'div[rel='+name+']';
			var toHeight = $(divS).height() + tabHeight + 40;
			jq.css({height: toHeight});
			if(currentTab.length) { this.hideCurrent(); }
			$(tabS).addClass('active');
			$(divS).fadeIn(speed, function(){inAction=false;});
			currentTab = name;
		} else if(currentTab == name) {
			var divS = 'div[rel='+name+']';
			var toHeight = $(divS).height() + tabHeight + 40;
			jq.css({height: toHeight});
		
		}	
	}
	this.hideCurrent = function(h) {
		var tabS = '#'+currentTab;
		var divS = 'div[rel='+currentTab+']';
		$(tabS).removeClass('active');
		$(divS).fadeOut(speed);
	}
	//open the first tab
	var initial = $.param.fragment();
	if(initial.length > 0 && $('#'+initial).hasClass('tab')) {
		this.showTab(initial);
	} else if (initial.substr(0,5)=='cmt::' && $('#guestbook').hasClass('tab')) {
		this.showTab('guestbook');
	} else {
		this.showTab($('h2.tab:first',jq).attr('id'));
	}
}
