//$(document).ready(function() {
jQuery(document).ready(function($) { // noconflict


		// COOKIE BUSINESS -- Read back cookis and restore various saved states...
	var cookiesAccepted = true; // TODO: gather user acceptance of cookies (upcoming legislation).
	if (cookiesAccepted) {
		// Init all cookie states -----------------------------------------------------------
		// Read state of footer #sitemap
		var sitemapOpen = $.cookie('sitemapOpen');
		if (sitemapOpen == 'true') {
			$("#sitemap").show(); // no anim.
		}
		// Read text set size
		var savedTextSize = $.cookie('textSize');
		if (savedTextSize) {
			setTextSize(savedTextSize);
		}
		//
		
		// ----------------------------------------------------------------------------------
	} // eo cookie business.
	
	// TEXT RESIZER
	 $("#textresizer li a").click(function() {		
		// size to set (as class) (APPLY TO BODY TAG)
		var setSize = String($(this).parent().attr("class"));
		setTextSize(setSize);			
		// save text size in cookie
		if (cookiesAccepted == true) {
			$.cookie('textSize', setSize, { expires: 7, path: '/' });			
		}
	 });
	 function setTextSize(theSize) {
		 // first, remove previous size class from body tag
		 if ($("body").hasClass("size1")) {	$("body").removeClass("size1");	}
		 if ($("body").hasClass("size2")) {	$("body").removeClass("size2");	}
		 if ($("body").hasClass("size3")) {	$("body").removeClass("size3");	}
		 // apply the relevant class to the body tag
		 $("body").addClass(theSize);
	 }
	 // -----

	// MAIN MENU
	//$('ul.sf-menu').superfish();
	$("ul.menu").supersubs({ 
		minWidth:    12,   // minimum width of sub-menus in em units 
		maxWidth:    14,   // maximum width of sub-menus in em units 
		extraWidth:  1     // extra width can ensure lines don't sometimes turn over 
						   // due to slight rounding differences and font-family 
	}).superfish( // call supersubs first, then superfish, so that subs are not display:none when measuring. Call before initialising containing tabs for same reason.		
		{
			animation:   {opacity:'show'}, // removed height:'show' as it fails in ie6
			 speed:       100,
			 disableHI:   true,					// set to true to disable hoverIntent detection
			 delay:         200	                // the delay in milliseconds that the mouse can remain outside a submenu without it closing 
		}			
	);
	// Remove left border from nav menu's first top level item (for browsers != css3)
	$("#menu-primary-navigation li:first a").css("border", 0);
	// -----
	
	// SITE MAP
	// Remove border-top on first <dt> elements (also in css3)
	$("#sitemap dl dt:first-child").css("border-top","0");
	
	
	// Vertical sub nav (accordian)
	$(".mainsubnav li:last-child").css("border-bottom","0");
	var expandIcon = "<span class='expand'>+</span>";
	var collapseIcon = "<span class='collapse'>-</span>";
	$(".mainsubnav > li").each(function(index) {
		if($(this).has("ul").length) {
			$(this).prepend("<a href='#' class='toggleicon'>"+expandIcon+"</a>");
		}
  	});	
	$(".mainsubnav > li > a.toggleicon").click(function(event){
		//if ($(this).parent().hasClass("current") == false){
		if (!$(this).parent().find("ul.sub-menu").is(":visible")){ // only expand if the sub menu is currently hidden.
			// expand me
			$(this).parent().parent().find("li").removeClass("current");
			$(this).parent().addClass("current");
			$(this).parent().parent().find("li .toggleicon").html(expandIcon);
			$(this).html(collapseIcon);
			$(this).parent().parent().find("li ul").slideUp();
			$(this).siblings("ul").slideToggle();
			
		} else {
			//collapse me
			$(this).parent().removeClass("current");
			$(this).html(expandIcon);
			$(this).siblings("ul").slideUp();
			$(".mainsubnav > li.current_page_item").addClass("current"); // put call back on current LI.
		}
		return false;
	});
	// auto open
	var $currentSubPage = $(".mainsubnav li.current_page_item");
	if ($currentSubPage.parent().hasClass("sub-menu")) {
		//window.log("is sub");
		//window.log($currentSubPage.find("a").text());
		$currentSubPage.parent().show();
		$currentSubPage.parent().parent().addClass("current");
		$currentSubPage.parent().parent().find("a.toggleicon").html(collapseIcon);
	} else {
		//window.log("not sub");
		$currentSubPage.addClass("current");
	}

//	$('#cycle').cycle({
//		fx: 'fade'
//	});

//	$('figure.gallery-item a').attr('rel','gallery');
//	$('figure.gallery-item a[rel="gallery"]').fancybox();

});
