function addHover(strSelector)
{
	$$(strSelector).addEvents({
		click: function() { window.location = this.getElement('a').getProperty('href') },
		mouseenter: function() {
			this.toggleClass('hover');
			this.getElement('.portfolioTitle').setStyle('opacity', 0.66);
		},
		mouseleave: function() {
			this.toggleClass('hover');
			this.getElement('.portfolioTitle').setStyle('opacity', 0);
		}
	});
}

var SIFR = function( strFile )
{
	var titles = $(document.body).getElements('h1,h2,h3,h4,h5');

	var valid_titles = titles.filter(function(elem) {
		return elem.get('rel') !== 'skip';
	});

	valid_titles.each(function(title) {
		
		var text = title.get('html');
		var dimension = title.getSize();
		var styles = title.getStyles(
			'text-align',
			'padding-top',
			'padding-bottom',
			'padding-left',
			'padding-right',
			'font-size',
			'color',
			'padding-left',
			'line-height'
		);

		var swf_width = dimension['x'].toInt() - ( styles['padding-left'].toInt() + styles['padding-right'].toInt() );
		var swf_height = dimension['y'].toInt() - ( styles['padding-top'].toInt() + styles['padding-bottom'].toInt() );

		var obj = new Swiff(strFile, {
			width: swf_width ,
			height: swf_height ,
			container:title,
			params: {
				wmode: 'transparent'
			},
			vars: {
				txt: text,
				w: swf_width ,
				h: swf_height ,
				textalign: styles['text-align'],
				textcolor: styles['color'],
				offsetTop: styles['padding-top']
			}
		});
		
		title.setStyle('visibility', 'visible');
	});
}

var HomeGallery = new Class({
	
	arrSources: [],
	intPrevious: 0,
	
	initialize: function(arrSources)
	{
		this.arrSources = arrSources;
		this.intPrevious = arrSources.indexOf( $('pageImageHolder').getFirst().get('src') );
		
		new Asset.images(
			arrSources,
			{
				onComplete: function()
				{
					this.change.periodical(4000, this);
				}.bind(this)
			}
		);
	},
	
	change: function()
	{
		intCurrent = (this.intPrevious + 2 <= this.arrSources.length) ? this.intPrevious + 1 : 0;
		eleTopImage = $('pageImageHolder').getFirst();
		eleTopImage.setStyles({
			zIndex: 100
		});
		
		eleBottomImage = new Element(
			'img',
			{
				src: this.arrSources[intCurrent],
				'class': 'pageImage',
				style: {
					zIndex: 1
				}
			}
		).inject('pageImageHolder');
		
		eleTopImage.set(
			'tween',
			{
				duration: 800,
				onComplete: function() { this.element.destroy() }
			}
		).tween('opacity', 0);
		this.intPrevious = intCurrent;
	}
	
});

var Gallery = new Class({
	intPrevious: 0,
	arrThumbnails: [],
	arrImages: [],
	objFx: {},
	intPage: false,
	
	initialize: function(strImages, strThumbnails)
	{
		this.arrImages = $(strImages).getElements( 'img' );
		this.arrThumbnails = $(strThumbnails).getElements( 'img' );
		
		this.arrThumbnails.each( function(eleThumb, intIndex) {
			//if( intIndex != 0 ) eleThumb.setStyle('opacity', 0.5);
			eleThumb.addEvent('click', this.showImage.bind(this, intIndex));
		}.bind(this) );
	},
	
	showImage: function(intIndex)
	{
		if(this.intPrevious != intIndex)
		{
			// interrupt any currently running transition effects
			if( this.objFx.element ) {
				this.objFx.cancel();
				this.objFx.element.setStyle('display', 'none');
			}
			
			this.objFx = new Fx.Tween(
				this.arrImages[this.intPrevious],
				{
					onComplete: function(intPrevious, intIndex)
					{	
						this.arrImages[intPrevious].setStyle('display', 'none');
						this.arrImages[intIndex].setStyles({
							display: 'block',
							opacity: 0
						});
						this.arrImages[intIndex].fade(0, 1);
						$('photoCredit').innerHTML = this.arrThumbnails[intIndex].className;
					}.bind(this, [this.intPrevious, intIndex])
				}
			);
			this.objFx.start('opacity', 0);
			this.arrThumbnails[this.intPrevious].setStyle('opacity', 0.5);
			this.arrThumbnails[intIndex].setStyle('opacity', 1);
			this.intPrevious = intIndex;
		}
	},
	
	showNext: function()
	{
		intIndex = ((this.intPrevious + 1) < this.arrImages.length) ? this.intPrevious + 1 : 0;
		this.showImage(intIndex);
	},
	
	showPrevious: function()
	{
		intIndex = (this.intPrevious != 0) ? this.intPrevious - 1 : this.arrImages.length - 1;
		this.showImage(intIndex);
	},
	
	loadThumbnails: function()
	{
		arrImages = $unlink( this.arrThumbnails );
		var arrSources = [];
		this.arrThumbnails.each(function(eleImage) {
			arrSources.push(eleImage.get('src'));
		});

		new Asset.images(arrSources, {
			onComplete: function() {
				var objChain = new Chain;
				var eleImage = arrImages.shift();
				objChain.chain(function() {
					eleImage.set('tween', {
						duration: 300
					}).tween('opacity', 0, 1);
					this.callChain.delay(100, this);
				});
				arrImages.each(function(eleImage) {
					objChain.chain(function() {
						eleImage.set('tween', {
							duration: 300
						}).tween('opacity', 0, 0.5);
						this.callChain.delay(100, this);
					});
				});
				objChain.callChain();
			}
		} );
	}
});

window.addEvent('load', function() {
	SIFR('http://www.goforthgill.com/assets/flash/din-bold.swf');
});