
var server = {
	url: function () {
		return {
			domain: Request.ServerVariables("SERVER_NAME") + "",
			folder: Request.ServerVariables("URL") + ""
		};
	},
	redirect: function (url) {
		Response.Redirect(url);
		Response.End();
	}
}

var browser = {
	url: function () {
		return {
			domain: window.location.hostname,
			folder: window.location.pathname
		};
	},
	redirect: function (url) {
		document.location=url;
	}
}

//:~

function Site (params) {

	for (var param in params)
		this[param] = params[param];

	this.toString = function () {
		return "http://" + this.domains[0] + (this.folders.length > 0 ? this.folders[0] : "");
	}

	this.get404 = function () {
		var html = "";
		html += "<center>";
		html += "<img src=\"/_redirect/logo/"+this.image+"\">";
		html += "<p>The page you are looking for cannot be found,<br>please return to the "+this.name+" homepage.</p>";
		html += "<p><a href=\""+this+"\" style=\"font-size:16px;font-weight:bold;\">"+this.toString().replace("http://","")+"</a></p>";
		html += "<p>You shall be redirected shortly ...</p>";
		html += "</center>";
		return html;
	}
}

//:~

var redirector = {

	mode: "server",
	action: server,

	sites: [
		new Site({
			name: "Parari",
			root: true,
			image: "parari.gif",
			domains: ["www.parari.com.au"],
			//folders: ["/live/2009","/live","/parari", "/parari2007"]
			folders: ["/live/2009","/live"]
		}),
		new Site({
			name: "Thales Protected Mobility",
			root: true,
			image: "thales.jpg",
			domains: ["www.thalesprotectedmobility.com.au"],
			folders: ["/tpm"]
		}),
		new Site({
			name: "Handloaders Guide",
			root: false,
			image: "handloaders.gif",
			domains: ["www.adi-limited.com", "www.adi-limited.com.au"],
			folders: ["/handloaders-guide"]
		})
	],

	global: new Site({
		name: "Thales Group",
		image: "thales.jpg",
		domains: ["www.thalesgroup.com"],
		folders: ["/Countries/Australia/Home"]
	}),

	//:~

	match: function () { 

		for (var i = 0; i < this.sites.length; i++)
			for (var j = 0; j < this.sites[i].domains.length; j++)
				if (this.url().domain.indexOf(this.sites[i].domains[j]) >= 0)
					this.sites[i].match_domain = true;
		
		for (var i = 0; i < this.sites.length; i++)
			for (var j = 0; j < this.sites[i].folders.length; j++)
				if (this.url().folder.indexOf(this.sites[i].folders[j]) == 0)
					this.sites[i].match_folder = true;
	},

	redirectRoot: function () {
		
		if ((Request.ServerVariables("SERVER_NAME")+"").indexOf("thalesgroup.com.au") >= 0) {
			Response.Redirect("http://www.thalesgroup.com/Countries/Australia/Home");
			Response.End();
		}

		this.match();
		
		for (var i = 0; i < this.sites.length; i++)
			if (this.sites[i].root && this.sites[i].match_domain)
				this.action.redirect(this.sites[i]);
		
		this.action.redirect(this.global);
	},

	redirect404: function () {
		
		this.match();

		var site = this.global;

		for (var i = 0; site == this.global && i < this.sites.length; i++)
			if (this.sites[i].match_folder)
				site = this.sites[i];

		for (var i = 0; site == this.global && i < this.sites.length; i++)
			if (this.sites[i].match_domain)
				site = this.sites[i];

		if (!site.match_domain && site != this.global)
			return this.action.redirect("http://"+site.domains[0]+this.url().folder);

		document.getElementById("message").innerHTML = site.get404();
		window.setTimeout(new Function("document.location='"+site+"'"), 4 * 1000);
	},

	redirectSniff: function () {

		this.match();

		var site;
		for (var i = 0; site == null && i < this.sites.length; i++)
			if (this.sites[i].match_folder)
				site = this.sites[i];

		if (site != null) {
			
			for (var i = 0; i < site.domains.length; i++)
				if (this.url().domain.indexOf(site.domains[i]) >= 0)
					return true;
			
			this.action.redirect("http://"+site.domains[0]+this.url().folder);
			return false;
		}
		return true;
	},

	initSniff: function (cb) {
		
		var loc = document.location + "";
		if (loc.indexOf("/parari") >= 0) {
			
			loc = loc.replace("/parari","/live");
			
			if (loc.indexOf("page-271.htm") >= 0)
				loc = loc.replace("page-271.htm","index.htm")
			
			if (loc.indexOf("/page-") >= 0)
				loc = loc.replace("/page-","/")
			
			if ((document.location+"") != loc)
				document.location = loc;
		}


		if (this.redirectSniff())
			return cb();
		return false;		
	},

	//:~

	url: function () {
		if (this.url_o == null)
			this.url_o = this.action.url();
		return this.url_o;
	}
};