
var Control = {
	emailAFriendFormValidator: {},
	beInTheKnowFormValidator: {},
	load: function() {
		$$('.elemToFade').setStyles({ display: 'block', opacity: 0 });

		var navImagesToOver = $$('.NavOver');
		navImagesToOver.each(function(item) {
			item.addEvent('mouseenter', Control.onNavOver);
			item.addEvent('mouseleave', Control.onNavOver);
			Asset.image(item.src.replace('.png', 'Over.png'));
		});

		$('NavEmailAFriendImg').addEvent('click', Control.onNavMenuClick);
		$('NavEmailAFriendCloseButton').addEvent('click', Control.onNavMenuClick);
		$('NavBeInTheKnowImg').addEvent('click', Control.onNavMenuClick);
		$('NavBeInTheKnowCloseButton').addEvent('click', Control.onNavMenuClick);
		$('FriendsEmailTextBox').set('value', $('FriendsEmailTextBox').getProperty('label'));
		$('FriendsEmailTextBox').addEvent('focus', Control.manageTextLabel);
		$('FriendsEmailTextBox').addEvent('blur', Control.manageTextLabel);
		$('YourEmailTextBox').set('value', $('YourEmailTextBox').getProperty('label'));
		$('YourEmailTextBox').addEvent('focus', Control.manageTextLabel);
		$('YourEmailTextBox').addEvent('blur', Control.manageTextLabel);
		$('EmailTextBox').set('value', $('EmailTextBox').getProperty('label'));
		$('EmailTextBox').addEvent('focus', Control.manageTextLabel);
		$('EmailTextBox').addEvent('blur', Control.manageTextLabel);

		$('NavEmailAFriendForm').addEvent('submit', Control.submitForm);
		$('NavBeInTheKnowForm').addEvent('submit', Control.submitForm);

		Control.emailAFriendFormValidator = new FormValidator('NavEmailAFriendForm', { useTitles: true, errorPrefix: '', evaluateFieldsOnBlur: false, onElementValidate: Control.ValidateElem });
		Control.beInTheKnowFormValidator = new FormValidator('NavBeInTheKnowForm', { useTitles: true, errorPrefix: '', evaluateFieldsOnBlur: false, onElementValidate: Control.ValidateElem });

		if (Browser.Engine.trident) {
			var areas = $$('area');
			areas.each(function(item) {
				item.addEvent('focus', function(event) { event.target.blur(); });
			});
		}
	},
	ValidateElem: function(isValitaded, elem, validatorName) {
		if (isValitaded) {
			if (elem.hasClass(elem.id + 'Error')) elem.removeClass(elem.id + 'Error').addClass(elem.id);
		}
		else {
			if (elem.hasClass(elem.id)) elem.removeClass(elem.id).addClass(elem.id + 'Error');
		}
	},
	submitForm: function(e) {
		e.stop();
		var requestJsonData = new Hash();
		requestJsonData.set('formId', e.target.id);
		switch (e.target.id) {
			case 'NavEmailAFriendForm':
				if (Control.emailAFriendFormValidator.validate()) {
					requestJsonData.set('friendsEmail', $('FriendsEmailTextBox').get('value'));
					requestJsonData.set('yourEmail', $('YourEmailTextBox').get('value'));
				}
				break;
			case 'NavBeInTheKnowForm':
				if (Control.beInTheKnowFormValidator.validate()) {
					requestJsonData.set('email', $('EmailTextBox').get('value'));
				}
		}

		if (requestJsonData.getLength() > 1) { //form is valid, query service
			$(this.id.replace('Form', 'ActiveDiv')).fade('out').get('tween').chain(function() {
				$(this.element.id.replace('ActiveDiv', 'LoadingDiv')).fade('in');
			});
			var request = new Request.JSON({
				urlEncoded: false,
				headers: { 'Content-Type': 'application/json; charset=utf-8' },
				url: this.get('action'),
				data: JSON.encode(requestJsonData),
				onComplete: Control.onFormSubmitComplete
			}).send();
		}
	},
	onFormSubmitComplete: function(result) {
		var resultDataItem = result.d;
		$(resultDataItem.formId.replace('Form', 'ConfirmationDiv')).set('html', resultDataItem.userMessage);
		$(resultDataItem.formId.replace('Form', 'LoadingDiv')).set('tween', { link: 'chain' });
		$(resultDataItem.formId.replace('Form', 'LoadingDiv')).fade('out').get('tween').chain(function() {
			$(this.element.id.replace('LoadingDiv', 'ConfirmationDiv')).fade('in').get('tween').chain(function() {
				(function() { this.fade('out'); }).bind($(this.element.id)).delay(3000);
			});
		});
	},
	manageTextLabel: function(event) {
		var label = $(event.target).getProperty('label');

		switch (event.type) {
			case 'focus':
				if (this.get('value') == label) this.set('value', '');
				break;
			case 'blur':
				if (this.get('value') == '') this.set('value', label);
				break;
		}
	},
	onNavMenuClick: function(event) {
		if (event.target.id.indexOf('Img') > 0) {
			$(event.target.id.replace('Img', '') + 'ActiveDiv').fade('in');
		}
		else if (event.target.id.indexOf('Close') > 0) {
			$(event.target.id.replace('CloseButton', '') + 'ActiveDiv').fade('out');
		}
	},
	onNavOver: function(event) {
		if (event.type == 'mouseover') {
			if (event.target.src.indexOf('Over') > 0) {
				event.target.src = event.target.src.replace('Over.png', '.png');
				event.target.height = 30;
			}
			else {
				event.target.src = event.target.src.replace('.png', 'Over.png');
				if (event.target.id == 'ShareImg')	event.target.height = 65;
				else								event.target.height = 40;
			}
		}
		else if (event.type == 'mouseout') {
			event.target.src = event.target.src.replace('Over.png', '.png');
			event.target.height = 30;
		}
	}
};

window.addEvent('load', Control.load);

