/*******************************
 *
 *	Hardy & Grey Multimedia Gallery Class
 *
 *  Version: 1.1
 *
 *	Author: 
 *	The Roundhouse [DN]
 *
 *  © The Roundhouse 2008 -
 * 	ALL RIGHTS RESERVED
 */
 

var Gallery = new Class({
	objTarget 			: null,
	objMenu 			: null, 
	objDescription 		: null,
	aMenuItems			: null,
	iPrevHeight			: null,
	iTweenSpeed 		: 250,
	fTweenEase			: Fx.Transitions.Sine.easeOut,
	objCurrentImg 		: null,
	bBusy				: false,
	
	initialize: function()
	{
		// get all the anchors in the menu
		this.aMenuItems				= $$(this.objMenu+' a');
		this.aMenuImgs				= $$(this.objMenu+' img');
		for (var i = 0; i < this.aMenuItems.length; i++)
		{
			// store all the information we want to store in the element object
			this.aMenuItems[i].store("idx", i);
			this.aMenuItems[i].store("daddy", this);
			this.aMenuItems[i].store("imagePath", this.aMenuItems[i].get("rel"));
			this.aMenuItems[i].store("title", this.aMenuItems[i].get("title"));
			this.aMenuItems[i].store("discription", this.aMenuImgs[i].get('alt'));
			this.aMenuItems[i].addEvent('click', function()
			{
				//this.retrieve("daddy").setDescriptionText(this.retrieve("title"), this.retrieve("discription"));
				this.retrieve("daddy").loadNewImage(this.retrieve("imagePath"),this.retrieve("idx"));
			});			
		}
	},
	loadNewImage: function(param_imgPath,index)
	{
		if (!this.bBusy)
		{
			this.setBusy(true);
			
			for (var i = 0; i < this.aMenuItems.length; i++)
			{
				if(i != index && this.aMenuItems[i].hasClass("active"))
					this.aMenuItems[i].removeClass("active");
				if(i == index)
					this.aMenuItems[i].addClass("active");
			}
			// remove the old image
			this.objTarget.empty();
			// put in the new one
			var myImage 			= new Asset.image(param_imgPath, {id: 'myImage', title: 'myImage', onload: this.loadedImage});
			myImage.store('daddy', this);
			this.objCurrentImg 		= this.objTarget.adopt(myImage);	
			this.objCurrentImg.fade('hide');
		}
	},
	loadedImage: function()
	{
		// resize to the height of the new image 
		var myEffect 				= new Fx.Tween(this.retrieve("daddy").objTarget, {	duration: 	this.retrieve("daddy").iTweenSpeed, 
																						transition: this.retrieve("daddy").fTweenEase	});
 		myEffect.start('height',this.getSize().y);
		this.retrieve("daddy").transIn.delay(this.retrieve("daddy").iTweenSpeed, this);
	}, 
	transIn: function()
	{
		// fade our new image in 
		var myEffect 				= new Fx.Tween(this.retrieve("daddy").objCurrentImg, {	duration: 	this.retrieve("daddy").iTweenSpeed, 
																							transition: this.retrieve("daddy").fTweenEase	});
 		myEffect.start('opacity', 1);		
		this.retrieve("daddy").setBusy.delay(this.retrieve("daddy").iTweenSpeed, this.retrieve("daddy"));
	},
	setBusy: function(param_bol)
	{
			this.bBusy				= param_bol;
	},
	setDescriptionText: function(param_title, param_discription)
	{
		var heading3  				= new Element('h3');
		var paragraph  				= new Element('p');
		// empty out the old content
		this.objDescription.empty();
		// throw in the new content
		heading3.appendText(param_title);
		paragraph.appendText(param_discription);
		// add the new elements to page		
		this.objDescription.adopt(heading3);
		this.objDescription.adopt(paragraph);
	},
	setTargetObj: function(param_target)
	{
		this.objTarget				= param_target;
	},
	setMenuObj: function(param_menu)
	{
		this.objMenu				= param_menu;
	},
	setDescriptionObj: function(param_disc)
	{
		this.objDescription			= param_disc;
	},
	manualLoad: function(param_selected)
	{
		this.aMenuItems[Number(param_selected)].fireEvent('click');
	}
	
});

var spImgGallery 									= new Gallery();
window.addEvent('domready', function()
						    {
								spImgGallery.setTargetObj($('galleryframe'));
								spImgGallery.setDescriptionObj($('description'));
								spImgGallery.setMenuObj('div#thumbnailholder');	
								spImgGallery.initialize();
								// have a look in the query string, do we need
								//var sGetQuery		= swfobject.getQueryParamValue("img");
								//var iIndex			= (sGetQuery == "")? 0 : sGetQuery;
								//if(iIndex != "" && iIndex != 0)
									spImgGallery.manualLoad(0);
						    });