var lightbox;

function lightbox(popup, image, caption, close, links, url)
{
	async_rpc(url, createDelegate(this, 'processfeed'));
	
	this.popup = domcheck(popup);
	
	this.image_name = image;
	this.caption_name = caption;
	this.close_name = close;
	this.links_name = links;
	
	this.loadcount = 0;
	this.currentspot = 0;
	
	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]) {
			}
		} else if ( e.srcElement ) {
			e.srcElement.obj.loaded = true;
			if ( e.srcElement.obj == this.photos[0]) {
			}
		}
			
		this.loadcount++;
	}
	
	this.show = function(currentspot)
	{
		this.loaded = false;
		
		if ( typeof(currentspot) != 'undefined' ) {
			this.currentspot = currentspot;
		}
		
		this.blackout = document.createElement('div');
		this.blackout.style.position = 'absolute';
		this.blackout.style.top = '0px';
		this.blackout.style.left = '0px';
		
		if ( window.innerWidth ) {
			this.blackout.style.width = document.body.clientWidth + 'px'
		} else {
			if ( document.documentElement.clientWidth > document.body.clientWidth ) {
				this.blackout.style.width = document.documentElement.clientWidth + 'px';
			} else {
				this.blackout.style.width = document.body.clientWidth + 'px';
			}
		}
		
		if ( window.innerHeight ) {
			if ( window.innerHeight > document.body.clientHeight ) {
				this.blackout.style.height = window.innerHeight + 'px'
			} else {
				this.blackout.style.height = document.body.clientHeight + 'px'
			}
		} else {
			if ( document.documentElement.clientHeight > document.body.clientHeight ) {
				this.blackout.style.height = document.documentElement.clientHeight + 'px';
			} else {
				this.blackout.style.height = document.body.clientHeight + 'px';
			}
		}
		
		this.blackout.style.backgroundColor = '#000';
		this.blackout.style.opacity = 0;
		this.blackout.style.filter = 'progid:DXImageTransform.Microsoft.Alpha(opacity=0)';
		this.blackout.style.zIndex = 100;
		
		document.body.appendChild(this.blackout);
			
		new Activa_FX(this.blackout, 'FadeIn', 150, null, 0.75);
		
		this.clone = this.popup.cloneNode(true);
		this.clone.style.zIndex = 150;
		document.body.appendChild(this.clone);
		
		this.image = find_by_id(this.clone, this.image_name);
		this.image.style.filter = 'progid:DXImageTransform.Microsoft.Alpha(opacity=0)';
		
		this.caption = find_by_id(this.clone, this.caption_name);
		this.closebar = find_by_id(this.clone, this.close_name);
		this.links = find_by_id(this.clone, this.links_name);
		
		height = window.innerHeight ? window.innerHeight : document.documentElement.clientHeight;
		width = window.innerWidth ? window.innerWidth : document.documentElement.clientWidth;
		
		if ( typeof(window.pageYOffset) != 'undefined' ) {
			this.clone.style.top = (window.pageYOffset + ((height/2) - (this.clone.offsetHeight/2))) + 'px';
		} else {
			this.clone.style.top = (document.documentElement.scrollTop + ((height/2) - (this.clone.offsetHeight/2))) + 'px';
		}
		
		if ( typeof(window.pageXOffset) != 'undefined' ) {
			this.clone.style.left = (window.pageXOffset + ((width/2) - (this.clone.offsetWidth/2))) + 'px';
		} else {
			this.clone.style.left = (document.documentElement.scrollLeft + ((width/2) - (this.clone.offsetWidth/2))) + 'px';
		}

		this.clone.style.display = '';
		
		this.fx = new Activa_FX(this.clone);
		this.boxUp();
	}
	
	this.boxUp = function()
	{
		this.fx.startEngine('BoxUp', 500, createDelegate(this, 'boxOut'));
	}
	
	this.boxOut = function()
	{
		this.fx.startEngine('BoxOut', 500, createDelegate(lightbox, 'next'));
	}
	
	this.previous = function()
	{
		if ( !this.loaded ) {
			this.fadeIn();
			
			this.loaded = true;
		} else {
			if ( this.photos[this.currentspot - 1] ) {
				this.currentspot--;
			} else if ( this.photos[this.photos.length - 1] ) {
				this.currentspot = this.photos.length - 1;
			}
			this.preload(this.currentspot);
			this.fadeOut();
		}
	}
	
	this.next = function()
	{
		if ( !this.loaded ) {
			this.fadeIn();
			
			this.loaded = true;
		} else {
			if ( this.photos[this.currentspot + 1] ) {
				this.currentspot++;
			} else if ( this.photos[0] ) {
				this.currentspot = 0;
			}
			this.preload(this.currentspot);
			this.fadeOut();
		}
	}
	
	this.fadeIn = function()
	{
		this._curWidth = this.clone.style.width.replace('px', '')
		this._curHeight = this.clone.style.height.replace('px', '')
		
		widthChange =  ((this.photos[this.currentspot].width+30) - this._curWidth) / 2;
		heightChange =  ((this.photos[this.currentspot].height+100) - this._curHeight) / 2;
		
		this.clone.style.width = (this.photos[this.currentspot].width+30)+'px';
		this.clone.style.height = (this.photos[this.currentspot].height+100)+'px';
		
		this.clone.style.left = (this.clone.offsetLeft - widthChange)+'px';
		this.clone.style.top = (this.clone.offsetTop - heightChange)+'px';
		
		this.image.style.width = this.photos[this.currentspot].width+'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;
		
		this.caption.style.width = (this.photos[this.currentspot].width)+'px';
		this.closebar.style.width = (this.photos[this.currentspot].width)+'px';
		this.links.style.width = (this.photos[this.currentspot].width)+'px';
		
		new Activa_FX(this.image, 'FadeIn', 300);
	}
	
	this.fadeOut = function()
	{
		new Activa_FX(this.image, 'FadeOut', 300, createDelegate(this, 'fadeIn'));
	}
	
	this.fadeOutBlackout = function()
	{
		new Activa_FX(this.blackout, 'FadeOut', 150, createDelegate(this, 'removeBlackout'));
	}
	
	this.removeBlackout = function()
	{
		this.blackout.parentNode.removeChild(this.blackout);
	}
	
	this.fadeOutPopup = function()
	{
		new Activa_FX(this.clone, 'FadeOut', 100, createDelegate(this, 'removePopup'));
	}
	
	this.removePopup = function()
	{
		this.clone.style.left = -1000+'px';
		this.clone.style.top = -1000+'px';
		
		this.fadeOutBlackout();
	}
	
	this.close = function()
	{
		this.fadeOutPopup();
		
		this.currentspot = -1;
	}
}