window.addEvent('domready', function () {
	var forms = $$('form'); // Get all forms
	var excludeInputs = ['optin']; // Inputs that should not hide labels

	forms.each(function (form) {
		if (!form.hasClass('recipeSearch') && !form.hasClass('exclude')) {
			var labels = form.getElements('label'); // Get labels

			labels.each(function (label) { // Loop through labels
				var labelFor = label.get('for');
				// If labels for is not in excludeInputs
				if (!excludeInputs.contains(labelFor)) {
					label.addClass('hide'); // Hide label

					// Set input value to label text
					var input = form.getElement('#'+labelFor);
					if ($defined(input) && input.get('value').length < 1) {
						input.set('value', label.get('text'));
						input.store('default', label.get('text'));

						// Make wrapper clickable
						var wrap = input.getParent('.wrap .wrap');
						if (wrap) {
							wrap.addEvent('click', function () {
								input.focus();
							});
						}

						// Clear value when input gets focus
						input.addEvent('focus', function () {
							if (input.retrieve('default').toLowerCase() === input.get('value').toLowerCase())
								input.set('value', '');
						});

						input.addEvent('blur', function () {
							if (input.get('value').length < 1) {
								input.set('value', input.retrieve('default'));
								input.store('user_text', false);
							} else {
								input.store('user_text', true);
							}
						});
					}
				}
			});

			form.addEvent('submit', function () {
				this.getElements('input[type=text], textarea').each(function (field) {
					if (!field.retrieve('user_text')) {
						field.set('value', '');
					}
				});
			});
		}
	});

	// Used to fake min-height in IE 6 by injecting a prop div
	var minHeightProp = function (target, height) {
		if ($type(target) !== 'array') {
			target = [].include(target);
		}

		target.each(function (item) {
			item.setStyles({
				position: 'relative'
			});
			var prop = new Element('div', {
				styles: {
					width: 0,
					height: height,
					'float': 'right'
				}
			});

			prop.inject(item, 'top');
		});
	};

	if (Browser.Engine.trident && Browser.Engine.version === 4) {
		switch ($(document.body).get('id')) {
			case 'home':
				// Get all elements we need to apply a prop to
				var topCols = [
					$$('.col.mealPlanner'),
					$$('.col.freshAssurance'),
					$$('.col.products')
				].flatten();

				// Apply min height prop
				minHeightProp(topCols, 500);

				var lowerCols = [
					$$('.col.recipesOfTheWeek'),
					$$('.col.whatsCooking'),
					$$('.col.signUp')
				].flatten();

				minHeightProp(lowerCols, 400);
			break;
		}
	}

	$$('ul.nav').each(function (ul) {
		ul.getElements('li').each(function (li) {
			var ul = li.getFirst('ul');

			if ($defined(ul) && !li.hasClass('active')) {
				ul.addClass('hide');
				
				li.getFirst('a').addEvent('click', function (e) {
					e.stop();
					ul.toggleClass('hide');
					li.toggleClass('active');
				});
			}
		});
	});

	if ($('searchDrop')) {
		$('searchDrop').addEvent('submit', function (e) {
			e.stop();

			var url = this.getElement('select').getSelected().get('value');
			window.location.href = BASE_URL+'products/'+url+'/#content';
		});
	}

	if ($('terms-link')) {
		$('terms-link').addEvent('click', function (e) {
			e.stop();
			window.open(this.get('href'), 'terms', 'width=800,height=500,scrollbars=yes');
		});
	}

	if ($$('.iconPrint')) {
		$$('.iconPrint').addEvent('click', function (e) {
			e.stop();
			window.print();
		})
	}

	if ($$('.rateList').length > 0) {
		var rateList = $$('.rateList')[0];
		var rateLinks = rateList.getElements('li a');

		rateLinks.each(function (link) {
			link.addEvent('mouseover', function (e) {
				var linkI = rateLinks.indexOf(e.target)+1;
				for (var i = 0; i < rateLinks.length; i++) {
					if (i < linkI) {
						rateLinks[i].addClass('active');
					} else {
						rateLinks[i].removeClass('active');
					}
				}
			});
		});

		rateList.addEvent('mouseleave', function (e) {
			for (var i = 0; i < rateLinks.length; i++) {
				rateLinks[i].removeClass('active');
			}
		});
	}

	if ($(document.body).get('id') === 'contact') {
		var form = $('contact-form');
		var reason = form.getElement('select#reason');
		var cf = form.getElements('.complaint');

		var checkForm = function () {
			if (reason.get('value') === 'complaints') {
				cf.setStyle('display', 'block');
			} else {
				cf.setStyle('display', 'none');
			}
		};

		reason.addEvent('change', checkForm);

		checkForm();
	}
});

window.addEvent('load', function () {
	if ($('hero')) {
		var middle = $('middle');
		var mS = middle.getSize();

		var hero = $('hero');
		var hS = hero.getSize();

		hero.setStyle('left', (mS.x/2)-(hS.x/2));
	}
});