var HighlightFlow_speed = 7;
var HighlightFlow_windowWidth = 300;

function HighlightFlow_SmoothMovement(position, endPosition, velocity) {
	position = Math.round(position);
	endPosition = Math.round(endPosition);
	velocity = (velocity ? Math.round(velocity) : 0);
	//infos = document.getElementById("infos");
	
	this.updatePosition = function() {
		if (((velocity < 0) && (position - velocity > endPosition)) ||
			 ((velocity > 0) && (position - velocity < endPosition)))
			position = endPosition;
		else
			position -= velocity;

		return position;
	};
	
	this.changeTarget = function(newEndPosition) {
		endPosition = Math.round(newEndPosition);
	};
	
	this.getPosition = function() {
		return position;
	};
	
	this.getVelocity = function() {
		return velocity;
	};
	
	this.hasStopped = function() {
		return (position == endPosition && velocity == 0);
	};
}

var g_bInMove = false;

function HighlightFlow_MoveCollectionLeft(itemsContainerId) {
	if(g_bInMove) return ;
	
	g_bInMove = true;
	var photoContainer = document.getElementById(itemsContainerId);
	if(photoContainer) {		
		var currentPosition = 0;
		if(photoContainer.style.marginLeft != null && photoContainer.style.marginLeft != "") 
			currentPosition = parseInt(photoContainer.style.marginLeft);
		
		photoContainer.style.marginLeft = currentPosition + "px";
		if(currentPosition < 0) {
			var endPosition = currentPosition + HighlightFlow_windowWidth; // this should be a parameter!!!
			var animator = new HighlightFlow_SmoothMovement(currentPosition, endPosition, -HighlightFlow_speed); //this should be a parameter!!!
			window.setTimeout(function() {HighlightFlow_UpdateCollectionSlide(photoContainer, animator, endPosition)}, 20);
		}
		else 
			g_bInMove = false
	}
}

function HighlightFlow_MoveCollectionRight(itemsContainerId) {
	if(g_bInMove) return;
	g_bInMove = true;
	
	var photoContainer = document.getElementById(itemsContainerId);
	if(photoContainer) {
		var currentPosition = 0;
		var containerWidth = 0;
		containerWidth = parseInt(photoContainer.style.width);
			
		if(photoContainer.style.marginLeft != null && photoContainer.style.marginLeft != "")
			currentPosition = parseInt(photoContainer.style.marginLeft);
			
		photoContainer.style.marginLeft = currentPosition + "px";
		var newPosition = currentPosition - HighlightFlow_windowWidth;//this should be a parameter!!!
		if(Math.abs(newPosition) < containerWidth) {
			var animator = new HighlightFlow_SmoothMovement(currentPosition, newPosition, HighlightFlow_speed);//this should be a parameter!!!
			window.setTimeout(function() {HighlightFlow_UpdateCollectionSlide(photoContainer, animator, newPosition)}, 20);
		}
		else 
			g_bInMove = false;
	}
}

function HighlightFlow_UpdateCollectionSlide(photoContainer, animator, endPosition) {
	var newPosition = animator.updatePosition();
	photoContainer.style.marginLeft = newPosition + "px";
	if(newPosition != endPosition)
		window.setTimeout(function() {HighlightFlow_UpdateCollectionSlide(photoContainer, animator, endPosition)}, 20);
	else {
		g_bInMove = false;
		var prevBtn = document.getElementById("collectionpreviousbtn");
		if(endPosition >= 0) 
			prevBtn.src = "/images/previousphoto_disabled.gif";
		else 
			prevBtn.src = "/images/previousphoto.gif";
		   
		var nextBtn = document.getElementById("collectionnextbtn");
		if(Math.abs(endPosition - HighlightFlow_windowWidth) >= parseInt(photoContainer.style.width)) //YAMN - Yet Another Magic Number...
			nextBtn.src = "/images/nextphoto_disabled.gif";
		else 
			nextBtn.src = "/images/nextphoto.gif";
	}
}

