
var loadedPhotosARR=new Array();
var cachedPhotosARR=new Array();
var loadedCaptionsARR=new Array();
var loadedURLsARR=new Array();
var loadedWidthsARR=new Array();
var loadedHeightsARR=new Array();
var loadedExtensionsARR=new Array();
var currentPhotoIndex=new Array();
var fadeoutTimeout=new Array();
var fadeInterval=new Array();

var activeBox=new Array();
var inactiveBox=new Array();
var tempInactiveBox=new Array(); // for use in showPreviousThumbnailset
var prevButton=new Array();
var nextButton=new Array();

var	focusLeft=new Array();
var	focusTop=new Array();
var	offscreenLeft=new Array();
var	offscreenRight=new Array();

// sliders
var activeMainPhotoBox=new Array();
var inactiveMainPhotoBox=new Array();
var	focusMainPhotoLeft=new Array();
var	focusMainPhotoTop=new Array();
var	offscreenMainPhotoLeft=new Array();
var	offscreenMainPhotoRight=new Array();
var	offscreenMainPhotoTop=new Array();
var	offscreenMainPhotoBottom=new Array();


var clickable=new Array();
var prevClickable=new Array();
var nextClickable=new Array();

// instance vars
var activePageNumber=new Array();
var slideshowRunning=new Array();
var thumbnailSetsARR=new Array();

// instance var
var slideshowTimeout=new Array();

var photosFolder=new Array();
var thumbsFolder=new Array();
var thumbWidth=new Array();
var thumbHeight=new Array();
var thumbsPerPage=new Array();
var clickableColor=new Array();
var unclickableColor=new Array();
var activeBorderColor=new Array();
var backgroundColor=new Array();
var canLoop=new Array();
var galleryType=new Array();
var totalPages=new Array();
var totalPhotos=new Array();

// custom for BirdBrain
var articleIDs=new Array();


var currentAlbumID=null;

function createGallery(albumID){


	loadedPhotosARR[albumID]=new Array();
	cachedPhotosARR[albumID]=new Array();
	loadedCaptionsARR[albumID]=new Array();
	loadedURLsARR[albumID]=new Array();
	loadedWidthsARR[albumID]=new Array();
	loadedHeightsARR[albumID]=new Array();
	loadedExtensionsARR[albumID]=new Array();
	currentPhotoIndex[albumID]=0;
	fadeoutTimeout[albumID]=null;


	activeBox[albumID]=$('#firstBox_'+albumID);
	inactiveBox[albumID]=$('#secondBox_'+albumID);
	prevButton[albumID]=$('#previousButton_'+albumID);
	nextButton[albumID]=$('#nextButton_'+albumID);

	if((galleryType[albumID]=='h-slider')||(galleryType[albumID]=='v-slider')){

		$('#firstMainPhotoBox_'+albumID).show();
		$('#secondMainPhotoBox_'+albumID).show();

	} else { 

		$('#mainPhoto_'+albumID).show();
		$('#mainPhotoMask_'+albumID).show();
	
	}

	// instance vars
	focusLeft[albumID]=(activeBox[albumID].parent().width()-activeBox[albumID].width())/2;
	focusTop[albumID]=(activeBox[albumID].parent().height()-activeBox[albumID].height())/2;
	offscreenLeft[albumID]=0-activeBox[albumID].width();
	offscreenRight[albumID]=activeBox[albumID].parent().width();

	// slide vars
	if((galleryType[albumID]=='h-slider')||(galleryType[albumID]=='v-slider')){
	


		activeMainPhotoBox[albumID]=$('#firstMainPhotoBox_'+albumID);
		inactiveMainPhotoBox[albumID]=$('#secondMainPhotoBox_'+albumID);

		focusMainPhotoLeft[albumID]=(activeMainPhotoBox[albumID].parent().width()-activeMainPhotoBox[albumID].width())/2;
		focusMainPhotoTop[albumID]=(activeMainPhotoBox[albumID].parent().height()-activeMainPhotoBox[albumID].height())/2;
		offscreenMainPhotoLeft[albumID]=0-activeMainPhotoBox[albumID].width();
		offscreenMainPhotoRight[albumID]=activeMainPhotoBox[albumID].parent().width();
		offscreenMainPhotoTop[albumID]=0-activeMainPhotoBox[albumID].height();
		offscreenMainPhotoBottom[albumID]=activeMainPhotoBox[albumID].parent().height();
		
		if(galleryType[albumID]=='h-slider'){
			inactiveMainPhotoBox[albumID].css('left',offscreenMainPhotoLeft[albumID]);
			inactiveMainPhotoBox[albumID].css('top',focusMainPhotoTop[albumID]-activeMainPhotoBox[albumID].height());
			activeMainPhotoBox[albumID].css('left',focusMainPhotoLeft[albumID]);
			activeMainPhotoBox[albumID].css('top',focusMainPhotoTop[albumID]);
		} else { // v=slider
			inactiveMainPhotoBox[albumID].css('left',focusMainPhotoLeft[albumID]);
			inactiveMainPhotoBox[albumID].css('top',offscreenMainPhotoTop[albumID]-activeMainPhotoBox[albumID].height());
			activeMainPhotoBox[albumID].css('left',focusMainPhotoLeft[albumID]);
			activeMainPhotoBox[albumID].css('top',focusMainPhotoTop[albumID]);
		}

	}



	// instance vars
	inactiveBox[albumID].css('opacity',0);
	inactiveBox[albumID].css('left',offscreenLeft[albumID]);
	inactiveBox[albumID].css('top',focusTop[albumID]-activeBox[albumID].height());
	activeBox[albumID].css('left',focusLeft[albumID]);
	activeBox[albumID].css('top',focusTop[albumID]);

	// instance vars
	clickable[albumID]=true;
	prevClickable[albumID]=false;
	prevButton[albumID].css('color',unclickableColor[albumID]);
	nextClickable[albumID]=false;
	nextButton[albumID].css('color',unclickableColor[albumID]);
	if(totalPages[albumID]>1){
		nextClickable[albumID]=true;
		nextButton[albumID].css('color',clickableColor[albumID]);
	}

	// instance vars
	activePageNumber[albumID]=1;
	slideshowRunning[albumID]=false;
	thumbnailSetsARR[albumID]=new Array();

	// instance car

	slideshowTimeout[albumID]=null;


	initializeGallery(albumID);

}




function showPreviousThumbset(albumID){


	if((clickable[albumID]==true)&&(prevClickable[albumID]==true)){

		clickable[albumID]=false;
		prevButton[albumID].css('color',unclickableColor[albumID]);
		nextButton[albumID].css('color',unclickableColor[albumID]);


		// move off active box right
		tempInactiveBox[albumID]=activeBox[albumID];
		activeBox[albumID].animate({left:offscreenRight[albumID],opacity:0},500,function(){

			// move offscreen left to get rid of scrollbar
			tempInactiveBox[albumID].css('left',offscreenLeft[albumID]);
		});

		// move on inactive box from left
		inactiveBox[albumID].css('left',offscreenLeft[albumID]);
		inactiveBox[albumID].css('opacity',0);

		// resave active thumbnail set before it gets removed
		unhighlightAllThumbs(albumID);				
		var activeThumbnailSet=activeBox[albumID].children().clone(true);
		thumbnailSetsARR[albumID][activePageNumber[albumID]]=activeThumbnailSet;

		


		inactiveBox[albumID].children().remove();
		inactiveBox[albumID].append(thumbnailSetsARR[albumID][activePageNumber[albumID]-1]);


		inactiveBox[albumID].animate({left:focusLeft[albumID],opacity:1},500,function(){

			activePageNumber[albumID]--;
			clickable[albumID]=true;
			if(activePageNumber[albumID]>1){
				prevClickable[albumID]=true;
				prevButton[albumID].css('color',clickableColor[albumID]);
			} else {
				prevClickable[albumID]=false;
				prevButton[albumID].css('color',unclickableColor[albumID]);
			}
			if(activePageNumber[albumID]<totalPages[albumID]){
				nextClickable[albumID]=true;
				nextButton[albumID].css('color',clickableColor[albumID]);
			} else {
				nextClickable[albumID]=false;
				nextButton[albumID].css('color',unclickableColor[albumID]);
			}


		});

		
		var newInactiveBox=activeBox[albumID];
		var newActiveBox=inactiveBox[albumID];

		activeBox[albumID]=newActiveBox;
		inactiveBox[albumID]=newInactiveBox;

	
	}
	
}



			
function showNextThumbset(albumID){


	if((clickable[albumID]==true)&&(nextClickable[albumID]==true)){

		clickable[albumID]=false;
		prevButton[albumID].css('color',unclickableColor[albumID]);
		nextButton[albumID].css('color',unclickableColor[albumID]);



		// move off active box left
		activeBox[albumID].animate({left:offscreenLeft[albumID],opacity:0},500);

		// move on inactive box from right
		inactiveBox[albumID].css('left',offscreenRight[albumID]);
		inactiveBox[albumID].css('opacity',0);

		// resave active thumbnail set before it gets removed
		unhighlightAllThumbs(albumID);				
		var activeThumbnailSet=activeBox[albumID].children().clone(true);
		thumbnailSetsARR[albumID][activePageNumber[albumID]]=activeThumbnailSet;



		inactiveBox[albumID].children().remove();
		inactiveBox[albumID].append(thumbnailSetsARR[albumID][activePageNumber[albumID]+1]);


		inactiveBox[albumID].animate({left:focusLeft[albumID],opacity:1},500,function(){

			activePageNumber[albumID]++;
			clickable[albumID]=true;
			if(activePageNumber[albumID]<totalPages[albumID]){
				nextClickable[albumID]=true;
				nextButton[albumID].css('color',clickableColor[albumID]);
				if((thumbnailSetsARR[albumID][activePageNumber[albumID]+1])==null){
					loadNextThumbnails(albumID,activePageNumber[albumID]+1);
				} 
			} else {
				nextClickable[albumID]=false;
				nextButton[albumID].css('color',unclickableColor[albumID]);
			}
			if(activePageNumber[albumID]>1){
				prevClickable[albumID]=true;
				prevButton[albumID].css('color',clickableColor[albumID]);
			} else {
				prevClickable[albumID]=false;
				prevButton[albumID].css('color',unclickableColor[albumID]);
			}

			fadeInThumbs(albumID);


		});

		var newInactiveBox=activeBox[albumID];
		var newActiveBox=inactiveBox[albumID];

		activeBox[albumID]=newActiveBox;
		inactiveBox[albumID]=newInactiveBox;



	} 
	

}



function showPreviousPhoto(albumID){

	

	if(currentPhotoIndex[albumID]>0){

		if(((currentPhotoIndex[albumID])%thumbsPerPage[albumID])==0){showPreviousThumbset(albumID);}

		currentPhotoIndex[albumID]--;

		activatePhoto(albumID,loadedPhotosARR[albumID][currentPhotoIndex[albumID]]);



	} 


}

function showNextPhoto(albumID){



	if(albumID==null){albumID=currentAlbumID;}
	

	if(currentPhotoIndex[albumID]<loadedPhotosARR[albumID].length-1){

		if(((currentPhotoIndex[albumID]+1)%thumbsPerPage[albumID])==0){showNextThumbset(albumID);}

		currentPhotoIndex[albumID]++;

		activatePhoto(albumID,loadedPhotosARR[albumID][currentPhotoIndex[albumID]]);
						


	} else {
		
		if(canLoop[albumID]==true){

			loopToStart(albumID);

		} 
	}


}





function loadNextThumbnails(albumID,page){
// returns thumbnail ul object

	if(page!=1){
		var myBox=inactiveBox[albumID];
	} else{
		var myBox=activeBox[albumID];
	}

	if(articleIDs[albumID]!=null){
		// custom birdbrain
		ajaxURL=_masterURL+'_ajax/gallery.php?func=getPhotosByArticle&aid='+albumID+'&arid='+articleIDs[albumID]+'&p='+page;
	} else {
		ajaxURL=_masterURL+'_ajax/gallery.php?func=getPhotos&aid='+albumID+'&p='+page;
	}

	$.ajax({
		url: ajaxURL,
		success: function(data) {//alert(data);

			var newUnorderedList=$(document.createElement('ul'));
			newUnorderedList.addClass('thumbnailSet');

			var photosQUE=JSON.parse(data);

			$.each(photosQUE,function(key,photoARR){


				var photoID=photoARR.photoID;
				var caption=photoARR.caption;
				var url=photoARR.url;
				var imageExtension=photoARR.imageExtension;
				var photoWidth=photoARR.photoWidth;
				var photoHeight=photoARR.photoHeight;


				loadedPhotosARR[albumID][loadedPhotosARR[albumID].length]=photoID;
				loadedCaptionsARR[albumID][loadedCaptionsARR[albumID].length]=caption;
				loadedURLsARR[albumID][loadedURLsARR[albumID].length]=url;
				loadedWidthsARR[albumID][loadedWidthsARR[albumID].length]=photoWidth;
				loadedHeightsARR[albumID][loadedHeightsARR[albumID].length]=photoHeight;
				loadedExtensionsARR[albumID][loadedExtensionsARR[albumID].length]=imageExtension;



				var myCurrentPhotoIndex=loadedPhotosARR[albumID].length-1;

				var newListItem=$(document.createElement('li'));
				newListItem.addClass('thumb');
				newListItem.addClass('unloaded');
				newListItem.hide();
				var newAnchor=$(document.createElement('a'));
				newAnchor.bind('click',{myPhotoID:photoID,myCurrentPhotoIndex:myCurrentPhotoIndex,myPhotoWidth:photoWidth,myPhotoHeight:photoHeight}, function(event){
					currentPhotoIndex[albumID]=event.data.myCurrentPhotoIndex;
					if(galleryType[albumID]=='lightbox'){
						openLightbox(albumID,event.data.myPhotoID,$(this));
					} else {
						activatePhoto(albumID,event.data.myPhotoID);
					}
					pauseSlideshow(albumID);
					return false;
				});

				var newImage=$(document.createElement('img'));
				newImage.attr('id','img_thumb-'+photoID);
				newImage.attr('src',thumbsFolder[albumID]+albumID+'_'+photoID+'_'+thumbWidth[albumID]+'x'+thumbHeight[albumID]+'.'+imageExtension);

				newAnchor.append(newImage);
				newListItem.append(newAnchor);
				newUnorderedList.append(newListItem);


				// cache photo
				var cachedPhoto=$(document.createElement('img'));
				cachedPhoto.attr('id','img_photo-'+photoID);
				cachedPhoto.attr('src',photosFolder[albumID]+albumID+'_'+photoID+'.'+imageExtension);
				cachedPhotosARR[albumID][loadedPhotosARR[albumID].length]=cachedPhoto;



			});



			thumbnailSetsARR[albumID][page]=newUnorderedList;

			// initial thumbsets

			if(page==1){

				myBox.append(thumbnailSetsARR[albumID][1]);

				if(galleryType[albumID]!='lightbox'){
					highlightThumb(albumID,loadedPhotosARR[albumID][0]);
					activatePhoto(albumID,loadedPhotosARR[albumID][0]);
				}

				if(totalPages[albumID]>1){
					loadNextThumbnails(albumID,2);
				}

				fadeInThumbs(albumID);

			}



		}
	});		
	
}




function loopToStart(albumID){

	currentPhotoIndex[albumID]=0;
	activatePhoto(albumID,loadedPhotosARR[albumID][0]);


	if(totalPages[albumID]>1){

		if(clickable[albumID]==true){

			clickable[albumID]=false;
			prevButton[albumID].css('color',unclickableColor[albumID]);
			nextButton[albumID].css('color',unclickableColor[albumID]);

			// move off active box left
			activeBox[albumID].animate({left:offscreenLeft[albumID],opacity:0},500);

			// move on inactive box from right
			inactiveBox[albumID].css('left',offscreenRight[albumID]);
			inactiveBox[albumID].css('opacity',0);

			// resave active thumbnail set before it gets removed
			unhighlightAllThumbs(albumID);				
			var activeThumbnailSet=activeBox[albumID].children().clone(true);
			thumbnailSetsARR[albumID][activePageNumber[albumID]]=activeThumbnailSet;



			inactiveBox[albumID].children().remove();
			inactiveBox[albumID].append(thumbnailSetsARR[albumID][1]);


			inactiveBox[albumID].animate({left:focusLeft[albumID],opacity:1},500,function(){

				activePageNumber[albumID]=1;
				clickable[albumID]=true;
				if(activePageNumber[albumID]<totalPages[albumID]){
					nextClickable[albumID]=true;
					nextButton[albumID].css('color',clickableColor[albumID]);
					if((thumbnailSetsARR[albumID][activePageNumber[albumID]+1])==null){
						loadNextThumbnails(albumID,activePageNumber[albumID]+1);
					} 
				} else {
					nextClickable[albumID]=false;
					nextButton[albumID].css('color',unclickableColor[albumID]);
				}
				if(activePageNumber[albumID]>1){
					prevClickable[albumID]=true;
					prevButton[albumID].css('color',clickableColor[albumID]);
				} else {
					prevClickable[albumID]=false;
					prevButton[albumID].css('color',unclickableColor[albumID]);
				}


			});

			var newInactiveBox=activeBox[albumID];
			var newActiveBox=inactiveBox[albumID];

			activeBox[albumID]=newActiveBox;
			inactiveBox[albumID]=newInactiveBox;



		} 

	}

}



function runSlideshow(albumID){
	
	if(slideshowRunning[albumID]==true){

		if(currentPhotoIndex[albumID]!=loadedPhotosARR[albumID].length-1){

			slideshowTimeout[albumID]=setTimeout(function(){

				showNextPhoto(albumID);
				runSlideshow(albumID);

			},fadeInterval[albumID]);

		} else { // last photo

			slideshowTimeout[albumID]=setTimeout(function(){

				loopToStart(albumID);
				runSlideshow(albumID);

			},fadeInterval[albumID]);

		}
	}

}


function playSlideshow(albumID){
	
	showNextPhoto(albumID);
	slideshowRunning[albumID]=true;
	runSlideshow(albumID);

	$('#playSlideshowButton_'+albumID).css('color',unclickableColor[albumID]);
	$('#playSlideshowButton_'+albumID).hide();
	$('#pauseSlideshowButton_'+albumID).css('color',clickableColor[albumID]);
	$('#pauseSlideshowButton_'+albumID).show();

}

function pauseSlideshow(albumID){


	slideshowRunning[albumID]=false;
	clearTimeout(slideshowTimeout[albumID]);

	$('#playSlideshowButton_'+albumID).css('color',clickableColor[albumID]);
	$('#playSlideshowButton_'+albumID).show();
	$('#pauseSlideshowButton_'+albumID).css('color',unclickableColor[albumID]);
	$('#pauseSlideshowButton_'+albumID).hide();



}


function initializeGallery(albumID){


	if(galleryType[albumID]=='lightbox'){

		$('#previousPhotoButton').unbind('click');
		$('#nextPhotoButton').unbind('click');
		$('#playSlideshowButton').unbind('click');
		$('#pauseSlideshowButton').unbind('click');
		
		$('#previousPhotoButton').click(function(){showPreviousPhoto();pauseSlideshow();});
		$('#nextPhotoButton').click(function(){showNextPhoto();pauseSlideshow();});
		$('#playSlideshowButton').click(function(){playSlideshow()});
		$('#pauseSlideshowButton').click(function(){pauseSlideshow()});

	} else {


		$('#previousPhotoButton_'+albumID).click(function(){showPreviousPhoto(albumID);pauseSlideshow(albumID);});
		$('#nextPhotoButton_'+albumID).click(function(){showNextPhoto(albumID);pauseSlideshow(albumID);});
		$('#playSlideshowButton_'+albumID).click(function(){playSlideshow(albumID)});
		$('#pauseSlideshowButton_'+albumID).click(function(){pauseSlideshow(albumID)});

	}

	$('#previousButton_'+albumID).click(function(){showPreviousThumbset(albumID);pauseSlideshow(albumID);});
	$('#nextButton_'+albumID).click(function(){showNextThumbset(albumID);pauseSlideshow(albumID);});

	// set intitial button colors
	$('#pauseSlideshowButton_'+albumID).css('color',unclickableColor[albumID]);
	$('#previousPhotoButton_'+albumID).css('color',unclickableColor[albumID]);


	// load initial thumbnail sets
	loadNextThumbnails(albumID,1);


	if(galleryType[albumID]!='lightbox'){
		setTimeout(function(){

			playSlideshow(albumID);

		},5000);
	}

	if(galleryType[albumID]=='lightbox'){
		$('#mask_'+albumID).click(function(){pauseSlideshow(albumID);});
	}


}





function activatePhoto(albumID,photoID){


	if(galleryType[albumID]=='lightbox'){
		
		if(currentPhotoIndex[albumID]!=0){
			var previousPhotoIndex=currentPhotoIndex[albumID]-1;
		} else {
			var previousPhotoIndex=loadedExtensionsARR.length;
		}
		switchLightboxPhoto(albumID,photoID);
		clearTimeout(fadeoutTimeout[albumID]);
		fadeoutTimeout[albumID]=setTimeout(function(){

				$('#mainPhotoMask').fadeOut(500,function(){

					$('#mainPhotoMask').attr('src',photosFolder[albumID]+albumID+'_'+photoID+'.'+loadedExtensionsARR[albumID][previousPhotoIndex]).fadeIn(10);

				});

		},500);

	} else if((galleryType[albumID]=='h-slider')||(galleryType[albumID]=='v-slider')){

			slidePhoto(albumID,photoID);
			switchCaption(albumID);
			changePhotoCounter(albumID);



	} else {

		$('#mainPhotoMask_'+albumID).css("background-image","url('"+photosFolder[albumID]+albumID+"_"+photoID+"."+loadedExtensionsARR[albumID][currentPhotoIndex[albumID]]+"')");
		$('#mainPhotoMask_'+albumID).fadeIn(500);
		clearTimeout(fadeoutTimeout[albumID]);
		fadeoutTimeout[albumID]=setTimeout(function(){

			switchPhoto(albumID,photoID);
			switchCaption(albumID);
			changePhotoCounter(albumID);
			$('#mainPhotoMask_'+albumID).fadeOut(500);



		},500);

	}



}



function switchCaption(albumID){

	if(galleryType[albumID]=='lightbox'){
		$('#photoCaption').html(loadedCaptionsARR[albumID][currentPhotoIndex[albumID]]);
	} else { 
		$('#photoCaption_'+albumID).html(loadedCaptionsARR[albumID][currentPhotoIndex[albumID]]);
	}
	
}



function changePhotoCounter(albumID){

	if(galleryType[albumID]=='lightbox'){

		$('#photoCounter').text((currentPhotoIndex[albumID]+1)+" out of "+totalPhotos[albumID]);

	}

}


function switchPhoto(albumID,photoID){

	if(galleryType[albumID]=='lightbox'){
		$('#mainPhoto').css("background-image","url('"+photosFolder[albumID]+albumID+"_"+photoID+"."+loadedExtensionsARR[albumID][currentPhotoIndex[albumID]]+"')");
	} else {
		$('#mainPhoto_'+albumID).css("background-image","url('"+photosFolder[albumID]+albumID+"_"+photoID+"."+loadedExtensionsARR[albumID][currentPhotoIndex[albumID]]+"')");
	}
	unhighlightAllThumbs(albumID);
	highlightThumb(albumID,photoID);


	if(currentPhotoIndex[albumID]==0){

		$('#previousPhotoButton_'+albumID).css('color',unclickableColor[albumID]);

	} else {

		$('#previousPhotoButton_'+albumID).css('color',clickableColor[albumID]);

	}


	if((currentPhotoIndex[albumID]==loadedPhotosARR[albumID].length-1)&&(canLoop[albumID]==false)){

		$('#nextPhotoButton_'+albumID).css('color',unclickableColor[albumID]);

	} else {

		$('#nextPhotoButton_'+albumID).css('color',clickableColor[albumID]);

	}


}

function slidePhoto(albumID,photoID){

		// prevents crazy dequeue'ing of all animations when on another tab
		activeMainPhotoBox[albumID].clearQueue();
		inactiveMainPhotoBox[albumID].clearQueue();


		inactiveMainPhotoBox[albumID].css('background-image','url(\''+photosFolder[albumID]+albumID+'_'+photoID+'.jpg\')');
		inactiveMainPhotoBox[albumID].click(function(){
			navigateURL(_masterURL+loadedURLsARR[albumID][currentPhotoIndex[albumID]]);
			//alert(loadedURLsARR[albumID][currentPhotoIndex[albumID]]);
		});


		if(galleryType[albumID]=='h-slider'){
	
			// move off active box left
			activeMainPhotoBox[albumID].animate({left:offscreenMainPhotoLeft[albumID]},500);

			// move on inactive box from right
			inactiveMainPhotoBox[albumID].css('left',offscreenMainPhotoRight[albumID]);
			inactiveMainPhotoBox[albumID].animate({left:focusMainPhotoLeft[albumID]},500);

		} else { // v-slider
			

			if(activeMainPhotoBox[albumID].hasClass('firstMainPhotoBox')){
				var activeOffset=0;									
			} else {
				var activeOffset=offscreenMainPhotoTop[albumID];
			}

			if(inactiveMainPhotoBox[albumID].hasClass('firstMainPhotoBox')){
				var inactiveOffset=0;
			} else {
				var inactiveOffset=offscreenMainPhotoTop[albumID];
			}

			activeMainPhotoBox[albumID].clearQueue();
			inactiveMainPhotoBox[albumID].clearQueue();


			// move off active box down
			activeMainPhotoBox[albumID].animate({top:offscreenMainPhotoBottom[albumID]+activeOffset},1000);


			// move on inactive box from top
			inactiveMainPhotoBox[albumID].css('top',offscreenMainPhotoTop[albumID]+(inactiveOffset*1));
			inactiveMainPhotoBox[albumID].animate({top:focusMainPhotoTop[albumID]+inactiveOffset},1000);


		}


		unhighlightAllThumbs(albumID);
		highlightThumb(albumID,photoID);


		var newInactiveMainPhotoBox=activeMainPhotoBox[albumID];
		var newActiveMainPhotoBox=inactiveMainPhotoBox[albumID];

		activeMainPhotoBox[albumID]=newActiveMainPhotoBox;
		inactiveMainPhotoBox[albumID]=newInactiveMainPhotoBox;



}



function highlightThumb(albumID,photoID){

		$('#img_thumb-'+photoID).css("border-color",activeBorderColor[albumID]);

}



function unhighlightAllThumbs(albumID){

		$('.thumb>a>img').css("border-color",backgroundColor[albumID]);


}

function openLightbox(albumID,photoID,thumb){

		if($('#modal_lightbox').is(':hidden')){

			currentAlbumID=albumID;

			// render mask
			$('.flashObject').css('visibility','hidden');

			var documentMargin = ($(window).width()-$('body').width())/2;
			var maskHeight = $(document).height();
			var maskWidth = $(window).width()+documentMargin;
			var maskX = 0-documentMargin;
			$('#mask').css({'width':maskWidth,'height':maskHeight});  
			$('#mask').css({'left':maskX});  
			$('#mask').fadeTo(1000,0.85);      
			$('#mask').fadeTo(500,0.65);

			// resize all to thumb size
			var thumbWidth=100;
			var thumbHeight=100;
			$('#modal_lightbox').width(thumbWidth);
			$('#modal_lightbox').height(thumbHeight);
			$('#mainPhotoImg').width(thumbWidth);
			$('#mainPhotoImg').height(thumbHeight);
			$('#mainPhotoMask').width(thumbWidth);
			$('#mainPhotoMask').height(thumbHeight);

			// position over thumb initially
			var thumbLeft=thumb.offset().left;
			var thumbTop=thumb.offset().top-$(window).scrollTop();;
			$('#modal_lightbox').css('left',thumbLeft+'px');
			$('#modal_lightbox').css('top',thumbTop+'px');

			
			// animate to full size and center
			var photoWidth=loadedWidthsARR[albumID][currentPhotoIndex[albumID]];
			var photoHeight=loadedHeightsARR[albumID][currentPhotoIndex[albumID]];

			var left=($(window).width()/2)-(photoWidth/2);
			var top=($(window).height()/2)-(photoHeight/2);

			//switchPhoto(albumID,photoID);

			// switch photo
			$('#mainPhotoImg').attr('src',photosFolder[albumID]+albumID+'_'+photoID+'.'+loadedExtensionsARR[albumID][currentPhotoIndex[albumID]]);
			$('#mainPhotoImg').fadeIn(500).animate({width:photoWidth,height:photoHeight},500);
			$('#mainPhotoMask').attr('src',photosFolder[albumID]+albumID+'_'+photoID+'.'+loadedExtensionsARR[albumID][currentPhotoIndex[albumID]]);
			$('#mainPhotoMask').delay(500).animate({width:photoWidth,height:photoHeight},500).fadeIn(10);
			


			$('#modal_lightbox').fadeIn(500).animate({width:photoWidth,left:left,height:photoHeight,top:top},500);

			unhighlightAllThumbs(albumID);
			highlightThumb(albumID,photoID);
			switchCaption(albumID);
			changePhotoCounter(albumID);

		}



}

function switchLightboxPhoto(albumID,photoID){

				
			// animate to full size and center
			var photoWidth=loadedWidthsARR[albumID][currentPhotoIndex[albumID]];
			var photoHeight=loadedHeightsARR[albumID][currentPhotoIndex[albumID]];

			var left=($(window).width()/2)-(photoWidth/2);
			var top=($(window).height()/2)-(photoHeight/2);

			// switch photo


			$('#mainPhotoImg').attr('src',photosFolder[albumID]+albumID+'_'+photoID+'.'+loadedExtensionsARR[albumID][currentPhotoIndex[albumID]]);
			
			$('#mainPhotoImg').animate({width:photoWidth,height:photoHeight},500);
			$('#mainPhotoMask').animate({width:photoWidth,height:photoHeight},500);
			$('#modal_lightbox').animate({width:photoWidth,left:left,height:photoHeight,top:top},500);

			unhighlightAllThumbs(albumID);
			highlightThumb(albumID,photoID);
			switchCaption(albumID);
			changePhotoCounter(albumID);

}


function fadeInThumbs(albumID){


	var delayLength=200;
	var i=1;
	$.each(activeBox[albumID].children().children('.unloaded'),function(){
		

		$(this).delay(delayLength*i).fadeIn(1000);
		$(this).removeClass('unloaded');
		i++;

	
	});


}
