//smartsite site root: equivalent to ~/
var siteroot = (typeof(fullsiteroot) == "function") && (typeof(sitehost) == "function") ? fullsiteroot().replace(sitehost(), "") : "/";
//the current page, mostly for Google Analytics (fixes virtual directories)
var absolutePath = location.pathname.replace(siteroot, "/").replace(".ssx", "");
//get google-analytics-friendly path name from a fully qualified url
var extractPath = function(url) {return "/" + url.substring(url.indexOf(siteroot) + siteroot.length);};
//language
var lang = location.href.indexOf("/en/") > - 1 ? "en" : "fr";
//fixes double slash in URLs
var normalizePath = function(string) {return string.replace(/(\/+)|\\+/g, "/");}
//escapes <, > & and " into corresponding html entities
var escapeHTML = function(string) {return string.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;");}
//rounds a number to two decimals places. Returns a string
var toCurrency = function(number) {return number.toFixed(2);}
//changes a string to something that can used as a Google Analytics parameter
var toGatString = function(string) {
	var str = string.replace(/^[\w]+\:\/\//, "");
	var offset = str.indexOf("/");
	var path = offset > -1 ? str.substring(offset) : "";
	str = str.substring(0, offset);
	str = str.replace(/\s|\./g, "_").replace(/[^\w_]/gim, "") + path;
	return str;
}
//IE background image cache script
//@cc_on document.execCommand("BackgroundImageCache", false, true);

//google analytics: pass relative url + keyword
var customGaValue = null, lastGaValue;
var overridePageGa = false;
var googleanalytics = function(keyword) {
	var id = normalizePath(absolutePath + "/" + keyword + "/");
	if (id != lastGaValue) {
		if (typeof(pageTracker) != "undefined") pageTracker._trackPageview(id);
		else setTimeout(function() {googleanalytics(keyword)}, 10);
	}
	lastGaValue = id;//safeguard against double calls
	//printfire(id);
}

var EmailFriendLink = function () {
	googleanalytics("email");
	window.location = siteroot + "en/email_friend/?Url=" + escape(window.location);
}

var EmailFriendLinkFr = function () {
	googleanalytics("email");
	window.location = siteroot + "fr/email_friend/?Url=" + escape(window.location);
}

var printPage = function() {
	googleanalytics("print");
	window.print();
}

Event.observe(window, "load", function() {
	$$("a[target]").each(function(el) {
		if (el.href.indexOf(location.host) < 0) {
			$(el).observe("click", function() {
				pageTracker._trackPageview("/outboundlink" + absolutePath + toGatString(el.href))
			})
		}
	})
});
