var togglers = new Class( {
	options : {
		togglers : [],
		toggleds : []
	},
	initialize : function(options) {
		this.setOptions(options);
		this.options.togglers.each(function(toggler, index) {
			toggler.addEvent('click', this.toggle.bindAsEventListener(this,
					index));
			if (toggler.hasClass('active')) {
				this.active = index;
			} else {
				if (this.options.toggleds[index] != undefined) {
					this.options.toggleds[index].addClass('hidden');
				}
			}
			;
		}, this);
	},
	toggle : function(event, index) {
		var event = new Event(event);

		this.hideCur();
		this.show(index);

		this.active = index;

		event.preventDefault();
		return false;
	},
	hideCur : function() {
		this.options.togglers[this.active].removeClass('active');
		this.options.toggleds[this.active].addClass('hidden');
	},
	show : function(index) {
		this.options.togglers[index].addClass('active');
		this.options.toggleds[index].removeClass('hidden');
	},
	toggleTo : function(where, keepHash) {
		var me = this, keepHash = keepHash || false;
		this.hideCur();
		this.options.togglers.each(function(t, index) {
			if (t.getProperty('href') === ("#" + where)) {
				me.show(index);
				me.active = index;
				if (!keepHash) {
					location.hash = "#";
				}

			}
		});
	}
});

togglers.implement(new Options, new Events);

var toggler_dd = new Class( {
	options : {
		toggler : $('toggler_dd')
	},
	initialize : function(options) {
		this.setOptions(options);
		this.active = options.toggler.getProperty('value');
		options.toggler.addEvent('change', this.toggle.bindAsEventListener(
				this, options));
	},
	toggle : function(event, options) {
		var event = new Event(event);
		var changeTo = options.toggler.getProperty('value');
		this.hideCur();
		this.show(changeTo);
		this.active = changeTo;
		return false;
	},
	hideCur : function() {
		$(this.active).addClass('hidden');
		$(this.active).setStyle('display', 'none');
	},
	show : function(id) {
		$(id).removeClass('hidden');
		$(id).setStyle('display', '');
	}
});

toggler_dd.implement(new Options, new Events);

var expanders = new Class(
		{
			options : {
				expanders : [],
				expandees : []
			},
			initialize : function(o) {
				this.setOptions(o);
				var me = this;
				this.options.expanders.each(function(exp, i) {
					if (!exp.hasClass('active')) {
						me.options.expandees[i].addClass('hidden');
					}
					exp.addEvent('click', function() {
						me.toggle(i);
						return false;
					});
				});
			},
			toggle : function(i) {
				var expandee = this.options.expandees[i].hasClass('hidden') ? 'remove'
						: 'add';
				var expander = this.options.expanders[i]
						.hasClass('expand_button') ? 'collapse_button'
						: 'expand_button';
				this.options.expandees[i][expandee + 'Class']('hidden');
				if (expander === 'collapse_button') {
					this.options.expanders[i].removeClass('expand_button');
					this.options.expanders[i].setText('Hide Details');
				} else {
					this.options.expanders[i].removeClass('collapse_button');
					this.options.expanders[i].setText('Show Details');
				}
				this.options.expanders[i].addClass(expander);
				return false;
			}
		});

expanders.implement(new Options, new Events);

window.addEvent('domready', function() {
	if ($('togglers')) {
		var el = $('togglers');
		pattern_togglers = new togglers( {
			togglers : $ES('a', el),
			toggleds : $ES('.toggled')
		});
	}
	if ($('togglers_local') && typeof window.disableLocalNav === 'undefined') {
		var el = $('togglers_local');
		pattern_togglers_local = new togglers( {
			togglers : $ES('a', el),
			toggleds : $ES('.toggled_local')
		});
	}
	if ($('togglers_alt_lg')) {
		var el = $('togglers_alt_lg');
		pattern_togglers_alt_lg = new togglers( {
			togglers : $ES('a', el),
			toggleds : $ES('.toggled_alt_lg')
		});
	}
	if ($('togglers_alt_sm')) {
		var el = $('togglers_alt_sm');
		pattern_togglers_alt_sm = new togglers( {
			togglers : $ES('a', el),
			toggleds : $ES('.toggled_alt_sm')
		});
	}
	if ($('toggler_dd')) {
		var el = $('toggler_dd');
		pattern_togglers_alt_sm = new toggler_dd( {
			toggler : el
		});
	}
	if ($$('.expander')) {
		var exp = $$('.expander');
		expand_togglers = new expanders( {
			expanders : exp,
			expandees : $ES('.expandee')
		});
	}
});