$(function() {
	// Opsætning af scroll
	var fastScrollSpeed		= 15;
	var fastScrollStepSize	= 2;
	var normalScrollSpeed	= 20;
	var viewNewsTimeout		= 3000;

	//Get our elements for faster access and set overlay width
	var wrapper = $('div.scrollwrapper');
	var leftscroll = $('img.news_arrow:first');
	var rightscroll = $('img.news_arrow:last');
	var scroller = $('div.scroller');
	var direction = 'right';
	var scrollInterval = 0;
	//Get menu width
	var wrapperWidth = wrapper.width();

	//Sæt bredden på scrolleren ud fra antal entries.
	scroller.css('width', wrapperWidth * $('div.entry').length);

	//When user move mouse over the arrows
	// User scroll left
	leftscroll.mouseover(function() {
		clearInterval(scrollInterval);
		scrollInterval = setInterval(scrollLeft, fastScrollSpeed);
	});
	leftscroll.mouseout(function() {
		direction = 'left';
		startScroll();
	});
	function scrollLeft() {
		wrapper.scrollLeft(wrapper.scrollLeft() - fastScrollStepSize);
	}

	// User scroll right
	rightscroll.mouseover(function() {
		clearInterval(scrollInterval);
		scrollInterval = setInterval(scrollRight, fastScrollSpeed);
	});
	rightscroll.mouseout(function() {
		direction = 'right';
		startScroll();
	});
	function scrollRight() {
		wrapper.scrollLeft(wrapper.scrollLeft() + fastScrollStepSize);
	}

	// Standard scrolling
	function startScroll() {
		clearInterval(scrollInterval);
		scrollInterval = setInterval(scrollNews, normalScrollSpeed);
	}
	function scrollNews() {
		// Hold pause ud for en nyhed
		if ((wrapper.scrollLeft() % wrapper.width()) == 0) {
			clearInterval(scrollInterval);
			scrollInterval = setTimeout(startScroll, viewNewsTimeout);
		}
		var offset = wrapper.scrollLeft() + wrapper.width();
		if(offset == scroller.outerWidth()) {
			direction = 'left';
		}
		else if (wrapper.scrollLeft() == 0) {
			direction = 'right';
		}
		if(direction == 'right') {
			wrapper.scrollLeft(wrapper.scrollLeft() + 1);
		}
		else {
			wrapper.scrollLeft(wrapper.scrollLeft() - 1);
		}
	}
	startScroll();
});
