/* THESE ARE VARIABLES YOU CAN ADJUST */
width = 600;	//box width, in pixels
speed = 150;		//arbitrary figure, really. For instant refresh, change to 1000.
noBoxes = 3;	//number of different div boxes, relating to id numbers
pause = 0;	//delay between auto-movements, in milliseconds, eg. 2000 = 2 seconds	
elementWidth = 95;
/* END OF VARIABLES YOU CAN ADJUST */

start = 0;
cBox = 1;
function setBoxes(boxCount,boxWidth) {
	width = boxWidth;
	noBoxes = boxCount;
	init();
}
function init(type, boxes) {
	if(boxes) noBoxes = boxes;
	cID = 'box'+cBox;
	stopauto = 0;
	if(type==-1) document.getElementById(cID).style.left = 0;
	if (noBoxes > 1) {
		for (p=1;p<=noBoxes;p++) {				
			if (p != cBox || start == 0) {
				tID = 'box'+p;
				if (type <= 1) document.getElementById(tID).style.left = (width+50)+'px'; else document.getElementById(cID).style.left = ((-width)-50)+'px';
			}
		}
	}
}
function startautomovement() {
	if (cBox == noBoxes ) {
		boxFrom = noBoxes; 
		boxTo = 1;
	} else {
		boxFrom = cBox; 
		boxTo = cBox + 1;
	}
	go = startmovement(boxFrom,boxTo,1);
}
function stopautomovement() {
	if (stopauto == 0) {
		stopauto = 1;
		xr = clearTimeout();
		xs = clearTimeout();
	} else {
		startautomovement();
	}
}
function startmanualmovement(direction) {
	if (direction == 0) {	
		if (cBox == noBoxes ) {
			boxFrom = noBoxes; 
			boxTo = 1;
			cBox = 1;
		} else {
			boxFrom = cBox; 
			boxTo = cBox + 1;
		}
		go = startmovement(boxFrom,boxTo,0);
	} else {
		if (cBox == 1 ) {
			boxFrom = 1; 
			boxTo = noBoxes;
			cBox = noBoxes;
		} else {
			boxFrom = cBox; 
			boxTo = cBox - 1;
		}
		go = startmovement(boxFrom,boxTo,2);
	}
}
function clearTimeouts() {
	try {
		xr = clearTimeout();
	} catch(err) {}
	try {
		xs = clearTimeout();
	} catch(err) {}
	cBox = 1;
	startautomovement();
	//temp = setTimeout("clearTimeouts()", 10000);
}
function startmovement(box1,box2,auto) {
	wait = 0;
	try {
		if (xr||xs) {
			wait = 1;
			//alert('i waiting');
		}
	}
	catch (err) {
		wait = 0;
	}
	if (wait == 0) {
		init(auto);
		if (auto!=1) newpause = 0; else newpause = pause;
		if (auto == 2) {
			xr = setTimeout("movebox(0,"+width+","+speed+","+box1+","+auto+",1)",newpause);
			xs = setTimeout("movebox("+(-width)+",0,"+speed+","+box2+","+auto+",1)",newpause);
		} else {
			//xr = setTimeout("movebox(0,"+(-width)+","+speed+","+box1+","+auto+",1)",newpause);
			xs = setTimeout("movebox("+width+",0,"+speed+","+box1+","+auto+",1)",newpause);
		}
	}
}
function movebox(start,end,cSpeed,boxNo,auto,beginMovement) {	
	if (beginMovement == 1 && stopauto == 1) return;
	cBox = boxNo;
	cid = 'box'+boxNo;
	ctextvid = 'text'+(boxNo*2-1);
	ctextmid = 'text'+(boxNo*2);		
	if (auto == 2) {//move left to right (manual only)
		cSpeed = 1.5 + ((end-start)/width) * speed;
		start = start + parseInt(cSpeed);
		if (start + cSpeed < end + 2) {
			document.getElementById(cid).style.left = start+'px';
			xr = setTimeout("movebox("+start+","+end+","+speed+","+boxNo+","+auto+",0)",1);
		} else {
			if (document.all) {
				newend = end;
			} else {
				newend = end+((end / width) * 2);
			}
			document.getElementById(cid).style.left = newend+'px';
			xr = clearTimeout();
			xs = clearTimeout();
		}
	} else {//move right to left (manual & automatic)
		cSpeed = 1.5 + ((start-end)/width) * speed;
		start = start - parseInt(cSpeed);
		if (start + cSpeed >= end + 2) {
			document.getElementById(cid).style.left = start+'px';
			xr = setTimeout("movebox("+start+","+end+","+speed+","+boxNo+","+auto+",0)",1);
		} else {
			if (document.all) {
				newend = end;
			} else {
				newend = end+((end / width) * 2);
			}
			//xr = clearTimeout();
			try {xs = clearTimeout();} catch(err) {}
			if (auto==1 && stopauto == 0) {
				if (boxNo <= noBoxes - 1) {
					createend = (boxNo * elementWidth);
					xs = setTimeout("movebox("+width+","+createend+","+speed+","+(boxNo+1)+","+auto+",1)",pause);
				}
			}
		}
	}
}
