function LighterBox() {}
LighterBox.instance = null;
LighterBox.initialized = false;
LighterBox.isLoading = false;
LighterBox.cancel = false;
LighterBox.create = function(href)
{
	LighterBox.init();
	
	var instance = (LighterBox.instance)? LighterBox.instance : (LighterBox.instance = new LighterBox());
	instance.href = href;
	
	$('#lighterbox').css({ width: '200px', height: '200px' });
	
	instance.show().position();
	
	return instance;
};
LighterBox.init = function()
{
	LighterBox.isLoading = false;
	LighterBox.cancel = false;
	
	if (LighterBox.initialized) { return; }
	LighterBox.initialized = true;
	
	var isIE6 = (jQuery.browser.msie && parseInt(jQuery.browser.version) === 6);
	
	jQuery('<div id="lighterbox-screen"></div>').appendTo(document.body).css({ position: (!isIE6)? 'fixed' : 'absolute', opacity: 0.7 }).bgiframe();
	jQuery('<div id="lighterbox"><div id="lighterbox-img"></div><div id="lighterbox-loading"></div><div id="lighterbox-close" title="close"></div></div>').appendTo(document.body);
	jQuery('#lighterbox-close').click(LighterBox.onCloseClick);
	
	if (isIE6)
	{
		jQuery(window).scroll(LighterBox.onScroll);
	}
};
LighterBox.position = function()
{
	if (LighterBox.cancel) { return this; }
	
	var $win = jQuery(window);
	var $lb = jQuery('#lighterbox');
	
	/*var y = $win.height() - $lb.outerHeight();
		y = (y < 0)? 0 : y/2;
		y = Math.round(y + $win.scrollTop());*/
	
	var x = $win.width() - $lb.outerWidth();
		x = (x < 0)? 0 : x/2;
		x = Math.round(x + $win.scrollLeft());
	
	if (!jQuery.browser.msie || parseInt(jQuery.browser.version) > 6)
	{
		$lb.css({ left: x+'px' });
	}
	else
	{
		var y = $win.scrollTop() + 40;
		$lb.css({ top: y+'px', left: x+'px' });
	}
};
LighterBox.positionScreen = function()
{
	if (!jQuery.browser.msie || parseInt(jQuery.browser.version) > 6) { return; }
	jQuery('#lighterbox-screen').css('top', jQuery(window).scrollTop()+'px');
};
LighterBox.onScroll = function(evt)
{
	LighterBox.positionScreen();
	LighterBox.position();
};
LighterBox.onCloseClick = function(evt)
{
	evt.preventDefault();
	if (LighterBox.isLoading)
	{
		LighterBox.isLoading = false;
		LighterBox.cancel = true;
	}
	jQuery('#lighterbox-img').empty();
	jQuery('#lighterbox').hide();
	jQuery('#lighterbox-screen').fadeOut();
};
LighterBox.onScreenShow = function()
{
	if (LighterBox.cancel) { return this; }
	
	jQuery('#lighterbox-img').hide().empty();
	jQuery('#lighterbox-loading').show();
	jQuery('#lighterbox').show();
	
	LighterBox.instance.loadImage();
};
LighterBox.onImageLoad = function()
{
	if (LighterBox.cancel) { return this; }
	
	jQuery('#lighterbox-loading').hide();
	
	var src = this.src;
	var w = this.width;
	var h = this.height;
	
	this.onload = function(){};
	this.onerror = function(){};
	
	jQuery('#lighterbox').animate(
		{ width: w+'px', height: h+'px' },
		{
			duration: 'fast',
			step: LighterBox.position,
			complete: function() { LighterBox.onResizeComplete.call(null, src); }
		}
	);
};
LighterBox.onResizeComplete = function(src)
{
	if (LighterBox.cancel) { return this; }
	jQuery('#lighterbox-img').show().append('<img src="' + src + '" alt="" />');
};

LighterBox.prototype.show = function()
{
	if (LighterBox.cancel) { return this; }
	LighterBox.positionScreen();
	
	jQuery('#lighterbox-screen').fadeIn('fast', LighterBox.onScreenShow);
	return this;
}	
LighterBox.prototype.position = function()
{
	if (LighterBox.cancel) { return this; }
	LighterBox.position();
	return this;
};

LighterBox.prototype.loadImage = function(href)
{
	if (LighterBox.cancel || LighterBox.isLoading) { return; }
	LighterBox.isLoading = true;
	
	var img = new Image();
	var onImageLoad = function() { LighterBox.onImageLoad.call(img); };
	img.onload = onImageLoad;
	img.onerror = onImageLoad;
	img.src = this.href;
	return this;
};


jQuery('a.lighterbox').click(function(evt)
{
	evt.preventDefault();
	var lb = LighterBox.create(this.href);
});