/**
 *	jQuery.Limiter plugin.
 *
 *	Provide the interface horziontal nice looking tabs.
 *	Amount of tabs is not limited.
 *	Text label can be set any.
 *
 *	@www: http://goldenman.spb.ru/development/jquery/gtabs/
 *	@author: Denis Golovtsov (aka GolDen)
 @	@version 0.1
 */

jQuery.fn.gTabs = function(tabs,prefix,index,callback) {

	var cContainers = this;
	var curIndex = 0;
	var lastIndex = tabs.length - 1;
	var prefixIdTabs = prefix || '_tabs_';
	var customCallback = callback || false;
	var selIndex = index || 0;

	var _tabClick = function (index)
	{
		$('._tabs_cur',cContainers).each(function(){
			$(this).removeClass('_tabs_cur');
			var id = $(this).attr('id');
			if (id.indexOf(prefixIdTabs + 'sep_first') != -1) $(this).removeClass('_tabs_sep_first_active');
			else if (id.indexOf(prefixIdTabs + 'sep_last') != -1) $(this).removeClass('_tabs_sep_last_active');
			else if (id.indexOf(prefixIdTabs + 'sep_') != -1) $(this).removeClass('_tabs_sep_left_active').removeClass('_tabs_sep_right_active');
			else if (id.indexOf(prefixIdTabs + 'cell') != -1) $(this).removeClass('_tabs_cell_cur');
		});

		$('#' + prefixIdTabs + 'cell_' + index).addClass('_tabs_cur').addClass('_tabs_cell_cur')[0].blur();

		if (index == 0)
		{
			$('#' + prefixIdTabs + 'sep_first',cContainers)
				.addClass('_tabs_sep_first_active')
				.addClass('_tabs_cur');
		}
		else
		{
			$('#' + prefixIdTabs + 'sep_'+index,cContainers)
				.addClass('_tabs_sep_left_active')
				.addClass('_tabs_cur');
		}

		if (index == lastIndex) $('#' + prefixIdTabs + 'sep_last',cContainers).addClass('_tabs_cur').addClass('_tabs_sep_last_active');
		else $('#' + prefixIdTabs + 'sep_'+(index+1),cContainers).addClass('_tabs_cur').addClass('_tabs_sep_right_active');

		if (customCallback)
		{
			customCallback.call(this,index);
		}

		for (var i=0; i<=lastIndex; i++) {
			$('#' + prefixIdTabs + i).hide();
		}
		$('#' + prefixIdTabs + index).show();
	}

	var _addTab = function (name)
	{

		if (curIndex == 0) {
			tr = $('<tr>');
			cContainers.append(
					$('<table>')
						.attr({'border':0,'cellPadding':0,'cellSpacing':0})
						.addClass('_tabs_table')
					.append($('<tbody>').append(tr))
				);
		}

		if (curIndex == 0) tr.append($('<td>').attr('id',prefixIdTabs + 'sep_first').addClass('_tabs_sep_first_inactive'));
		else tr.append($('<td>').attr('id',prefixIdTabs + 'sep_'+curIndex).addClass('_tabs_sep_middle_inactive'));

		tr.append(
			$('<td>').attr({'id':prefixIdTabs + 'cell_'+curIndex}).addClass('_tabs_cell').append(
				$('<a/>').attr('href','javascript:void(0)').append(""+name).click(
						function(curIndex){return function(){_tabClick(curIndex);};}(curIndex)
					)
			)
		);

		if (curIndex == lastIndex) tr.append($('<td>').attr('id',prefixIdTabs + 'sep_last').addClass('_tabs_sep_last_inactive'));

		curIndex++;
	}

	jQuery.each(tabs,function(){_addTab(this);});

	for (var i=0; i<=lastIndex; i++) {
		$('#' + prefixIdTabs + i).hide();
	}

	if (selIndex != undefined) {
		_tabClick(selIndex);
	}
}
