// gallery vars to keep track of animations
var curImgElmnt;
var lastImgElmnt;
var loadedOnce = false;
var clickedOnce = false;
var origX;
var origY;
var yMoveDist = 340;
var newImgStr;
var newGalleryHtml;
var curImage;
var imgPreloader;

// initialization
function initGallery()
{
	// find elements that we load images into and slide around
	curImgElmnt = $('galleryImg1');
	lastImgElmnt = $('galleryImg2');
	
	if( curImgElmnt != null )
	{
		//setTimeout("loadFirstArtistImage()",1000);
		setTimeout("loadFirstGallery()",1000);
	}
}

// auto-load the first gallery
function loadFirstGallery()
{
	// grab all gallery links
	var links = $$('.artistThumb');
	
	// call the javascript link in the first thumbnail button
	var pureJsCall = links[0].href.replace('javascript:','');
	eval( pureJsCall );	
}

// auto-load the first image
function loadFirstArtistImage()
{
	// grab all thumb links
	var thumbLinks = $$('.worksThumb');
	
	// call the javascript link in the first thumbnail button
	var pureJsCall = thumbLinks[0].href.replace('javascript:','');
	eval( pureJsCall );	
}

// respond to a click to reload the thumbnail list
function loadWorks( galleryHtml )
{
	// get rid of little highlight that appears when you click
	$('worksList').blur();
	
	// fade out thumbs list with callback to load next thumbs
	newGalleryHtml = galleryHtml;
	Effect.FadeNoCollapse( $('worksList'), { duration: .6, afterFinish: loadNewThumbs } );
	
	// swap classes to highlight selected button
	var activeLinks = $$('.artistThumbActive');
	for( var i = 0; i < activeLinks.length; i++ )
	{
		$( activeLinks[i] ).removeClassName('artistThumbActive');
		$( activeLinks[i] ).addClassName('artistThumb');
	}
	//now loop through and find the button we clicked
	var links = $$('.artistThumb');
	for( var i = 0; i < links.length; i++ )
	{
		if( links[i].href == "javascript:loadWorks('"+ newGalleryHtml +"');" )
		{
			$( links[i] ).removeClassName('artistThumb');
			$( links[i] ).addClassName('artistThumbActive');
		}
	}
}

// reload the thumbnail list after it animates out
function loadNewThumbs()
{
	new Ajax.Updater('worksList', newGalleryHtml, 
	{
		evalScripts:true,
		method: 'get',
		onComplete: fadeInThumbs
	});
}

// fade in the thumbnail list onLoad
function fadeInThumbs()
{
	// fade thumbnail list
	Element.Methods.setOpacity( $('worksList'), 0 );
	Effect.AppearTweaked( $('worksList'), { duration:.5 } );
	
	setTimeout("loadFirstArtistImage()",200);
}

// respond to a thumbnail click and kcik off a new image load
function viewWork( image )
{
	curImage = image;
	imgPreloader = new Image();									// new image loader obj
	newImgStr = '<img src="' + image + '" />';					// create new html to insert
	imgPreloader.onload = fadeNewArtistImg;							// image loaded callback
	imgPreloader.src = image;									// kick off the preload
	
	setThumbStates();
}

function setThumbStates()
{
	// swap classes to highlight selected button
	var activeLinks = $$('.worksThumbActive');
	for( var i = 0; i < activeLinks.length; i++ )
	{
		$( activeLinks[i] ).removeClassName('worksThumbActive');
		$( activeLinks[i] ).addClassName('worksThumb');
	}
	//now loop through and find the button we clicked
	var links = $$('.worksThumb');
	for( var i = 0; i < links.length; i++ )
	{
		if( links[i].href == "javascript:viewWork('"+ curImage +"');" )
		{
			$( links[i] ).removeClassName('worksThumb');
			$( links[i] ).addClassName('worksThumbActive');
		}
	}	
}

// fade in the new image now that it's loaded
function fadeNewArtistImg()
{
	// swap references to the current div that we're loading into
	lastImgElmnt = curImgElmnt;
	if( curImgElmnt == $('galleryImg1') )
		curImgElmnt = $('galleryImg2');
	else
		curImgElmnt = $('galleryImg1');
	
	// swap depths
	curImgElmnt.style.zIndex = 11;
	lastImgElmnt.style.zIndex = 10;
	
	// load image into current container and hide initially
	curImgElmnt.hide();
	curImgElmnt.update( newImgStr );
	
	// fade new img in
	Effect.AppearTweaked( curImgElmnt, { duration:.9 } );
}


// bio / contact stuff
var resumeFile = '';
var bioFile = '';

function showResume()
{
	if( resumeFile != '' ) window.open( resumeFile, '_blank' );
}

function showBio()
{
	if( bioFile != '' ) window.open( bioFile, '_blank' );
}




Event.observe(window, 'load', initGallery, false);
