var slideshow;

function slideshow(popup, imagebox, image, caption, url)
{
	async_rpc(url, createDelegate(this, 'processfeed'));
	
	this.popup = domcheck(popup);
	
	this.imagebox_name = imagebox;
	this.image_name = image;
	this.caption_name = caption;
	
	this.imagebox = find_by_id(this.popup, this.imagebox_name);
	this.imagebox.style.filter = 'progid:DXImageTransform.Microsoft.Alpha(opacity=0)';
	this.image = find_by_id(this.imagebox, this.image_name);
	this.caption = find_by_id(this.imagebox, this.caption_name);
	
	this.loadcount = 0;
	this.currentspot = -1;
	
	this.url = url;
	
	this.processfeed = function(res)
	{
		this.photos = res;
		this.preload(0);
		this.preload(1);
	}
	
	this.preload = function(index)
	{
		if ( index >= this.photos.length ) {
			return;
		}

		var cur = this.photos[index];
		cur.elem = document.createElement('img');

		cur.elem.obj = cur;

		registerEvent(cur.elem, 'load', createDelegate(this, 'imageload'));
		
		cur.elem.style.position = 'absolute';
		cur.elem.style.top = '-5000px';
		cur.elem.style.left = '-5000px';
		
		cur.loaded = false;
		document.body.appendChild(cur.elem);
		
		cur.elem.src = cur.image;
	}
	
	this.imageload = function(e)
	{
		if ( e.currentTarget ) {
			e.currentTarget.obj.loaded = true;
			if ( e.currentTarget.obj == this.photos[0]) {
				this.next();
			}
		} else if ( e.srcElement ) {
			e.srcElement.obj.loaded = true;
			if ( e.srcElement.obj == this.photos[0]) {
				this.next();
			}
		}
			
		this.loadcount++;
	}
	
	this.previous = function()
	{
		if ( this.photos[this.currentspot] ) {
			this.currentspot--;
			
			if ( this.photos[this.currentspot] ) {
				this.fadeOut();
			} else {
				this.currentspot++;
			}
		} else {
			this.currentspot--;
			
			if ( this.photos[this.currentspot] ) {
				this.fadeIn();
			} else {
				this.currentspot++;
			}
		}
	}
	
	this.next = function()
	{
		if ( this.photos[this.currentspot] ) {
			this.currentspot++;
			
			if ( this.photos[this.currentspot] ) {
				this.fadeOut();
			} else {
				this.currentspot--;
			}
		} else {
			this.currentspot++;
			
			if ( this.photos[this.currentspot] ) {
				this.fadeIn();
			} else {
				this.currentspot--;
			}
		}
	}
	
	this.fadeIn = function()
	{
		//this.imagebox.style.width = this.photos[this.currentspot].width+'px';
		this.image.style.width = this.photos[this.currentspot].width+'px';
		//this.imagebox.style.height = this.photos[this.currentspot].height+'px';
		this.image.style.height = this.photos[this.currentspot].height+'px';
		
		this.image.src = this.photos[this.currentspot].image;
		
		this.caption.innerHTML = this.photos[this.currentspot].caption;
		
		new Activa_FX(this.imagebox, 'FadeIn', 300);
	}
	
	this.fadeOut = function()
	{
		new Activa_FX(this.imagebox, 'FadeOut', 300, createDelegate(this, 'fadeIn'));
	}
}