var FluidHelpers = new Class({
	Implements: [Options, Events],

	options: {
		attachTo:		'',			// ID of element to use as parent object
		panelHeader:	'',			// If accordion-like panels are to be used, set this to the heading tag
		tweenDelay:		500,		// how long to delay between chained tween events
		tweenDuration:	2000,		// how long to carry a tween out over
		effectType:		'slideshow' // the type of effect to attach. one of 'slideshow', 'stepthrough'
	},
	
	initialize: function (options) {
		this.setOptions(options);
		if (this.options.panelHeader) {
			this.setupPanels();
		}
		
		this.setupOverlay();
		
		
		if (this.options.effectType == 'slideshow') {
			this.setupSlideshow();
		}
		else if (this.options.effectType == 'stepthrough') {
			this.el = $$('#'+this.options.attachTo + ' > ul')[0];
			$$('a.left').addEvent('click', function (e) { e.stop(); this.stepBackward();}.bindWithEvent(this));
			$$('a.right').addEvent('click', function (e) { e.stop(); this.stepForward();}.bindWithEvent(this));
		}
	},

	setupPanels: function() {
		var selector = '#' + this.options.attachTo + ' > div';
		this.buildPanels();
		var panels = $$(selector);
		panels.each(function(el) {
			var expand = new Element('span', { 'class': 'expand open'});
			expand.injectInside(el.getPrevious());
			
			el.store('origHeight', el.getSize().y).set('tween', { duration: this.options.tweenDuration });
			
			var title = el.getPrevious().addEvent('click', function(e){
				e.stop();
				
				var elem = $(e.target).getNext();
				
				this.currentItem = elem;
				this.closePanels();
				
				$(e.target).set('class', 'open');
				elem.tween('height', elem.getStyle('height').toInt() > 0 ? 0 : elem.retrieve('origHeight'));
				
				if (elem.getStyle('height').toInt() > 0) {
					expand.set('class', 'expand open');
				}
				else {
					expand.set('class', 'expand closed');
				}
			}.bindWithEvent(this));
		}.bind(this));
		this.closePanels();
	},
	
	closePanels: function() {
		var selector = '#' + this.options.attachTo + ' > div';
		var panels = $$(selector);
		
		panels.each(function(el) {
			if (this.currentItem != el) {
				el.getPrevious().set('class', '');
				el.getPrevious().getElement('span.expand').set('class', 'expand open');
				el.tween('height', 0);
			}
		});
	},
	
	buildPanels: function () {
		var header;
		$(this.options.attachTo).getChildren().each(function(el) {
		    if (el.get('tag') == this.options.panelHeader) {
		        header = new Element('div').inject(el, 'after');
		    } else if (header) {
		        header.grab(el);
		    }
		}.bind(this));
	},
	
	setupSlideshow: function () {
		this.el = $$('#'+this.options.attachTo + ' > ul')[0];
		if (this.el == null) {
			return;
		}
		this.el.getElements('li').each(function (elem) {
			elem.set('opacity', 0);
		}.bind(this));
		this.el.getFirst('li').set('opacity', 1);
		this.transitionIN();
	},
	
	transitionIN: function () {
		this.current = this.el.getFirst();
		if (this.current == null) {
			return;
		}
		
		this.tween = new Fx.Tween(this.current, {
			duration: this.options.tweenDuration,
			transition: this.options.transition,
			link: 'chain',
			property: 'opacity'
		}).start(1)
			.addEvent('onComplete', function () {
				if (this.image) {
					this.image.dispose();
				}
				this.transitionOUT.delay(this.options.fadeDelay, this);
			}.bind(this));
	},
	
	transitionOUT: function () {
		this.current.getParent().getParent().setStyle('background', 'url(\''+ this.current.getNext().getElement('img').get('src')+'\')');
		if (this.current == null) {
			return;
		}
		this.tween = new Fx.Tween(this.current, {
			duration: this.options.tweenDuration,
			transition: this.options.transition,
			link: 'chain',
			property: 'opacity'
		}).start(1, 0)
			.addEvent('onComplete', function () {
				this.el.getFirst().dispose().inject(this.el);
				this.transitionIN();
			}.bind(this));
	},
	
	stepForward: function (e) {
		this.current = this.el.getFirst();
		if (this.current == null) {
			return;
		}
		this.current.getParent().setStyle('background', 'url(\''+ this.current.getNext().getElement('img').get('src')+'\')');
		this.tween = new Fx.Tween(this.current, {
			duration: this.options.tweenDuration,
			transition: this.options.transition,
			link: 'chain',
			property: 'opacity'
		}).start(1, 0)
			.addEvent('onComplete', function () {
				this.el.getFirst().dispose().inject(this.el);
			}.bind(this));
	},
	
	stepBackward: function (e) {
		this.current = this.el.getFirst();
		if (this.current == null) {
			return;
		}
		this.current.getParent().setStyle('background', 'url(\''+ this.el.getLast().getElement('img').get('src')+'\')');
		this.tween = new Fx.Tween(this.current, {
			duration: this.options.tweenDuration,
			transition: this.options.transition,
			link: 'chain',
			property: 'opacity'
		}).start(1, 0)
			.addEvent('onComplete', function () {
				this.el.getLast().dispose().inject(this.el);
			}.bind(this));
	},
	
	setupOverlay: function () {
		var assets = $$('a[class=popup]');
		if (assets) {
			assets.each(function (el) {
				el.addEvent('click', function (e) {
					e.stop();
					
					var elem = new Element('img', {'src': e.target.get('href') });//.inject(document.body).setStyle('display', 'none');
					
					SqueezeBox.open(elem, {
						size: { x: 370, y: 500 },
						handler: 'adopt',
						onClose: function () {
							this.destroy();
						}.bind(elem)
					});
				});
			});
		}
	}
	
	
});

SiteUtils = {
	applyTextReplacement: function(selector, mode) {
		if (mode == 'navigation') {
			var options = { hover: true };
		} else if (mode == 'bold-headings') {
			var options = {fontFamily:"LubalGraph Bd BT"};
		}
		 else if (mode == 'light-headings') {
			var options = {fontFamily:"LubalinGraph LT Demi"};
		}
		else {
			var options = {};
		}

		Cufon.replace(selector, options);
	},

	switchTextSize: function(mode) {
		$(document.body).erase('class').addClass(mode + 'Text');

		Cookie.write('textMode', mode, { path: '/', duration: 30 });
	}
};

window.addEvent('domready', function () {
	new FluidHelpers({
		attachTo: 'promo'
	});
	new FluidHelpers({
		attachTo: 'homepageGallery',
		effectType: 'stepthrough'
	});
});
