(function(){
	var menu = function() {
		var _container, 
			_lastover, 
			_firstHover = false,
			_menus = {};
		var mousemove =  function(e) {
			if ((menuItem = $.getParent(e.target, _container[0], 'LI')) !== false) {
				if (_lastover != menuItem) {
					setLastOver(menuItem);
				}
			} else {
				setLastOver(null);
			}
		};
		var mouseout = function(e) {
			if (_lastover && !$.isChildOf(_lastover, e.relatedTarget, _container[0])) {
				setLastOver(null);
			}
			if (!$.isChildOf(_container[0], e.relatedTarget, _container[0])) {
			}
		};
		var setLastOver = function(value) {
			if (_lastover) {
				$('ul', _lastover).hide();
				$('>a', _lastover).removeClass('active');
			}
			_lastover = value;
			if (_lastover) {
				$('ul', _lastover).show();
				$('>a', _lastover).addClass('active');
			}
		};
		return {
			build: function() {
				_container = $('ul.mainMenu')
					.bind('mousemove', mousemove)
					.bind('mouseout', mouseout);
			}
		}
	}();
	$(document).ready(function(){
		menu.build();
		if (typeof deleteCommentConfirm != 'undefined') {
			$('a.commentRemove').bind('click', removeComment);
		}
		if (typeof deleteEventConfirm != 'undefined') {
			$('a.eventRemove').bind('click', eventRemove);
		}
		$('dt a.newsletterItem').bind('click', showNewsletter);
		$('#overlay').bind('click', hideOverlay).css('opacity', 0.5);
		$('#closeOverlay').bind('click', hideOverlay);
		
		$('#thankyouTrigger').bind('click', showThankyou);
		$('#thankyou')
			.find('a')
				.bind('click', function(){
					this.blur();
					$('#thankyouType').val($(this).attr('rel'));
					$(this).parent().parent().submit();
					return false;
				});
	});
	
	var showThankyou = function(e) {
		this.blur();
		showOverlay();
		$('#thankyou').fadeIn(700);
		return false;
	}; 
	
	var showNewsletter = function(e) {
		showOverlay();
		$('#overlayContent').slideDown(700);
		$('#loadingIndicator').show();
		//$(document.body).css('overflow', 'hidden');
			$.ajax({
				data: $.param({
					request: 'loadNewsletter',
					newsletter: parseInt($(this).attr('rel'))
				}),
				url: '/ajaxserver.php',
				dataType: 'xml',
				type: 'post',
				success: function(XMLResponse) {
					var oErrors = $('error', XMLResponse);
					if (oErrors.size() > 0) {
						oErrors.each(function(){
							alert($(this).text());
						});
					} else {
						if ($('#overlayContent').is(':visible')) {
							$('#ajaxContent').html($('content', XMLResponse).text());
							$('#loadingIndicator').hide();
						}
					}
				},
				error: function() {
					hideOverlay();
				}
			});
		this.blur();
	};
	var hideOverlay = function(e) {
		$('#overlay').fadeOut(700);
		$('#loadingIndicator').show();
		$(document.body).css('overflow', 'auto');
		$('#overlayContent').slideUp(700);
		$('#thankyou:visible').hide();
		$('#ajaxContent').empty();
		return false;
	};
	var showOverlay = function() {
		var clt = $.getScroll();
		$('#overlay')
			.css({
				width: Math.max(clt.iw, clt.w) + 'px',
				height: Math.max(clt.ih, clt.h) + 'px',
				display: 'block'
			});
	};
	var removeComment = function(e) {
		this.blur();
		if (confirm(deleteCommentConfirm)) {
			commentId = $(this).attr('rel');
			$.ajax({
				data: $.param({
					request: 'deleteComment',
					comment: commentId
				}),
				url: '/ajaxserver.php',
				dataType: 'xml',
				type: 'post'
			});
			$(this)
				.parent()
				.parent()
				.find('dt[@rel="' +commentId  + '"], dd[@rel="' +commentId  + '"]')
				.fadeOut(600)
		}
		return false;
	}
	var eventRemove = function(e) {
		this.blur();
		if (confirm(deleteEventConfirm)) {
			eventId = $(this).attr('rel');
			$.ajax({
				data: $.param({
					request: 'deleteEvent',
					event: eventId,
					user: userId
				}),
				url: '/ajaxserver.php',
				dataType: 'xml',
				type: 'post'
			});
			$(this)
				.parent()
				.fadeOut(600)
		}
		return false;
	}
})();