

	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("sid") || "";
			
			
			if ((!sessionId) || (sessionId== "") || (sessionId<=0)) {
				var refUrl = escape(document.referrer);

				$.get(
					"/cgi-bin/session.pl?ref=" + refUrl + "&refId=" + $.urlParam("r") + "&url=" + escape(window.location) + "&d=" + Math.random(),
					function(sid) {
					
						$.cookie('sid', sid, { path: '/', domain: 'mygreenlighting.co.uk'});
				
					},
					
					"text"
				);
			}
			
		}
	);
	
