// (C) 2008 QSC Audio Products, LLC
// All Rights Reserved

// Configuration variables
var slide_delay = 8; // how long to wait between animations (in seconds)
var frame_delay = 6; // refresh rate (in milliseconds), lower = smoother but more demanding on CPU
var frame_size = 790; // width of each frame in pixels
var sliderate = 1600; // speed of slide animation in pixels per second

// these arrays determine which app button to highlight after the slide
// the position of the slide frame will determine which item is chosen
// the first frame is repeated at the end to make the whole thing seamless
var app_ids = new Array('mkt_concert','mkt_entertainers','mkt_installed','mkt_cinema','mkt_concert');


// other vars
var currentframe = 1;
var slider = document.getElementById('moving');
var current_pos = 0;
var ending_pos =  0;
var timer = null;
var start_time = 0;


function slide(){
	starting_pos = slider.style.left;
	starting_pos = starting_pos.replace(/[^0-9|-]/g,'');
	ending_pos = Math.floor(starting_pos) - frame_size;
	t = new Date();
	start_time = t.getTime();
	move();
	clearTimeout(timer);
	
}

function move(){
	current_pos = slider.style.left;
	current_pos = current_pos.replace(/[^0-9|-]/g,'');
	current_pos = Math.floor(current_pos);
	// calc step_distance based on the difference between the current rate and sliderate
	// if we're running slow, the step_distance should increase
	t = new Date();
	tnow = t.getTime();
	elapsed = (tnow - start_time) / 1000;
	currentrate = Math.abs((ending_pos - current_pos) / elapsed) ;
	expected_pos = Math.ceil((ending_pos + frame_size)-(elapsed * sliderate));
	step_distance = Math.abs(expected_pos - current_pos); 
	//document.getElementById('debug').innerHTML = expected_pos + "||" + current_pos  + "||" +  ending_pos + "||" + step_distance;
	if(current_pos > ending_pos + step_distance){
		slider.style.left = (current_pos - step_distance) + "px";
		setTimeout('move()',frame_delay);	
	} else if(current_pos > ending_pos){
		slider.style.left =  ending_pos + "px";
		if(ending_pos <= -1 * (frame_size * 4)){
				// reset position back to starting point
				slider.style.left = "0px";
		}
		// after stop, highlight applications buttons
		find_active();
		autorun();
	}
}

function autorun(){
	if(navigator.userAgent.indexOf('iPhone')==-1){
		// don't autostart the slide for iPhones (too slow)
		timer = setTimeout('slide()',(slide_delay * 1000));
	}
}

function randomize(){
	// used when the page loads to select a random starting position
	num_frames = 4;
	pick = Math.floor(Math.random() * num_frames);
	slider.style.left = -(pick * frame_size) + "px";
	document.getElementById("slide_container").style.height = "316px";
	// highlight the first app button
	currbuttonid = app_ids[pick];
	highlight(currbuttonid);
	autorun();
}

function find_active(){
	current_pos = slider.style.left;
	current_pos = current_pos.replace(/[^0-9|-]/g,'');
	current_pos = Math.floor(current_pos);
	currframe = Math.abs(current_pos/frame_size);
	currbuttonid = app_ids[currframe];
	// reset all buttons first
	// see the stylesheet for correct colors and background images
	for(i=1;i<app_ids.length;i++){
		normal(app_ids[i])
	}
 	highlight(currbuttonid);
}

function highlight(id){
	document.getElementById(id).style.backgroundPosition = "-200px 0px";
	document.getElementById(id).style.color = "#FFFFFF";
	document.getElementById(id).style.backgroundColor = "#404c56";
}
function rollover(id){
	document.getElementById(id).style.backgroundPosition = "-400px 0px";
	document.getElementById(id).style.color = "#4a1e01";
	document.getElementById(id).style.backgroundColor = "#f4d01a";
}
function normal(id){
		document.getElementById(id).style.backgroundPosition = "0px 0px";
		document.getElementById(id).style.color = "#FFFFFF";
		document.getElementById(id).style.backgroundColor ="#323232";
}