/**Designed By Michael Howard with JQuery*
/****************************************/

$(document).ready(function() {
	var light = $('#lightbox'); var overlay = $('#overlay');
	var exists = false; var changed = false;
	var originalW = 400; var originalH = 200;
	var originalS = topPos("scroll");
	var margins = 20; var old; var P;
	var lightWidth; var lightHeight; var topMargin;
	var links = $('#info2 a');
	overlay.css({'opacity' : .7,'filter' : 'alpha(opacity=70)'});
	light.append('<span id="close">Close X</span><p id="altText"><span></span></p><div id="leftA"></div><div id="rightA"></div>')
		.css({'marginTop' : topPos(200), 'marginLeft' : -200});
	altTextP = $('#altText');
	var RandL = $('#rightA,#leftA');
	RandL.css({'opacity' : .9,'filter' : 'alpha(opacity=80)'});
	
	$(window).resize(function() {
		var topMargin = topPos(originalH) + topPos("scroll");
		var checkWindow = topPos(false);
		if (checkWindow[1] <= originalH) {
			light.stop().css({'marginTop' : topPos("scroll")}).fadeTo(100,1);
			checkHeight();
		} else {
			light.stop().animate({'marginTop' : topMargin, 'marginLeft' : '-' + Math.ceil(originalW / 2) + 'px'}).fadeTo(100,1);
			checkHeight();
		}
	});
	
	light.hover(function() {
		altTextP.stop().animate({'height' : '20px'});
		RandL.stop().animate({'width' : '40px'});
		},function() {
		altTextP.stop().animate({'height' : '1px'});
		RandL.stop().animate({'width' : '1px'});
	});
	
	links.bind('click',function(e) {
		e.preventDefault();
		var newImgSrc = $(this).attr('href');
		var newImage = $('<img id="old" src="' + newImgSrc + '" />');
		P = links.index(this);
		originalS = topPos("scroll");
		newImage.hide();
		light.append(newImage).css('marginTop',originalS);
		old = $('#old');
		var grabText = $('a[href="' + newImgSrc + '"] img').attr('alt');
		$('#altText span').text(grabText);
		overlay.fadeIn(800,function(){$(this).css({'opacity' : .7,'filter' : 'alpha(opacity=70)'})}); light.show(1000);
		newImage.load(function() {
		lightWidth = newImage.width(); lightHeight = newImage.height();
		originalW = lightWidth; originalH = lightHeight;
		topMargin = topPos(originalH) + topPos("scroll");
		newImg(newImage);
		});
	});
	
	$('#leftA').bind('click',function() {
		if (P == 0) { P = links.length - 1;
		} else {
			P--;
		}
		old.remove();
		var newImgSrc = links.eq(P).attr('href');
		var newImage = $('<img id="old" src="' + newImgSrc + '" />');
		newImage.hide();
		light.append(newImage)
		old = $('#old');
		var grabText = $('a[href="' + newImgSrc + '"] img').attr('alt');
		$('#altText span').text(grabText);
		newImage.load(function() {
		lightWidth = newImage.width(); lightHeight = newImage.height();
		originalW = lightWidth; originalH = lightHeight;
		topMargin = topPos(originalH) + topPos("scroll");
		newImg(newImage);
		});
	});
	
	$('#rightA').bind('click',function() {
		if (P == links.length - 1) { P = 0;
		} else {
			P++;
		}
		old.remove();
		var newImgSrc = links.eq(P).attr('href');
		var newImage = $('<img id="old" src="' + newImgSrc + '" />');
		newImage.hide();
		light.append(newImage)
		old = $('#old');
		var grabText = $('a[href="' + newImgSrc + '"] img').attr('alt');
		$('#altText span').text(grabText);
		newImage.load(function() {
		lightWidth = newImage.width(); lightHeight = newImage.height();
		originalW = lightWidth; originalH = lightHeight;
		topMargin = topPos(originalH) + topPos("scroll");
		newImg(newImage);
		});
	});
	
	$('#old').live('click',function() {
		closeLight();
	});
	
	$('#close').click(function() {
		closeLight();
	});
	
	$('#overlay').click(function() {
		closeLight();
	});
	
	$(window).scroll(function() {
		if (exists == false) return;
		var topMargin = topPos(originalH) + topPos("scroll");
		if (topPos(originalH) <= 0) {
			light.stop().animate({'marginTop' : topPos("scroll")});
		} else {
		light.stop().animate({'marginTop' : topMargin});
		}
	});
	
function checkHeight() {
/*Check to see if browser window is smaller than image height*/
/*If so, resize image to fit into browser window*/
	if (exists == false) return;
	var newHeight = topPos(false);
	var newWidth = Math.ceil(newHeight[0] / 2);
	if (topPos(originalH) <= 0) {
		var decrease = newHeight[1] - 20;
		old.height(decrease);		
		var a = old.width();
		var aa = Math.ceil(a / 2);
		var b = old.height() + 10;
		changed = true;
		light.css({'width' : a, 'height' : b, 'marginTop' : topPos("scroll"), 'marginLeft' : '-' + aa + 'px'});
		RandL.css({'top' : b / 2});
		return;
	} else {
		if (changed == true) {
		old.height(originalH - margins);
		light.css({'width' : originalW - margins, 'height' : originalH - margins, 'marginTop' : topPos(originalH), 'marginLeft' : '-' + Math.ceil(originalW / 2) + 'px'});
		changed = false;
		RandL.css({'top' : originalH / 2});
		return;		
		}
	}
}
function closeLight() {
/*Closes the lightbox and overlay Divs*/
	window.scrollTo(0,originalS);
	overlay.fadeOut(800);
	light.hide(600,function() {$(this).css('marginTop',originalS)});
	$('#old').remove();
	$('#close').css({'width' : '1px','height' : '1px'});
	exists = false;
}
function newImg(n) {
	light.animate({'width' : lightWidth, 'marginLeft' : '-' + Math.ceil(lightWidth / 2) + 'px', 'height' : lightHeight, 'marginTop' : topMargin
		},function() {originalW += margins; originalH += margins;
			n.show(); exists = true; checkHeight();
		});
	$('#close').css({'width' : '30px'}).animate({'height' : '100px'},1000);
	RandL.css({'top' : originalH / 2});
}
function topPos(H) {
/*Returns browser scroll position, height, width,
/*or equation to center lightbox vertically*/
	if (H == "scroll") return $(window).scrollTop();
	var a = [$(window).width(),$(window).height()];
	if (H == false) return a;
	var Hdiv = Math.ceil(H / 2);
	var last = Math.ceil(a[1] / 2) - Hdiv;
	return last;}
});
