$.fn.liScroll = function(){
		return this.each(function(){
				var travelocity = 0.02;
				var $strip = $(this);
				$strip.addClass("newsticker")
				var stripWidth = 0;
				var $mask = $strip.wrap("<div class='mask'></div>");
				var $tickercontainer = $strip.parent().wrap("<div class='tickercontainer'></div>");								
				var containerWidth = $strip.parent().parent().width();	//a.k.a. 'mask' width 	
				$strip.find("li").each(function(i){
				stripWidth += $(this, i).width();
				});
				$strip.width(stripWidth);			
				var defTiming = stripWidth/travelocity;
				var totalTravel = stripWidth+containerWidth;								
				function scrollnews(spazio, tempo){
				$strip.animate({left: '-='+ spazio}, tempo, "linear", function(){$strip.css("left", containerWidth); scrollnews(totalTravel, defTiming);});
				}
				scrollnews(totalTravel, defTiming);				
				$strip.hover(function(){
				jQuery(this).stop();
				},
				function(){
				var offset = $(this).offset();
				var residualSpace = offset.left + stripWidth;
				var residualTime = residualSpace/travelocity;
				scrollnews(residualSpace, residualTime);
				});			
		});	
};

$(document).ready(function(){
// run this when the whole page has been downloaded
if(jQuery.support.scriptEval == true){
	var item_number = 0;

	$.ajax({
		url: '/svt/road/Classic/shared/xmlexport/articleInfo851Xml.jsp?d=110321',
		type: 'GET',
		dataType: 'xml',
		timeout: 10000,
		error: function(){
			console.log("Laddade inte xml.");
		},
		success: function(xml){			
			$(xml).find('article').each(function(){
				if($(this).attr('variant') == 851){
					var item_text = $(this).attr('title');
					var item_link = $(this).attr('link');
					// var item_date = $(this).attr('pubDate'); <span class="tickerDate">' + item_date + '</span>
					var item_time = $(this).attr('pubHour');
					
					var item_text_replaced = item_text
										.replace(/Ã¼/g,'ü')
										.replace(/Ã¤/g,'ä')
										.replace(/Ã¥/g,'å')
										.replace(/Ã¶/g,'ö')
										.replace(/Ã©/g,'é')
										.replace(/Ã…/g,'Å')
										;
		
					$('<li></li>')
					.html('<span class="tickerTime">' + item_time + '</span><a class="tickerLink" href="' + item_link + '">' + item_text_replaced + '</a>')
					.appendTo('ul#ticker01');
					
					item_number++;
				}
				
				if(item_number >= 5){
					return false;
				}
					
			});
			
			$(function(){ 
				$('ul#ticker01').liScroll(); 
			});
			
			if(jQuery.support.boxModel == true){
				$('div.tickercontainer').append('<div id="leftFade"><img src="/content/2/c6/11/02/96/fade_left.png" /></div>');
				$('div.tickercontainer').append('<div id="rightFade"><img src="/content/2/c6/11/02/96/fade_right.png" /></div>');
			}
	
		}
	});
}
});
