
$(function(){
		var speed = 30;
		var delay = 2000;
		var scrollbar = 1;
		var scrollimages = 1;
		var runs = "unlimited"; //specify number of runs or "unlimited"
		var expand_effects = 0;
		var pause_between_cycles = 0;


		var mheight = 180;
		var kjoer;
		var go;
		var runcount=0;


		$('#mw_newsfeed').wrapInner('<div class="alternate" style="position:relative; overflow:hidden; height:180px;"></div>');
		$('#mw_newsfeed .alternate').wrapInner('<div class="scroller" style="position:absolute; top:0px;"></div>');
		$('#mw_newsfeed .alternate')
			.append('<div class="scrollbar" style="position:absolute; top:0px; right:2px; background:transparent; width:12px; height:180px;"></div>')
			.append('<div class="scrollicon" style="width:12px; height:12px; position:absolute; top:0px; right:2px; opacity:.5; filter:alpha(opacity=50)"></div>');
		$('#mw_newsfeed .scrollicon')
			.css({'background':'url(arrow_up.gif) no-repeat top left'})
			.append('<div>')
			.children()
			.css({'background':'url(arrow_down.gif) no-repeat top left', 'position':'absolute', 'top':'13px', 'width':'12px', 'height':'12px'});
		$('#mw_newsfeed .article').width($('#mw_newsfeed .article').width() - 16);

		var scroller_height = $(".scroller").clone().appendTo(".alternate").height();
		var si_height = Math.round(mheight*mheight/scroller_height)>9?Math.round(mheight*mheight/scroller_height):10;
		
		si_height = 25;
		var scrollicon = $(".scrollicon:eq(0)");
		scrollicon.height(si_height);

		$(".scroller:eq(1)").css("top", scroller_height);
		var elems_to_scroll = [ $(".scroller:eq(0)"), $(".scroller:eq(1)") ]

		var dim = new Array(0,scroller_height);

		var scroll = function(){

			if (dim[1] <= 0) {
				runcount += 1;
			if ( (pause_between_cycles != 0) && ( ( runs==('unlimited'||0) ) || (runcount <runs) ) ){
				clearInterval(kjoer);
				clearTimeout(go);
				go = setTimeout(wait,delay);

			};

			dim = [0, scroller_height];
			run_limiter();
		};

		$.each(elems_to_scroll, function(i){
			this.css("top",dim[i]);
			dim[i] -= 1;
		});

		scrollicon.css("top", Math.round( (mheight - si_height) * (0-dim[0]) / scroller_height) );};

		var manual_scrolling=0;

		scrollicon.mousedown(function(u){
			manual_scrolling=1;
			clearInterval(kjoer);
			var start = [parseInt(scrollicon.css("top")), u.pageY];
			u.preventDefault();
			$("body").mousemove(function(e){
				var relative_move =  parseInt(e.pageY) - start[1];
				var scrollicon_move = (start[0]+relative_move>0?start[0]+relative_move:0)
				dim[0] = Math.round( -1 * scroller_height / (mheight-si_height) * (scrollicon_move<=(mheight-si_height)?scrollicon_move:mheight-si_height) ) - 1;
				dim[1] = dim[0] + scroller_height;
				scroll();
			});

		});

		$("body").add(window).mouseup(function(){
			manual_scrolling=0;
			$("body").unbind("mousemove");
		});

		$("#mw_newsfeed").mouseout(function(){
			$("body").click();
		});

		$(".alternate").hover(function(){manual_scrolling=1;}, function(){manual_scrolling=0;});

		function wait(){
			clearInterval(kjoer);
			kjoer = setInterval(scroll, speed);
		}

		var go = setTimeout(wait,delay)
		var scroller_event = function(){
				$(".alternate").hover(function(){clearTimeout(go); clearInterval(kjoer); kjoer = 0;}
				 ,function(){
					if (kjoer<1){
					clearInterval(kjoer);
					kjoer = setInterval(scroll, speed);
				}
			});
		};

		function run_limiter(){
			if ((runs != ("unlimited"||0)) && (runcount>=  runs)) {
				clearInterval(kjoer);
				scroll = function(){
				return false;
			};

			$("body").unbind("mousemove");
			$(".alternate").unbind("mousein").unbind("mouseout");
			if (expand_effects !=0){
				$(".alternate").add(".scrollbar").animate({height:scroller_height},4000, function(){ });
				$(".scrollicon").add(".scrollbar").fadeOut("5000", function(){ $(this).remove(); });
			}else {
				$(".alternate").add(".scrollbar").height(scroller_height);
				$(".scrollicon").add(".scrollbar").remove();
			}

			$(".scroller:eq(1)").remove();
			$(".scroller").css("top", "0px");
			}
			else{ return true; };
			};

		scroller_event();

		$(".article").hover(function(){$(this).css("background", "transparent"); }, function(){$(this).css("background", "transparent");});


	});
