/*

Horizontal NEWS TICKER JS

makes news items scroll and pause and restarts on mousoever/mouseout

heavily adapted and jQuery'd from code from here:
http://www.magclearsmines.org/js/main.js

*/


// variables
var scrollspeed = 60;
var scrolldistance = 3;
var scrolling = true;
var offsetWidth = "-1px";
var newsItems;
var maxWidth = 0; // total width
var maxWidthTotal = 0; // total width (including padding+margins)

// ticker mechanism.
// moves up content using CSS' left property e.g. left: -1px
function ticker() {
	if (scrolling && $('#scroll_inner')) { // run if scrolling is true
		var width = $("div#scroll_inner").width(); // work out width from total items * item width
		var left = parseInt(document.getElementById('scroll_inner').style.left); // get starting (left) position of inner div
		newsItems = $("div.item").size; // total number of items	
		
		//reset if max width reached;		
		if($("#scroll_inner").position().left == "-"+maxWidth || $("#scroll_inner").position().left < "-"+maxWidth){
			var left = $("#scroll").width();
			$("#scroll_inner").css("left",left);
		}
		// first scroll
		else if (left <= (0-(width + 20)) || !left) {
			$("#scroll_inner").css("left",-1);
		}
		// repeated scrolls increment upwards
		else {
			var newLeft = (left-scrolldistance)+'px';
			$("#scroll_inner").css("left",newLeft);
			
			return false;
		}
	}
}

// start the ticker (run on load)
function startTicker() {
	if (!document.getElementById) return false; // cancel if broswer doesn't support the DOM
	if (!document.getElementsByTagName) return false;// cancel if broswer doesn't support the DOM	
	if (!document.getElementById('scroll_inner')) return false; 
	if (document.getElementById('scroll')) {
		prepareTickerItems(); // add mouse interaction to scrolling items
		var interval = window.setInterval('ticker()',scrollspeed);
	}
}

// add on mouseover/onmouseout events to <divs>
function prepareTickerItems(){

	// hide extra wideness of news ticker
	$("div#scroll").css("overflow","hidden");
	
	// get width of all items and add them together
	$("div#scroll_inner div.item").each(
		function(){
			maxWidthTotal += $(this).outerWidth(); // with paddings etc
			maxWidth += $(this).width(); // w/out paddings
		}
	);
	
	// set the width fo the item container to be the width of all the items (plus paddings+margins)
	$("div#scroll_inner").css("width",maxWidthTotal+"px");

	// stop start news ticker when hovered over.
	$("div.item").hover(
		function(){
  			scrolling = false;
		},
		function(){
  			scrolling = true;	
		}
	);
}

// start
$(document).ready(
	function() {
		startTicker();
	}
);