/*
ABC Home Page Scroller script
Created by: Geoff Pack, Febraury 2008
Last modified: GP, Nov 2010 - moved to separate file, made more modular

*/

function scroll(functionName, scrollerName, deltaN, storyWidth) {
	this.functionName = functionName;
	this.scrollerName = scrollerName;
	this.deltaN = deltaN; 			// number of stories visible in the frame
	this.storyWidth = storyWidth; 	// width of each story in pixels

	var N = 0;  	// total number of stories
	var n = 0;  	// current position
	var X = 0;		// target position in pixels
	var x0 = 0; 	// previous position in pixels
	var x1 = 0; 	// current position in pixels	
	var v0 = 0;		// previous velocity of scroller
	var v1 = 0;		// current velocity of scroller
	var a0 = 0;		// acceleration of scroller	
	var k = 0.05	// spring constant
	var c = 0.37	// damping constant
		
	var scrollTimer;
	var backButton, nextButton;
	var indicatorDot = [];

	// find total number of stories
	var kids = document.getElementById(scrollerName).childNodes;
	for (var i=0; i<kids.length; i++) {
		if (kids[i].nodeType == 1) N++;	
	}
	
	if (N > deltaN) {
		// write the back & next buttons and indicator dots - if more stories than space
		var navString = '<a href="#" onclick="return '+functionName+'.back()"><img id="'+scrollerName+'Back" class="back" src="/homepage/2008/styles/blank.gif" alt="back" width="15" height="13"></a>'; 
		for (var i=0; i<N; i++) {
			navString += '<img id="'+scrollerName+'Indicator'+i+'" class="scrollIndicator'+ ((i<deltaN)?' on':'') +'" src="/homepage/2008/styles/blank.gif" alt="" width="13" height="13">';
		}
		navString += '<a href="#" onclick="return '+functionName+'.next()"><img id="'+scrollerName+'Next" class="next on" src="/homepage/2008/styles/blank.gif" alt="next" width="15" height="13"></a>'; 
		
		// add nav string to scroller grandparent
		var navNode = document.createElement('P');
		navNode.id = scrollerName + 'Nav';
		navNode.innerHTML = navString;
		document.getElementById(scrollerName).parentNode.parentNode.appendChild(navNode);
	
		// create variables for buttons and dots
		backButton = document.getElementById(scrollerName+'Back');
		nextButton = document.getElementById(scrollerName+'Next');
		for (var i=0; i<N; i++) {
			indicatorDot[i] = document.getElementById(scrollerName+'Indicator'+i);
		}	
	}
	this.highlightButtons = function() {
		// highlights correct buttons and indicator dots
		if (backButton && nextButton) {
			classRemove(backButton,'on');
			classRemove(nextButton,'on');
			if (n > 0) {classAdd(backButton,'on')};
			if (n < (N-deltaN)) {classAdd(nextButton,'on')};			
		}
		for (var i=0; i<N; i++) {
			// turn on/off indicator dots
			if (i>=n && i<(n+deltaN)) {
				classAdd(indicatorDot[i],'on')			
			} else {
				classRemove(indicatorDot[i],'on')
			}
		}		
	}
	this.back = function() {
		return this.move(-deltaN);
	}
	this.next = function() {
		return this.move(+deltaN);
	}
	this.move = function(dn) {
		n += dn;
		if (n < 0) n = 0;
		else if (n > N-deltaN) n = N-deltaN;		
		this.scrollTo(-n * storyWidth);
		this.highlightButtons();
		return false;	
	}
	this.scrollTo = function(X) {
		if (this.scrollTimer) clearTimeout(this.scrollTimer); // clear other timers
		
		// Simple Euler integration with mass=1, dt=1
		a0 = -k * (x0 - X) - c * v0;
		v1 = v0 + a0;
		x1 = x0 + v1;

		// move scroller
		document.getElementById(scrollerName).style.marginLeft = x1 + 'px';	

		if ((Math.abs(v1) < 0.01) && (Math.abs(a0) < 0.01)) {
			x1 = X;
			v1 = 0;	// stop motion jitter
		} else {
			this.scrollTimer = setTimeout(functionName+".scrollTo("+X+")",20);
		}
		// update values
		x0 = x1;
		v0 = v1;
	}
}


// Best of ABC scroller and links
var bestof, featured;

function scrollerInit() {
	//weather = new scroll('weather',4,210,'weather');
	bestof = new scroll('bestof', 'bestOfScroller', 2, 350); // 341 for UIG
	featured = new scroll('featured', 'featuredSitesScroller', 4, 239); // 236 for UIG
}
addLoadEvent(scrollerInit);

