var HD = HD || new (function() {
	
	// private members
	
	var DEBUG = !!(location.hash == '#debug');
	
	// privileged methods
	
	return {
		createButton: function(text) {
			if(text.constructor !== String && DEBUG) {
				throw("Invalid argument supplied to createButton method")
				return;
			}
			var spanClasses = ["bl","br","tl","tr","arrow"];
			var spans = [];
			for (var i = 0, l = spanClasses.length; i<l; i++) {
				spans[i] = document.createElement("span");
				spans[i].className = spanClasses[i];
				if (i>0) {
					spans[i-1].appendChild(spans[i]);
				}
			}
			spans[spans.length-1].appendChild( document.createTextNode(text) );
			return spans[0];
		}
	};	
});

HD.convertButtons = new (function() {
	return {
		init: function() {
			$('.makeButton').each(function() {
				var txt = this.innerHTML;
				this.innerHTML = "";
				this.appendChild(HD.createButton(txt));
				$(this).addClass("jsButton");
			});
		}
	};
});

HD.setH2 = new (function() {
	return {
		init: function() {
			var h2 = $('#mainContent h2')[0] || null;
			if(h2 === null) {
				$('body').addClass('noH2');
			}
		}
	};
});

HD.enhanceJobList = new (function() {
	return {
		init: function() {
			$('#jobList li:first').css('border', 'none');
		}
	};
});

HD.trackUser = new (function() {
	return {
		init: function() {
			if($.browser.safari) {
				// Filter out Safari 1 & test whether cookie already exists
				if(window.XMLHttpRequest && $.cookie('HD') === null) {
					$.cookie('HD', '', { path: '/' })
				}
			}
		}
	};
});

HD.setNav = new (function() {					  
	return {
		init: function() {
			var home = window.location.pathname == '/', address = window.location.toString();
			jQuery.each($('#navigation a'), function(i) { 	
           		 if( (i === 0 && home) || (i>0 && address.indexOf(this.href.replace('-intro', '')) == 0 && !home) ) { $(this).addClass('current'); }
  			});
		}
	};
});

HD.highlight = new (function() {					  
	return {
		init: function() {
			var home = window.location.pathname == '/',
				address = ( /approvals\/case-study/.test(window.location) ) ? "/planning/approvals/index.html" : window.location.toString()
				
			jQuery.each($('#navigation a'), function(i) {
           		 if( (i === 0 && home) || (i>0 && address.indexOf( this.href ) == 0 && !home) ) { $(this).addClass('current'); }
  			});
		}
	};
});

HD.externalLinks = new (function() {
	return {
		init: function() {
			$("#mainContent a").each(function() {
				if (this.getAttribute("href") && ( this.getAttribute("rel") === "external" || /.pdf/.test(this.href))) {	
					this.target = "_blank";
					if (typeof this.title != "undefined") { this.title = "Opens in a new window"; }
					$(this).addClass('external');
				}
			});
		}
	};
});

// execute on domReady

if(jQuery) {
	$(function() {
		if($.cookie('HD')) { document.getElementById('mainContent').style.left = "-2000em" };
		// Nudge needed for IE6
	    if(jQuery.browser.msie) {
			$('.slot2, .slot1, body').each(function redrawNodes() {
				var cacheDisplay = this.style.display;
				this.style.display = "block";
				this.style.display = cacheDisplay;
			});
		}
		for(var member in HD) {
			if(HD[member]["init"]) {
				HD[member]["init"]();
			}
		}
		if($('#flash').length > 0) {
			swfobject.embedSWF(SSP.file, "ssp", ($('#test').length !== 0) ? "1500" : "960", ($('#test').length !== 0) ? "760" : "505", "9", "/assets/swf/expressInstall.swf", {xmlfile: SSP.images}, {allowFullScreen: true, wmode: "transparent"});
		}
	});
}

// Preload high-res hero images before showing page

$(window).bind('load', function() {
		// Check for cookie (if true, UA == Safari), test if already visited page (delay appearance if not)
		if($.cookie('HD') !== null && $.cookie('HD').indexOf(window.location.pathname) == -1) {
			 $.cookie('HD', ($.cookie('HD')+window.location.pathname+'|'), { path: '/' });
			 setTimeout(view, 1500);
		} else {
			view();	
		}
		
		function view() {
			document.getElementById('mainContent').style.left = "0";
		}
});