var j = 0;

jQuery.fn.slideView = function(settings)
{
	var currentNewsSelect = 0;
	var timerHandle = null;
	// Control rotation on mouse hover.
	var isHovering = false;
	var rotationSuspended = false;
	
	settings = jQuery.extend(
	 	{
     		easeFunc: "easeInOutQuint",
     		easeTime: 450,
     		toolTip: false
  		},
  		settings
  	);

	// Make the slideViewStep function available from the jQuery object.
	jQuery.extend({
		slideViewStep: function(container, steps)
		{
			var pictWidth = container.find('.stripElement').width();
			var pictEls = container.find('.stripElement').size();
			
			// Find new step to show.
			currentNewsSelect = (currentNewsSelect + steps) % pictEls;
			// Modulo of a negative number is not guaranteed to return the right result. Fix.
			if (currentNewsSelect < 0) currentNewsSelect += pictEls;
			
			var cnt = - (pictWidth*currentNewsSelect);
			container.find('.stripRotator').animate({ left: cnt}, settings.easeTime, settings.easeFunc);
		},
		slideViewGoto: function(container, dest)
		{
			var steps = dest - currentNewsSelect;
			$.slideViewStep(container, steps);
		},
		slideViewStartTimer: function(container)
		{
			// Start the automatic rotation if it is not running.
			if ($.slideViewTimerRunning()) return;
			
			if (rotationSuspended) {
				// Rotate immediately to compensate.
				$.slideViewStep(container, 1);
				rotationSuspended = false;
			}
			
			timerHandle = setInterval(function()
				{
					// Only rotate when the mouse is not hovering.
					if (!isHovering) {
						$.slideViewStep(container, 1);
					} else {
						// Indicate that we missed a rotation.
						rotationSuspended = true;
						// Stop the timer.
						$.slideViewStopTimer();
					}
				},
				5000
			);
			
			// Hide start button and show stop button.
			$('#rotatorStart').hide();
			$('#rotatorStop').show();
		},
		slideViewStopTimer: function()
		{
			// Stop the rotation if it is running.
			if (!$.slideViewTimerRunning()) return;
			clearInterval(timerHandle);
			timerHandle = null;
			
			// Hide stop button and show start button.
			$('#rotatorStop').hide();
			$('#rotatorStart').show();
		},
		slideViewTimerRunning: function()
		{
			return (timerHandle !== null);
		}
	});
	 
	return this.each(function(){
		var container = $(this);
		var rotationWasStarted = false;
		container.addClass("stripViewer");		
		var pictWidth = container.find('.stripElement').width();
		var pictHeight = container.find('.stripElement').height()-4;
		var pictEls = container.find('.stripElement').size();
		var stripViewerWidth = pictWidth*pictEls;
		container.find('.stripRotator').css("width" , stripViewerWidth);	
		
		// Rotate forward
		$('#rotatorForward').bind('click',
			function()
			{
				// Stop the rotation.
				$.slideViewStopTimer();
				$.slideViewStep(container, 1);
				// Start the automatic rotation.
				$.slideViewStartTimer(container);
				return false;
			}
		);
		
		// Rotate backwards
		$('#rotatorBack').bind('click',
			function()
			{
				// Stop the rotation.
				$.slideViewStopTimer();
				$.slideViewStep(container, -1);
				// Start the automatic rotation.
				$.slideViewStartTimer(container);
				return false;
			}
		);
		
		// Start/stop automatic rotation.
/*		$('#rotatorStart').bind('click',
			function(evt)
			{
				if (!$.slideViewTimerRunning()) {
					// Rotate immediately.
					$.slideViewStep(container, 1);
					// Start the automatic rotation.
					$.slideViewStartTimer(container);
					evt.stopPropagation();
					return false;
				}
			}
		);
		$('#rotatorStop').bind('click',
			function(evt)
			{
				// Stop the automatic rotation.
				$.slideViewStopTimer();
				evt.stopPropagation();
				return false;
			}
		);*/
		
		// Stop automatic rotation when the mouse is hovering.
		container.find('.stripElement').hover(
			function()
			{
				isHovering = true;
				rotationWasStarted = $.slideViewTimerRunning();
			},
			function()
			{
				isHovering = false;
				// Try to start the rotation again if it was stopped.
				if (rotationWasStarted) {
					$.slideViewStartTimer(container);
				}
			}
		);
		
		// Start the automatic rotation.
		$.slideViewStartTimer(container);

		container.find('.stripElement').bind('click',
			function()
			{
				window.location.href = $(this).find('a').attr('href');
			}
		);
		
		container.find('.stripElement').each(
			function()
			{
			//	$(this).find('img').css('height', '180px');
				//$(this).find('img').css('width', '295px');
			//	$(this).find('.sliderText').css('width', '434px');
			}
		);

		j++;
	});
};
