$.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;
	
	$('ul li:first',this).before($('ul li:last',this)); 
	
	
	
	if(auto_slide == 1)
	{
		setTimeout(function(){
			timer = setInterval('$.fn.productNavigation.slide("left",that)', auto_slide_delay); 
		}, initial_delay);
	}
	
	//check if hover pause is enabled
	if(hover_pause == 1)
	{
	    $(this).hover(function(){
			//window.console.log('shenanigans');
	        //stop the interval
	        if (typeof(timer) != 'undefined') {
				clearInterval(timer);
			}
	    },function(){
	        //and when mouseout start it again
			/*
			setTimeout(function(){
				timer = setInterval('$.fn.productNavigation.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('$.fn.productNavigation.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('$.fn.productNavigation.slide("right",that)', auto_slide_delay);
		},
		function(){
			clearInterval(sr);
		}
	);
	
	$("div.product_navright", this).click(
		function(){
			return false;
		}
	);
};

$.fn.productNavigation.slide = function(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}); 
	});

};

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