var app = {
	init: function() {
		app.FormSwitchers.initialize();
	},

	/**
	 * Class for toggling and grouping form switchers (defined by
	 * formSwitcherContainer class)
	 */
	FormSwitchers: {
		SwitcherContainer: Class.create({
			initialize: function(containerElement) {
				this.switchers = [];
				this.container = $(containerElement);
				this.ul = $(Builder.node('ul'));
				this.container.insert({
					top: $(Builder.node('div', {'class': 'headersContainer'}, [
						this.ul,
						$(Builder.node('div', {'style': 'clear: both'}))
					]))
				})

				var childs = this.container.childElements();
				var size = childs.size();

				// 1 because 0th element is our <ul>
				for (var index = 1; index < size; index = index + 2) {
					var header = childs[index];
					var content = childs[index + 1];

					this.addSwitcher(header, content);
				}

				this.activate(0);
			},

			addSwitcher: function(header, content) {
				var switcher = new app.FormSwitchers.Switcher(this, header, content);
				var index = this.switchers.push(switcher);

				// Add as LI element
				this.ul.insert({
					bottom: $(Builder.node('li', {}, header.remove()))
				});
			},

			activate: function(index) {
				this.switchers.invoke('hide');
				this.switchers[index].show();
			},

			activateBySwitcher: function(switcher) {
				this.activate(this.switchers.indexOf(switcher));
			}
		}),

		Switcher: Class.create({
			initialize: function(parent, header, content) {
				this.header = header;
				this.content = content;
				this.parent = parent;
				this.index = null;

				this.header.observe('click', this.headerClicked.bindAsEventListener(this));
			},

			headerClicked: function(event) {
				if (this.parent) {
					this.parent.activateBySwitcher(this);
					event.stop();
				}
			},

			show: function() {
				this.header.writeAttribute({'class': 'switcherHeaderActive'});
				this.content.show();
			},

			hide: function() {
				this.header.writeAttribute({'class': 'switcherHeader'});
				this.content.hide();
			},

			setIndex: function(value) {
				this.index = value;
			}
		}),

		initialize: function() {
			$$('.formSwitcherContainer').each(function(containerElement) {
				var container = new this.SwitcherContainer(containerElement);
			}.bind(this));
		}
	},

	siteAdmin: {
		editAdminLocations: function() {
			$('checkButtons').show();

			var eachCheckbox = function(eachFunction) {
				return function(event) {
					$$('#locationsForm input[type=checkbox]').each(eachFunction);

					event.stop();
				}
			}

			$('checkAll').observe('click', eachCheckbox(function(checkbox) {
				checkbox.checked = true;
			}));
			$('checkNone').observe('click', eachCheckbox(function(checkbox) {
				checkbox.checked = false;
			}));
			$('checkInvert').observe('click', eachCheckbox(function(checkbox) {
				checkbox.checked = ! checkbox.checked;
			}));
		}
	}
};

document.observe('dom:loaded', app.init);
