/*
 * yNewsScroll 1.0 (2009-02-26)
 *
 * based on jdNewsScroll 1.1 by Jonathan Sharp (http://jdsharp.us)
 * Dual licensed under the MIT (MIT-LICENSE.txt)
 * and GPL (GPL-LICENSE.txt) licenses.
 *
 * Built upon jQuery 1.1.1 (http://jquery.com)
 * Tested with jQuery 1.2.6
 * Requires jQuery dimensions plugin
 */


(function($){
 	var ELMS = [];
 	$.fn.yNewsScroll = function(settings){
		settings = $.extend({}, arguments.callee.defaults, settings);
		$(this).each(function(){
			this.$settings = settings;
			this.$pause = false;
			this.$direction = this.$settings.direction;
			this.$counter = this.$settings.start;
			$(this).hover(function(){$(this).yNewsScrollPause(true)}, function(){$(this).yNewsScrollPause(false)});
			// pause scroll while mouse over
			$('> ul', this)
				.bind('mouseover', function(e){
					if ($(e.target).is('li')){
						$(e.target).addClass('hover');
					}
				})
				.bind('mouseout', function(e){
					if ($(e.target).is('li')){
						$(e.target).removeClass('hover');
					}
				});
			ELMS.push(this);
		});
		return this;
	};
	
	$.fn.yNewsScroll.defaults = {
		delay: 60,				// Delay in seconds is (delay * 85)/1000
		step: 2,				// Number of pixels to step
		start: 100,				// delay before start
		direction: "vertical"	// direction
	};
	
	$.fn.yNewsScrollPause = function(pause){
		return this.each(function(){
			this.$pause = pause;
		});
	}
	
	// Activate scrolling
	setInterval(scroll, 20);

	// Go through list of elements and step each one for vertical scrolling
	// TODO: some optimisation would be nice
	// FIXME: horizontal and vertical scrolls on one page acts strange
	function scroll(){
		for (var i = 0; i < ELMS.length; i++){
			var elm = ELMS[i];
			if (elm && !elm.$pause){
				if (elm.$counter == 0){
					// Some initialisation
					var ul = $('> ul', elm)[0];
					var back = elm.$settings.step < 0;
					if (!elm.$steps){
						// get height/width of element to switch
						if (elm.$direction == 'vertical'){
							elm.$steps = back ? $('> li:last-child', ul).outerHeight() : $('> li:first-child', ul).outerHeight();
							position = 'top';
						}else if(elm.$direction == 'horizontal'){
							elm.$steps = back ? $('> li:last-child', ul).outerWidth() : $('> li:first-child', ul).outerWidth();
							position = 'left';
						}
						elm.$step = 0; // reset step
					}
					if ((back && elm.$step == 0) || (!back && (elm.$steps + elm.$step) <= 0)){
						elm.$counter = elm.$settings.delay;
						if (back){
							elm.$step = -elm.$steps;
							$(ul).css(position, elm.$step).find('> li:first-child').before($('> li:last-child', ul));
						}else{
							elm.$steps = false;
							$(ul).css(position, '0').find('> li:last-child').after($('> li:first-child', ul));
						}
						$('> *', ul).not('li').remove();
					}else{
						elm.$step -= elm.$settings.step / 10;
						if(back && elm.$step >= 0){
							elm.$step = 0;
							elm.$steps = false;
						}
						if (!back && -elm.$step > elm.$steps){
							elm.$step = -elm.$steps;
						}
						$(ul).css(position, elm.$step);
					}
				}
				else {
					elm.$counter--;
				}
			}
		}
	};
})(jQuery);