
function ImageThrower(blockId) {
	this.url = 'imagethrower.php?a=a';
	this.imageBlocks = new Array();
	this.blockId = blockId;
	this.width = 490;
	this.height = 400;
	this.lastValue = null;
	this.limit = 50;
	this.blocks = 4;
	this.blockIndex = 0;
	this.currentPage = 1;
	this.pageCount = 0;


	this.buildURL = function(value) {
		return (
			this.url +
			'&page=' + this.currentPage +
			'&perPage=' + (this.limit * this.blocks) +
			'&query=' + value
		);
	}

	this.loadImagesList = function(value) {
		this.lastValue = value;
		this.load(this.buildURL(value), true);
		this.render();
		setTimeout('imageThrower.onTimer();', 1000); 
	}

	this.refresh = function() {
//		alert('refresh');
		if ((this.currentPage + 1) <= this.pageCount)
			this.currentPage++;
		else {
	//		alert('current: ' + this.currentPage + ', maximum: ' + this.pageCount);
			this.currentPage = 1;
		}
		this.load(this.buildURL(this.lastValue), false);
	}

	this.load = function(name, sync) {
		
		ajax_load(
			name,
			'',
			this,
			'handle',
			'text/plain',
			sync,
			'GET',
			'text/plain'
		);
	}

	this.handle = function(content) {
	
		eval('this.' + content + ';');
	//	this.loadedItem = item;
	}

	this.jsonFlickrApi = function(item) {
		this.pageCount = item.photos.pages;

		if (this.pageCount > 40)
			this.pageCount = 40;

		if (this.timerId != null)
			clearTimeout(this.timerId);
		var images = null;
		var imageBlocks = new Array();
		var i = 0;
		for (var photoIndex in item.photos.photo) {
			if (images == null) {
				images = new Array();
				imageBlocks[imageBlocks.length] = images;
			}
			var photo = item.photos.photo[photoIndex];
			images[images.length] = photo;
			i++;
			if (i >= this.limit) {
				i = 0;
				images = null;
			}
		}
		this.imageBlocks = imageBlocks;
	//	alert('loaded [' + this.imageBlocks.length + '] blocks, [' + item.photos.photo.length + '] images.');
		this.blockIndex = 0;
	}

	this.speed = 0.05;
	this.refreshCounter = 0;

	this.onTimer = function() {
		for (var i = 0; i < 100; i++) {
			var div = document.getElementById('image-' + i);
			if (div == null)
				break;
			var direction = div.getAttribute("currentDirection");
			if (direction == null) {
				if (Math.random() > 0.5)
					direction = 'in';
				else
					direction = 'out';
			}
			var currentValue = div.getAttribute("currentValue");
			if (currentValue == null)
				currentValue = Math.random();
			else
				currentValue = new Number(currentValue);
			if (direction == 'in') {
				currentValue += this.speed;
				if (currentValue > 1) {
					direction = 'out';
					currentValue = 1.0;
				}
			} else if (direction == 'out') {
				currentValue -= this.speed;
				if (currentValue < 0.0) {
					direction = 'in';
					currentValue = 0.0;
					var image = this.imageBlocks[this.blockIndex][i];
					if (image != null) {
						if ((typeof(image.blockIndex) == 'undefined') || (image.blockIndex == null)) {
							image.blockIndex = 1;
						} else
							image.blockIndex++;
						if (image.blockIndex >= this.imageBlocks.length)
							image.blockIndex = 0;
						image = this.imageBlocks[image.blockIndex][i];
						if ((typeof(image) != 'undefined') && (image != null)) {
							var e = document.getElementById('image-src-' + i);
							if (e != null) {
								e.setAttribute("src", this.buildImageURL(image));
							}
						}
					}
				}
			}
			div.style.opacity = '' + currentValue;
			div.style.filter = 'alpha(opacity = ' + Math.floor(currentValue * 100) + ')';
			div.setAttribute("currentDirection", direction);
			div.setAttribute("currentValue", currentValue);
		}
		setTimeout('imageThrower.onTimer();', 100); 
		this.refreshCounter++;
		if ((this.refreshCounter % 100) == 0)
			setTimeout('imageThrower.refresh()', 100);
	}

	this.render = function() {
		var block = document.getElementById(this.blockId);
		if (block == null)
			return;
		this.removeAllChildren(block);
		var line = '';
		var index = 0;
		for (var i in this.imageBlocks[this.blockIndex]) {
			var image = this.imageBlocks[this.blockIndex][i];
			var img = document.createElement("img");
			var div = document.createElement("div");
			var x = Math.floor(Math.random() * (this.width)) - 100;
			var y = Math.floor(Math.random() * (this.height)) - 100;
			if (x < 0)
				x = 0;
			if (y < 0)
				y = 0;
			var width = this.width - x;
			var height = this.height - y;

			line += 'x: ' + x + ', y: ' + y + '\n';
			img.setAttribute("id", "image-src-" + index);
			img.setAttribute("src", this.buildImageURL(image));
			div.setAttribute("style", "position: absolute; margin-left: " + x + "px; margin-top: " + y + "px; width: " + width + "px; height: " + height + "px; overflow: hidden;");
			div.setAttribute("id", "image-" + index);
			div.appendChild(img);
			block.appendChild(div);
			index++;
			if (index > this.limit)
				break;
		}
	}

	this.buildImageURL = function(image) {
		return (
			'http://farm' + image.farm + '.static.flickr.com/' + image.server + '/' +
			image.id + '_' + image.secret + '_' + 'm' + '.jpg'
		);
	}

	this.removeAllChildren = function(node) {
		if (node == null)
			return;
		while (node.childNodes.length > 0)
			node.removeChild(node.firstChild);
	}

}



