var navContainer, timer, shiftObj;
var moveToSpeed = 1;
var moveIncrementSpeed = 1;

function moveShiftObjToLocation(time) {
    locationManager.speed+=moveIncrementSpeed;
    if (locationManager.currentLocation < locationManager.targetedLocation) {
        locationManager.currentLocation+=locationManager.speed;
        if (locationManager.currentLocation > locationManager.targetedLocation) locationManager.currentLocation = locationManager.targetedLocation;
    } else if (locationManager.currentLocation > locationManager.targetedLocation){
        locationManager.currentLocation-=locationManager.speed;
        if (locationManager.currentLocation < locationManager.targetedLocation) locationManager.currentLocation = locationManager.targetedLocation;
    } else if (locationManager.currentLocation == locationManager.targetedLocation) {
		timer.stopTimer();
		locationManager.speed = moveToSpeed;
	}
    shiftObj.style.left = locationManager.currentLocation+"px";
}

var locationManager = {
	currentLocation : 0,
    targetedLocation : undefined,
    initLocation : undefined,
    speed : moveToSpeed
}

var Timer = function() {
    var isRunning   = false;
    var sleepTime   = 10;
    var t;
    var sleep = function(ms) {
        if (isRunning) {
            clearTimeout(t);
            t = setTimeout("timer.notifyListeners()",ms);
        }
    }
    this.getFPS = function() {
        return (1000/sleepTime);
    }
    this.notifyListeners = function() {
        sleep(sleepTime);
        moveShiftObjToLocation(sleepTime);
    }
    this.startTimer = function() {
        isRunning = true;
        this.notifyListeners();
    }
    this.stopTimer = function() {
        isRunning = false;
    }
}

window.onload = function() {
    navContainer = document.getElementById("menu");
    shiftObj = document.getElementById("slider");
    locationManager.initLocation = parseInt(shiftObj.style.left);
    locationManager.currentLocation = locationManager.initLocation;
    timer = new Timer();
    var button = navContainer.getElementsByTagName("ul")[0].getElementsByTagName("li");
    for (var i=0; i<button.length;i++) {
        button[i].position = (125*i);
        button[i].onmouseover = function() {
            locationManager.targetedLocation=this.position;
            timer.startTimer();
        }
    }
    navContainer.onmouseout = function() {
        locationManager.targetedLocation=locationManager.initLocation;
        timer.startTimer();
    }

}