/* Copyright (c) 2008 GoldenSubmarine SKA http://www.goldensubmarine.com
 * Licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
 * Copyright notice and license must remain intact for legal use
 * gsTabs 0.1
 * Version: 0.1 (2008-07-04)
 * Tested on jQuery 1.2.6
 * 
 * Author: Michal Lipek (m.lipek@goldensubmarine.com)
 */
jQuery.fn.gsTabs = function() {  

	var selectTab = function(element) { 
		var parent = jQuery(element).parent();
		var left = parent.prev();
		var right = parent.next();
		var tabs = parent.parent();
		// reset all tabs
		jQuery('> .left_active', tabs).attr('class', 'left_inactive');
		jQuery('> .right_active', tabs).attr('class', 'right_inactive');
		jQuery('> .active_inactive', tabs).attr('class', 'inactive_inactive_l');
		jQuery('> .inactive_active', tabs).attr('class', 'inactive_inactive_r');
		jQuery('> .active', tabs).attr('class', 'inactive');
		
		// left tab
		if (left.prev().length) {
			left.attr('class', 'inactive_active');
			left.prev().attr('class', 'inactive');
		}
		else {
			left.attr('class', 'left_active');
		}
		
		// right tab 
		if (right.next().length) {
			right.attr('class', 'active_inactive');
			right.next().attr('class', 'inactive');
		}
		else {
			right.attr('class', 'right_active');
		}
		
		parent.attr('class', 'active');
	}

	return this.each(function() {  
		var obj = jQuery(this);
		obj.addClass('gsTabulate');
		
		// build structure
		jQuery('> a:gt(0)', obj).before(jQuery('<span/>').addClass('inactive_inactive_r'));
		jQuery('> a:first', obj).before(jQuery('<span/>').addClass('left_inactive'));
		jQuery('> a:last', obj).after(jQuery('<span/>').addClass('right_inactive'));
		jQuery('> a', obj).wrap(jQuery('<span/>').addClass('inactive'));
		
		selectTab(jQuery('> span > a:first', obj));
		
		jQuery('> span > a', obj).click(function() {
			selectTab(this);
		});
	});  
};
   