var tImage;
var tImageWidth, maxLeft;
var displace = 0;
var menuArray = new Array();	// contains all menu arrays

var disinc = 445;
var disArray = [0, -555,-1015, -1700,-2290];
var disPoint = 0;

var scrollSpeed = 1;	// speed of automovement, always to the right
var scrollInterval = 25; // milliseconds

var maxffSpeed = 15;
var ffSpeedStart = 0;


var autoTimer;
var scrollTimer;
function setScollTimer(start) {
	if (start) {
		//console.log("prepare scroll");
		scrollTimer = setTimeout( "autoControl(true)", 1000 );	
	}
	else {
		//console.log("cancel scroll");
		clearTimeout(scrollTimer);
	}	
}

var autoMove = false;
var autoSpeed = 0;	
function autoControl(start) {
	if (start) {
		autoMove = true;
	}
	else {
		autoMove = false;
	}
}

var ffTimer;
var ffSpeed = 1;
var speedDir = 1;
var goDir = -1;

var speedTimer;
var pressRight = false;
var pressLeft = false;

function moveFF() {	
	var autoSpeed = 0;
	if (pressRight) {	// if buttons are pressed increase speed.
		//console.log("goRight");
		ffSpeed = ffSpeed - 1;
	}
	else if (pressLeft) {
		ffSpeed = ffSpeed + 1;
	}
	else {	// when not pressed reduce speed.
		if (ffSpeed < 0) ffSpeed = Math.ceil(ffSpeed/2);
		if (ffSpeed > 0) ffSpeed = Math.floor(ffSpeed/2);	
		if (autoMove) autoSpeed = scrollSpeed;
	}
	if (ffSpeed > maxffSpeed) ffSpeed = maxffSpeed;
	if (ffSpeed < -maxffSpeed) ffSpeed = -maxffSpeed;
	//console.log(ffSpeed);
	
	var cLeft = Number(tImage.css("left").replace("pt","").replace("px",""));	// current left position
	//console.log(autoMove);
	//console.log(goDir);
	//console.log(ffSpeed);
	displace = cLeft + ffSpeed + autoSpeed * goDir;
	checkBounds();
	//console.log(displace);
	tImage.css("left",displace);
}




function goRight(start) {
	autoControl(false); // turn off scrolling;
	setScollTimer(!start);
	
	
	navHideAll(null);	
	pressRight = start;
}
function goLeft(start) {
	autoControl(false); // turn off scrolling;
	setScollTimer(!start);
	
	navHideAll(null);
	pressLeft = start;
}


function checkPointer() {
	if (disPoint < 0) {
		disPoint = 0;
	}
	if (disPoint > disArray.length - 1) {
		disPoint = disArray.length - 1;
	}	
	
}
function checkBounds() {		
	//console.log(displace);
	$("#arrowRight").show();
	$("#arrowLeft").show();
	if (Math.floor(displace) >= 0) { 
		displace = 0;
		pressLeft = false;
		flipDirection(-1);
		//goDir = -1;
		$("#arrowLeft").hide();
	}	
	if (Math.ceil(displace) < maxLeft) {
		displace = maxLeft;
		pressRight = false;
		flipDirection(1);
//		goDir = 1;
		$("#arrowRight").hide();
	}	
}

var flipTimer;
var flipDirGlobal;
function flipDirection(flipDir) {
	//console.log(flipDir);
	if (flipDir == flipDirGlobal) return;
	clearTimeout(flipTimer);
	flipTimer = setTimeout("flipNow(" + flipDir + ")", 4000 );	
	flipDirGlobal = flipDir;
}

function flipNow(wDir) {
	//console.log("flipNow" + wDir);
	goDir = wDir;
}

function menuObj(index, li) {
	this.index = index;
	this.button = li;
	this.active = false;
	this.timeout = null;
	
	this.turnon = function(goX) {
		var newX = displace + goX - 75;
		if(goX) this.button.animate({left: newX}, 1);
		//if (this.active) return;
		// TODO: only show menu if hidden and vice versa
		navHideAll(this.index);
		clearTimeout(this.timeout);
		if($.browser.msie)  {
			this.button.show();
		}
		else {
			this.button.fadeIn();
		}
		this.active = true;
		
	}
	this.turnoff = function() {
		clearTimeout(this.timeout);
		this.timeout = setTimeout("navHideNow(" + this.index + ");", 0);
	}	
	this.turnoffnow = function() {
		clearTimeout(this.timeout);
		if($.browser.msie)  {
			this.button.hide();
		}
		else {
			this.button.hide();
//			this.button.fadeOut();
		}
		this.active = false;
	}
	
}

/* JQUERY FUNCTIONS */
jQuery().ready(function (){		
	// SETUP DROPDOWN MENUS
	//$(".hovbox").hide();	
	var menuItems = $(".hovbox");
	menuItems.each(											 
		function(i){
			//index = $(this).attr("id");
			$(this).mouseover( function() { navShow(i,null); } );
			$(this).mouseout( function()  { navHide(i); } );
			menuArray.push(new menuObj(i,$(this)));
 	});		
	
	tImage = $("#timeImage");	
	tImageWidth = tImage.width();
	maxLeft = -tImageWidth + 809;
	
	numberClicks = disArray.length;
	
	disPoint = Math.floor(Math.random()*numberClicks);
	//disPoint = 0; // temporarily set for testing
	displace = disArray[disPoint];
	checkBounds();
	tImage.css("left",displace);	
	
	ffTimer = setInterval ( "moveFF()", scrollInterval );	
	autoControl(true);
	
});

function navFind(wNum) {	// return menu object based on ID
	for (var i = 0; i < menuArray.length; i++) {
		var cMenu = menuArray[i];
		if (cMenu.index == wNum) {
			return cMenu;
		}
	}
	return null;
}

function setVisible(wNum, wState) {	
	cNav = navFind(wNum);
	if(cNav) cNav.active = wState;
}

function navShow(wNum,wTag) {	
	setScollTimer(false);
	autoControl(false); // turn off scrolling;

	var wX = null;
	if (wTag) {
		var tagCoords = $(wTag).attr("coords")
		var firstCoord = tagCoords.split(",")[0];
		wX =  Number(firstCoord);
	}
	cNav = navFind(wNum);
	if(cNav) cNav.turnon(wX);
}
function navHide(wNum) {	
	cNav = navFind(wNum);
	if(cNav) cNav.turnoff();
}
function navHideNow(wNum) {	
	cNav = navFind(wNum);
	setScollTimer(true);
	if(cNav) cNav.turnoffnow();
}

function navHideAll(wNum) {
//	console.log("navHideAll" + wNum);
	for (var i = 0; i < menuArray.length; i++) {
		var cMenu = menuArray[i];
		if (cMenu.index != wNum) {
			cMenu.turnoffnow();
		}
	}
}
