function SmoothMovement(position, target, velocity){
  position = Math.round(position);
  target   = Math.round(target);
  velocity = (velocity ? Math.round(velocity) : 0);

  this.updatePosition = function(){
    position += velocity;
    if (velocity < 0){
      if (position - velocity * (velocity  - 1) / 2 < target){
        velocity++;
      }else if (position - (velocity - 1) * (velocity - 2) / 2 >= target){
        velocity--;
      }
    }else{
      if (position + velocity * (velocity + 1) / 2 > target){
        velocity--;
      }else if (position + (velocity + 1) * (velocity + 2) / 2 <= target){
        velocity++;
      }
    }
    return position;
  }
  
  this.setPosition = function(newPosition) {
  	position = newPosition;
  	target = newPosition;
  }	
  
  this.changeTarget = function(newTarget){
    target = Math.round(newTarget);
  }
  
  this.getPosition = function(){
    return position;
  }

  this.getTarget = function(){
    return target;
  }

  this.getVelocity = function(){
    return velocity;
  }
  
  this.hasStopped = function(){
    return (position == target && velocity == 0);
  }

}

var movementBar;
var leftMovementBar;
var movementBarInner;
var demonstration;
var featRotPos = 0;



function updateDemonstration() {
	movementBarInner.style.right = demonstration.updatePosition() + 'em';
}

function updateTarget(e) {
	if (!e) e=window.event;
	var obj=e.target;
	if (!obj) obj=e.srcElement;

	while (obj.nodeName!='A') obj=obj.parentNode;
	clickId=obj.getAttribute('id');
	
	window.clearInterval(autoInterval);
	
	if (clickId == 'right_button1') {
		if	(featRotPos == numFeatured - 2) {
			demonstration.setPosition(1);
			featRotPos = -1;
			demonstration.changeTarget(17);
		} else {
			demonstration.changeTarget(demonstration.getTarget() + 17);
			featRotPos++;
		}
	} else {
		if	(featRotPos == -1) {
			demonstration.setPosition(((numFeatured + 1) * 17) - 1);
			featRotPos = numFeatured - 2;
			demonstration.changeTarget(numFeatured * 17);
		} else {
			demonstration.changeTarget(demonstration.getTarget() - 17);
			featRotPos--;
		}
	}
}

function updateTargetAuto() {
	if	(featRotPos == numFeatured - 2) {
		demonstration.setPosition(1);
		featRotPos = -1;
		demonstration.changeTarget(17);
	} else {
		demonstration.changeTarget(demonstration.getTarget() + 17);
		featRotPos++;
	}
}

function smoothLoad() {

	demonstration = new SmoothMovement(34,34,0);
	demoInterval = window.setInterval(updateDemonstration, 20);
	
	movementBar = document.getElementById('right_button1');
	leftMovementBar = document.getElementById('left_button1');
	movementBarInner = document.getElementById('featured_content_container');

	addEventObj(movementBar,'click',updateTarget);
	addEventObj(leftMovementBar,'click',updateTarget);

	autoInterval = window.setInterval(updateTargetAuto, 9000, numFeatured);
}

var SL_oldLoad=window.onload;
if (typeof window.onload!='function')
	window.onload=smoothLoad;
else window.onload=function() {
	SL_oldLoad();
	smoothLoad();
}
