// Gallery manager

var SKIP_DISTANCE = 183;

var CurrentPosition = 0;
var TargetPosition = 0;

var anim = 0;
var animated = null;
var container = null;
var numItems = 0;

function Move(direction) {
	if (direction == "right") {
		TargetPosition += SKIP_DISTANCE;
	} else if (direction == "left") {
		TargetPosition -= SKIP_DISTANCE;
	}
	if (TargetPosition > 0) TargetPosition = 0;
	if (TargetPosition < ((numItems - 3) * -1 * SKIP_DISTANCE)) TargetPosition = ((numItems - 3) * -1 * SKIP_DISTANCE);
}

function Animation() {
	if (CurrentPosition != TargetPosition) {
		var distance = TargetPosition - CurrentPosition;
		if (Math.abs(distance) < 0.05) {
			CurrentPosition = TargetPosition;
		} else {
			CurrentPosition += (distance / 3);		
		}
		animated.style.left = Math.round(CurrentPosition) + "px";
	}
	if (CurrentPosition==0) document.getElementById("leftBut").className="button";
	else document.getElementById("leftBut").className="buttonA";
	if (CurrentPosition==((numItems - 3) * -1 * SKIP_DISTANCE)) document.getElementById("rightBut").className="button";
	else document.getElementById("rightBut").className="buttonA";
	
	
}

function InitAnimation() 
{	
	var container = document.getElementById('galleryContainer');
	var gal = document.getElementById('gallery');
	var data = document.getElementById('galData');
	if (data == null || data.innerHTML=="")
	{
		var noResult = document.createElement('div');
		noResult.className = "no_results";
		noResult.innerHTML = "Currently there are no open tours matching your search.  Please search again using different dates or see all tours on one page.";		
		gal.style.display = "none";
		container.appendChild(noResult);
		document.getElementById('leftBut').style.display = "none";
		document.getElementById('rightBut').style.display = "none";			
	}	
	else
	{
		anim = setInterval(Animation, 25);
		animated = document.getElementById("gallery");
		container = document.getElementById("galleryContainer");
		numItems = (animated.rows[0].cells.length + 1) / 2;
		if (numItems>3)
		{
			document.getElementById("rightBut").className="buttonA";
			document.getElementById("leftBut").className="button";				
		}
		else
		{
			document.getElementById("rightBut").style.display="none";
			document.getElementById("leftBut").style.display="none";
		}

	}
}