var $E = function $E(selector) {
	return $$(selector)[0];
};

var Overlay = new Class({
	overlay: function(elem, id) {
		var overlay = new Element('div', {'id': 'overlay', 'styles': {'opacity': '0', 'visibility': 'visible', 'height': '0', 'overflow': 'hidden'}}).inject(document.body),
			e = $(id) || new Element(elem, {'id': id}).inject(document.body);
		return e.addClass('lightbox');
	},
	showOverlay: function(close) {
		var h = window.getScroll().y,
			w = (Browser.Engine.presto) ? window.innerWidth/2: window.getScrollSize().x/2;
		$('overlay').set('tween').setStyles({
			'top': -$(window).getScroll().y,
			'height':$(window).getScrollSize().y+$(window).getScroll().y,
			'visibility': 'visible',
			'display': 'block'
		}).tween('opacity', '0.7');
		$E('.lightbox').setStyles({'display': 'block', 'visibility': 'visible'});
		$E('.lightbox').setStyles({'top': h + 100 + 'px', 'left': w - ($E('.lightbox').getScrollSize().x/2) + 'px'});
		close.addEvent('click', function(event) {
			$E('.lightbox').destroy();
			$('overlay').destroy();
		});
	}
});

var Lightbox = new Class({
	Implements: [Options, Events, Overlay],
	options: {
		// onShow: $empty,
		elems: false,
		containerId: null,
		url: false,
		closeId: 'closeLightbox',
		closeSelector: false,
		cache: true
	},
	initialize: function(options) {
		this.setOptions(options);
		this.content = [];
		if (this.options.elems) {
			$$(this.options.elems).each(function(elem) {
				elem.addEvent('click', function(event) {
					event.preventDefault();
					this.display();
				}.bind(this));
			}, this);
		} else if (this.options.elems !== null) {
			this.display();
		}
	},
	display: function() {
		var containerId = $type(this.options.containerId) === 'array' ? this.options.containerId[index]: this.options.containerId,
			contentIndex = $type(this.options.containerId) === 'array' ? index: 0,
			container = this.overlay('div', containerId),
			close;
		if (this.content[contentIndex]) {
			container.innerHTML = this.content[contentIndex];
			close= this.options.closeSelector ? $E(this.options.closeSelector) : $(this.options.closeId);
			this.showOverlay(close);
		} else {
			if (this.options.url) {
				this.fetchContent(container, contentIndex, this.options.cache);
			} else {
				if (this.options.cache) this.content[contentIndex] = container.innerHTML;
				close = this.options.closeSelector ? $E(this.options.closeSelector) : $(this.options.closeId);
				this.showOverlay(close);
			}
		}
		this.fireEvent('show', [container, this.options.elems]);
	},
	fetchContent: function(container, index, cache) {
		var close,
		requester = new Request({
			url: this.options.url,
			method: 'get',
			onSuccess: function(responseText) {
				container.innerHTML = responseText;
				this.content[index] = responseText;
				close = this.options.closeSelector ? $E(this.options.closeSelector) : $(this.options.closeId);
				this.showOverlay(close);
			}.bind(this)
		});
		requester.send();
	}
});

var parseUrl = function parseUrl(url, segment) {
	var parts,
		partNames,
		partsObject,
		parseUrl = /^(?:([A-Za-z]+):)?(?:\/{0,3})?([A-Za-z0-9\.\-]+)?(\/[\.A-Za-z\-0-9\/]+)(?:\?([A-Za-z0-9\s=\.&@$%\*\(\)!,\:\+]+))?(?:#([A-Za-z0-9]+))?$/;
	if (parseUrl.test(url)) {
		parts = parseUrl.exec(url);
		partNames = ['url', 'protocol', 'base', 'path', 'query', 'hash'];
		partsObject = parts.associate(partNames);
		return (segment) ? partsObject[segment]: partsObject;
	} else {
		return false;
	}
};

var parseQuery = function parseQuery(url, exclude) {
	var query,
		parts,
		queryObject = {},
		parseQuery = /^([A-Za-z0-9]+)(?:=([A-Za-z0-9\.@$%\*\(\)!,\+]+))$/,
		parsed = parseUrl(url, 'query');
	if (parsed) {
		query = parsed.split('&');
		for (var i = 0, l = query.length; i < l; i += 1) {
			if (parseQuery.test(query[i])) {
				parts = parseQuery.exec(query[i]);
				if (parts[1] !== exclude) queryObject[parts[1]] = parts[2];
			}
		}
	}
	return queryObject;
}

var Login = new Class({
	Implements: [Options, Events, Chain],
	options: {
		// onFinish: $empty,
		url: null,
		required: 3,
		error: {
			'email': 'Please enter your email address.',
			'password': 'Please enter your password.',
			'incorrect': 'Sorry, you are not logged in. Your e-mail address and/or password was invalid.'
		}
	},
	initialize: function(forms, options) {
		this.setOptions(options);
		this.forms = $$(forms);
		this.forms.each(function(form, index) {
			form.addEvent('submit', function(event) {
				event.preventDefault();
				this.checkForm(form);
			}.bind(this));
		}, this);
	},
	checkForm: function(form) {
		var params = [],
			inputs = form.getElements('input');
		inputs.each(function(input) {
			if (input.get('type') !== 'submit' && input.get('type') !== 'image') {
				if ( input.value !== '') {
					params.push(input.get('name') + '=' + encodeURIComponent(input.value));
				} else {
					alert(this.options.error[input.get('name')]);
				}
			}
		}, this);
		if (params.length === this.options.required) this.sendForm(form, params);
	},
	sendForm: function(form, params) {
		var url = this.options.url;
		var requester = new Request({
			url: url,
			onComplete: function(responseText) {
				if (responseText.length === 0) {
					alert(this.options.error['incorrect']);
				} else {
					this.fireEvent('onFinish', [this.forms, this.forms.indexOf(form)]);
				}
			}.bind(this)
		});
		requester.send(params.join('&'));
	}
});

var Accordion = new Class({
	Implements: [Options, Events],
	options: {
		// onCollapse: $empty,
		// onExpand: $empty,
		trigger: 'click',
		direction: 'vertical',
		openedClass: 'expanded',
		closedClass: 'collapsed',
		duration: 'normal',
		allClosed: false,
		multiOpen: false,
		opacity: true,
		fixedHeight: false,
		fixedWidth: false,
		startOpen: 0
	},
	initialize: function(togglers, folds, options) {
		this.setOptions(options);
		this.togglers = $$(togglers);
		this.folds = $$(folds);
		this.open = [];
		this.params = this.options.opacity ? {'opacity': '0'}: {};
		this.dimension = this.options.direction === 'horizontal' ? 'width': 'height';
		this.params[this.dimension] = '0';
		this.folds.each(function(fold, index) {
			if (!fold.hasClass(this.options.closedClass)) this.open.push[fold];
			if (this.options.allClosed || (index !== this.options.startOpen && !this.options.multiOpen)) {
				fold.setStyles(this.params);
			}
		}, this);
		this.togglers.each(function(toggler, index) {
			toggler.addEvent(this.options.trigger, function(event) {
				event.preventDefault();
				var i = (this.togglers.length > this.folds.length) ? 0: index;
				this.display(this.folds[i]);
			}.bind(this));	
		}, this);
	},
	display: function(fold) {
		if (fold.hasClass(this.options.openedClass)) {
			this.collapse(fold);
		} else {
			this.expand(fold);
		}
	},
	collapse: function(fold) {
		this.open.splice[this.folds.indexOf(fold), 1];
		this.fireEvent('collapse', [this.togglers, this.folds, this.folds.indexOf(fold)]);
		fold.set('morph', { duration: this.options.duration, onComplete: function() {
			fold.removeClass(this.options.openedClass).addClass(this.options.closedClass);
		}.bind(this)});
		for (var fx in this.params) this.params[fx] = '0';
		fold.morph(this.params);
	},
	expand: function(fold) {
		if (!this.options.multiOpen) for (var i = 0, l = this.open.length; i < l; i += 1) this.collapse(this.open[i]);
		this.open.push(fold);
		this.fireEvent('expand', [this.togglers, this.folds, this.folds.indexOf(fold)]);
		fold.set('morph', { duration: this.options.duration });
		fold.removeClass(this.options.closedClass).addClass(this.options.openedClass);
		h = this.options.fixedHeight ? this.options.fixedHeight: fold.scrollHeight + 'px';
		w = this.options.fixedWidth ? this.options.fixedWidth: fold.offsetWidth + 'px';
		this.params[this.dimension] = this.options.direction === 'horizontal' ? w: h;
		this.params['opacity'] = '1';
		fold.morph(this.params);
	}
});

/*
I took this out of more.js so that we didn't have to include the entire more library on every page.
Script: Tips.js
	Class for creating nice tips that follow the mouse cursor when hovering an element.
	License:
		MIT-style license.
	Authors:
		Valerio Proietti
		Christoph Pojer
*/
var Tips = new Class({
	Implements: [Events, Options],
	options: {
		onShow: function(tip){
			tip.setStyle('visibility', 'visible');
		},
		onHide: function(tip){
			tip.setStyle('visibility', 'hidden');
		},
		title: 'title',
		text: function(el){
			return el.get('rel') || el.get('href');
		},
		showDelay: 100,
		hideDelay: 100,
		className: null,
		offset: {x: 16, y: 16},
		fixed: false
	},
	initialize: function(){
		var params = Array.link(arguments, {options: Object.type, elements: $defined});
		if (params.options && params.options.offsets) params.options.offset = params.options.offsets;
		this.setOptions(params.options);
		this.container = new Element('div', {'class': 'tip'});
		this.tip = this.getTip();
		
		if (params.elements) this.attach(params.elements);
	},
	getTip: function(){
		return new Element('div', {
			'class': this.options.className,
			styles: {
				visibility: 'hidden',
				display: 'none',
				position: 'absolute',
				top: 0,
				left: 0
			}
		}).adopt(
			new Element('div', {'class': 'tip-top'}),
			this.container,
			new Element('div', {'class': 'tip-bottom'})
		).inject(document.body);
	},
	attach: function(elements){
		var read = function(option, element){
			if (option == null) return '';
			return $type(option) == 'function' ? option(element) : element.get(option);
		};
		$$(elements).each(function(element){
			var title = read(this.options.title, element);
			element.erase('title').store('tip:native', title).retrieve('tip:title', title);
			element.retrieve('tip:text', read(this.options.text, element));
			
			var events = ['enter', 'leave'];
			if (!this.options.fixed) events.push('move');
			
			events.each(function(value){
				element.addEvent('mouse' + value, element.retrieve('tip:' + value, this['element' + value.capitalize()].bindWithEvent(this, element)));
			}, this);
		}, this);
		
		return this;
	},
	detach: function(elements){
		$$(elements).each(function(element){
			['enter', 'leave', 'move'].each(function(value){
				element.removeEvent('mouse' + value, element.retrieve('tip:' + value) || $empty);
			});
			
			element.eliminate('tip:enter').eliminate('tip:leave').eliminate('tip:move');
			
			if ($type(this.options.title) == 'string' && this.options.title == 'title'){
				var original = element.retrieve('tip:native');
				if (original) element.set('title', original);
			}
		}, this);
		
		return this;
	},
	elementEnter: function(event, element){
		$A(this.container.childNodes).each(Element.dispose);
		
		['title', 'text'].each(function(value){
			var content = element.retrieve('tip:' + value);
			if (!content) return;
			
			this[value + 'Element'] = new Element('div', {'class': 'tip-' + value}).inject(this.container);
			this.fill(this[value + 'Element'], content);
		}, this);
		
		this.timer = $clear(this.timer);
		this.timer = this.show.delay(this.options.showDelay, this, element);
		this.tip.setStyle('display', 'block');
		this.position((!this.options.fixed) ? event : {page: element.getPosition()});
	},
	elementLeave: function(event, element){
		$clear(this.timer);
		this.tip.setStyle('display', 'none');
		this.timer = this.hide.delay(this.options.hideDelay, this, element);
	},
	elementMove: function(event){
		this.position(event);
	},
	position: function(event){
		var size = window.getSize(), scroll = window.getScroll(),
			tip = {x: this.tip.offsetWidth, y: this.tip.offsetHeight},
			props = {x: 'left', y: 'top'},
			obj = {};
		
		for (var z in props){
			obj[props[z]] = event.page[z] + this.options.offset[z];
			if ((obj[props[z]] + tip[z] - scroll[z]) > size[z]) obj[props[z]] = event.page[z] - this.options.offset[z] - tip[z];
		}
		
		this.tip.setStyles(obj);
	},
	fill: function(element, contents){
		if(typeof contents == 'string') element.set('html', contents);
		else element.adopt(contents);
	},
	show: function(el){
		this.fireEvent('show', [this.tip, el]);
	},
	hide: function(el){
		this.fireEvent('hide', [this.tip, el]);
	}
});

var Search = new Class({
	Implements: [Options, Events],
	options: {
		// onFinish: $empty,
		url: null,
		events: 'submit',
		preventDefault: true,
		cache: true,
		params: null,
		query: 'value',
		container: null,
		containerID: null,
		inputIndex: 0
	},
	initialize: function(elems, containers, options) {
		this.setOptions(options);
		this.elems = $$(elems);
		var containers = $$(containers);
		this.elems.each(function(elem, index) {
			if (this.elems.length === containers.length) {
				this.addSection(elem, containers[index]);
			} else {
				this.addSection(elem, containers[0]);
			}
		}, this);
	},
	addSection: function(elem, container) {
		elem.store('container', container);
		this.attach(elem);
	},
	attach: function(elem) {
		var es = this.options.events;
		if (typeof es === 'object' && es.constructor === Array) {
			es.each(function(e) {
				elem.addEvent(e, function(event) {
					this.getResults(elem, event);
				}.bind(this));
			});
		} else {
			elem.addEvent(es, function(event) {
				this.getResults(elem, event);
			}.bind(this));
		}
	},
	serialize: function(elem) {
		var i = this.elems.indexOf(elem),
			params = [], serialized, e, url;
		$H(this.options.params).each(function(value, key) {
			if ($type(key) === 'object') {
				$H(key).each(function(v, k) {
					params.include(key + '=' + $$(k)[i].get(v));
				});
			} else {
				params.include(key + '=' + value);
			}
		});
		serialized = params.join('&');
		e = (elem.nodeName === 'FORM') ? elem.getElements('input[type^=text]')[this.options.inputIndex]: elem;
		url = (this.options.params) ? this.options.url + e.get(this.options.query) + '&' + serialized :
			this.options.url + e.get(this.options.query);
		return url;
	},
	getResults: function(elem, e) {
		if (this.options.preventDefault) { e.preventDefault() }
		var container = elem.retrieve('container');
		if (!container.retrieve('hasResults')) {
			var requester = new Request({
				method: 'get',
				url: this.serialize(elem),
				onSuccess: function(responseText) {
					container.innerHTML = responseText;
					if (this.options.cache) container.store('hasResults', true);
					this.fireEvent('onFinish', [elem, container]);
				}.bind(this)
			});
			requester.send();
		}
	}
});

var Autocomplete = new Class({
	Extends: Search,
	options: {
		// onFormSubmit: $empty,
		chars: 1,
		events: 'keyup',
		resultsElement: 'li',
		clickSubmit: false,
		containerClass: 'autocomplete',
		resultsClass: 'highlighted',
		container: 'ul',
		url: null
	},
	initialize: function(elems, options) {
		this.setOptions(options);
		this.elems = $$(elems);
		this.elems.each(function(elem) {
			var input = elem.getElements('input[type^=text]')[0];
			input.store('form', elem);
			this.attach(input);
		}, this);
	},
	getResults: function(elem, event) {
		var keys = [9, 16, 17, 18, 19, 20, 27, 33, 34, 35, 36, 37, 38, 39, 40, 91, 92, 93, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 145],
			container, containerClass, url, position;
		event.preventDefault();
		if (this.requester) this.requester.cancel();
		if (keys.indexOf(event.code) === -1) {
			containerClass = this.options.containerClass;
			container = $('autocomplete') || new Element(this.options.container, {'id': 'autocomplete', 'className': containerClass});
			container.setStyle('visibility', 'visible');
			this.cur = elem.get('value');
			url = this.options.url + this.cur;
			this.requester = new Request({
				method: 'get',
				url: url,
				onSuccess: function(responseText) {
					container.innerHTML = responseText;
					container.inject(elem.parentNode);
					position = -1;
					this.resultsListeners(elem, container, position);
				}.bind(this)
			});
			if (this.cur.length >= this.options.chars) this.requester.send();
		}
	},
	resultsListeners: function(elem, container, position) {
		var searchForm = elem.retrieve('form'),
			results = container.getElements(this.options.resultsElement);
		results.each(function(result) {
			result.addEvents({
				'mouseover': function(event) {
					position = results.indexOf(result);
					this.updatePosition(elem, results, results.indexOf(result));
				}.bind(this),
				'mousedown': function(event) {
					event.preventDefault();
					elem.set('value', result.firstChild.nodeValue);
					container.setStyle('visibility', 'hidden').empty();
					if (this.options.clickSubmit) searchForm.submit();
				}.bind(this)
			});
		}, this);
		elem.addEvents({
			'keyup': function(event) {
				if (event.code === 38) {
					if (position === -1 || position === 0) {
						position = results.length - 1;
					} else {
						position -= 1;
					}
					this.updatePosition(elem, results, position, true);
				} else if (event.code === 40 ) {
					if (position === results.length - 1) {
						position = 0;
					} else {
						position += 1;
					}
					this.updatePosition(elem, results, position, true);
				} else if (event.code === 8 && elem.get('value').length === 0) {
					container.setStyle('visibility', 'hidden').empty();
				}
			}.bind(this),
			'blur': function(event) {
				setTimeout(function() { container.setStyle('visibility', 'hidden').empty(); }, 100);
			}
		});
	},
	updatePosition: function(elem, results, position, setvalue) {
		for (var i = 0, l = results.length; i < l; i+=1) results[i].removeClass(this.options.resultsClass);
		results[position].addClass(this.options.resultsClass);
		if (setvalue) elem.set('value', results[position].firstChild.nodeValue);
	}
});

var CheckboxSearch = new Class({
	Extends: Search,
	options: {
		events: 'click',
		indexify: false,
		cache: false,
		delay: 400
	},
	getResults: function(elem) {
		var container = elem.retrieve('container');
		var checkNum = this.getChecked();
		var url = (this.options.params) ? this.options.url + checkNum + '&' + this.serialize(elem) :
			this.options.url + checkNum;
		if (!this.timer) $clear(this.timer);
		if (checkNum.length > 0) {
			var requester = new Request({
				method: 'get',
				url: url,
				onSuccess: function(responseText) {
					container.innerHTML = responseText;
					this.fireEvent('onFinish', [elem, container]);
				}.bind(this)
			});
			if (this.options.delay) {
				setTimeout(function() {
					requester.send();
				}, this.options.delay)
			} else { requester.send(); }
		} else {
			container.empty();
		}
	},
	getChecked: function() {
		var checkedArray = [];
		this.elems.each(function(elem) {
			var c = elem.get('checked');
			if (c === 'checked' || c === true) { checkedArray.push(elem.get(this.options.query)); }
		}, this);
		return checkedArray.join(',');
	}
});

var Tabs = new Class({
	Implements: [Events, Options],
	options: {
		// onShow: $empty,
		// onHide: $empty,
		showClass: 'showTab',
		hideClass: 'hideTab'
	},
	initialize: function(tabs, containers, options) {
		this.setOptions(options);
		this.fireEvent('onInit', [tabs, containers]);
		this.tabs = $$(tabs);
		this.containers = $$(containers);
		this.tabs.each(function(tab, index) {
			if (this.containers.length > 1) {
				tab.store('container', this.containers[index]);
			} else {
				tab.store('container', this.containers[0]);
			}
			this.attach(tab, index);
		}, this);
	},
	attach: function(tab, index) {
		var container = tab.retrieve('container');
		tab.addEvent('click', function(event) {
			event.preventDefault();
			this.show(container, index);
		}.bind(this));
	},
	show: function(container, index) {
		this.hideAll(index);
		this.fireEvent('onShow', [this.tabs, this.containers, index]);
		container.removeClass(this.options.hideClass).addClass(this.options.showClass);
	},
	hideAll: function(index) {
		for (var i = 0, l = this.containers.length; i < l; i+=1) this.containers[i].removeClass(this.options.showClass).addClass(this.options.hideClass);
		this.fireEvent('onHide', [this.tabs, this.containers, index]);
	}
});

var AjaxTabs = new Class({
	Extends: Tabs,
	options: {
		// setListeners: $empty,
		urls: null,
		query: false,
		cache: true,
		cacheCurrent: true
	},
	attach: function(tab, index) {
		var container = tab.retrieve('container');
		if (this.options.cacheCurrent && tab.hasClass(this.options.showClass)) tab.store('content', container.innerHTML);
		tab.addEvent('click', function(event) {
			event.preventDefault();
			this.show(tab, container, index);
		}.bind(this));
	},
	show: function(tab, container, index) {
		var url = this.options.urls[index] || this.tabs[index].get('href');
		if (this.options.query) url += tab.get(this.options.query);
		this.hideAll(index);
		if (this.tabs[index].retrieve('content')) {
			container.innerHTML = this.tabs[index].retrieve('content');
		} else if (!url) {
			 this.parent(container, index);
		} else {
			this.getContent(container, url, index);
		}
		this.fireEvent('onShow', [this.tabs, this.containers, index]);
	},
	getContent: function(container, url, index) {
		var requester = new Request({
			method: 'get',
			url: url,
			onComplete: function(responseText) {
				container.innerHTML = responseText;
				container.removeClass(this.options.hideClass).addClass(this.options.showClass);
				if (this.options.cache) this.tabs[index].store('content', responseText);
				this.fireEvent('setListeners', [this.tabs, this.containers, index]);
			}.bind(this)
		});
		requester.send();
	}
});

var AjaxPaging = new Class({
	Implements: [Options, Events],
	options: {
		// onFinish: $empty,
		url: false
	},
	initialize: function(links, container, options) {
		this.setOptions(options);
		$$(links).each(function(a) {
			a.addEvent('click', function(event) {
				event.preventDefault();
				var url = (this.options.url) ? this.options.url: a.get('href');
				var requester = new Request.HTML({
					method: 'get',
					url: url,
					update: container,
					onSuccess: function(responseText) {
						this.initialize(links, container, options);
						this.fireEvent('onFinish', [links, container]);
					}.bind(this)
				});
				requester.send();
			}.bind(this));	
		}, this);
	}
});
var Slideshow = new Class({
	Implements: [Options, Events, Chain],
	options: {
		// onInit: $empty,
		// onHide: $empty,
		// onShow: $empty,
		start: 0,
		delay: 7000,
		duration: 500,
		hideClass: 'hideSlide',
		showClass: 'showSlide',
		opacity: true,
		progressButton: false,
		progressPanel: 'panelProgress',
		previous: null,
		pause: null,
		next: null
	},
	initialize: function(slides, options) {
		var progress = $(this.options.progressPanel);
		this.setOptions(options);
		this.slides = $$(slides);
		this.index = this.options.start;
		if (this.options.opacity) {
			for (var i = 0, l = this.slides.length; i < l; i += 1) {
				if (this.options.progressButton) this.slides[i].store('button', new Element('a').inject(progress));
				if (i !== this.index) this.slides[i].setStyle('opacity', '0');
				this.slides[i].set('tween', {
					duration: this.options.duration,
					onComplete: function() {
						this.callChain();
					}.bind(this)
				});
			}
		}
		this.timer = setInterval(function() {
			this.display(true);
		}.bind(this), this.options.delay);
		this.setControls(this.options.previous, this.options.pause, this.options.next);
		this.fireEvent('onInit', [this.slides, this.index]);
	},
	display: function(effects, show, pause) {
		this.hideSlide(this.slides[this.index], effects);
		if ($type(show) === 'number') {
			this.index = show;
		} else if (show === 'previous') {
			this.index = this.index === 0 ? this.slides.length - 1: this.index - 1;
		} else {
			this.index = this.index === this.slides.length - 1 ? 0: this.index + 1;
		}
		this.showSlide(this.slides[this.index], effects);
		if (pause) clearTimeout(this.timer);
	},
	hideSlide: function(slide, effects) {
		this.chain(
			function() {
				if (effects) {
					slide.tween('opacity', '0');
				} else {
					slide.setStyle('opacity', '1');
					this.callChain();
				}
			},
			function() { slide.removeClass(this.options.showClass).addClass(this.options.hideClass); this.callChain(); },
			function() {
				this.fireEvent('onHide', [this.slides, this.index]);
				this.callChain();
			}
		);
	},
	showSlide: function(slide, effects) {
		this.chain(
			function() {
				this.fireEvent('onShow', [this.slides, this.index]);
				this.callChain();
			},
			function() { slide.removeClass(this.options.hideClass).addClass(this.options.showClass); this.callChain(); },
			function() {
				if (effects) {
					slide.tween('opacity', '1');
				} else {
					slide.setStyle('opacity', '1');
					this.callChain();
				}
			}
		);
		this.callChain();
	},
	setControls: function(prevLink, pauseLink, nextLink) {
		if (prevLink) {
			$(prevLink).addEvent('click', function(event) {
				event.preventDefault();
				this.display(false, 'previous', true);
			}.bind(this));
		}
		if (nextLink) {
			$(nextLink).addEvent('click', function(event) {
				event.preventDefault();
				this.display(false, 'next', true);
			}.bind(this));
		}
		if(pauseLink) {
			$(pauseLink).addEvent('click', function(event) { 
				event.preventDefault(); clearTimeout(this.timer);
			}.bind(this));
		}
	}
});

var Slidereel = new Class({
	Implements: [Options, Events],
	options: {
		// onChange: $empty,
		slidesSelector: 'div',
		wrapper: false,
		wrapperStyles: {},
		prevLink: false,
		nextLink: false,
		start: 0,
		duration: 500
	},
	initialize: function(container, options) {
		var scrollH = container.getScrollSize().y,
			h = container.getSize().y,
			w = container.getSize().x,
			scrollW = scrollH > h ? w * (scrollH/h): w;
		this.setOptions(options);
		this.now = this.options.start;
		this.container = container;
		this.slides = this.container.getElements(this.options.slidesSelector);
		this.wrapper = this.options.wrapper || new Element('div').wraps(this.container);
		this.wrapper.setStyles($merge(this.options.wrapperStyles, {
			width: w,
			position: 'relative',
			overflow: 'hidden'
		}));
		this.container.setStyles({
			'left': this.options.start,
			'width': scrollW,
			'overflow': 'visible'
		}).set('tween', {'duration': this.options.duration, 'onStart': function() {
			this.fireEvent('onChange', [this.wrapper, this.container, this.slides, this.now]);
		}.bind(this)});
		if (this.options.prevLink) this.setControls(this.options.prevLink, 'previous');
		if (this.options.nextLink) this.setControls(this.options.nextLink, 'next');
	},
	setControls: function(elem, action) {
		elem.addEvent('click', function(event) {
			event.preventDefault();
			this.display(action);
		}.bind(this));
	},
	display: function(action) {
		var w = this.wrapper.getSize().x,
			scrollW = this.container.getScrollSize().x;
		if (action === 'previous') {
			if (this.now === 0) {
				this.now = w - scrollW;
			} else {
				this.now = this.now + w;
			}
		} else {
			if (this.now === w - scrollW) {
				this.now = 0;
			} else {
				this.now = this.now - w;
			}
		}
		this.container.tween('left', this.now);
	}
});

var InputClear = new Class({
	Implements: [Options, Events],
	options: {
		elems: 'input[type^=text]',
		reset: true
	},
	initialize: function(options) {
		this.setOptions(options);
		$$(this.options.elems).each(function(elem) {
			if (elem.get('type') === 'password') elem.store('orig', elem.getStyle('background-image'));
			this.attach(elem);
		}, this);
	},
	attach: function(elem) {
		elem.addEvents({
			'focus': function() {
				if (elem.get('type') !== 'password') elem.store('orig', elem.get('value'));
				elem.set('value', '');
				if (elem.get('type') === 'password') elem.setStyle('background-image', 'none');
			},
			'blur': function() {
				if (elem.get('value') === '') {
					if (elem.get('type') === 'password') {
						elem.setStyle('background-image', elem.retrieve('orig'));
					} else {
						elem.set('value', elem.retrieve('orig'));
					}
				}
			}
		});
	}
});

var suckerfish = function suckerfish(elems) {
	if (Browser.Engine.trident4) {
		var sfEls = $$(elems);
		for (var i=0, l = sfEls.length; i < l; i++) {
			sfEls[i].onmouseover=function() {
				this.className+=" sfhover";
			}
			sfEls[i].onmouseout=function() {
				this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
			}
		}
	}
};

var externalLinks = function externalLinks(items) {
	var links = (items) ? $$(items): $$('a.external');
	links.each(function(a) {
		a.addEvent('click', function(e) {
			e.preventDefault;
			open(a.get('href'));
			return false;
		});
	});	
};

var printPage = function printPage(elems) {
	$$(elems).each(function(elem) {
		elem.addEvent('click', function(event) {
			event.preventDefault();
			window.open(elem.get('href'), 'printWindow', 'width=790,height=400,toolbar=0,location=0,directories=0,status=0,menubar=1,scrollbars=1,resizable=1');
		});
	});	
};
var emailPage = function emailPage(elems) {
	$$(elems).each(function(elem) {
		elem.addEvent('click', function(event) {
			event.preventDefault();
			window.open(elem.get('href'), 'printWindow', 'width=625,height=400,toolbar=0,location=0,directories=0,status=0,menubar=1,scrollbars=1,resizable=1');
		});
	});	
};
var smallWindow = function smallWindow(elems, width, height) {
	$$(elems).each(function(elem) {
		elem.addEvent('click', function(event) {
			event.preventDefault();
			var w = (width) ? width: '520';
			var h = (height) ? height: '450';
			window.open(elem.get('href'), 'printWindow', 'width=' + w + ',height=' + h + ',toolbar=0,location=0,directories=0,status=0,menubar=1,scrollbars=1,resizable=1');
		});
	});	
};
var createOverlay = function createOverlay(elem, id) {
	var overlay = new Element('div', {'id': 'overlay', 'styles': {'opacity': '0', 'visibility': 'visible', 'height': '0', 'overflow': 'hidden'}}).inject(document.body),
		e = $(id) || new Element(elem, {'id': id}).inject(document.body);
	return e;
};

window.addEvent('domready', function() {
	suckerfish($$('#globalheader li'));
	new InputClear({ elems: $$('.clearField')});
	externalLinks();
	var tooltips = new Tips ($$('.tooltip'), {
		className: 'tipContainer',
		text: null
	});
	if (window.showPromo) showPromo();
});