(function() {

	/**
	* @author NPrice
	*/
	var NewMind = window.NewMind || {};
	NewMind.Site = window.NewMind.Site || {};

	/**
	* RolloverNav
	* @Requires JQuery.1.2.2
	*/
	NewMind.Site.RolloverNav = function(selector) {

		//add hover mouseover and mouseout events for the selected element
		//hover is used because it caters for bubbling problems 
		//that mouseover and mouseout dont do separately
		$(selector).hover(
			function() {

				var liObj = $(this);

				//add hover class to the li
				liObj.addClass('hover');

				//get the classes of the li
				//using attr because it returns a string of the value
				var classAttr = String(liObj.attr('class'));

				//assign just the first class found to the parent ul
				//this needs to be the pagename for the top level
				liObj.parent().addClass(classAttr.substring(0, classAttr.indexOf(' ')));
			},
			function() {
				//for mouseout just remove class from the li and its parent
				$(this).removeClass('hover').parent().removeClass();
			}
		)
	};

	$(document).ready(
		function() {

			// date replace functionality, here for simplicity
			var sSuffix1 = $("#header p.date").text();
			sSuffix1 = sSuffix1.replace(/th /i, " "); // eg, 19th
			sSuffix1 = sSuffix1.replace(/1st/i, "1");
			sSuffix1 = sSuffix1.replace(/2nd/i, "2");
			sSuffix1 = sSuffix1.replace(/3rd/i, "3");
			$('#header p.date').text(sSuffix1);

			var sSuffix2 = $("#mainCol .ctl_ArticleDetail p.date").text();
			sSuffix2 = sSuffix2.replace(/th /i, " "); // eg, 19th
			sSuffix2 = sSuffix2.replace(/1st/i, "1");
			sSuffix2 = sSuffix2.replace(/2nd/i, "2");
			sSuffix2 = sSuffix2.replace(/3rd/i, "3");
			$('#mainCol .ctl_ArticleDetail p.date').text(sSuffix2);

			//optional extra bit for IE - fixes background image load flickering when hovering anchors
			//this issue will not happen if a user goes to tools > internet options > settings... > selects 'automatically'
			if ($.browser.msie) {
				try {
					document.execCommand('BackgroundImageCache', false, true);
				} catch (e) {
					// just in case this fails dont do anything
				}
			}

			//selector adds rollover to main nav direct child li of a direct child ul
			NewMind.Site.RolloverNav('.MWEMainNavigation > ul > li');
		}
	);


})();