/* -------- Prototype Creative :: Sliding Box (including wrap around movement) -------- */
// Set the blank variables
var x = 0; var end = 0; var millisec = 0; var timer; var direction = 'none'; var total;

// Set the amount to increment the time by
var increment = 150;

// Set the box width (difference)
var diff = 684; 

// The direction to move
var move = 'left';

// Set the slider speed (the higher the slower)
var sliderspeed = 8;

// The function to increment the time
function time(){ millisec+=increment; }

// Function to reset the time to 0 and clear the Timeout call
function reset() { clearTimeout(timer); millisec = 0; clearTimeout(startport); startportfolio(total); }

// The moveme function to move a box to the relative position
function moveme(a, tot) {
	// Is this box going right or left?
	if(a=='right') {
		// We're going right!
		// Is the end plus the width less than or equal to 0 (it should be else we're wrapping round)
		if((end + diff) <= 0) {
			// Set the end as the current end plus a full box width
			end = end + diff;
			
			// Set the page as the end variable (converted to a negative) divided by a box with + 1 (for good measure)
			pg = ((end - (end*2)) / diff) + 1;
			
			// Start the timer and move this box to the right
			time();
			timer = setTimeout('moveright()',1);
		} else {
			if(move != 'none') {
				move = 'left';
				reset();
			} else {
				direction = 'left';
			}
		}
	} else if(a=='left') {
		// We're going left!
		// Set the rev variable as the box width multiplied by the total number of boxes		
		rev = diff*tot;
		
		// A quick sum to see if we can do left (if we can't then we're wrapping around)
		if((end - diff) > rev - (rev*2)) {
			// Set the end as the current end (current location) minus a box width
			end = end - diff;
			
			// Set the page as the end variable (converted to a negative) divided by a box with + 1 (for good measure)
			pg = ((end - (end*2)) / diff) + 1;
			
			// Start the timer and move this box to the left
			time();
			timer = setTimeout('moveleft()',1);
		} else {
			if(move != 'none') {
				move = 'right';
				reset();
			} else {
				direction = 'right';
			}
		}
	}
}

// The move right function
function moveright() {
	// If the current x location is less than the end then keep moving
	if (x < end) {
		// Set a new x position based on the milliseconds and the slider speed
		x += millisec / sliderspeed;
		
		// Use the style to move the box to where it should be
		document.getElementById('slider').style.left = x + 'px';
				
		timer = setTimeout('moveright()',1);
	} else {
		// We're at the end so set the left style to the end location
		document.getElementById('slider').style.left = end + 'px';
		
		// Make sure the x position is set as the end and reset the timer
		x = end;
		reset();
	}
}

// The move left function
function moveleft() {
	// If the current x location is greater than the end then keep moving
	if (x > end) {
		// Set a new x position based on the milliseconds and the slider speed
		x -= millisec / sliderspeed;
		
		// Use the style to move the box to where it should be
		document.getElementById('slider').style.left = x + 'px';
		timer = setTimeout('moveleft()',1);
	} else {
		// We're at the end so set the left style to the end location
		document.getElementById('slider').style.left = end + 'px';

		// Make sure the x position is set as the end and reset the timer
		x = end;
		reset();
	}
}


// The startportfolio function 
function startportfolio(sp_total) {
	total = sp_total;
	startport = setTimeout("moveme(move, total)", 2000);
}

// The function to pause the movie on hover
function hoverpause(oo) {
	if(oo == 'over') {
		direction = move;
		move = 'none';
	} else {
		move = direction;
		reset();
	}
}



