var lurpak = {
	BASE_URL: null,
	PAGE: '',

	start: function () {
		this.PAGE = $(document.body).get('id');

		if ($defined(this[this.PAGE]))
			this[this.PAGE]();

		this.all();
	},

	homepage: function () {
		var featuredList = $("featuredList"), li;

		if (featuredList) {
			li = featuredList.getElements("li");

			if (li) {
				li.each(function(item){
					var featured = item.getElement("div.featuredItem");
					var imgSel = item.getElement("img");
					var imgFx = new Fx.Morph(imgSel, {duration:500});

					featured.setStyle("display", "none");

					item.addEvent("mouseenter", function(e){
						featured.setStyle("display", "block");
						imgFx.start({opacity:0.5});
					});

					item.addEvent("mouseleave", function(e){
						imgFx.cancel();
						featured.setStyle("display", "none");
						imgFx.start({opacity:1});
					});
				});
			}
		}

		// Used to close the overlay and reset cookie
		var opacity = 0.85,
		md5 = null,
		
		closeOverlay = function () {
			overlay.morph({opacity: 0});
			box.morph({opacity: 0});
			Cookie.write('hash', md5);
		},

		showOverlay = function () {
			overlay.morph({opacity: opacity});
			box.morph({opacity: 1});
		},

		// Setup our request instance for fetching the HTML for the overlay
		reqHtml = new Request.HTML({
			url: BASE_URL+'overlay',
			onComplete: function (tree, elems, html, js) {
				// Create our box for injecting content
				box = new Element('div', {'class': 'box', styles: {opacity: 0}});

				// Inject our elements
				overlay.inject($(document.body), 'top');
				box.inject($(document.body), 'top');

				box.set('html', html); // Set box html

				box.getElements('a').addEvent('click', function () {
					Cookie.write('hash', md5);
				});
				
				//showOverlay();
			}
		}),

		reqJson = new Request.JSON({
			url: BASE_URL+'overlay/status',
			onComplete: function (json) {
				if (json.status === 1) {
					md5 = json.md5;
					
					if (Cookie.read('hash') !== md5) {
						reqHtml.send();
					}
				}
			}
		}),

		// Create overlay element
		overlay = new Element('div', {
			'class': 'overlay',
			styles: {
				opacity: 0,
				width: '100%',
				height: $(window).getScrollSize().y
			},
			events: {click: closeOverlay}
		});

		// Get current status
		reqJson.get();


		$(window).addEvents({
			keydown: function (e) {
				if (e.key === 'esc') {
					closeOverlay();
				}
			},
			load: function () {
				overlay.setStyle('height', $(window).getScrollSize().y);
			}
		});
	},

	all: function () {
		this.forms();
		this.archiveList();
		this.recipesList();
	},
	
	archiveList: function () {
		var listArch = $('listArchives');
		if (listArch) {
			var as = listArch.getElements('a');

			as.each(function (a) {
				var ul = a.getNext('ul');

				if ($defined(ul))
					ul.addClass('hide');

				a.addEvent('click', function (e) {
					if ($defined(ul)) {
						e.stop();
						ul.toggleClass('hide');
			    }
				});
			});
		}
	},
	
	recipesList: function () {
		var listRecip = $('listRecipes');
		if (listRecip) {
			var as = listRecip.getElements('a');

			as.each(function (a) {
				var ul = a.getNext('ul');

				if ($defined(ul))
					ul.addClass('hide');

				a.addEvent('click', function (e) {
						if ($defined(ul)) {
							e.stop();
							ul.toggleClass('hide');
				    }
				});
			});
		}
	},

	forms: function () {
		// Get forms
		var forms = $$('form:not(.nofill)');
		forms.each(function (form) {
			form.addEvent('submit', function () {
				form.getElements('input[type=text]').each(function (input) {
					var label = form.getElement('label[for='+input.get('id')+']');
					var labelText = label.get('text');
					if (input.get('value') === labelText)
						input.set('value', '');
				});
			});
			// Get all text inputs
			form.getElements('input[type=text]').each(function (input) {
				var label = form.getElement('label[for='+input.get('id')+']');
				if (!$defined(label))
					return;
				var labelText = label.get('text');

				var setInputValue = function () {
					if (input.get('value').length === 0) {
						
							input.set('value', labelText);
					
					}
				};

				setInputValue();

				input.addEvents({
					focus: function () {
						if (input.get('value') === labelText)
							input.set('value', '');
					},
					blur: setInputValue
				})
			});
		});
	}
};
window.addEvent('domready', lurpak.start.bind(lurpak));