document.observe("dom:loaded", function() {
	$$("div.expandable ul").each(function(e) {
		e.hide();
		var morelink = document.createElement("a");
		morelink.innerHTML = " more"; morelink.href="#";
		morelink.style.fontStyle="italic";
		$(morelink).observe('click',function(e) {
			this.ancestors()[0].next().toggle();
			Event.stop(e);
		});
		e.previous().appendChild(morelink);
	});
	
	/* How very odd this malarky is, the result of a confused development process */
	var url = (window.location.toString()).split("?");
	var formerror = false;
	if(url[1]) {
		qstring = url[1].toQueryParams();
		formerror = qstring["formerror"];
	}
	
	if(formerror!="subscribe") { $$("div#contact-signup form").each(function(e){e.hide();}); }
	if(formerror=="subscribe") {
		var error = document.createElement("p");
		error.className="error";
		error.innerHTML="Please enter your name and a valid email address";
		$$('#contact-signup form')[0].insert({before:error});
	} 
	if(formerror=="contact") {
		var error = document.createElement("p");
		error.className="error";
		error.innerHTML="Your request has not been sent, please complete all fields.";
		$$('#contactform')[0].insert({before:error});
	}
		
	$$("div#contact-signup h3 a").each(function(link) {
		link.observe('click',function(e) {
			this.ancestors()[1].select('form')[0].toggle();
			Event.stop(e);
		});
	});
	
	/* slideshow */
	var totalImages
	var manual = false;
	$$("div#slideshow").each(function(slideshow){
		
		var controlContainer = document.createElement("div");
		controlContainer.id="controls";
		
		var controlHeader = document.createElement("h2");
		controlHeader.className="accessibility";
		controlHeader.innerHTML = "Slideshow controls";
		
		var control = document.createElement("ul");
		control.id="slideshow-controls";
		
		var previous = document.createElement("li");
		previous.className="slideshow-previous";
		var previousAction = document.createElement("a");
		previousAction.href="#"; previousAction.innerHTML = "View previous image<span></span>"; previousAction.className="ir";
		previousAction.onclick=function(){ slide(-1); manual = true;  return false; }
		previous.appendChild(previousAction);
		
		var next = document.createElement("li");
		next.className="slideshow-next";
		var nextAction = document.createElement("a");
		nextAction.href="#"; nextAction.innerHTML = "View next image<span></span>"; nextAction.className="ir";
		nextAction.onclick=function(){ slide(1); manual = true; ; return false; }
		next.appendChild(nextAction);
		
		control.appendChild(previous); control.appendChild(next);
		controlContainer.appendChild(controlHeader);
		controlContainer.appendChild(control);
		
		$('content-main').insert({top:controlContainer});
		totalImages = $$("div#slideshow img").length-1;
		if($$("div#slideshow img").length >= 2) { setTimeout(loopSlide,4000); }
		
		/*Remove controls if there is only one image in the slideshow*/		
		if(totalImages == 0) {
			$("slideshow-controls").setStyle({
				display: "none"
			})
		}
			
	});
	
	function loopSlide() {
		if(!manual) {
			slide(1);
			setTimeout(loopSlide,3500);			
		}
	}

	function slide(count) {
		
		if($$("div#slideshow img").length >= 2) {
			$$("div#slideshow img").each(function(e){e.setStyle({zIndex: 80}); e.show(); });
			
			topImage = totalImages;
			$$("div#slideshow img")[topImage].setStyle({zIndex: 100});
			$$("div#slideshow img")[topImage].show();
			
			if((totalImages - count) > ($$("div#slideshow img").length-1)) {
				totalImages = 0;
			} else if((totalImages - count) < 0) {	
				totalImages = $$("div#slideshow img").length-1;
			} else {
				totalImages -= count;
			}
			
			$$("div#slideshow img")[totalImages].setStyle({zIndex: 90});
			$$("div#slideshow img")[totalImages].show();
	
			$$("div#slideshow img")[topImage].fade(); 
		}
	}
	
	/* Tracking */
	if (document.getElementsByTagName) {
		var hrefs = document.getElementById('content-main').getElementsByTagName("a");
		for (var l = 0; l < hrefs.length; l++) {
			try{ //protocol, host, hostname, port, pathname, search, hash
				if (hrefs[l].protocol == "mailto:") {
					startListening(hrefs[l],"click",trackMailto);
				} else if (hrefs[l].hostname == location.host) {
					var path = hrefs[l].pathname + hrefs[l].search;
					var isDoc = path.match(/\.(?:doc|eps|jpg|png|svg|xls|ppt|pdf|xls|zip|txt|vsd|vxd|js|css|rar|exe|wma|mov|avi|wmv|mp3)($|\&|\?)/);
					
					if (isDoc) { 
						hrefs[l].onclick=function() { window.open(this.href); return false; }
						startListening(hrefs[l],"click",trackDocumentLinks); }
				} else if (hrefs[l].hostname != location.host && hrefs[l].hostname != "#"  && hrefs[l].hostname.length > 0) { 
					hrefs[l].onclick=function() { window.open(this.href); return false; }
					startListening(hrefs[l],"click",trackExternalLinks);
				}
			}
			catch(e){
				continue;
			}
		}
	}

	function startListening (obj,evnt,func) {
	    if (obj.addEventListener) {
	            obj.addEventListener(evnt,func,false);
	    } else if (obj.attachEvent) {
	            obj.attachEvent("on" + evnt,func);
	    }
	}
	 
	function trackMailto (evnt) {
	    var href = (evnt.srcElement) ? evnt.srcElement.href : this.href;
	    var mailto = "/mailto/" + href.substring(7);
	    if (typeof(pageTracker) == "object") pageTracker._trackPageview(mailto);
	}
	 
	function trackDocumentLinks (evnt) {
	    var href = (evnt.srcElement) ? evnt.srcElement.href : this.href;
	    var mailto = "/downloads/" + href.substring(7);
	    if (typeof(pageTracker) == "object") pageTracker._trackPageview(mailto);
	}
	 
	function trackExternalLinks (evnt) {
	    var e = (evnt.srcElement) ? evnt.srcElement : this;
	    while (e.tagName != "A") { e = e.parentNode; }
	    var lnk = (e.pathname.charAt(0) == "/") ? e.pathname : "/" + e.pathname;
	    if (e.search && e.pathname.indexOf(e.search) == -1) lnk += e.search;
	    lnk = "/external/" + e.hostname + lnk;
	    if (typeof(pageTracker) == "object") pageTracker._trackPageview(lnk); 
	}
	
});

var supersleight = function() {
	
	var root = false;
	var applyPositioning = true;
	// Path to a transparent GIF image
	var shim = '/release/images/assets/x.gif';
	// RegExp to match above GIF image name
	var shim_pattern	= /x\.gif$/i;
	
	var fnLoadPngs = function() { 
		try{	
		if (root) {root = document.getElementById(root);
		}else{root = document;}
		for (var i = root.all.length - 1, obj = null; (obj = root.all[i]); i--) {
			// background pngs
			if (obj.currentStyle.backgroundImage.match(/\.png/i) !== null) { bg_fnFixPng(obj); }
			// image elements
			if (obj.tagName=='IMG' && obj.src.match(/\.png$/i) !== null){ el_fnFixPng(obj); }
		}
		} catch(e){}
	};

	var bg_fnFixPng = function(obj) {
		var mode = 'scale';
		var bg	= obj.currentStyle.backgroundImage;
		var src = bg.substring(5,bg.length-2);
		if (obj.currentStyle.backgroundRepeat == 'no-repeat') {
			mode = 'crop';
		}
		obj.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "', sizingMethod='" + mode + "')";
		obj.style.backgroundImage = 'url('+shim+')';
	};

	var el_fnFixPng = function(img) {
		var src = img.src;
		img.style.width = img.width + "px";
		img.style.height = img.height + "px";
		img.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "', sizingMethod='scale')";
		img.src = shim;
	};
	
	var addLoadEvent = function(func) {
		var oldonload = window.onload;
		if (typeof window.onload != 'function') {
			window.onload = func;
		} else {
			window.onload = function() {
				if (oldonload) {
					oldonload();
				}
				func();
			};
		}
	};
	
	return {
		init: function() { 
			if (navigator.platform == "Win32" && navigator.appName == "Microsoft Internet Explorer" && window.attachEvent) {
			addLoadEvent(fnLoadPngs);
		}
		},
		
		limitTo: function(el) {	root = el; },
		
		run: function() {
			if (navigator.platform == "Win32" && navigator.appName == "Microsoft Internet Explorer" && window.attachEvent) {
				fnLoadPngs();
			}
		}
	};
}();

supersleight.init();