var td;
(function($) {
	// Vertically centre an object within its parent
	// Adapted from http://www.seodenver.com/simple-vertical-align-plugin-for-jquery/
	$.fn.vCenter = function(options) {
		options = $.extend({property: 'margin-top'}, options);
		return this.each(function(){
			var t = $(this);
			var th = t.height();
			var p = t.parent().is("body") ? $(window) : t.parent();
			var ph = p.height();
			var tn = parseInt( ( (ph - th) / 2 ) + 0.5 );
			var tn = tn > 0 ? tn : 0;
			t.css(options.property, tn);
		});
	};
	
	td = {
		init : function() {
			$("#header ul > li")
			// Main menu hover functionality
			.hover(
				function() {
					$(this).find("img:not(.nofade)")
						.stop().fadeTo(100, 0.5)
					.end().children("ul")
						.stop().show().fadeTo(0, 0).fadeTo(100, 0.9);
				},
				function() {
					$(this).find("img:not(.nofade)")
						.stop().fadeTo(250, 1)
					.end().children("ul")
						.stop().animate({
							opacity: 'hide'
						}, 100);
				}
			);
			
			// Remove PNG screen for IE 7 and below
			if ( $.browser.msie && $.browser.version <= 7 ) {
				$(".screen").removeClass('screen');
			}
			
			// Vertically centre page, and keep it centred
			td.vCenterPage();
			$(window).resize(td.vCenterPage);
			
			// Initialize menus after page is fully loaded, due to WebKit
			// quirks in measuring object dimensions at $(document).ready()
			$(window).load(td.onLoad);
		},
		onLoad : function() {
			$("#header ul ul")
			// Set widths of lists, list items and links
			.each(function() {
				var maxWidth = 0;
				var maxOuterWidth = 0;
				$(this).find("a").each(function() {
					maxWidth = $(this).width() > maxWidth ? $(this).width() : maxWidth;
					maxOuterWidth = $(this).outerWidth() > maxOuterWidth ? $(this).outerWidth() : maxOuterWidth;
				}).css({width: maxWidth + 1, display: 'block'});
				$(this).children("li").width(maxOuterWidth + 1);
				return this;
			})
			.children("li")
				// Make list items clickable
				.click(function() {
					window.location = $(this).find("a").attr("href");
					return false;
				})
				// Vertically center links in list items
				.children("a")
					.wrapInner("<span />")
					.children("span")
						.css('display', 'block')
						.vCenter({property: 'padding-top'})
					.end()
					.css({backgroundColor: '#F2F2F3'})
				.end()
				// Submenu hover functionality
				.hover(function() {
					$(this).children("a")
						.fadeTo(1, 1).stop().animate({backgroundColor: '#D9D9DB'}, 100);
				}, function() {
					$(this).children("a")
						.stop().animate({backgroundColor: '#F2F2F3'}, 250);
				})
			.end()
			// Set menu position relative to parent
			.offset(function() {
				var t = $(this);
				var tw = t.outerWidth();
				var p = t.closest("li");
				var po = p.offset();
				var pw = p.width();
				var o = {
					left: po.left + parseInt( (pw - tw) / 2 ),
					top: po.top + 57
				}
				return o;
			})
			// Finally, hide the submenus
			.hide();
		},
		vCenterPage : function() {
			$("#page").vCenter();
		}
	}
	$(document).ready(td.init);
})(jQuery);
