/*
 * jQuery Simple Scrolling Plugin
 *
 * Copyright (c) 2009 Tamcy
 */
;(function($)
{
	var opts;


	$.fn.extend({
		
		simpleScroll: function(options)
		{
			$(this).each(function() {

				var $this = $(this);
				var $container = $('div:first', $this);

				$this.css('position', 'relative');
				$container.css('position', 'absolute');
				$container.data('simplescroll', {
					duration: Math.round($container.height() / 15) * options.speed, 
					speed: options.speed,
					parent: $this
				});

				animateContainer($container);

			}).mouseover(function() {
				stopAnimation(this);
			}).mouseout(function() {
				resumeAnimation(this);
			});
		}
	});

	function animateContainer(obj)
	{
		var data = obj.data('simplescroll');

		var target = (-1 * obj.height());
		var pos = obj.position();
		var offset = Math.abs(target - pos.top);

		var duration = Math.round(offset / 15 * data.speed);

		obj.animate(
			{
				top: target
			}, 
			duration, 
			'linear', 
			function() {
				var data = $(obj).data('simplescroll');
				var parent = data.parent;
				$(obj).css('top', parent.height());
				animateContainer($(obj));
			}
		);
	}

	function stopAnimation(obj)
	{
		$('div:first', obj).stop();
	}

	function resumeAnimation(obj)
	{
		animateContainer($('div:first', obj));
	}

})(jQuery);

