$(function(){
	$('.buttonbox').mouseover(function(){
			$(this).children('.activebox').css('background-position','-215px 0px');
			$(this).children('.activebox').stop().animate(
				{backgroundPosition:"0px 0px"}, 
				{duration:300}
			);
	});
	
	$('.buttonbox').mouseout(function(){
			$(this).children('.activebox').fadeTo('200',0, function(){
				$(this).css('background-position','-215px 0px');
				$(this).fadeTo('0',100)
			});
		});
	
	
	
/****************************************************
	Attach Lightbox
****************************************************/
	//Load all the images that need to be loaded.
	lightBoxLoader= new Image(); 
  lightBoxLoader.src="/images/lightbox/lightbox-load.gif";
	lightboxclose= new Image(); 
	lightboxclose.src="/images/lightbox/close.png"; 
	lightboxinfo= new Image(); 
	lightboxinfo.src="/images/lightbox/info.png"; 
	lightboxprev= new Image(); 
	lightboxprev.src="/images/lightbox/previous.png"; 
	lightboxnext= new Image(); 
	lightboxnext.src="/images/lightbox/next.png"; 
	
	//add lightbox functionality to any links to images. 
	$(".entry a").each(function(){
			linkPath = this.href;
			linkArray = linkPath.split('.');
			ext = linkArray[linkArray.length-1];
			ext = ext.toLowerCase();
			//alert(ext);
			if(ext == 'jpg' || ext == 'png' || ext == 'gif' || ext == 'jpeg'){
				altText = $(this).children('img').attr('alt');
				$(this).addClass('zoom');
				$(this).attr('title',altText);
			}
		});
	
	//attach lightbox to the zoom class
	$('.zoom').click(function(){
		lightBoxZoom(this);
		return false;
	});


});

function loadMov(cont, movie) { 
	cont.innerHTML = '<object width="320" height="260" classid="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B" codebase="http://www.apple.com/qtactivex/qtplugin.cab"><param name="src" value="'+movie+'"><param name="autoplay" value="true"><param name="controller" value="true"><embed src="'+movie+'" width="320" height="260" autoplay="true" controller="true" pluginspage="http://www.apple.com/quicktime/download/"></embed></object>';
}



var tabAction = function(el){
	$('.tab').removeClass('active');
	$('.tabcontent').hide();
	$(el).addClass('active');
	var elID = el.id;
	$('#' + elID + 'content').show();
}


var lbStage = 'ready';
var lbArray = new Array();
var lbTitleArray = new Array();
var lbGallery = false;

//Set up all the HTML for the lightbox
var lbLoadHTML = '<img src="/images/lightbox/lightbox-load.gif" alt="loading" title="loading" id="lightboxload" />'
var lbMaskHTML = '<div id="mask" onclick="closeLightBox();" title="Click to Close"></div>'+lbLoadHTML;
var lbHTML = '<div id="lightbox"><div id="lightboxutil"><img src="/images/lightbox/close.png" alt="Close" title="Close" onclick="closeLightBox();" id="lbclose" /></div></div>';
var lbCloseBtn = '<img src="/images/lightbox/mini-close.png" id="lbminiclose" onclick="captionFade();" alt="Hide Caption" title="Hide Caption" />';
var lbPrevLink = '';
var lbNextLink = '';

//core function that sets up the mask and the light box
var lightBoxZoom = function(el){
	lbMaskLoad();
	//Set up arrays for Lightbox Galleries Mode.
	$(".zoom").each(function(i) {
		lbArray[i] = this.href;
		lbSetImgCaption(this);
		lbTitleArray[i] = imgCaption;
	});
	if(lbArray.length > 1) { 
		lbGallery = true;
	}
	lbLoadContent(el);
}

//function to set up the backgound and preloader
var lbMaskLoad = function() { 
	lbLoadTopPos = (((Math.round( $(window).height())/2) + $(window).scrollTop() )-16);
	lbLoadLeftPos = ((Math.round($(window).width())/2)-16);
	lbStage = 'loading';
	$("body").prepend(lbMaskHTML);
	$("#mask").css("height",$(document).height());
	$("#lightboxload").css("top",lbLoadTopPos);
	$("#lightboxload").css("left",lbLoadLeftPos);
	/*alert(
		'Document Height is: ' + $(document).height() + 
		'\nWindow Height is: ' + $(window).height() + 
		'\nWindow Width is: ' + $(window).width() + 
		'\nScrollTop is: ' + $(window).scrollTop()
	);*/
}

var lbSetTopPos = function() { 
	var lbTopPos = Math.round((($(window).height() - $('#lightbox').height())/2)) + $(window).scrollTop();
	$("#lightbox").css("top",lbTopPos);
}

var lbSetImgCaption = function(el){
	imgCaption = el.title;
	if(imgCaption == '') { 
		imgCaption = $(el).children('img').attr('title');
	} 
}

//Core function that loads the Image to the lightbox
var lbLoadContent = function(el){
	//Set up the Image properties
	var imgPath = el.href;
	//pick the title of the image if it exists then the a tag
	

	lbSetImgCaption(el);
	var lbImg = new Image();
	
	$(lbImg).load(function() { 
		//This all happens after the image loads
		var lbImgHeight = this.height;
		var lbImgWidth = this.width;
		var targetImgHeight = parseInt($(window).height() * .67);
		var targetImgWidth = parseInt($(window).width() * .8);
		var lbHeightDiff = lbImgHeight - targetImgHeight;
		var lbWidthDiff = lbImgWidth - targetImgWidth;
		/*alert(
			'window height is ' + $(window).height() + 
			'\nwindow width is ' + $(window).width() + 
			'\nImage height is ' + lbImgHeight + 
			'\nImage width is ' + lbImgWidth +
			'\nTarget height is ' + targetImgHeight + 
			'\nTarget width is ' + targetImgWidth +
			'\nHeight Difference is ' + lbHeightDiff + 
			'\nWidth Difference  is ' + lbWidthDiff 
		);*/
		$("#lightboxload").replaceWith(lbHTML);
		$(this).attr('id','lightboximage');
		$("#lightbox").append(this);
		
		//Resizing the image if it is too large for the browser window
		if (lbHeightDiff > 0 || lbWidthDiff > 0) { 
			if (lbHeightDiff > lbWidthDiff) { 
				this.height = targetImgHeight;
				if($.browser.msie == true) {  
					//set the other dim for IE
					var imgDimRatio = targetImgHeight / lbImgHeight;
					this.width = parseInt(lbImgWidth * imgDimRatio);
				}
			} else
			if (lbWidthDiff > lbHeightDiff) { 
				this.width = targetImgWidth;
				if($.browser.msie == true) {  
					//set the other dim for IE
					var imgDimRatio = targetImgWidth / lbImgWidth;
					this.height = parseInt(lbImgHeight * imgDimRatio);
				}
			}
		}

		//Set up Previous and next content for Lightbox Galleries
		var lbCaption = '';
		if(lbGallery == true){
			lbx=0;
			lastImg = lbArray.length - 1;
			for (lbx=0; lbx<lbArray.length; lbx++){
				if(imgPath == lbArray[lbx]){
					lbCaption = lbTitleArray[lbx];
					switch (lbx){
						case 0:
							prevImg = lbArray[lastImg];
							nextImg = lbArray[lbx+1];
							prevCaption = lbTitleArray[lastImg];
							nextCaption = lbTitleArray[lbx+1];
						break;
						case lastImg:
							prevImg = lbArray[lbx-1];
							nextImg = lbArray[0];
							prevCaption = lbTitleArray[lbx-1];
							nextCaption = lbTitleArray[0];
						break;
						default:
							prevImg = lbArray[lbx-1];
							nextImg = lbArray[lbx+1];
							prevCaption = lbTitleArray[lbx-1];
							nextCaption = lbTitleArray[lbx+1];
					}
				}
			}
			var lbPrevLink = '<a href="'+prevImg+'" onclick="prevNextBox(this); return false;" title="'+prevCaption+'" class="lightboxprev"><img src="/images/lightbox/previous.png" alt="previous" /></a>';
			var lbNextLink = '<a href="'+nextImg+'" onclick="prevNextBox(this); return false;" title="'+nextCaption+'" class="lightboxnext"><img src="/images/lightbox/next.png" alt="next" /></a>';
			$("#lightboxutil").prepend(lbPrevLink+lbNextLink);
			$("#lightboximage").wrap('<a href=' + nextImg + ' onclick="prevNextBox(this); return false;" />');
		}
		
		//Add in the caption if it exists
		if(lbCaption != '' && lbCaption != undefined) { 
			$("#lightbox").append('<div id="lightboxcaption"></div>');
			$("#lightboxcaption").width($('#lightbox').width()-12);
			$("#lightboxcaption").html(lbCaption);
		} 
		var lbwidth = $("#lightbox").width();
		var lbLeftPos = (Math.round($(window).width()/2))-(Math.round(lbwidth/2));
		$("#lightbox").css("left",lbLeftPos);
		//Must have PNG fix enabled to fix the close button PNG on the fly
		$("#lightboxutil img").css("behavior","url(/iepngfix.htc)");
		$("#lightbox").fadeIn('normal',function() {
		//set the docHeight again because the lightbox can push it down.
		docHeight = $(document).height();
		$("#mask").css("height",docHeight);
			lbStage = 'loaded';
		});
		
		
		//Set up the top position of the LB this should happen right after we have content 
		lbSetTopPos();
	}).attr('src',imgPath).error(function () {
			 alert('there was an error loading the image');
			 $("#lightboxload").remove();
				$("#mask").remove();
				lbImg = '';
				lbStage = 'ready';
	});
}

var prevNextBox = function(el){
	$("#lightbox").fadeOut('fast', function(){
		$("#lightbox").remove();
		$("#mask").after(lbLoadHTML);
		$("#lightboxload").css("top",lbLoadTopPos);
		$("#lightboxload").css("left",lbLoadLeftPos);
		lbStage = 'loading';
		lbLoadContent(el);
	});
}

var closeLightBox = function(){
	if (lbStage == 'loading') { 
		if ($.browser.msie == false) {
			//do nothing if it is IE until the window is done loading.
			stop();
			$("#lightboxload").remove();
			$("#lightbox").remove();
			$("#mask").remove();
			lbStage = 'ready';
		}
	} else {
		$("#lightbox").fadeOut('fast', function(){
			$("#lightbox").remove();
			$("#mask").remove();
			lbStage = 'ready';
		});
	}
}
