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


function load()
{
	// galleryShow.execute();
	galleryShow.start();
}

function GUnload()
{
	// setImage(0);
}


var galleryDuration = 0.25;
var artworks = [];
var artworkIndex = 0;
var artworkCount = 0;
// var artworkPermalinks = new Array();
// var artworkThumbs = new Array();
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;

  galleryShow = new PeriodicalExecuter(function(){nextImage()}, 7);

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





function requestContent()
{
	 new Ajax.Request(artworks[artworkIndex][0], {asynchronous:true, evalScripts:true, method:'get', 
													onComplete:function(request)
													{
														safeToShowArtwork(1); 
														showCount = artworkIndex; 
														if (artworkIndex == 1) 
															{ 
																galleryShow.start(); 
																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', 'onclick':'galleryShow.stop()', '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 showFirst()
{
	showPause(4000);
	$(currentImage).visualEffect('appear', {duration:galleryDuration})
	$(currentSelect).addClassName('gallery_selected');
}

function safeToShowArtwork(v)
{
	safe_to_show_artwork=v;
}

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

function appearCurrent()
{
	currentImage = 'artwork_'+showIndex;
	currentSelect = 'select_'+showIndex;
	$(currentImage).visualEffect('appear', {duration:galleryDuration, 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)
	{
		if (artworkCount > 1)
		{
			showPause(4000);
			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 showPause(v)
 {
   galleryShow.stop();
// $('transport_pause').visualEffect('appear');
   // setTimeout('galleryShow.start();',v);
   // galleryShow.start().delay(v*0.001);
 }


