var Site = {
	start: function() {
		if ($('mm')) { // on home page
			Site.start_age_form();
			$('entersite').addEvent('click', function(event) {
				event.stop();
				if (Site.is_of_age()) {
					$('age_check_form').submit();
				}
			});

		}
	},
	start_age_form: function() {
		var form_fields = ['mm', 'dd', 'yyyy'];
		var defaults = ['MM', 'DD', 'YEAR'];
		form_fields.each(function(v, k) {
			$(v).set('autocomplete', 'off');
			$(v).set('value', defaults[k]);
			$(v).addEvent('focus', function() {
				if (this.get('value') == defaults[k]) this.set('value', '');
			});
			$(v).addEvent('blur', function() {
				if (this.get('value') == '') this.set('value', defaults[k]);
			});
			$(v).addEvent('keyup', function(event) {
				Site.validate_age_form();
				if (this.get('value').length == v.length && k < form_fields.length - 1) {
					$(form_fields[k + 1]).focus();
				}
			});
			$(v).addEvent('keydown', function(event) {
				if (event.key == 'enter') {
					if (Site.is_of_age()) {
						$('age_check_form').submit();
					}
				}
			});

		});
	},
	validate_age_form: function() {
		if (Site.is_of_age()) { // old enough
			$('entersite').removeClass('not_of_age');
		}
		else {
			$('entersite').addClass('not_of_age');
		}
	},
	is_of_age: function() {
		var year = $('yyyy').get('value').length == 4 ? $('yyyy').get('value').toInt() : new Date().getFullYear(), month = $('mm').get('value').toInt(), day = $('dd').get('value').toInt();
		var now = new Date(), y = now.getFullYear(), m = now.getMonth() + 1, d = now.getDate();
		var age = y - year + (month > m ? -1 : month == m && day > d ? -1 : 0);

		return age >= 21 ? true : false;		
	},
	
	start_flavors: function() {
		var bottles = ['tanzarine', 'pink', 'haze'];
		var bottle_selected = false;
		bottles.each(function(v) {

			$('bottle_' + v).addEvent('mouseenter', function() {
				bottles.each(function(v) {
					$('bottle_link_' + v).removeClass('on');
				});
				$('bottle_link_' + v).addClass('on');
			});

			$('bottle_' + v).addEvent('click', function(event) {
				event.stop();
				bottle_selected = v;
				bottles.each(function(v) {
					$('flavors_' + v).setStyle('display', 'none');
					$('flavors_' + v).fade('hide');
				});
				$('flavors_default').setStyle('display', 'none');
				$('flavors_default').fade('hide');
				$('flavors_' + v).setStyle('display', '');
				$('flavors_' + v).fade('in');
			});

		});

		$('bottles_container').addEvent('mouseleave', function() {
			if (bottle_selected) {
				bottles.each(function(v) {
					$('bottle_link_' + v).removeClass('on');
					if (v == bottle_selected) {
						$('bottle_link_' + v).addClass('on');
					}
				});
			}
			else {
				bottles.each(function(v) {
					if (!$('bottle_link_' + v).hasClass('on')) $('bottle_link_' + v).addClass('on');
				});
			}
		});
	}
};

window.addEvent('domready', function() {
	Site.start();
});