var scrollContainer;
var imagesX = 0;
var imagesY = 0;
var imageArray = new Array();
var arrayPointer = 0;
var imageCount = 0;
var imageWidth = 150;
var imageHeight = 200;
var margin = 5;
var enabled = false;
var speed = 1;
var direction = 1;
var scrollTimeout;
var containerWidth;
var containerHeight;

function loadImageArray() {
	var image;
	var div;
	var link;
	var table;
	var tr;
	var td;
	for (var i=0;i<images.length;i++) {
		image = new Image();
		div = document.createElement("div");
		link = document.createElement("a");

		link.appendChild(image);
		div.appendChild(link);
		imageArray[i] = div;
		
		image.onload = function() { this.style.margin = parseInt((imageHeight - parseInt(this.height)) / 2) + "px 0px"; };
		image.src = images[i];
		image.style.border = "none";
		image.title = imageTitles[i];
		image.style.verticalAlign = "middle";
		
		div.style.position = "absolute";
		div.style.width = imageWidth + margin * 2 + "px";
		div.style.height = imageHeight + margin * 2 + "px";
		div.style.overflow = "hidden";
		div.style.textAlign = "center";
		div.style.verticalAlign = "middle";
		
		link.style.border = "none";
		link.href = 'http://' + imageLinks[i]; 
		link.target = "_blank";
		link.style.verticalAlign = "middle";
	}
}

function switchDirection(/* int */ newDir) {
	stopScroll();
	
	if (newDir === 'up') direction = 0;
	if (newDir === 'down') direction = 1;
	
	if (direction) {
		direction = 0;
		arrayPointer = arrayPointer - imageCount - 1;
		if (arrayPointer < 0)
			arrayPointer = arrayPointer + imageArray.length - 1;
	} else {
		direction = 1;
		arrayPointer = arrayPointer + imageCount + 1;
		if (arrayPointer >= imageArray.length)
			arrayPointer = arrayPointer - imageArray.length + 1;
	}
	
	startScroll();
}

function getNextImage() {
	var result = imageArray[arrayPointer];
	
	if (direction) arrayPointer++;
	else arrayPointer--;
	if (arrayPointer >= imageArray.length)
		arrayPointer = 0;
	if (arrayPointer < 0)
		arrayPointer = imageArray.length - 1;
		
	return result.cloneNode(true);
}

function getImageTop() {
	var top;
	var result = 0;
	var index = 0;
	while (image = scrollContainer.childNodes[index]) {
		if (image.tagName == "DIV") {
			top = parseInt(image.style.top);
			if (top < result) result = top;
		}
		index++;
	}
	result -= imageHeight + margin * 2;
	return result;
}

function getImageBottom() {
	var top;
	var result = 0;
	var index = 0;
	while (image = scrollContainer.childNodes[index]) {
		if (image.tagName == "DIV") {
			top = parseInt(image.style.top);
			if (top > result) result = top;
		}
		index++;
	}
	result += imageHeight + margin * 2;
	return result;
}

function addImageRow() {
	var offset;
	if (direction) offset = getImageBottom();
	else offset = getImageTop();
	
	for (var i=0;i<imagesX;i++) {
		var image = getNextImage();
		if (direction) {
			image.style.top = offset + "px";
			image.style.left = i * (imageWidth + margin * 2) + "px";
		}	else {
			image.style.top = offset + "px";
			image.style.left = (imagesX - i - 1) * (imageWidth + margin * 2) + "px";
		}
		scrollContainer.appendChild(image);
		imageCount++;
	}
	
	//alert(offset);
}

function startScroll() {
	stopScroll();
	enabled = true;
	scrollTimeout = setTimeout("scroll()", speed);
}

function stopScroll() {
	enabled = false;
	if (scrollTimeout != null)
		clearTimeout(scrollTimeout);
	scrollTimeout = null;
}

function manualScroll() {
}

function scroll() {
	var index = 0;
	var image = null;  
	while (image = scrollContainer.childNodes[index]) {
		if (image.tagName == "DIV") {
			var top = parseInt(image.style.top);
			if (!top) top = 0;
			if (direction) top--;
			else top++;
			
			if (direction && top < (0 - imageHeight - margin * 2)) {
				scrollContainer.removeChild(image);
				imageCount--;
			} else if (!direction && top > (containerHeight + margin * 2)) {
				scrollContainer.removeChild(image);
				imageCount--;
			} else {						
				image.style.top = top + "px";
				index++;
			}
		} else {
			index++;
		}
	}
	
	if (parseInt(imageCount / imagesX) - imagesX < imagesY) {
		addImageRow();
	}

	if (enabled) scrollTimeout = setTimeout("scroll()", speed);					
}

function initScroll() {
	scrollContainer = document.getElementById("ScrollContainer");
	containerWidth = scrollContainer.clientWidth;
	containerHeight = scrollContainer.clientHeight;
	imagesX = parseInt(containerWidth / (imageWidth + margin * 2));
	imagesY = parseInt(containerHeight / (imageHeight + margin * 2));
	loadImageArray();
	
	startScroll();
}

