﻿var speed = 30;
var fast = 5;

var kk = pic.length;
var ii;
var hhh;
var nnn;
var myInterval;
var myPause;
var mode = 0;
var action = 0;
var fadewidth=0;


var imgArray = new Array(kk);
var myLeft = new Array(kk);

for (ii=0;ii<kk;ii++){
	imgArray[ii] = new Image();
	imgArray[ii].src = pic[ii].name;
	imgArray[ii].width = pic[ii].width;

	hhh=0 ;
	for (nnn=0;nnn<ii;nnn++){
		hhh=hhh+pic[nnn].width+gap;
	}
	myLeft[ii] = hhh;
}

function ready(){
return true;
	if (document.images)
	{
		for (ii=0;ii<kk;ii++){ 
			if (document.images[ii].complete == false){
				return false;	
				break;
			}
		}
	}
return true;
}


function startScrolling(){
	if (ready() == true){		
		window.clearInterval(myPause);
		myInterval = setInterval("autoScroll()",action == 0 ? speed : fast);
	}
}	
	
var SCROLL = 0;
var NEXT = 1;
var PREVIOUS = 2;

var target_ii = -1;
function setTarget() {
	// Determine which image is the one we are trying to move into place.
	// If next then it is the image with smallest +ve myLeft
	// If previous it is either the one image with -ve myLeft else if none then the one with max myLeft
	var move_left = 0;
	for (ii=0;ii<kk;ii++) {
		if (action == NEXT) {
			if (myLeft[ii] > 0 & (myLeft[ii] < move_left || move_left == 0)) {
				target_ii = ii;
				move_left= myLeft[ii];
			}
		}
		else {
			if (myLeft[ii] < 0) {
				target_ii = ii;
				break;
			}
			else {
				if (myLeft[ii] > move_left) {
					target_ii = ii;
					move_left = myLeft[ii];
				}
			}
		}
	}
	// alert("target is " + target_ii + " with left = " + myLeft[target_ii]);
}

function autoScroll() {
	var increment = -1;
	var stopped = false;
	// speeds below 30 don't make much difference so jump 2x pixels instead.
	// but take care to keep position even so will stop at zero
	switch (action) {
	case NEXT:
		increment = myLeft[target_ii] < 2 ? -myLeft[target_ii] : -2 ;
		break;
	case PREVIOUS:
		increment = myLeft[target_ii] > -2 ? -myLeft[target_ii] : 2;
		break;
	}
	for (ii=0;ii<kk;ii++) {
		myLeft[ii] = myLeft[ii] + increment;
		// If not going previous, check to see if image needs to jump to rhs;
		// For previous, the jump is handled in previous() - not sure why not here too but it works
		if (action != PREVIOUS) {
			if (myLeft[ii] == -(pic[ii].width)-fadewidth-gap) {
				hhh = gap;
				for (nnn=0;nnn<kk;nnn++){
					if (nnn!=ii){
						hhh = hhh + pic[nnn].width + gap;
					}			
				}
				myLeft[ii] =  hhh-fadewidth-gap;
			}
		}
		getObject("Carousel" + ii).style.left = myLeft[ii];
		// allow all images to reposition before stopping
		if (action != SCROLL) if (myLeft[ii] == 0) stopped = true;
	}
	mode = 1;
	if (stopped) {
		stop();
		getObject("CarouselStopStart").innerHTML = "start";
	}
}

function stop(){
	if (mode == 1){
		window.clearInterval(myInterval);
	}
	if (mode == 0){
		window.clearInterval(myPause);
	}
}


function go(){
	stop();	// cancel any next/prev
	action = SCROLL;
	if (mode == 1){
		myInterval = setInterval("autoScroll()",speed);
	}
	if (mode == 0){
		myPause = setInterval("startScrolling()",0);
	}
	return true;	
}

function stopstart() {
	var o = getObject("CarouselStopStart");
	if (o.innerHTML == 'start') {
		go();
		o.innerHTML = 'stop';
	}
	else {
		stop();
		o.innerHTML = 'start';
	}
}
function previous() {
	stop();
	// if there is an image at 0 position move last image to before it
	for (ii=0;ii<kk;ii++) {
		if (myLeft[ii] == 0) {
			var jumper = (ii + pic.length - 1) % pic.length;
			myLeft[jumper] = myLeft[ii] -(pic[jumper].width + gap + fadewidth);
			break;
		}
	}
	action = PREVIOUS;
	setTarget();
	if (mode == 1){
		myInterval = setInterval("autoScroll()",fast);
	}
	if (mode == 0){
		myPause = setInterval("startScrolling()",0);
	}
}

function next() {
	stop();
	action = NEXT;
	setTarget();
	if (mode == 1){
		myInterval = setInterval("autoScroll()",fast);
	}
	if (mode == 0){
		myPause = setInterval("startScrolling()",0);
	}
}
// now we have loaded, make the controls visible
if (pic.length > slides) getObject("CarouselControls").style.visibility = "visible";
