/*
Author	: Sandeep Kondam
version	: jquery 1.4.4
*/

/*
Usage: Plugin looks for 

div #tab_navigation_container (holds the tab navigation elements)
div #tab_content_container (holds content for the tabs)
example:
	<div id="tab_navigation_container">
		<ul>
			<li><a href="#tab1"></a></li>	
			<li><a href="#tab1"></a></li>		
		</ul>
	</div>
	
	<div id="tab_content_container">
			<div id="tab1">				
			</div>
			<div id="tab2">			
			</div>
	</div>

*/


(function($)
{	
	var stopTimer;
	
	//this is jquery object referring to the object plugin was invoked on.
	//plugin definition
	$.fn.actionTabWithAutoRotate = function()
	{
		//plugin implementation		
		//hide all content
		
		$('#tab_content_container').find('div').hide();
		
		//mark first item in tabnavigation as selected
		$('#tab_navigation_container').find('ul').find('li:first').addClass('selected').show();
		
		
		//select the first item content
		$('#tab_content_container').find('div:first').show();
		$('#tab_content_container').find('div:first').addClass('selected').show();
		
		afunction();
		
		//declare event handler for the tab click event
		$('#tab_navigation_container').find('ul li').click(function()		
		{
			//alert('click tab event');
			
			//remove the selected class from all li items
			$('#tab_navigation_container').find('ul li').removeClass('selected');
			
			//add selected class to the current li item
			$(this).addClass('selected');
			
			
			//hide all content
			$('#tab_content_container').find('div').hide();
			
			//show selected tab content			
			$('#tab_content_container').find($(this).find('a').attr('href')).show();
		});
		
		//declare event handler for the tab hover event
		$('#tab_navigation_container').find('ul li').hover(function()		
		{		
			clearInterval(stopTimer);										
			//remove the selected class from all li items
			$('#tab_navigation_container').find('ul li').removeClass('selected');
			
			//add selected class to the current li item
			$(this).addClass('selected');
			
			
			//hide all content
			$('#tab_content_container').find('div').hide();
			
			//show selected tab content			
			$('#tab_content_container').find($(this).find('a').attr('href')).show();
			
		},function()
		{
			afunction();
		});		
	};
	
	function afunction()
	{		
		stopTimer = setInterval(rotateContent, 7000);
	}

	function rotateContent()
	{
		$('#tab_content_container').find('div').hide();
			
		var selObj = $('#tab_navigation_container').find('ul li.selected');
		//alert($('#tab_navigation_container').find('ul li').length);
		
		if($('#tab_navigation_container').find('ul li').index(selObj) >= $('#tab_navigation_container').find('ul li').length-1)
		{
			//remove the selected class from all li items
			$('#tab_navigation_container').find('ul li').removeClass('selected');	
						
			$('#tab_navigation_container').find('ul li:first').addClass('selected');
			$('#tab_content_container').find('div:first').show();
		}
		else
		{
			$('#tab_navigation_container').find('ul li').removeClass('selected');		
			var nextObj = $(selObj).next();
			$(nextObj).addClass('selected');
			$('#tab_content_container').find($(nextObj).find('a').attr('href')).show();
				
		}
								
		//show selected tab content			
		$('#tab_content_container').find($(this).find('a').attr('href')).show();

		//$('#tab_content_container').find('div:first').removeClass('selected').hide();
		//$('#tab_content_container').find('div:first').next().show();
	
	}

})(jQuery);
