
function load()
{
	// alert(artworks);
}

function GUnload()
{
	// alert(artworks[0][0]);
}



var artworkIndex = 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;


new PeriodicalExecuter(function() 
		{if (artworkIndex < artworkCount, safe_to_load_artwork == 1) 
			{ 	
				safe_to_load_artwork = 0; 
				if (artworkIndex == 0) { galleryShow.stop(); }; 
				// alert(artworks[artworkIndex][0]);
				requestContent();
			}}, 0.55);	


var galleryShow = new PeriodicalExecuter(nextImage, 4.5);


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(); 
																// alert(showCount);
																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()'})});
	$('artworkInfo_'+artworkIndex).update(artworks[artworkIndex][1]);
	select = new Element('img', {id:'select_'+artworkIndex, src:'/images/spacer.gif', 'class': 'gallery_select_active', 'onclick':'setImage('+artworkIndex+')'});
	$('selectable_'+artworkIndex).update();
	$('selectable_'+artworkIndex).update(select);
	artworkIndex++;					
}

function showFirst()
{
	showPause(2000);
	$(currentImage).visualEffect('appear', {duration:0.0})
	$(currentSelect).addClassName('gallery_selected');
}

function safeToShowArtwork(v)
{
	safe_to_show_artwork=v;
}

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

function appearCurrent()
{
	currentImage = 'artwork_'+showIndex;
	currentSelect = 'select_'+showIndex;
	$(currentImage).visualEffect('appear', {duration:0.3, 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(2000);
			safeToShowArtwork(0);
			incShowIndex();
			fadeCurrent();
		}		    
	}
}

function priorImage()
{

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


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


 function showPause(v)
 {
   galleryShow.stop();
// $('transport_pause').visualEffect('appear');
   setTimeout('galleryShow.start();',v);

 }

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

