// If we're not running this script on the local server, log stats with Google Analytics.
if (location.hostname !== 'boris') _gat._getTracker('UA-5882591-9')._trackPageview();

addLoadEvent(function () {
	buttonHover();
	panelHover();
	validateContactUsPanelForm();
});

function panelHover() {
	var a;
	var panels = [
		$('panelProjectShowcase'),
		$('panelTestimonial'),
		$('panelProperty')
	];
	var url;

	// Loop through panels.
	for (var i = 0; i < panels.length; i++) {

		// If the panel doesn't exist in the DOM, continue to the next.
		if (!panels[i]) { continue; }

		// Ensure the cursor turns into a pointer when the panel is hovered upon.
		panels[i].style.cursor = 'pointer';

		// Create handlers for mouseover, mouseout and click events.
		var mouseoverHandler, mouseoutHandler, clickHandler;

		// Create closure so that element-specific values aren't reset before handlers are fired.
		(function () {

			// Get child anchor and its URL.
			var a = panels[i].getElementsByTagName('a')[0];
			var url = a.getAttribute('href');

			// When the panel is hovered upon, ensure the button is highlighted.
			panels[i].onmouseover = function () {
				addClass(a, 'buttonHover');
			};

			// When the panel is moused off of, ensure button is no longer highlighted.
			panels[i].onmouseout = function () {
				removeClass(a, 'buttonHover');
			};

			// When the panel is clicked, visit its child anchor's URL.
			panels[i].onclick = function () {
				window.location = url;
			};
		})();
	}
}

function validateContactUsPanelForm() {
	var form = $('contactUsPanelForm');
	if (!form) { return; }

	var inputName = $('inputName');
	var inputEmail = $('inputEmail');
	var textareaMessage = $('textareaMessage');

	form.onsubmit = function () {
		if (!validate(inputName, 'Please enter your name', hasSubstance(inputName.value))) { return false; }
		if (!validate(inputEmail, 'Please enter your email address', hasSubstance(inputEmail.value))) { return false; }
		if (!validate(inputEmail, 'Please enter a valid email address. It should resemble \'name@company.com\'', isEmailAddress(inputEmail.value))) { return false; }
		if (!validate(textareaMessage, 'Please enter a message', hasSubstance(textareaMessage.value))) { return false; }
	};
}