/*
	Base
*/
var __Page;

/*--------------------------------------------------------------
	DOM Ready
--------------------------------------------------------------*/
window.addEvent('domready', 
function() {
	__Page = new Page.Base();
	new Explorer();
	new Gallery();
});



/*--------------------------------------------------------------
	Gallery
	- Gallery Setup
--------------------------------------------------------------*/
Gallery = new Class({

	initialize: function() {
		this.newGallery();
	},
	
	/*--------------------------------------------------------------
		ExternalLinks
		- Loads target link in new window
	--------------------------------------------------------------*/
	newGallery: function() {
		$$('.project').each(function(p) {
			var imgHolder = p.getElement('.imgHolder');
			var infoText = p.getElement('.content');
			var moreBtn = p.getElements('.btnMore');
			var moreBtnTxt = p.getElement('.info').getElement('.btnMore');
			
			infoText.slide('hide');
			
			
			moreBtn.each(function(a){
			a.addEvent('click', 
			function() {
				
				//On clicking the moreBtn, slide/fade the infoText
				infoText.setStyle('display','block');
				infoText.slide('toggle');
				
				//Switch the text/class of the moreBtn to a lessBtn
				if(moreBtnTxt.get('text') == "Expand"){
					moreBtnTxt.set('text','Collapse');
				
					//create the image item from the image URL
					thumbLocation = imgHolder.getElement('.thumbnail').get('src');
					thumbHeight = imgHolder.getElement('.thumbnail').getStyle('height');
					thumbDescription = p.getElement('.info').getElement('.hdr').getElement('span').get('text');
					thumbLocation = thumbLocation.substring(0,thumbLocation.length-4);				
					var imageURL =  thumbLocation + '_large.jpg';
					
					//Show a loading image (possibly a black screen over the image)
					var loader  = new Element('div', {'class': 'imgLoad', 'text': 'loading..'});
					var loaderIcon  = new Element('div', {'class': 'imgLoadIcon'});

					!$(imgHolder).getElement('.imgLoad') ? loader.inject(imgHolder,'top'):null;
					$(imgHolder).getElement('.imgLoad') ? loaderIcon.inject(imgHolder.getElement('.imgLoad'),'top'):null;
					
					var image = new Asset.image(imageURL, {
					'class': 'imageElement',
					title: thumbDescription,
					alt: thumbDescription,
					onload: function() {
							//Insert the bigger image inside the imgHolder. Once it is loaded, expand the box to show it
							imgHolder.getElement('.thumbnail').setStyle('display','none');
							!imgHolder.getElement('imageElement') ? image.inject(imgHolder.getElement('a'), 'bottom'): null;
							imgHolder.morph({
								height: image.get('height')//593								
							});
							imgHolder.getElement('.imgLoad') ? imgHolder.getElement('.imgLoad').destroy(): null;
						}.bind(this)
					});
					
				}else {
					moreBtnTxt.set('text','Expand');

					imgHolder.morph({
						height: 232 //thumbHeight - occasionally defaults to auto for no reason
					});
				}				
				
			}.bind(this));
		});
			
			//possibly allow for multiple images for each project with button on either side to slide between them		
		});
	}
});


/*--------------------------------------------------------------
	Page
--------------------------------------------------------------*/
var Page = {};

Page.Base = new Class({
	initialize: function() {
		//this.ExternalLinks();
		//this.InternalMailtos();
		this.ToTop();
	},

	/*--------------------------------------------------------------
		ExternalLinks
		- Loads target link in new window
	--------------------------------------------------------------*/
	ExternalLinks: function() {
		$$('a[rel=external]').each(function(a) {
			a.set('target', '_blank');
		});
	},

	/*--------------------------------------------------------------
		InternalMailtos
		- Prevent email scrappers from capturing exposed addys
	--------------------------------------------------------------*/
	InternalMailtos: function() {
		$$('a').each(function(a) {
			if (a.hasClass('mailed')) {
				var myObject = {
					prop0: '@',
					prop1: '<?= $DomainName; ?>'
				};
				a.set('href', 'mailto:' + a.get('text').substitute(myObject) + a.get('rel'));
				a.set('text', a.get('text').substitute(myObject));
				a.removeProperty('rel');
			}
		});
	},


	/*--------------------------------------------------------------
		ToTop
	--------------------------------------------------------------*/
	ToTop: function() {
		this.s = new Fx.Scroll(window, {
			transition: Fx.Transitions.Sine.easeOut
		});

		var b = $$('a.toTop');

		b.forEach(function(el) {
			el.set('href', 'javascript:void(0);');
			el.addEvent('click', 
			function() {
				this.s.toTop();
			}.bind(this));
		},
		this);
	}
});

/*--------------------------------------------------------------
	Explorer
	- Internet Explorer helpers
--------------------------------------------------------------*/
Explorer = new Class({

	initialize: function() {
		this.IEHover();
		this.IE6InlinePNG();
	},

	/*--------------------------------------------------------------
		IEHover
		- Adds hovering class
	--------------------------------------------------------------*/
	IEHover: function() {
		$$('.addHover').forEach(function(el) {
			el.addEvent('mouseover', 
			function() {
				this.addClass('hovering');
			});
			el.addEvent('mouseout', 
			function() {
				this.removeClass('hovering');
			});
		});
	},

	IE6InlinePNG: function() {
		if (Browser.Engine.trident4) {
			$$('img').each(function(img) {
				if (img.get('src').test('.png')) {
					img.setStyles({
						'filter': 'progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled="true", src="' + img.get('src') + '", sizingMethod="image")'
					});
					img.set('src', '/images//trans.gif');
				}
			});
		}
	}
});
