

	jQuery( function($) 

		{
		
			// =========================================================
			// set up H1 element in the #header
			// =========================================================
		
			// first make sure we have some h1 text
			var h1 = $("#header #h1 h1");
			if (h1.text() == "") {
				var text = $("#content h1:first").text();
				h1.text(text);
			}
						
			if (h1.text() != "") {
				
				// try to use more useful text if it's too long...
				if (h1.outerHeight() > 40) { // we wrapped onto 2 lines...
					h1.text(
							h1.text().replace(/.*> ?/, "")
					);
				}
				
				$("#h1").prepend('<div id="h1bg"></div>');	
				$("#h1bg").css({"background-color":"#FFFFFF", "opacity":"0.6"}) ;
			}
			
			
			
			// =========================================================
			// set up the crumb trail
			// =========================================================
			
			// hack the css a bit for IE
			if ($.browser.msie && $.browser.version >=7) {
				$('#header #searchFormContainer').css("padding-bottom", "1px");
			}
			
			$('#crumbTrail a:not(:first)').before(" &gt; ");
			var lastCrumb = $('#crumbTrail a:last');
			lastCrumb.replaceWith(lastCrumb.text());
	
	
			$("#crumbTrailBG").css({"background-color":"#FFFFFF", "opacity": "0.6"}) ;
			

			$.cookie("CID", currentSelectedCategory);
			
		
			// =========================================================
			// deal with the category menu and select the right thing
			// =========================================================
			var currentLink = $("#menu").find('#cat' + currentSelectedCategory).addClass('current');
			
			currentLink.closest(".content").css("display", "block");
			var currentTitle = currentLink.closest(".content").prev();
			
			
			if (!currentTitle.html()) {
				currentTitle = $(".parent" + currentSelectedCategory);
				currentTitle.next(".content").css("display", "block");
			}
			
			
			
			$("#menu").accordion(
				{
					header: '.title',
					selectedClass: "selectedTitle",
					autoHeight: false, 
					collapsible: true,
					active: currentTitle || false,
					navigation: false,
					animated: ($.browser.msie ? false : "swing"),
					icons: { 'header': 'ui-icon-triangle-1-e', 'headerSelected': 'ui-icon-triangle-1-s' }
				}
				
					//animated: "myStuff",	// <- our custom defined animation
			);
			
			
			// =========================================================
			// Are we logged in? - set the sign out button
			// =========================================================
		
			var uid = $.cookie("mglUID");
		
			if (uid) {
				var bits = uid.split(",");
				
				if (bits[1] > 0) {
					//$("#signinButton").css("background-image", "url(/images/buttons/signout.png)");
					$("#signinButton").text("Sign out");
					$("#signinButton").attr("href", "/cgi-bin/login.pl?com=signout");
				}
			}
			
			
			// =========================================================
			// Set up the basket summary function
			// =========================================================
			
			if ($("#basketSummary").length > 0) {
				$("#basketSummary").basketSummary();
				$("#basketSummary").bind("click", function() {window.location='/cgi-bin/basket.pl'});
				//$("#basketSummary").corner("10px");
			}
			
			// =========================================================
			// make the tool tips work
			// =========================================================
			
			$('.tooltipActiveNoDelay').tooltip({
				track: true,
				delay: 0,
				showURL: false,
				showBody: " - "
			});
			
			$('.tooltipActiveShortDelay').tooltip({
				track: true,
				delay: 250,
				showURL: false,
				showBody: " - "
			});
			
			
			// =========================================================
			// make the search form suggesting work
			// =========================================================
			if ($("#searchForm").length > 0) {
				$("#searchForm #q").suggest("/cgi-bin/suggest.pl",
						{	
							delay: 1,
							minchars: 1,
							onSelect: function() {$("#searchForm").submit() }
						}
					
				);
			}
			
			
			// =========================================================
			// create a session ID if we don't already have one...
			// =========================================================
			
			var sessionId = $.cookie("mglSID") || "";
			
			
			if ((!sessionId) || (sessionId== "") || (sessionId<=0)) {
				var refUrl = escape(document.referrer);

//			$.getJSON(this.options.url + "?d=" + (new Date()).getTime() + Math.random(), this.recieveJSON);
			
			$.getJSON(
					"/cgi-bin/session.pl?ref=" + refUrl + "&refId=" + $.urlParam("r") + "&url=" + escape(window.location) + "&d=" + Math.random(),
					function(data) {
					
						$.cookie(data.cookieName, data.sessionId, { path: '/', domain: data.cookieDomain});
//						console.log(data.sessionId);
				
					}
					
				);
			}
			
			
			// =========================================================
			// help icons popup...
			// =========================================================
			
			if ($('a.helpIcon').length > 0) {
				$("body").prepend("<div id='helpPopup' title='Help & information'></div>");
				
				$('#helpPopup').dialog({
					autoOpen: false,
					modal: true,
					resizable: false
				});
				
				$('.helpIcon').click(function() {
					$('#helpPopup').dialog(
										"option", "title", $(this).attr("title")
									).html(
										$(this).attr("rel")
									).dialog('open');
					return false;
				});
			}
			
			
		}
	);
	
	
function cookieCheck () {
	// check to see if cookies are enabled, in this browser...
	var cookieName = "MGLCookieCheck";
	var cookieValue = Math.random();
	
	// write a cookie
	document.cookie = escape(cookieName) + "=" + escape(cookieValue) + "; path=/";
	
	// read the cookie
	var readCookie = $.cookie(cookieName);
	
	if (readCookie && (readCookie == cookieValue)) {
		// cookies enabled
		// reset the cookie value
		document.cookie = escape(cookieName) + "=" + escape("") + "; path=/";
	} else {
		
		$('#content').append("<div id='JSCookieError' class='ui-state-error'><p class='errorHeading'>Problem detected!  (cookies disabled)</p><p>Your web browser doesn't currently seem to have cookies enabled. This means that there are elements of our website, such as the shopping functions, that will not function as intended, if at all.</p><p>Please enable cookies and refresh this page or <a href='/contact'>contact us</a> to discuss product details or to place an order by telephone.</div>");
		
		//alert("no cookies");	
	}
	
	
}



