// Set up for Home page

Behaviour.addLoader(function () {
	var scrollOne = new tScroll();
	scrollOne.init('Scroll-one',77);
	var scrollTwo = new tScroll();
	scrollTwo.init('Scroll-two',77);
	var scrollThree = new tScroll();
	scrollThree.init('Scroll-three',77);
});

/* Javascript by James Nisbet of bandit.co.nz and gooduse.co.nz */
/* Last modified: 07/06/07 by James Nisbet */

/* Usage:
 *****************************************
 * Create new scroll instance;
 * --> var scroll = new tScroll();
 *
 * Initialize the instance as below
 * myInstance.init('MyScrollDiv',imgWidth)
 * --> scroll.init('ScrollArea',80);
 *****************************************
*/

function tScroll() {	
	// do we want to cleanup the div after each addition
	// this will (possibly) help with memory consumption
	this.cleanUp = false;
	
	// should we automatically stop the scroll after 10mins?
	// this will help with memory issues and cpu usage
	this.autoStop = true;
	
	// image width in pixels
	// default to 100
	this.iwidth = 100;
	
	// define system vars
	var imgs = new Array();
	var links = Array();
	var loc = 0;
	var cnt = 0;
	
	var ctent = '';
	
	// this appends a thumb from the array to the div
	this.addThumb = function(howmany) {		
		for(i=0;i<howmany;i++) {
			ni = document.createElement('img');
			
			// grab next img from array
			if(imgs[cnt]) {
				ni.src = imgs[cnt].src;

				ni.border = 0;
				at = document.createElement('a');

				// grab associated link for this img
				at.href = links[cnt];

				// append new img element to the a element
				// then attach to scrolling element
				at.appendChild(ni);
				ctent.appendChild(at);
			}
			cnt++;
			if(cnt==imgs.length) cnt = 0;
		}
	}
	
	// this scrolls the underlying div and calls the addThumb function as required
	this.autoScroll = function() {
		// minus 1px from scrolling element's leftmost position
		loc++; ctent.style.left = -loc+'px';
		
		// increment scrolling element's width to fix weird floating issues
		ctent.style.width = (this.iwidth*4) + loc+'px';
		
		// append new images after every 100 pixels of scroll
		if(loc%(this.iwidth*2)==0) {
			// automatically delete the first image in the scroller
			// and move the scroller back into view accordingly
			if(this.cleanUp) {
				// TODO: fix flickering images after each 'clean'
				// --> check css margins on images inside scroller
				ctent.removeChild(ctent.childNodes[1]);
				loc = loc - iwidth; ctent.style.left = -loc+'px';
			}
			
			this.addThumb(2);
		}
		
		// stop after 600 seconds (10 minutes) if autoStop is set
		// this is to cut down on excessive memory consumption and cpu usage
		if(!this.autoStop||loc<12000) {
			var self = this;
			setTimeout(function(){self.autoScroll();},50);
		}
	}
	
	// initializes all variables and reads dom
	this.init = function(what,width) {
		// grab the designated scrolling dom element
		ctent = document.getElementById(what);
		
		// set width of images if req'd
		if(width>0) this.iwidth = width;
		
		// populate arrays with elements from dom
		cnodes = ctent.getElementsByTagName('a');
		for(i=0;i<cnodes.length;i++) {
			links[i] = cnodes[i].href;
			imgs[i] = new Image; imgs[i].src = cnodes[i].childNodes[0].src;
		}
		
		// clear the scrolling dom element
		ctent.innerHTML = '';
		
		// add new thumbs to element from array
		this.addThumb(4);
		
		// start scrollin'
		this.autoScroll();
	}
}

/* For more information on the usage of this script, please visit bandit.co.nz */