$.fn.imglightbox = function(){
	var elems = this;
	var style = '<style type="text/css" id="jlightboxstyle">#lb {overflow:hidden} #lb.loading {background: #fff url(../Images/load.gif) no-repeat center center !important} #lb p,#lb h2 {display:none;margin:0;padding:0;margin-right:10px;margin-left:10px;padding-bottom:10px;} #lb a {color:#000;display:inline;float:left;} #lbclose {float:right !important} #p {margin-right:5px} #overlay {background:#000;} #lb #lbtitle {font-size:16px;} #lb #nav {overflow:hidden;padding:0;} </style>';
	$('#jlightboxstyle').remove();
	$('head').append(style);
	elems.each(function(i){
		return $(this).click(function(){
			new imglb(this,elems);
			return false;
		});
	});
};

var imglb = function(pr,e){
	
	this.wx = $(window).width();
	this.wy = $(window).height();
	this.scroll = window.scrollY == undefined ? document.documentElement.scrollTop : window.scrollY;
	this.wmy = this.wy/2; // midpoint y
	this.wmx = this.wx/2; // midpoint x
	this.sbw = 20; //small box width
	this.sbh = 20; //small box height
	this.sbst = this.wmy + this.scroll; // the top of the small box start
	this.sbsl = this.wmx; // the left of the small box start
	this.sbat = this.sbst-(this.sbh/2); // the top point the small box animates to on start
	this.sbal = this.sbsl-(this.sbw/2); // the left point the small box animates to on start
	this.minheight = '40px'; // the height of the small box
	this.minwidth = '40px'; // the width of the small box
	this.src = $(pr).attr('href');
	this.title = $.trim(pr.getAttribute('title')) == '' || pr.getAttribute('title') == undefined ? undefined : pr.getAttribute('title');
	this.e = e; // all elements matching current set
	this.pr = $('body');
	this.lbfinaladd = 30;
	this.lb,this.p,this.n;
	this.animatespeed = 800;
	this.position = 'absolute';
	
	this.changelb = changelb;
	this.newlb = newlb;
	this.lightbox = lightbox;
	this.close = closelb;
	this.navsetup = lbnav;
	this.navsetup();
	var pr = this;
	if ($('#lb').size() > 0){
		this.lb = $('#lb').css({overflow:'hidden'});
		return this.changelb();
	} else {
		return this.newlb();
	}
}

var lbnav = function(){
	var pr = this;
	this.e.each(function(i){
		src = pr.src.replace(/ /g,'%20');
		if (src.indexOf(escape($(this).attr('href').toString())) > -1){ // this is the one
			p = i == 0 ? pr.e.eq(pr.e.size()-1) : pr.e.eq(i-1);
			n = i == pr.e.size()-1 ? pr.e.eq(0) : pr.e.eq(i+1);
		}
	});
	this.p = p;
	this.n = n;
}

var newlb = function(){
	$('<div id="overlay"></div>').appendTo(this.pr);
	$('#overlay').css({display:'block',position:'fixed',top:0,left:0,bottom:0,height:$(window).height()+'px',width:$(window).width()+'px',opacity:'0.8'});
	if ($.browser.msie == true){ //damn <IE6
		if ($.browser.version < 7){
			$('select').hide();
			this.position = 'absolute';
			$('#overlay').css({position:this.position});
		}
	}
	this.lb = $('<div id="lb" class="loading" style="position:'+this.position+';width:'+this.minwidth+';height:'+this.minheight+';"></div>').appendTo(this.pr);
	return this.lightbox();
};

var closelb = function(){
	$('#lb').fadeOut(100,function(){
		$(this).remove();
		$('#overlay').remove();
		$('select').fadeIn(500);
	})
	return false;
};

var changelb = function(){
	this.sbst = $('#lb').css('top');
	this.sbsl = $('#lb').css('left');
	return this.lightbox();
}

var lightbox = function(){
	var pr = this;
	pr.lb.css({top:pr.sbst,left:pr.sbsl}).attr('class','loading').fadeIn(500,function(){
		var im = $('<img src="'+pr.src+'" style="display:none"/>').css({margin:'10px'});
		im.load(function(){
			var im = this;	
			var x = $(im).width()+20;
			var y = $(im).height()+20
			
			var lbtitleheight = 0;
			if (pr.title != undefined){
				var lbtitle = $('<h2 id="lbtitle">'+pr.title+'</h2>').css({width:x-20+'px'}).appendTo(pr.lb);
				lbtitleheight = lbtitle.height() + 10;
			}
			var lbnav = $('<p id="nav"><a href="'+pr.p.attr('href')+'" title="'+pr.p.attr('title')+'" id="p">previous</a><a href="'+pr.n.attr('href')+'" title="'+pr.n.attr('title')+'" id="n">next</a><a href="#" id="lbclose">close</a></p>').css({width:x-20+'px'}).appendTo(pr.lb);
			var lbnavheight = lbnav.height() + 10;
			lbnav.hide();
			
			var it = y < pr.wy ? (pr.wy-y)/2+pr.scroll : 0 // top point of pr
			it = it - lbtitleheight - lbnavheight < 0 ? 0 : it - lbtitleheight - lbnavheight;
			var il = x < pr.wx ? (pr.wx-x)/2 : 0 // left point of pr

			pr.lb.css({backgroundColor:'#fff'}).animate({width:x,left:il},pr.animatespeed,null,function(){
				pr.lb.animate({height:y,top:it},pr.animatespeed,null,function(){
					$(this).attr('class','');
					var olh = $('body').height() < $('#lb').height() ? $('#lb').height()+10 : $('body').height();		
					if (olh < $(window).height()){
						olh = $(window).height();
					}			
					$('#overlay').css({height:olh+'px'});
					$(im).fadeIn();
					$('#lbtitle').fadeIn(1000);
					$('p#nav').fadeIn(1000);
					$('#lbclose').click(pr.close);
					$(this).animate({height:y+lbnavheight+lbtitleheight+'px'},500);
					$('#p,#n').click(function(){
						var to = this;
						// shrink the lightbox to hide the controls
						pr.lb.animate({height:y},500,null,function(){
							$('#lb').children().fadeOut(500,function(){
								$('#lb').html('');
								new imglb(to,pr.e);
							});
						});
						return false;
					});
				});
			});
		}).appendTo(pr.lb);
	});		
	return this.lb;
}
