function LightBox()
{
	var _this = this;
	this.show = function (prev,next)
	{
		_this.prev = $(prev);
		_this.next = $(next);		
		_this.overlay.fadeIn(0);
		_this.popup.fadeIn(600);		
	}

	this.hide = function()
	{
		_this.overlay.fadeOut(600);
		_this.popup.fadeOut(1000);

	}

	$(function(){
	
		$(document.body).append("<div id='overlay'></div>");

		$('#overlay').after("<div class='popup'>"+
			"<div class='tools'>"+
				"<a class='close' href='#' rel=''></a>"+					
				"<a class='next'  href='#' rel=''></a>"+
				"<a class='prev'  href='#' rel=''></a>"+			
						
			"</div><br clear=all>"+
			"<div class='content'></div>"+
		"</div>");
	
		
	
		$('.popup .tools a.close').click(function(){
			_this.hide();
			return false;						
		});

		$('.popup .tools a.prev').click(function(){
			_this.click_this(_this.prev);			
			return false;						
		});
		
		$('.popup .tools a.next').click(function(){
			_this.click_this(_this.next);
			return false;			
		});				

		_this.overlay	 = $('#overlay');
		_this.popup		 = $('.popup');
		_this.content	 = $('.content',_this.popup);						
	
		_this.click_this = 	function (obj)
		{
			var obj = $(obj);
			var img_src = $('img',obj).attr('src').replace("_sm","");

			var url  = $(obj).attr('href');
			if (url=="")
			{
				alert("введите url");
				return false;
			}
			var prev = $($(obj).attr('prev'));
			var next = $($(obj).attr('next'));		
			
			
			_this.content.load(url,function(responseText, textStatus, XMLHttpRequest)
			{
				if (textStatus=="error")
				{
					alert("файл '"+url+"' не найден..");
					return ;
				}
				if (responseText.length==0)
				{
					_this.content.html("<img src='"+img_src+"'>");
				}
				_this.show(prev,next);
			});
		
		}

	
		$(".lightbox").click(function(){
			_this.click_this(this);
			return false;
		});
		
	});
}

var a = new LightBox();
