/*******************************
 *
 *	Image Gallery Class
 *
 *  Version: 1.0
 *
 *	Author: 
 *	The Roundhouse
 *
 *  © The Roundhouse 2007 -
 * 	ALL RIGHTS RESERVED
 */
 

var Gallery = new Class({
	
	divTarget:		null,
	h3Title: 		null,
	aImageAnchors: 	null,
	aClickCatcher:	null,
	aNxtArray:		null,
	bActive:		null,
	strLastLoad:	null,
	aNext:			null,
	aPrev:			null,
	iCurrentImg:	null,
	
	initialize: function()
	{
		this.aClickCatcher							= new Array();	
		this.aNxtArray								= new Array();
		this.strLastLoad							= "";
	},
			
	setTarget :function(divTarget)
	{
		this.divTarget 								= divTarget;
	},	
	
	setTitle :function(h3Title)
	{
		this.h3Title 								= h3Title;
	},
	
	setActive :function(bActive)
	{
		this.bActive 								= bActive;
	},
	
	setNext :function(aNext)
	{
		this.aNext 									= $(aNext);
		this.aNxtArray								= $$('#'+aNext +' a');
		for (i = 0; i < this.aNxtArray.length; i++)
		{
			this.aNxtArray[i].refDaddy				= this;
			this.aNxtArray[i].addEvent('click', function(event)
			{
				if (this.refDaddy.iCurrentImg < this.refDaddy.aImageAnchors.length-1)
				{
					this.refDaddy.manualLoad(this.refDaddy.iCurrentImg+1);
				}
				event 								= new Event(event).stop();
			});
		}	
	},
	
	setPrev :function(aPrev)
	{
		this.aPrev 									= $(aPrev);
		this.aPrevArray								= $$('#'+aPrev +' a');
		for (i = 0; i < this.aPrevArray.length; i++)
		{
			this.aPrevArray[i].refDaddy				= this;
			this.aPrevArray[i].addEvent('click', function(event)
			{
				if (this.refDaddy.iCurrentImg > 0)
				{
					this.refDaddy.manualLoad(this.refDaddy.iCurrentImg-1);
				}
				event 								= new Event(event).stop();
			});
		}	
	},	
	
	manualLoad :function(iIndex)
	{		
		this.clickCatcher(this.aImageAnchors[Number(iIndex)], true);		
	},
	
	setImgAnchors :function(divThumbs)
	{
		this.aImageAnchors 							= $$(divThumbs+' a');
		// Bind all our anchors with a onClick function
		for (i = 0; i < this.aImageAnchors.length; i++)
		{
			this.aImageAnchors[i].onclick 			= this.clickCatcher.bindWithEvent(this.aImageAnchors[i]);
			this.aImageAnchors[i].refDaddy			= this;	
			this.aImageAnchors[i].intMyIndex		= i;
			this.aImageAnchors[i].addEvent('click', function(event)
			{
				// Scroll to 'top of page'
				var objScroll 						= new Fx.Scroll(window, {
															wait: 				false,
															duration: 			350,
															transition: 		Fx.Transitions.Expo.easeInOut
															});
			
				event 								= new Event(event).stop();
				objScroll.toElement('wrapper');
			});	
		}
	},
	
	ctrlBtns :function()
	{
		if(this.iCurrentImg < this.aImageAnchors.length-1)
		{
			var fxNxt 								= new Fx.Style(this.aNext, 'opacity');
			fxNxt.start(1);												   
		} else {
			var fxNxt 								= new Fx.Style(this.aNext, 'opacity');
			fxNxt.start(0);	
		}
		
		if(this.iCurrentImg  > 0)
		{
			var fxPrev 								= new Fx.Style(this.aPrev, 'opacity');
			fxPrev.start(1);												   
		} else {
			var fxPrev 								= new Fx.Style(this.aPrev, 'opacity');
			fxPrev.start(0);	
		}
	},
	
	clickCatcher :function(aOpt, bolManLoad)
	{			
		if (!bolManLoad)
		{
			this.refDaddy.iCurrentImg				= this.intMyIndex;
			if (this.refDaddy.strLastLoad != this.getAttribute('rel'))
			{			
				this.refDaddy.aClickCatcher.push(this);
				this.refDaddy.selectImage();
				this.refDaddy.strLastLoad			= this.getAttribute('rel');	
				this.refDaddy.ctrlBtns();
			} 
		} else {
			this.iCurrentImg						= aOpt.intMyIndex;
			this.aClickCatcher.push(aOpt);
			this.selectImage();
			this.strLastLoad						= aOpt.getAttribute('rel');	
			this.ctrlBtns();			
		}
	},
	
	selectImage :function()
	{
		if (!this.bActive)
		{
			this.bActive							= true;			
			// Get the current thumb's info			
			var objProp								= this.aClickCatcher.pop().getProperties('rel','title');		
			var arrImages							= new Array(objProp.rel);			
			// Take lights down		
			var fx 									= new Fx.Style(this.divTarget, 'opacity', 
			{
				onComplete: function() 
				{
					this.refDaddy.divTarget.setStyle('visibility', 'hidden');
					// Remove the current image
					$$('#' +this.refDaddy.divTarget.getAttribute('id') +' img').each(function(image) {	image.remove();	});
					// The divTarget has been hidden, the previous image has been removed...
					// ...it's time we loaded our new image									
					var cImageLoader				= new Asset.images(arrImages, 
					{
						objRef:						this.refDaddy,
						onProgress: function(i) 
						{				
							arrImages[i] 			= this;					
						},
						
						onComplete: function() 
						{
							this.objRef.divTarget.setStyle('visibility', 'visible');				
							for(var i = 0; i <arrImages.length; i++)						
							{
								var image			= arrImages[i];
								// Lets write it's alt tag
								image.setProperties({alt: 	objProp.title});					
								// Update the title  
								this.objRef.h3Title.setText(objProp.title);
								// Now, inject the image
								image.injectInside(this.objRef.divTarget);
								// Raise the lights											
								var fx2 									= new Fx.Style(this.objRef.divTarget, 'opacity', 
								{
									onComplete: function() 
									{		
										this.objRefExt.bActive				= false;	
										if (this.objRefExt.aClickCatcher.length)
										{
											this.objRefExt.aClickCatcher	= new Array(this.objRefExt.aClickCatcher[this.objRefExt.aClickCatcher.length - 1]);
											this.objRefExt.selectImage();												
										}										
									}
								});
								fx2.objRefExt								= this.objRef;
								fx2.start(1);						
							}
												
						}
					});
				}
			});
			fx.refDaddy								= this;
			fx.start(0);
		}				
	}	
});

document.misc 										= new TheRoundhouse.Frameworks.Misc();
var spImgGallery 									= new Gallery();
window.addEvent('domready', function()
						    {
								spImgGallery.setTarget($('gallery_img_large'));
								spImgGallery.setNext('btn_next');
								spImgGallery.setPrev('btn_prev');
								spImgGallery.setTitle($('img_title'));
								spImgGallery.setActive(false);
								spImgGallery.setImgAnchors('div#galery_img_thumb');								
								//
								var sGetQuery		= document.misc.$get("image");
								var iIndex			= (sGetQuery == "")? 0 : sGetQuery;
								spImgGallery.manualLoad(sGetQuery);
						    });