var Site = {

	start: function(){
		new textResizzle();

		Site.fixBoxListings();
		Site.setUpAccordions();
		Site.setUpHomeFader();
		Site.setUpExternalLinks();
		Site.setUpFormValidators();
	},

	formHandler: function(pass, form, submitEvent) {
		// Do anything necessary here
	},

	firstFail: null,

	setUpFormValidators: function() {
		var valForms = $$('form.validate-form');
		if ( valForms.length ) {
			valForms.each(function(elem, idx) {
				new FormValidator.Inline(elem, {
					'errorPrefix': '',
					'useTitles': true,
					'serial': false,
					'scrollToErrorsOnSubmit': false,
					'onFormValidate': function(passed, form, e) {
						Site.firstFail = null;
						elem.setProperty('valPassed', passed);

						if ( passed ) {
							elem.submit = null;
						}
					},
					'onElementFail': function(e, val_msgs) {
						if ( Site.firstFail == null ) {
							Site.firstFail = e;

							// If we're on the water bore form, all the validated
							// fields are in the first accordion. Open it.
							if ($('accordion-content-general')) {
								new Fx.Slide('accordion-content-general').show();
								$('accordion-title-general').addClass('accordion-open');

								new Fx.Scroll(window).toElement(e);
							}
						}
					}
				});
			});
		}

		var valForms = $$('form.validate-form-custom');
		if ( valForms.length ) {
			valForms.each(function(elem, idx) {
				elem.setProperty('valPassed', false);

				new FormValidator(elem, {
					'errorPrefix': '',
					'useTitles': true,
					'serial': false,
					'scrollToErrorsOnSubmit': false,
					'onFormValidate': function(passed, form, e) {
						elem.setProperty('valPassed', passed);

						if ( passed ) {
							elem.submit = null;
						}
					}
				});
			});
		}
	},

	setUpExternalLinks: function() {
		$$('a.external').each(function(elem){
			elem.setProperty('target', '_blank');
		});
	},

	setUpHomeFader: function() {
		if ($('home-feature')) {
			$('feature-images').fader({
				fadeWaitTime: 4000,
				duration: 1000
			});
		}
	},

	setUpAccordions: function() {
		// All the accordion-content divs are initially hidden using CSS (display: none).
		// They must be hidden using the slider tool (which applies it to a different element),
		// then unhidden from our CSS. If that makes sense.
		$$('.accordion-content').each(function(elem){
			new Fx.Slide(elem).hide();
			elem.show();
			// elem's parent is hidden, so elem is hidden too
		});

		// Add click handlers for titles
		$$('.accordion-title').each(function(elem){
			elem.addEvent('click', Site.toggleAccordionContent);
		});

		// If we've loaded the page with #item-1234 in the URL, open that accordion
		if (document.location.toString().indexOf('#') > 0) {
			var hash_value = document.location.toString().split('#')[1];
			if (hash_value.contains('item-')) {
				var node_id = hash_value.replace('item-', '');

				if ($('accordion-content-'+node_id)) {
					var slider = new Fx.Slide('accordion-content-'+node_id);
					slider.show();
					$('accordion-title-'+node_id).addClass('accordion-open');
				}
			}
		}
	},

	toggleAccordionContent: function(event) {
		// Get the node ID from the href and create the slider element
		var node_id = this.getProperty('href').replace('#item-', '');
		var slider = new Fx.Slide('accordion-content-'+node_id);

		// We can't use .open properly until after we toggle
		var anchor = this;
		slider.toggle().chain(function(){
			if(this.open) {
				anchor.removeClass('accordion-closed').addClass('accordion-open');
			} else {
				anchor.removeClass('accordion-open').addClass('accordion-closed');
			}
		});
	},

	fixBoxListings: function() {
		/*
		 * 	Requirements:
		 *	If a box listing has 3 items -> 3 on one line
		 *  If a box listing has 4 items -> 2 then 2
		 *  If a box listing has 5 items -> 3 then 2
		 *  If a box listing has 6 items -> 3 then 3
		 *  If a box listing has 7 items -> 3 then 2 then 2
		 *
		 */

		$$('.box-listing').each(function(ul) {
			var items = ul.getChildren('li');
			var num_items = items.length;

			// Exceptions:
			// - On the Community page the last item (FAQs) is full width
			if ($$('.pc87239').length) {
				num_items--;
			}

			switch (num_items) {
				case 1:
					items[0].addClass('box-width-full');
					break;
				case 2:
					items[0].addClass('box-width-half');
					items[1].addClass('box-width-half').addClass('box-position-right');
					break;
				case 3:
					items[0].addClass('box-width-third');
					items[1].addClass('box-width-third');
					items[2].addClass('box-width-third').addClass('box-position-right');
					break;
				case 4:
					items[0].addClass('box-width-half');
					items[1].addClass('box-width-half').addClass('box-position-right');
					items[2].addClass('box-width-half');
					items[3].addClass('box-width-half').addClass('box-position-right');
					break;
				case 5:
					items[0].addClass('box-width-third');
					items[1].addClass('box-width-third');
					items[2].addClass('box-width-third').addClass('box-position-right');
					items[3].addClass('box-width-half');
					items[4].addClass('box-width-half').addClass('box-position-right');
					break;
				case 6:
					items[0].addClass('box-width-third');
					items[1].addClass('box-width-third');
					items[2].addClass('box-width-third').addClass('box-position-right');
					items[3].addClass('box-width-third');
					items[4].addClass('box-width-third');
					items[5].addClass('box-width-third').addClass('box-position-right');
					break;
				case 7:
					items[0].addClass('box-width-third');
					items[1].addClass('box-width-third');
					items[2].addClass('box-width-third').addClass('box-position-right');
					items[3].addClass('box-width-half');
					items[4].addClass('box-width-half').addClass('box-position-right');
					items[5].addClass('box-width-half');
					items[6].addClass('box-width-half').addClass('box-position-right');
					break;
			}

			if (num_items <= 7) {
				items.each(function(item){
					item.removeClass('mod1');
				});
			}
		});
	}
};

window.addEvent('domready', Site.start);
