﻿/// <reference path="TheFarm.js" />
/// <reference path="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.4.1-vsdoc.js" />
/// <reference path="cufon-yui.js" />

TheFarm.registerNamespace('Sunbeam.Global');

Sunbeam.Global = (function () {
	var showLogin;

	var doCufon = function () {
		///<summary>
		///     Replaces all the Cufon elements.  The selectors are put in an array and joined later
		///     for readability.  Don't forget to leave off the comma for the last array item!
		///</summary>

		/*
		var hoverables = [
		];

		Cufon.replace(hoverables.join(','), {
		hover: true
		});
		*/

		var plain = [
			"#navigation > ul > li > span",
			"#navigation > ul > li > a > span",
			"#footer > ul > li"
		];

		Cufon.replace(plain.join(','));
	};

	var fixAspNetDefaultButton = function (buttonElement) {
		/// <summary>
		///     Make enter button work in standards compliant browsers (.net fix)
		///     Reference: http://kpumuk.info/asp-net/using-panel-defaultbutton-property-with-linkbutton-control-in-asp-net/
		/// </summary>
		///<param name="buttonElement" type="DOM Element">The DOM element which corresponds to the button of the form</param>
		if (buttonElement && typeof (buttonElement.click) == 'undefined') {
			buttonElement.click = function () {
				var result = true;
				if (buttonElement.onclick) {
					result = buttonElement.onclick();
				}
				if (typeof (result) == 'undefined' || result) {
					eval(buttonElement.getAttribute('href'));
				}
			};
		}
	};

	// Top nav scripts
	var setupNavigation = function () {
		// This bit handles the delayed opening/closing of the menu items
		var queuedItem = null;
		var $primaryItems = $("#navigation > ul > li");

		// A bunch of methods to control the menu's state
		var hoverItem = function ($li) {
			if (!$li.hasClass("hover")) {
				$li.addClass("hover");
				Cufon.replace($li.children("a,span")[0]);
			}
		};

		var unHoverItem = function ($li) {
			if ($li.hasClass("hover")) {
				$li.removeClass("hover");
				Cufon.replace($li.children("a,span")[0]);
			}
		};

		var openItem = function ($li) {
			if (!$li.hasClass("open")) {
				$primaryItems.each(function () {
					closeItem($(this));
				});
				hoverItem($li);
				$li.addClass("open");
			}
		};

		var closeItem = function ($li) {
			if ($li.hasClass("open")) {
				unHoverItem($li);
				$li.removeClass("open");
			}
		};

		$primaryItems.hover(function () {
			var $li = $(this);
			queuedItem = $li[0];
			hoverItem($li);
			setTimeout(function () {
				if (queuedItem === $li[0]) {
					openItem($li);
				}
			}, 100);
		}, function () {
			var $li = $(this);
			queuedItem = null;
			unHoverItem($li);
			setTimeout(function () {
				if (queuedItem !== $li[0]) {
					closeItem($li);
				}
			}, 100);
		});

		// Corner elements
		var $secondaryItems = $primaryItems.children(".panel");
		var $cornersTemplate = $('<div class="corners" />')
			.append('<div class="left" />')
			.append('<div class="right" />');

		$secondaryItems.append($cornersTemplate);

		// Login input mask
		$secondaryItems.find("input")
			.each(function () {
				if (!$(this).val()) {
					$(this).addClass("watermark");
				}

			})
			.focus(function () {
				$(this).removeClass("watermark");
			})
			.blur(function () {
				if (!$(this).val()) {
					$(this).addClass("watermark");
				}
			})
			.hover(function () {
				// The browsers like to think that the menu item has been
				// unhovered when the password textbox is hovered.
				// This tells the menu item to stay open
				$(this).parents("li").trigger("mouseover");
			});

		// Login panel dropdown
		var $loginPanel = $secondaryItems.find(".login");

		showLogin = function () {
			window.scrollTo(0, 0);
			openItem($loginPanel.parents("li"));
			$loginPanel.find("input.username").focus().select();
		};

		fixAspNetDefaultButton($loginPanel.children(".go")[0]);

		// Notify user upon error by opening the panel
		if ($loginPanel.children(".error").length) {
			showLogin();
		}
	};

	var SetUpLightBox = function () {
		jQuery('a.termsLightBox').fancybox({
			'type': 'iframe',
			'modal': false,
			'titleShow': false,
			'width': 650,
			'height': 391,
			'padding':0
		});
		jQuery('a.s2FLightBox').fancybox({
			'type': 'iframe',
			'modal': false,
			'titleShow': false,
			'width': 650,
			'height': 348,
			'padding':0
		});
		jQuery('a.recipieBox').fancybox({
			//'type': 'iframe',
			'modal': false,
			'titleShow': false,
			'width': 650,
			'height': 348,
			'padding': 0
		});
	};

	return {
		initialise: function () {
			// init stuff here that's valid for every page
			setupNavigation();
			SetUpLightBox();
		},
		doCufon: doCufon,
		showLogin: function () {
			showLogin();
		}
	};

})();
