$.lightBox = {
	options:		{
						excess_vert:	32
					},
	current:		0,
	photos:			{},
	descs:			{},
	shadow_open:	false,
	init:			function(e,img) {
						e.preventDefault();
						// Check to see if we've alread set the array by checking the 0th element
						if (!this.photos[0]) {
							this.photos = $('.thumbrow').find('.pic');
							this.descs  = $('.thumbrow').find('.short_desc');
							$('body').append('<div id="shadow"></div><div id="lightBox"><div id="description"></div><div id="click_to_close">[<a href="#" class="prev">&larr;</a>] [<a href="#" class="next">&rarr;</a>] [<a href="#" class="close">x</a>]</div><img id="photo" src="" alt="" /></div>');
						}
						$('#shadow').css({
							width: '100%',
							height: '100%',
							opacity: '0.6'
						}).fadeIn('fast');
						this._find_current(img);
						this._set_image();
						this.shadow_open = true;
						this._events();
						return;
					},
	_set_image:		function() {
						var image = new Image();
						image.onload = function(){
							var pic = {
								w: image.width,
								h: image.height
							};
							var doc = {
								w: $(window).width(),
								h: $(window).height()
							}
							var pos = {
								left:	(doc.w/2) - (pic.w/2) - 10,
								top:	(doc.h/2) - (pic.h/2) - 10 + $(window).scrollTop()
							};
							pos.left = (pos.left > 0 ? Math.round(pos.left) : 0);
							pos.top  = (pos.top  > 0 ? Math.round(pos.top)  : 0);
							((pic.h + $.lightBox.options.excess_vert) > doc.h ? $('#shadow').css('height', (pic.h + $.lightBox.options.excess_vert)) : null);
							$('#photo').attr('src',image.src).fadeIn();
							$('#description').html($($.lightBox.descs[$.lightBox.current]).html());
							$('#lightBox').animate({
								left:		pos.left+'px',
								top:		pos.top+'px',
								height:		pic.h+23+'px',
								width:		pic.w+'px'
							}).fadeIn('fast');
							$.post('../../photo_ajax.php', { name: image.src });
						};
						// Yes, set the src AFTER the onload function!
						image.src = $.lightBox.photos[$.lightBox.current];
					},
	_find_current:	function(img) {
						$.each(this.photos, function(i,v){
							if (img == v) {
								$.lightBox.current = i;
								return;
							}
						});
					},
	_events:		function() {
						$(document).keydown(function(e){
							switch (e.keyCode) {
								// right
								case 39:
									e.preventDefault();
									$.lightBox.move('forward');
									break;
								// left
								case 37:
									e.preventDefault();
									$.lightBox.move('backward');
									break;
								// x, esc
								case 88:
								case 27:
									e.preventDefault();
									$.lightBox.close_layer();
									break;
							}
						});
						$('.close').click(function(e){
							e.preventDefault();
							$.lightBox.close_layer();
						});
						$('#photo').click(function(e){
							e.preventDefault();
							$.lightBox.close_layer();
						});
						$('.prev').click(function(e){
							e.preventDefault();
							$.lightBox.move('backward');
						});
						$('.next').click(function(e){
							e.preventDefault();
							$.lightBox.move('forward');
						});
						$('#lightBox').hover(
							function(){
								$('#click_to_close').fadeIn('fast');
							},
							function(){
								$('#click_to_close').fadeOut('fast');
							}
						);	
					},
	move:			function(dir) {
						if (dir == 'forward') {
							this.current = (this.current == (this.photos.size()-1) ? 0 : this.current+1);
						} else if (dir == 'backward') {
							this.current = (this.current == 0 ? this.photos.size()-1 : this.current-1);
						}
						$('#photo').fadeOut('fast',function(){
							$('#photo').attr('src','');
							$.lightBox._set_image();
						})
					},
	close_layer:	function() {
						$('#lightBox').fadeOut('fast',function(){
							$('#shadow').fadeOut('fast',function(){
								$('#photo').attr('src','');
							});
						});
						this.shadow_open = false;
						$(document).unbind();
					}
}

$(document).ready(function(){
	$('a.pic').click(function(e){
		$.lightBox.init(e,this);
		return false;
	});
});