(function( $ ) {	
	$.fn.productNavigation = function () {
		
		that = this;
		
		var $wrapper = $('> div', this);
		$wrapper.before('<div class="product_navleft"><a href="" class="link_navleft"></a></div>');
		$wrapper.after('<div class="product_navright"><a href="" class="link_navright"></a></div>');
		
		var auto_slide = 1;  
		var hover_pause = 1;  
		var key_slide = 1;
		
		var auto_slide_delay = 1500;
		var initial_delay = 5000;
		
		if($('ul li', this).length <=5) {
			$('ul li', this).clone().appendTo($('ul',this));
		}
		
		$('ul li:first',this).before($('ul li:last',this)); 
				
		if(auto_slide == 1)
		{
			setTimeout(function(){
				timer = setInterval( function() { slide("left",that) }, auto_slide_delay ); 
			}, initial_delay);
		}
		
		//check if hover pause is enabled
		if(hover_pause == 1)
		{
		    $('ul', this).hover(function(){
		        //stop the interval
		        if (typeof(timer) != 'undefined') {
					clearInterval(timer);
				}
		    },function(){
		        //and when mouseout start it again
				/*
				setTimeout(function(){
					timer = setInterval( function() { slide("left",that) }, auto_slide_delay); 
				}, initial_delay);
				*/
		    });
		}
		
		// Monitor the navigation 
		$("div.product_navleft", this).hover(
			function(){
				if (typeof(timer) != 'undefined') {
					clearInterval(timer);
				}
				sl = setInterval( function() { slide("left",that) }, auto_slide_delay);
			},
			function(){
				clearInterval(sl);
			}
		);
		
		$("div.product_navleft", this).click(
			function(){
				return false;
			}
		);
	
		$("div.product_navright", this).hover(
			function(){
				if (typeof(timer) != 'undefined') {
					clearInterval(timer);
				}
				sr = setInterval( function () { slide("right",that) }, auto_slide_delay);
			},
			function(){
				clearInterval(sr);
			}
		);
		
		$("div.product_navright", this).click(
			function(){
				return false;
			}
		);
	};
	
	function slide(where, obj) {
		//get the item width 
		var item_width = $('li',obj).outerWidth();
		
		if(where == 'left') { 
			var left_indent = parseInt($('ul',obj).css('left')) - item_width;
		} else {
			var left_indent = parseInt($('ul',obj).css('left')) + item_width;
		}
		$('ul:not(:animated)',obj).animate({'left' : left_indent},500,function() {
			if(where == 'left') { 
				$('ul li:last', obj).after($('ul li:first',obj)); 
			} else {
				$('ul li:first',obj).before($('ul li:last',obj));
			}
			
			$('ul',obj).css({'left' : - item_width}); 
		});
		
	};
	
})( jQuery );


$(document).ready(function () {
	$('#product_navigation').productNavigation();
});
