
	
	var galleryIntervalTime = 15;
	var galleryFadeTime = 0.25;
	var artworks = [];
	var artworkIndex = 0;
	var artworkCount = 0;
	var showIndex = 0;
	var showCount = 1;
	var currentImage = 'artwork_0';
	var currentSelect = 'select_0';  
	var safe_to_load_artwork = 1;
	var safe_to_show_artwork = 1;

// var artworkPermalinks = new Array();
// var artworkThumbs = new Array();
// 


// function load()
// {
// 	// galleryShow.execute();
// 	// galleryShow.start();
// 	// galleryShow.setFreq(galleryIntervalTime);
// }
// 
// function GUnload()
// {
// 	// setImage(0);
// }

var galleryShow = new PeriodicalExecuter(function(){nextImage()}, galleryIntervalTime);
var preventCallback = 0

document.observe("dom:loaded", function(){
	

	var galleryLoader = new PeriodicalExecuter(function() 
		{if (artworkIndex < artworkCount && safe_to_load_artwork == 1) 
			{ 	
				safe_to_load_artwork = 0; 
				if (artworkIndex == 0) { preventCallback = 1; }; 
				if (artworkIndex == 1) { preventCallback = 0; preventCallback = 0;}; 
				if (artworkCount > 0) { requestContent(); }; 
				if (artworkIndex == artworkCount) { galleryLoader.stop(); }; 
			}}, 2);	
	

		requestContent();

	function requestContent()
	{
		 new Ajax.Request(artworks[artworkIndex][0], {asynchronous:true, evalScripts:true, method:'get', 
														onComplete:function(request)
														{
															safeToShowArtwork(1); 
															showCount = artworkIndex; 
															if (artworkIndex == 1 && artworkCount > 1) 
																{ 
																	preventCallback = 0;
																	showFirst(); 
																}
														}, 
														onInteractive:function(request)
														{
															if (artworkIndex >= artworkCount) 
																{ 
																	safe_to_load_artwork = 0; 
																} 	
															else 
																{ 
																	safe_to_load_artwork = 1; 
																}
														}, 
														parameters:'authenticity_token=' + encodeURIComponent('e1a9209e9e20e6516b65d78a48a481f8a654ef12')
														}
													); 
		$('artworkLink_'+artworkIndex).insert({top: new Element('img', {id:'artwork_'+artworkIndex, src:artworks[artworkIndex][0], 'class': 'gallery_image', 'style':'z-index:inherit;'})});
		$('artworkInfo_'+artworkIndex).update(artworks[artworkIndex][1]);
		select = new Element('img', {id:'select_'+artworkIndex, src:'/images/spacer.gif', 'class': 'gallery_select_active', 'onclick':'setImage('+artworkIndex+');return false;'});
		$('selectable_'+artworkIndex).update();
		$('selectable_'+artworkIndex).update(select);
		artworkIndex++;					
	}	


});


function userClickImage()
{
	preventCallback = 0;
	nextImage();
	showPause();
}

function showPause()
{
	preventCallback = 1;
	setTimeout( function(){ preventCallback = 0; }, galleryIntervalTime*3000);
}

function showFirst()
{
	 appearCurrent();
	// $(currentImage).visualEffect('appear', {duration:galleryFadeTime})
	// incShowIndex();
	// $(currentSelect).addClassName('gallery_selected');
}

function safeToShowArtwork(v)
{
	safe_to_show_artwork=v;
}

function fadeCurrent()
{
	if (artworkIndex>=1)
	{
		$(currentImage).visualEffect('fade', {duration:galleryFadeTime, from:1.0, to:0.0, afterFinish: appearCurrent});
		$(currentSelect).removeClassName('gallery_selected');
	}
}

function appearCurrent()
{
	currentImage = 'artwork_'+showIndex;
	currentSelect = 'select_'+showIndex;
	$(currentImage).visualEffect('appear', {duration:galleryFadeTime, from:0.0, to:1.0, afterFinish: safeToShowArtwork(1)});	
	$(currentSelect).addClassName('gallery_selected');
}

 function incShowIndex()
 {
   showIndex = mod((showIndex+1),(showCount-0));
 }

 function decShowIndex()
 {
 	showIndex = mod((showIndex-1),(showCount-0));
 }

function nextImage()
{
if (safe_to_show_artwork==1 && 	preventCallback == 0)
	{
		if (artworkCount > 1)
		{
			// showPause();
			safeToShowArtwork(0);
			incShowIndex();
			fadeCurrent();
		}		    
	}
}

function priorImage()
{

	if (safe_to_show_artwork==1)
		{
	    showPause(4000);
		safeToShowArtwork(0);
	    decShowIndex();
		fadeCurrent();
		}
}


function setImage(v)
{
 	if (safe_to_show_artwork==1)
		{
		showPause(9000);
		safeToShowArtwork(0);
	    showIndex = v;			
	    fadeCurrent();
		}		    
}










function mod(X, Y) 
{
var t;
t = X % Y;
return t < 0 ? t + Y : t;
}



