/*******************************
 *
 *	Gallery Class
 *
 *  Version: 1.2
 *
 *	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,
	iCurrent			: null,
	fForcedWidth		: 0,
	cScrollBar			: null,
	objScrollContent	: null,
	
	initialize: function()
	{
		//var galleryForcewidth = $('gallery_forcewidth');
		
		//if(galleryForcewidth)
		//{
			// 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++)
			{
				if(i < this.aMenuImgs.length)
				{
					// 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"));
					
					var strDescription = this.aMenuImgs[i].get('alt');
					if(strDescription != null && strDescription != '')
						strDescription = urldecode(strDescription);
					
					this.aMenuItems[i].store("discription", strDescription);
					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"));
						
					});	
					
					this.aMenuItems[i].addEvent('mouseup', function()
					{
							var imgPadding	= 5;
							//var step 		= (this.retrieve("daddy").objScrollContent.getScrollSize().x - this.retrieve("daddy").objScrollContent.getSize().x);
							//var newStep		= this.retrieve("daddy").objScrollContent.getParent().getPosition().x + imgPadding - (this.retrieve("daddy").objScrollContent.getPosition().x);
							//this.retrieve("daddy").cScrollBar.objSlider.set(Math.round(newStep));
					});
					
					this.fForcedWidth 		+= (this.aMenuImgs[i].getStyle('width').toInt()+20);
				}
			}
			//galleryForcewidth.setStyle('width', this.fForcedWidth);	
			//$('gallery_images').setStyle('width', this.fForcedWidth);	
		//}
		
	},
	loadNewImage: function(param_imgPath,index)
	{
		if (!this.bBusy)
		{
			this.setBusy(true);
			this.iCurrent = index;
			
			if(this.iCurrent == 0 && $('prev'))
				$('prev').addClass('hidden');
			else if($('prev'))
				$('prev').removeClass('hidden');
			
			if(this.iCurrent == (this.aMenuItems.length-3) && $('next'))
				$('next').addClass('hidden');
			else if($('next'))
				$('next').removeClass('hidden');
			
			for (var i = 0; i < this.aMenuItems.length; i++)
			{
				if(i != index && this.aMenuItems[i].getParent().hasClass("active"))
					this.aMenuItems[i].getParent().removeClass("active");
				if(i == index)
					this.aMenuItems[i].getParent().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');
			if (this.aMenuItems.length < 2)	$$('.paging')[0].setStyle('visibility', 'hidden');
		}
	},
	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('span');
		// empty out the old content
		this.objDescription.empty();
		// throw in the new content
		if (param_title) 			
		{	
			heading3.set('html', param_title);
			heading3.setProperty('id', 'title');
		}
		if (param_discription)	
		{
			paragraph.set('html', param_discription);
			paragraph.addClass("date");
		}		
		// 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;
	},
	setScrollBar: function(param_bar, param_content)
	{
		//this.cScrollBar				= param_bar;
		//this.objScrollContent		= param_content;
	},
	manualLoad: function(param_selected)
	{
		this.aMenuItems[Number(param_selected)].fireEvent('click');
		this.aMenuItems[Number(param_selected)].fireEvent('mouseup');
	}
	
});

var spImgGallery 									= new Gallery();
window.addEvent('domready', function()
						    {
								var test = $('dyn_image');
								if(test)
								{
									spImgGallery.setTargetObj($('dyn_image'));
									spImgGallery.setDescriptionObj($('title-info'));
									spImgGallery.setMenuObj('#gallery_images');	
									spImgGallery.initialize();
									
									$('next').addEvent('click', function()
									{
										if (spImgGallery.iCurrent < spImgGallery.aMenuItems.length-1)
											spImgGallery.manualLoad(parseFloat(spImgGallery.iCurrent)+1);
									});	
									
									$('prev').addEvent('click', function()
									{	
										if (spImgGallery.iCurrent > 0)
											spImgGallery.manualLoad(parseFloat(spImgGallery.iCurrent)-1);				
									});
							
									// 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);
								}
						    });

function urldecode (str) {
    // http://kevin.vanzonneveld.net
    // +   original by: Philip Peterson
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +      input by: AJ
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Brett Zamir (http://brett-zamir.me)
    // +      input by: travc
    // +      input by: Brett Zamir (http://brett-zamir.me)
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Lars Fischer
    // +      input by: Ratheous
    // +   improved by: Orlando
    // %        note 1: info on what encoding functions to use from: http://xkr.us/articles/javascript/encode-compare/
    // *     example 1: urldecode('Kevin+van+Zonneveld%21');
    // *     returns 1: 'Kevin van Zonneveld!'
    // *     example 2: urldecode('http%3A%2F%2Fkevin.vanzonneveld.net%2F');
    // *     returns 2: 'http://kevin.vanzonneveld.net/'
    // *     example 3: urldecode('http%3A%2F%2Fwww.google.nl%2Fsearch%3Fq%3Dphp.js%26ie%3Dutf-8%26oe%3Dutf-8%26aq%3Dt%26rls%3Dcom.ubuntu%3Aen-US%3Aunofficial%26client%3Dfirefox-a');
    // *     returns 3: 'http://www.google.nl/search?q=php.js&ie=utf-8&oe=utf-8&aq=t&rls=com.ubuntu:en-US:unofficial&client=firefox-a'
    
    var hash_map = {}, ret = str.toString(), unicodeStr='', hexEscStr='';
    
    var replacer = function (search, replace, str) {
        var tmp_arr = [];
        tmp_arr = str.split(search);
        return tmp_arr.join(replace);
    };
    
    // The hash_map is identical to the one in urlencode.
    hash_map["'"]   = '%27';
    hash_map['(']   = '%28';
    hash_map[')']   = '%29';
    hash_map['*']   = '%2A';
    hash_map['~']   = '%7E';
    hash_map['!']   = '%21';
    hash_map['%20'] = '+';
    hash_map['\u00DC'] = '%DC';
    hash_map['\u00FC'] = '%FC';
    hash_map['\u00C4'] = '%D4';
    hash_map['\u00E4'] = '%E4';
    hash_map['\u00D6'] = '%D6';
    hash_map['\u00F6'] = '%F6';
    hash_map['\u00DF'] = '%DF';
    hash_map['\u20AC'] = '%80';
    hash_map['\u0081'] = '%81';
    hash_map['\u201A'] = '%82';
    hash_map['\u0192'] = '%83';
    hash_map['\u201E'] = '%84';
    hash_map['\u2026'] = '%85';
    hash_map['\u2020'] = '%86';
    hash_map['\u2021'] = '%87';
    hash_map['\u02C6'] = '%88';
    hash_map['\u2030'] = '%89';
    hash_map['\u0160'] = '%8A';
    hash_map['\u2039'] = '%8B';
    hash_map['\u0152'] = '%8C';
    hash_map['\u008D'] = '%8D';
    hash_map['\u017D'] = '%8E';
    hash_map['\u008F'] = '%8F';
    hash_map['\u0090'] = '%90';
    hash_map['\u2018'] = '%91';
    hash_map['\u2019'] = '%92';
    hash_map['\u201C'] = '%93';
    hash_map['\u201D'] = '%94';
    hash_map['\u2022'] = '%95';
    hash_map['\u2013'] = '%96';
    hash_map['\u2014'] = '%97';
    hash_map['\u02DC'] = '%98';
    hash_map['\u2122'] = '%99';
    hash_map['\u0161'] = '%9A';
    hash_map['\u203A'] = '%9B';
    hash_map['\u0153'] = '%9C';
    hash_map['\u009D'] = '%9D';
    hash_map['\u017E'] = '%9E';
    hash_map['\u0178'] = '%9F';
    hash_map['\u00C6'] = '%C3%86';
    hash_map['\u00D8'] = '%C3%98';
    hash_map['\u00C5'] = '%C3%85';
 
    for (unicodeStr in hash_map) {
        hexEscStr = hash_map[unicodeStr]; // Switch order when decoding
        ret = replacer(hexEscStr, unicodeStr, ret); // Custom replace. No regexing
    }
    
    // End with decodeURIComponent, which most resembles PHP's encoding functions
    ret = decodeURIComponent(ret);
 
    return ret;
}
