var spotlight, defaultTarget, timeoutId, speed=.25, interfacesToLoad=2;

$(document).ready(function(){
	spotlight=$('#spotlight').get(0);
	defaultTarget=getCentreX( $('#nav .selected').get(0) );
	interfacesReady();
});

//This function is called "on DOM ready" and "on Flash ready"
function interfacesReady(){
	interfacesToLoad--;
	if(interfacesToLoad==0){
		moveToDefault();
		$('#nav a').hover(
			function(){
				spotlight.moveLight(getCentreX( $(this).get(0) ), speed);
			},
			function(){
				if(typeof timeoutId == 'number') clearTimeout(timeoutId);
				timeoutId=setTimeout(moveToDefault, 3000);
		});
	}
}

function getCentreX(el){
	return el.offsetLeft + el.offsetWidth / 2;
}

function moveToDefault(){
	spotlight.moveLight(defaultTarget, speed * 2);
}