(function($){
	$.fn.CMTab = function(options){
		var defaults = { 
			callback: null // function that executes after every move
		};  
		
		var options = $.extend(defaults, options);  
		var iTabSize, iCurrent;
		var oTab = $(this);
		var oTabItems = $(".item", oTab);
		
		return this.each(function(){
			initialize();
		});
		function initialize()
		{
			iTabSize = oTabItems.size();
			for(var i = 0; i < iTabSize; i++)
			{
				oTabItems.eq(i).attr('rel', i);
			}
						
			$(".item", oTab).click(function()
			{
				iCurrent = $(this).attr('rel');
				
				if($(this).hasClass('item_active'))
				{
					return;
				}
				
				$(".first_active",oTab).removeClass('first_active');
				$(".last_active",oTab).removeClass('last_active');
				$(".sep_active_prev",oTab).removeClass('sep_active_prev');
				$(".sep_active_next",oTab).removeClass('sep_active_next');
				$(".item_active",oTab).removeClass('item_active');
				
				$(this).addClass('item_active');
						
				if($(this).prev().hasClass('first'))
				{
					$(this).prev().addClass('first_active');
				}
				else
				{
					$(this).prev().addClass('sep_active_prev');
				}
				
				if($(this).next().hasClass('last'))
				{
					$(this).next().addClass('last_active');
				}
				else
				{
					$(this).next().addClass('sep_active_next');
				}
				
				if(typeof options.callback == 'function')
				{
					options.callback.call(this, iCurrent);	
				}
				
				
			});
			
			oTabItems.eq(0).click();
			
		}
		
	};
})(jQuery);
