function clearValue(varObj,varValue) {
	if(varObj.value == varValue) {
		varObj.value = "";
	}
}

function testValue(varObj,varValue) {
	if(varObj.value == "") {
		varObj.value = varValue;
	}
}

function RandRange( intFrom, intTo, intSeed ){
	// Make sure that we have integers.
	intFrom = Math.floor( intFrom );
	intTo = Math.floor( intTo );
	 
	// Return the random number.
	return(
		Math.floor(
			intFrom + 
			(
				(intTo - intFrom + 1) * 
				 
				// Seed the random number if a value was passed.
				Math.random( 
					(intSeed != null) ? intSeed : 0 
					)
			))
		);
}

var timer;
var timouttimer;

function initPromo(start,end,seconds) {
	// get random number, based on how many are available
	imgnum = RandRange(start, end);
	
	pNav = document.getElementById("co"+imgnum);
	pImg = document.getElementById("promo"+imgnum);
	speed = seconds * 1000;
	
	for(i=start; i<=end; i++) {
		// set all objects to invisible first
		document.getElementById("co"+i).className = document.getElementById("co"+i).className.replace(" active", "");
		document.getElementById("promo"+i).className = "none";
	}
	
	// set starting promo to visible
	pNav.className += " active";
	pImg.className = "promo";
	
	// run the rotating function to start
	rotatePromo(start,end,speed,imgnum);
}

function rotatePromo(start,end,speed,lastnum) {
	// clear interval/timeout to start
	clearInterval(timer);
	clearTimeout(timouttimer);

	lastNumber = parseInt(lastnum);
	newNum = 1;
	
	// figure out what the new number to use should be
	if(lastNumber < end) {
		newNum = lastNumber + 1;
	} else if(lastNumber >= end) {
		newNum = 1;
	} else {
		newNum = 1;
	}
	
	for(i=start; i<=end; i++) {
		// set all objects to invisible first
		document.getElementById("co"+i).className = document.getElementById("co"+i).className.replace(" active", "");
		document.getElementById("promo"+i).className = "none";
	}
	
	
	newNav = document.getElementById("co"+newNum);
	newImg = document.getElementById("promo"+newNum);
	
	// set newly selected promo to visible
	newNav.className += " active";
	newImg.className = "promo";
	
	// set the interval
    timer = setInterval("rotatePromo("+start+","+end+","+speed+","+newNum+")", speed);
}

function displayPromo(start,end,which) {
	// clear interval/timeout to start
	clearInterval(timer);
	clearTimeout(timouttimer);

	for(i=start; i<=end; i++) {
		// set all objects to invisible first
		document.getElementById("co"+i).className = document.getElementById("co"+i).className.replace(" active", "");
		document.getElementById("promo"+i).className = "none";
	}
	
	newNav = document.getElementById("co"+which);
	newImg = document.getElementById("promo"+which);	
	
	// set newly selected promo to visible
	newNav.className += " active";
	newImg.className = "promo";
}

function restartPromo(start,end,seconds,lastnum) {
	// clear interval/timeout to start
	clearInterval(timer);
	clearTimeout(timouttimer);

	speed = seconds * 1000;
    timouttimer = setTimeout("rotatePromo("+start+","+end+","+speed+","+lastnum+")", speed);
}

function findLeftPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	//return [curleft,curtop];
	return curleft;
}

function moveToPrevious() {
	parentPos = findLeftPos(document.getElementById('productThumbMask'));
	innerPos = findLeftPos(document.getElementById('productThumbs'));
	actualPos = parentPos - innerPos;
	//alert(actualPos);
	if(actualPos > 0) {
		new Effect.Move('productThumbs', { x: 174, y: 0, transition: Effect.Transitions.sinoidal });
	}
}  

function moveToNext(count) {
	parentPos = findLeftPos(document.getElementById('productThumbMask'));
	innerPos = findLeftPos(document.getElementById('productThumbs'));
	actualPos = parentPos - innerPos;
	stoppoint = (parseInt(count) * 58) - 174;
	if(actualPos < stoppoint) {
		new Effect.Move('productThumbs', { x: -174, y: 0, transition: Effect.Transitions.sinoidal });
	}
}

function toggleZoom(which) {
	varDiv = document.getElementById(which);
	if(varDiv.style.visibility == 'hidden') {
		varDiv.style.visibility = 'visible';
	} else {
		varDiv.style.visibility = 'hidden';
	}
}
