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(38);
		} else {
			demonstration.changeTarget(demonstration.getTarget() + 38);
			featRotPos++;
		}
	} else {
		if	(featRotPos == -1) {
			demonstration.setPosition(((numFeatured + 1) * 38) - 1);
			featRotPos = numFeatured - 2;
			demonstration.changeTarget(numFeatured * 38);
		} else {
			demonstration.changeTarget(demonstration.getTarget() - 38);
			featRotPos--;
		}
	}
}

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

function smoothLoad()
{
	movementBar = document.getElementById('right_button1');
	leftMovementBar = document.getElementById('left_button1');
	movementBarInner = document.getElementById('featured_content_container');

	demonstration = new SmoothMovement(76,76,0);
	if (typeof(window.movementBarInner) == 'object' && window.movementBarInner != null)  demoInterval = window.setInterval(updateDemonstration, 20);

	if (typeof(window.movementBar) == 'object' && window.movementBar != null)  addEventObj(movementBar, 'click', updateTarget);
	if (typeof(window.leftMovementBar) == 'object' && window.leftMovementBar != null)  addEventObj(leftMovementBar, 'click', updateTarget);

	if (typeof(window.numFeatured) != 'undefined')  autoInterval = window.setInterval(updateTargetAuto, 10000, numFeatured);
}

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