// JavaScript Document
var bodyHeight = "";
var bodyWidth = "";

$(document).ready(function() {
	// set up images for lightbox gallery
	var animate = "null";
	// Show lightbox											
	$(".galThumbs li a").click(function() {
		$(document).scrollTop(0);
		var clickedThumb = $(this).find('img').attr('src');
		var lgImg = $(this).attr('href');
		var lgCoords = $(this).attr('coords').split(',');
		var lgH =Number(lgCoords[0]);
		var lgW =Number(lgCoords[1]);
		var galTitle = $(this).attr('rel');
		var imgToShow = new Image();
		var activeGal = $(this).parents('.list-of-images').attr('id');
		var galImgs = $(this).parents('.list-of-images').find('img');
		var imgArr = $.makeArray(galImgs);
		for(i=0; i<imgArr.length; i++) {
			var loopImg = $(imgArr[i]).attr('src');
			if(loopImg == clickedThumb) {
				imgNum = i + 1;
			}
		}
		galLength = imgArr.length;
		
		var lbContent = "<div id='lightbox'>&nbsp;</div><div class='lightboxImg default-lightboxImg'><div class='lightboxImgContent'><div id='lightboxImg' class='" + activeGal + "'></div><div id='lightbox-nav'><div id='lightbox-nav-prev'>< Previous</div><div id='lightbox-nav-next'>Next ></div></div><div class='lightbox-content-wrapper'><div class='lightbox-info'><h2>"+ galTitle +"</h2><p>Image <span id='imgNum'>" + imgNum + "</span> of " + galLength +"</p></div><div id='lightbox-close'><img src='/common/images/template/close_lb.gif' alt='close' /></div></div></div></div>";
		
		getBodySize();
		animate = "yes";
		$(lbContent).appendTo(document.body);
		$(imgToShow).load(function() {
			$('#lightboxImg').append(this);
			var img = $(this);
			$('#lightbox').height(bodyHeight).width(bodyWidth).show();
			setOptions(lgW, lgH, animate);
			if(imgNum-1 == 0) {
				$("#lightbox-nav-prev").addClass('inactive');
			}
			else if(imgNum == imgArr.length)  {
				$("#lightbox-nav-next").addClass('inactive');
			}
		}).attr('src', lgImg);
		return false;
	});

	// Close lightbox
	$("#lightbox-close, #lightbox").live('click', function() {
		$('.lightboxImg').animate({height:0}, 400, function() {
			$('.lightboxImg').remove();
			$('#lightbox').remove();
		});
	});
	$("#lightbox-nav").live('mouseover', function() {
		$("#lightbox-nav div").css({'visibility': 'visible'});
	});
	$("#lightbox-nav").live('mouseout', function() {
		$("#lightbox-nav div").css({'visibility': 'hidden'});
	});
	// Display next/previous image in lightbox
	$("#lightbox-nav div").live('click', function() {
		animate = 'no';
		var parentGal = $('#lightboxImg').attr('class');
		var currImg = $('#lightboxImg img').attr('src');
		
		var lgImgs = $('#' + parentGal).find('a');
		var lgImgArr = $.makeArray(lgImgs);
		for(i=0; i<lgImgArr.length; i++) {
			var lgLoopImg = $(lgImgArr[i]).attr('href');
			if(currImg.indexOf(lgLoopImg) >= 0) {
				var lgImgNum = i;
			}
		}
		if($(this).attr('id') == 'lightbox-nav-prev') {
			var nextImg = lgImgNum - 1;
		}
		else {
			var nextImg = lgImgNum + 1;
		}
		$('#lightboxImg img').remove();

		var nextImgSrc = $(lgImgArr[nextImg]).attr('href');
		var nextCoords = $(lgImgArr[nextImg]).attr('coords').split(',');
		var nextH = Number(nextCoords[0]);
		var nextW = Number(nextCoords[1]);
		var nextImgCapt = $(lgImgArr[nextImg]).attr('rel');
		var nextImgObject = '<img src="'+ nextImgSrc +'" />';
		
		if(nextImg >= 0 && nextImg < lgImgArr.length) {
			$("#lightbox-nav-prev, #lightbox-nav-next").removeClass('inactive');
			$(nextImgObject).load(function() {
				$('.lightboxImg #lightboxImg').append(nextImgObject);
				$('.lightboxImg #lightboxImg img').css({'opacity': 0, 'display': 'block'});

				var imgHeight = $('.lightboxImg #lightboxImg img').height();
				var imgWidth = $('.lightboxImg #lightboxImg img').width();
				$('.lightbox-info h2').html(nextImgCapt);
				setOptions(nextW, nextH, animate);
				$('#lightboxImg img').animate({'opacity': 1});
			});
			if(nextImg == 0) {
				$("#lightbox-nav-prev").addClass('inactive');
			}
			else if(nextImg == lgImgArr.length-1)  {
				$("#lightbox-nav-next").addClass('inactive');
			}
			$('#imgNum').text(nextImg + 1);
		}
	});
});


// Set up for lightbox window
	function setOptions(imgWidth, imgHeight, animate) {
		var lightboxWidth = imgWidth + 20;
		var lightboxHeight = imgHeight + 20;
		var bodyWidth = $('body').width();
		var lbLeft = Math.round((bodyWidth - lightboxWidth)/2);
		$('.lightboxImg').css({'width': lightboxWidth, 'left': lbLeft})
		if(animate == 'yes') {
			$('.lightboxImg').stop().animate({'height':lightboxHeight});
		}
		else {
			$('.lightboxImg').css({'height': lightboxHeight});
		}
		$('#lightbox-nav, .lightbox-info').show();
		$('.lightboxImgContent').css({'visibility': 'visible'});
	}
	
	function getBodySize() {
		bodyHeight = $(document).height();
		bodyWidth = $(document).width();
	}

$(window).resize(function(){
	getBodySize();
	$('#lightbox').css({'height': bodyHeight, 'width': bodyWidth});
});

