/* -----------------------------------------------------------------
		scripts for ESAB splash page
*/
var countryListOpen = false;
var regionList = new Array("regionEurope", "regionNorthAmerica", "regionAfrica", "regionSouthAmerica", "regionAsia")
var imageMapList = new Array("imageMapEurope", "imageMapNorthAmerica", "imageMapAfrica", "imageMapSouthAmerica", "imageMapAsia")
var preloadImageArray = new Array;
var imageArray = new Array;

/* -----------------------------------------------------------------
		get inner height of browser window
*/
function getWindowInnerHeight() {
	var x;

	if (self.innerHeight) {
		x = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) {
		x = document.documentElement.clientHeight;
	} else if (document.body) {
		x = document.body.clientHeight;
	}

	return x;
}

/* -----------------------------------------------------------------
		get inner width of browser window
*/
function getWindowInnerWidth() {
	var x;

	if (self.innerWidth) {
		x = self.innerWidth;
	} else if (document.documentElement && document.documentElement.clientWidth) {
		x = document.documentElement.clientWidth;
	} else if (document.body) {
		x = document.body.clientWidth;
	}

	return x;
}

/* -----------------------------------------------------------------
		get querystring
*/
function getQueryString(varname) {
	var st = unescape(location.search);
	if (st.indexOf(varname) != -1){
		var tempst = st.substring(st.indexOf(varname + "=") + varname.length + 1,st.length);
		if (tempst.indexOf("&") != -1) {
			var newtemp = tempst.substring(0, tempst.indexOf("&"));
			return newtemp;
		} else {
			return tempst;
		}
	} else {
		return("");
	}
}

/* -----------------------------------------------------------------
		set cookie
*/
function setCookie(value) {
	var name = "remember_country_choice";
	var value = value;
	var expires = new Date(new Date().getTime() + (1000 * 60 * 60 * 24 * 365 * 10));
	var path = "/"
	var domain = null;

	document.cookie = name + "=" + escape(value) + ((expires == null) ? "" : "; expires=" + expires.toGMTString()) + ((path == null) ? "" : "; path=" + path) + ((domain == null)  ? "" : "; domain=" + domain);
	return;
}

/* -----------------------------------------------------------------
		get cookie
*/
function getCookie(name) {
	var start = document.cookie.indexOf(name + "=");
	var len = start + name.length + 1;
	var end = document.cookie.indexOf(";", len);

	if ((!start) && (name != document.cookie.substring(0, name.length))) return null;
	if (start == -1) return null;
	if (end == -1) end = document.cookie.length;

	return unescape(document.cookie.substring(len, end));
}

/* -----------------------------------------------------------------
		hide layer
*/
function hideLayer(e) {
	if (!e) var e = window.event;
	var target = (e.target) ? e.target : e.srcElement;

	if (elm = target.parentNode.parentNode) {
		elm.style.visibility = "hidden";
		countryListOpen = null;
		resetRegion();
	}
	return;
}

/* -----------------------------------------------------------------
		find x position of an element
*/
function findPosX(obj) {
	var curleft = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			curleft += obj.offsetLeft;
			obj = obj.offsetParent;
		}
	} else if (obj.x) {
		curleft += obj.x;
	}

	return curleft;
}

/* -----------------------------------------------------------------
		hilights a region on the map (switch image)
*/
function hilightRegion(e) {
	var elm, newImgSrc;
	if (!e) var e = window.event;
	var target = (e.target) ? e.target : e.srcElement;

	// if there's a country list open, don't do anything
	if (countryListOpen) return;

	// set new image src
	switch (target.id) {
		// europe
		case "regionEurope":
			newImgSrc = "map_europe.png";
			break;
		case "imageMapEurope":
			newImgSrc = "map_europe.png";
			underlineRegionLink("regionEurope");
			break;

		// north america
		case "regionNorthAmerica":
			newImgSrc = "map_north_america.png";
			break;
		case "imageMapNorthAmerica":
			newImgSrc = "map_north_america.png";
			underlineRegionLink("regionNorthAmerica");
			break;

		// africa and middle east
		case "regionAfrica":
			newImgSrc = "map_africa.png";
			break;
		case "imageMapAfrica":
			newImgSrc = "map_africa.png";
			underlineRegionLink("regionAfrica");
			break;

		// south america
		case "regionSouthAmerica":
			newImgSrc = "map_south_america.png";
			break;
		case "imageMapSouthAmerica":
			newImgSrc = "map_south_america.png";
			underlineRegionLink("regionSouthAmerica");
			break;

		// asia / pacific
		case "regionAsia":
			newImgSrc = "map_asia.png";
			break;
		case "imageMapAsia":
			newImgSrc = "map_asia.png";
			underlineRegionLink("regionAsia");
			break;

		default:
			newImgSrc = "map_world.png";
			break;
	}

	// change image
	if (!(elm = document.getElementById("mapImage"))) return;
	elm.src = "media/" + newImgSrc;

	return;
}

/* -----------------------------------------------------------------
		when hovering a region in the imagemap, underline region link
*/
function underlineRegionLink(regionName) {
	var elm;
	if (elm = document.getElementById(regionName)) {
		elm.className = "underlined";
	}

	return;
}

/* -----------------------------------------------------------------
		resets the region on the map (switch to world map)
*/
function resetRegion() {
	var elm, i;

	// if there's a country list open, don't do anything
	if (countryListOpen) {
		return;
	}

	// change back underlined region links
	for (i = 0; i < regionList.length; i++) {
		if (elm = document.getElementById(regionList[i])) {
			elm.className = "";
		}
	}

	// change image
	if (elm = document.getElementById("mapImage")) {
		elm.src = "media/map_world.png";
	}

	return;
}


/* -----------------------------------------------------------------
		open the country list layer
*/
function showCountryList(e) {
	if (!e) var e = window.event;
	var target = (e.target) ? e.target : e.srcElement;
	var x = target.id;

	if (x == "imageMapEurope") x = "regionEurope";
	if (x == "imageMapNorthAmerica") x = "regionNorthAmerica";
	if (x == "imageMapAfrica") x = "regionAfrica";
	if (x == "imageMapSouthAmerica") x = "regionSouthAmerica";
	if (x == "imageMapAsia") x = "regionAsia";
	
	// if there's a country list open already, close it first
	if (countryListOpen) {
		if (countryListOpen == x) {
			return;
		}
		if (elm = document.getElementById(countryListOpen + "CountryList")) {
			elm.style.visibility = "hidden";
		}
	}

	// show selected country list
	if (elm = document.getElementById(x + "CountryList")) {
		elm.style.visibility = "visible";
		countryListOpen = x;
	}

	return;
}

/* -----------------------------------------------------------------
		position logo, country lists and select region area
*/
function positionLayers() {
	var x;

	if (!(elm = document.getElementById("banner"))) return;
	x = findPosX(elm);

	if (elm = document.getElementById("logo")) {
		elm.style.left = x + "px";
		elm.style.visibility = "visible";
	}

	if (elm = document.getElementById("selectRegion")) {
		elm.style.left = x + "px";
		elm.style.visibility = "visible";
	}

	for (i = 0; i < regionList.length; i++) {
		if (elm = document.getElementById(regionList[i] + "CountryList")) {
			elm.style.left = (x + 255) + "px";
		}
	}

	return;
}

/* -----------------------------------------------------------------
		handle dropdown onchange and clicking on links in country list
*/
function redirect(e) {
	var elm, url;

	if (!e) var e = window.event;
	var target = (e.target) ? e.target : e.srcElement;
	var x = target.id;

	//  check if user chose an option in the dropdown
	if (target.id == "sitesDropdownSubmit") {
		if (elm = document.forms["internationalSites"]) {
			// anything selected in the dropdown?
			if (url = elm.sitesDropdown.options[elm.sitesDropdown.selectedIndex].value) {
				// remember choice?
				if (document.forms["remember"].rememberChoice.checked) {
					// set cookie, the url is the value
					setCookie(url);
				}
				// go to url
				document.location.href = url;
			}
		}
	} else {
		// clicked an url, do we need to remember it?
		if (document.forms["remember"].rememberChoice.checked) {
			// set cookie, the url is the value
			setCookie(target);
		}
		// go to url
		document.location.href = target;
	}

	return false;
}

/* -----------------------------------------------------------------
		execute on page load
*/
function onLoadDo() {
	var cookie, i, j, elm, x;

	// check if cookie exists, if it does go to that url
	if (cookie = getCookie("remember_country_choice")) {
		document.location.href = cookie;
	}

	// random image for banner
	x = parseInt(Math.random() * 6) + 1;
	if (elm = document.getElementById("banner")) {
		elm.src = "media/splash_banner_" + x + ".jpg";
	}

	// onmouseover, onmouseout and onclick on region links and image map
	for (i = 0; i < regionList.length; i++) {
		if (elm = document.getElementById(regionList[i])) {
			elm.onmouseover = hilightRegion;
			elm.onmouseout  = resetRegion;
			elm.onclick = showCountryList;
		}
	}
	for (i = 0; i < imageMapList.length; i++) {
		if (elm = document.getElementById(imageMapList[i])) {
			elm.onmouseover = hilightRegion;
			elm.onmouseout  = resetRegion;
			elm.onclick = showCountryList;
		}
	}

	// onclick on the close buttons
	for (i = 0; i < regionList.length; i++) {
		if (elm = document.getElementById(regionList[i] + "Close")) {
			elm.onclick = hideLayer;
		}
	}

	// onchange on sites dropdown
	if (elm = document.getElementById("sitesDropdownSubmit")) {
		elm.onclick = redirect;
	}

	var alerted = false;
	// onclick on all links in country list
	// find all div elements
	if (elm = document.getElementsByTagName("div")) {
		// loop thru node collection
		for (i = 0; i < elm.length; i++) {
			// must have className of "left" or "right"
			if (elm[i].className == "left" || elm[i].className == "right") {
				// must have child nodes
				if (elm[i].hasChildNodes()) {
					// loop thru all child nodes
					for (j = 0; j < elm[i].childNodes.length; j++) {
						// must be an anchor tag
						if (elm[i].childNodes[j].tagName == "A") {
							// attach event handler on the link
							elm[i].childNodes[j].onclick = redirect;
						}
					}
				}
			}
		}
	}

	// position logo and country lists
	positionLayers();

	// preload map images
	imageArray.push("media/map_africa.png");
	imageArray.push("media/map_asia.png");
	imageArray.push("media/map_europe.png");
	imageArray.push("media/map_north_america.png");
	imageArray.push("media/map_south_america.png");

	i = 0;
	while (imageArray.length > 0) {
		preloadImageArray[i] = new Image;
		preloadImageArray[i++].src = imageArray.pop();
	}
	return;
}

/* -----------------------------------------------------------------
		execute on page load
*/
function onResizeDo() {
	positionLayers();
}

/* -----------------------------------------------------------------
		attach onload event
*/
window.onload = onLoadDo;
window.onresize = onResizeDo;

/* -----------------------------------------------------------------
		attach onload event
*/


//for IE browsers
if (window.attachEvent) window.attachEvent("onload", onLoadDo);
//for IE browsers
if (window.attachEvent) window.attachEvent("onresize", onResizeDo);

// for Mozilla browsers
if (document.addEventListener) {
	document.addEventListener("DOMContentLoaded", onLoadDo, false);
}

if (document.addEventListener) {
	document.addEventListener("DOMContentLoaded", onresize, false);
}