var mainNavigation = {
	'animateNavLine': function(leftPosition) {
		/* Setting a local reference to the object. */
		var instance = this;

		$('#navLine').stop().animate({
			left: leftPosition + 'px'
		}, 400);
	},
	'placeNavLine': function(leftPosition) {
		/* Setting a local reference to the object. */
		var instance = this;

		/* Positions and shows the navigation line. */
		$('#navLine').css({
			display: 'block',
			left: leftPosition
		});
	},
	'init': function() {
		/* Setting a local reference to the object. */
		var instance = this;

		/* If there is an active top-level menu link, position the line accordingly. */
		var topLevelActive = $('#navigation > ul > li > a.active');
		if (topLevelActive.length > 0) {
			var leftPosition = topLevelActive.parent().position().left;
			mainNavigation.placeNavLine(leftPosition);
		}

		/**
		 *	Upon leaving the entire navigation list, the navLine will
		 *	return to underneath the active link, if there was one.
		 */
		$('> ul', '#navigation').bind('mouseleave', function() {
			var topLevelActive = $('#navigation > ul > li > a.active');
			if (topLevelActive.length > 0) {
				var leftPosition = topLevelActive.parent().position().left;
				mainNavigation.animateNavLine(leftPosition);
			}
		});

		/**
		 *	Selects the direct unordered list and direct list items
		 *	of that list, and fades in their hidden drop-down.
		 */
		$('> ul > li', '#navigation').hover(
			function() {
				var newLeft = $(this).position().left;
				if (!$('#navLine').is(':visible')) {
					mainNavigation.placeNavLine(newLeft);
				} else {
					mainNavigation.animateNavLine(newLeft);
				}

				var subMenu = $(this).children('ul');
				if (subMenu.length > 0) {
					if (!subMenu.is(':visible')) {
						subMenu.fadeIn(300);
					} else {
						subMenu.fadeOut(300);
					}
				}
			},
			function() {
				var subMenu = $(this).children('ul');
				if (subMenu.length > 0) {
					if (subMenu.is(':visible')) {
						subMenu.fadeOut(300);
					}
				}
			}
		);

		/**
		 *	Fades the background colours of any sub-menu link
		 *	that isn't already active, as you hover over it.
		 */
		$('ul li ul li a[class!=" active"]', '#navigation').hover(
			function() {
				$(this).stop().animate({
					'backgroundColor': '#237e99'
				}, 400);
			},
			function() {
				$(this).stop().animate({
					'backgroundColor': '#35a59c'
				}, 400);
			}
		);
	}
}

/**
 *	Basic e-mail validation.
 */
function checkMail(email) {
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (filter.test(email)) {
		return true;
	} else {
		return false;
	}
}

$(document).ready(function() {

	/**
	 *	Initialising hover and click event handlers for
	 *	the main navigation and also logic for navLine.
	 */
	mainNavigation.init();

	/**
	 *	Clicking the logo will act as a link.
	 */
	$('#logo').bind('click', function() {
		window.location = $(this).children('a').attr('href');
	});

	/**
	 *	Makes any links marked as external open in a new window.
	 */
	$('a[rel*=external]').live('click', function(e) {
		window.open(this.href);
		e.preventDefault();
	});

	/**
	 *	Clicking the top-level link will slide up or down the nested
	 *	list and handle active classes appropriately.
	 */
	$('> ul > li > a', '#userLinks').bind('click', function(e) {
		if (!$(this).hasClass('active')) {
			$(this).addClass('active');
		}

		var nestedList = $(this).siblings('ul');
	
		if (!nestedList.is(':visible')) {
			nestedList.slideDown(400);
		} else {
			nestedList.slideUp(400, function() {
				$(this).siblings('a').removeClass('active');
			});
		}

		e.preventDefault();
	});

	/**
	 *	Whenever hovering off the list, if the nested list was still
	 *	open then it is slid up and the active class is removed.
	 */
	$('> ul > li', '#userLinks').bind('mouseleave', function() {
		var nestedList = $(this).children('ul');

		if (nestedList.is(':visible')) {
			nestedList.slideUp(400, function() {
				$(this).siblings('a').removeClass('active');
			});
		}
	});

	/**
	 *	Clicking the currently active breadcrumb will do nothing.
	 */
	$('#breadcrumbs a.active').bind('click', function(e) {
		e.preventDefault();
	});

	/**
	 *	Hacks the last feature box to be one pixel longer than the others.
	 */
	var featureBoxes = $('div.pod', '#quickLinks');
	if (featureBoxes.length > 3) {
		featureBoxes.filter('.last').css('width', '246px').children().css('width', '246px');
	}

	/**
	 *	Hovering over a pod will make its image expand or contract.
	 */
	$('div.pod', '#quickLinks').hover(
		function() {
			var thisPod = $(this);

			thisPod.children('img').stop().animate({
				left: -10,
				height: 150,
				top: -10,
				width: 265
			}, 250);
		},
		function() {
			var thisPod = $(this);

			if (featureBoxes.length > 3 && thisPod.hasClass('last')) {
				var podWidth = 246;
			} else {
				var podWidth = 245;
			}

			thisPod.children('img').stop().animate({
				left: 0,
				height: 130,
				top: 0,
				width: podWidth
			}, 250);
		}
	);

	/**
	 *	Clicking the image inside a pod will force a click on the link inside.
	 */
	$('div.pod img', '#quickLinks').bind('click', function() {
		var link = $(this).siblings('h2').children('a');
		var url = link.attr('href');
		var external = link.attr('rel');

		if (external) {
			window.open(url);
		} else {
			window.location = url;
		}
	});

});

/**
 * Standard popup new window function
 */
function popup(url, name, width, height){
     //Pop up custom window
    newWindow = window.open(url, name, 'width='+width+',height='+height+', menubar=no, toolbar=no, scrollbars=yes, resizable=yes, location=no');
    newWindow.focus();
}

/**
 * Concatenates the values of a variable into an easily readable string
 * by Matt Hackett [scriptnode.com]
 * @param {Object} x The variable to debug
 * @param {Number} max The maximum number of recursions allowed (keep low, around 5 for HTML elements to prevent errors) [default: 10]
 * @param {String} sep The separator to use between [default: a single space ' ']
 * @param {Number} l The current level deep (amount of recursion). Do not use this parameter: it's for the function's own use
 */
function print_r(x, max, sep, l) {

	l = l || 0;
	max = max || 10;
	sep = sep || ' ';

	if (l > max) {
		return "[WARNING: Too much recursion]\n";
	}

	var
		i,
		r = '',
		t = typeof x,
		tab = '';

	if (x === null) {
		r += "(null)\n";
	} else if (t == 'object') {

		l++;

		for (i = 0; i < l; i++) {
			tab += sep;
		}

		if (x && x.length) {
			t = 'array';
		}

		r += '(' + t + ") :\n";

		for (i in x) {
			try {
				r += tab + '[' + i + '] : ' + print_r(x[i], max, sep, (l + 1));
			} catch(e) {
				return "[ERROR: " + e + "]\n";
			}
		}

	} else {

		if (t == 'string') {
			if (x == '') {
				x = '(empty)';
			}
		}

		r += '(' + t + ') ' + x + "\n";

	}

	return r;

};

