 
//
// theGallery = new EmmGallery(artInfoArray);
//
// Art img tag must have ID: "artImg" 
// "prev" button for art must be: "prevArtBtn", and looks like:
//
// <a href="#" class="whatever" id="prev_art_tn" onclick="theGallery.prevArt();">&lt; previous</a>
//
// "next" button for art must be: "nextArtBtn". Like:
//
// <a href="#" class="whatever" id="next_art_btn" onclick="theGallery.nextArt();">next &gt;</a>
//
// If you use thumbnails, they must have IDs: "thNN" where NN is the number on the page
// You will also need to create CSS classes: "thmbs" and "thmbson"
// The thumb prev and next thumbnail page buttons need to be: "prevThumbPageBtn" and "nextThumbPageBtn"
//
// Thumbnail files must be named: 'prefix'NNth.jpg
//
// &&&& need to put in thumbnail docs...
//

var ArtInfo = new Class({
	
    initialize: function(imgFileName, htmlContent)
	{
		this.imgFileName = imgFileName;
		this.htmlContent  = htmlContent;	
     }
});


var EmmGallery = new Class({
	
	initialize: function(artInfoArray, thumbNailCnt)
	{
		this.infoArray = artInfoArray;
		this.artCount  = artInfoArray.length;
		this.artImgId = 'artImg';
		this.htmlContentId = 'artDesc'
		this.prevArtBtnId = 'prevArtBtn';
		this.nextArtBtnId = 'nextArtBtn';
		
		if (thumbNailCnt == null)
		    thumbNailCnt = 0;
		this.thumbCount = thumbNailCnt;
		this.prevThumbBtnId = 'prevThumbPageBtn';
		this.nextThumbBtnId = 'nextThumbPageBtn';
			
		this.curArt = 0;
		this.curThumbPage = -1;		
			
     },
  
	selectArt: function(choice)
	{
		this.curArt = choice;
	
		if (this.thumbCount > 0)
		{
			// Turn off the locked thumbnail
			$$('a.thmbson').addClass('thmbs');	
			$$('a.thmbson').removeClass('thmbson');
	
			setHoverClasses('a.thmbs', 'thmbs', 'thmbson');	
		
			// Do we need to  go to a different page?
			var newPage = parseInt(this.curArt / this.thumbCount);
			if (newPage != this.curThumbPage)
			{
				this.curThumbPage = newPage;
						
				// deal with prev and next buttons
				btn = $(this.prevThumbBtnId);
				if (btn != null)
				{
				    if (this.curThumbPage == 0 )
					btn.setStyle('visibility', 'hidden');
				    else
					btn.setStyle('visibility', 'visible');					
				}
					
	
				// deal with prev and next buttons				
				btn = $(this.nextThumbBtnId);
				if (btn != null)
				{
				    if (this.curThumbPage == parseInt(this.artCount / this.thumbCount) )
					btn.setStyle('visibility', 'hidden');
				    else
					btn.setStyle('visibility', 'visible');				
				}
						
						
				// need to reload all the thumbnails
				for (var thNum=0; thNum<this.thumbCount; thNum++ )
				{
					var thNumStr;
					if (thNum < 10)
						thNumStr = '0' + thNum;
					else
						thNumStr = thNum;
						
					var artNum = thNum + (this.curThumbPage * this.thumbCount);
					var artNumStr;
					if (artNum < 10)
						artNumStr = '0' + artNum;
					else
						artNumStr = artNum;
						
					var divId = 'th' + thNumStr;			
					
					var imgSel = '#' + divId + ' img';
					
					if ( artNum < this.artCount)
					{
					        var artInfo = this.infoArray[artNum];
						var thumbFile = artInfo.imgFileName
                        thumbFile = thumbFile.substring(0,thumbFile.length-4) + "th.jpg";

						$(divId).setStyle('visibility', 'visible');		
						$$(imgSel).setProperty('src', thumbFile);				
					}
					else
					{
						$(divId).setStyle('visibility', 'hidden');				
					}
				}		
			}
			
			// Lock the thumbnail div
			var curThNum = choice - (this.curThumbPage * this.thumbCount);
			var curThNumStr;
			if (curThNum < 10)
				curThNumStr = '0' + curThNum;
			else
				curThNumStr = curThNum;	 
			
			
            var thDivId = '#th' + curThNumStr;
			lockHoverClasses(thDivId, 'thmbson', 'thmbs');
		}
		
		// Set the big image
		var curInfo = this.infoArray[choice];	
		$(this.artImgId).setProperty('src', curInfo.imgFileName);	
		
		// set the html info
		if (curInfo.htmlContent != '')
		{
			$(this.htmlContentId).set('html', curInfo.htmlContent);
		}
		
		
		// deal with prev and next art buttons
		btn = $(this.prevArtBtnId)
		if (btn != null)
		{
		    if (choice == 0)
			btn.setStyle('visibility', 'hidden');
		    else
			btn.setStyle('visibility', 'visible');	
		}
	
		btn = $(this.nextArtBtnId)
		if (btn != null)
		{
		    if (choice == this.artCount - 1)
			btn.setStyle('visibility', 'hidden');
		    else
			btn.setStyle('visibility', 'visible');
		}
	},
	
	nextArt: function()
	{
		this.curArt++;
	
		if(this.curArt>=this.artCount)
			this.curArt = 0;
		
		this.selectArt(this.curArt);
	},

	prevArt: function()
	{
		this.curArt--;
		if(this.curArt<0)
			this.curArt = this.artCount-1;
			
		this.selectArt(this.curArt);
	},

	thumbClick: function(thumbNum)
	{
		var artNum = thumbNum + (this.curThumbPage * this.thumbCount);
		this.selectArt(artNum);
	},

	nextThumbPage: function()
	{
		var artNum = (this.curThumbPage + 1) * this.thumbCount;
		if (artNum >= this.artCount)
			artNum = 0;
		
		this.selectArt(artNum);
	},

	prevThumbPage: function()
	{
		var artNum = (this.curThumbPage - 1 ) * this.thumbCount;
		if (artNum < 0)
			artNum = parseInt(this.artCount / this.thumbCount) * this.thumbCount;
		
		this.selectArt(artNum);
	}
});

