/* 
	newsScroller
*/


var newsScroller = function($) {
	var isVertical = false;
	var $parent = null;
	var $content = null;
	var $window = null;
	var isAnimating = false;
	var timer = null;
	var speed = 1;
	
	var _toggleButton = function($img){
		var src = $img.attr("src");
		if (src.indexOf("-off.") > -1) {
			$img.attr("src", src.replace("-off.", "-on."));
		} else {
			$img.attr("src", src.replace("-off.", "-on."));
		}
	}
	
	return {

		scrollBack : function() {
			if (!isAnimating) {
				if ($content.position().top < 0) { 
					isAnimating = true;
					$content.animate({
						top: '+=10'
						}, speed, function() {
							isAnimating = false;
						}); 
					//_toggleButton($(".next > img",$parent));
				} else {
					//_toggleButton($(".prev > img",$parent));
				}
			};
		},

		scrollNext : function() {
			if (!isAnimating) {
			    	if (($content.outerHeight() + parseInt($content.position().top)) > $window.height()) { 
					isAnimating = true;
					$content.animate({
						top: '-=10'
						}, speed, function() {
							isAnimating = false;
						}); 
					//_toggleButton($(".prev > img",$parent));
				} else {
					//_toggleButton($(".next > img",$parent));
				};
			};
		},

		init : function(rootId, direction) {
			if (direction == "v") isVertical = true;
			$parent = jQuery("#"+rootId);
			$content = $(".scrollerContent", $parent);
			$window = $(".scrollerWindow", $parent);
			
			
			$(".prev", $parent).mousedown(function(e){
				timer = setInterval("newsScroller.scrollBack()", speed);
			}).click(function(e){
				e.preventDefault();
			}).mouseup(function(e){
				clearInterval(timer);
			}).each(function(i){
				//if ($content.height() < $window.height())
				//	_toggleButton($("img",$(this)));
					
			});

			$(".next", $parent).mousedown(function(e){
				timer = setInterval("newsScroller.scrollNext()", speed);
			}).click(function(e){
				e.preventDefault();
			}).mouseup(function(e){
				clearInterval(timer);
			}).each(function(i){
				//if ($content.height() > $window.height())
				//	_toggleButton($("img",$(this)));
					
			});
;

		}
	};
}($);



