﻿function setOrientationClass(orientation) {
	var bodyClass = document.body.className;
	switch (orientation) {
		case "portrait":
			document.body.className = (bodyClass.indexOf("portrait") == -1)? bodyClass + " portrait" : bodyClass;
			document.body.setAttribute("orient", "portrait"); // not utilised: CSS cannot detect on-the-fly attribute value change
			break;
		case "landscape":
			document.body.className = bodyClass.split("portrait").join(" ");
			document.body.setAttribute("orient", "landscape"); // not utilised: CSS cannot detect on-the-fly attribute value change
			break;
	}
}

function setOrientation() {
	setOrientationClass((window.orientation==0 || parseInt(document.body.clientWidth)<320)? "portrait" : "landscape");
}

function doLoad() {
	if (navigator.userAgent.indexOf("iPhone") == -1 && false) {
		document.getElementById("foriPhone").innerHTML = "This Website is intended specifically for the iPhone or iPod touch.<br /><br />Click here to access the Full <a href=\"http://www.metrotrains.com.au\">Metro Trains Melbourne</a> Website.";
		document.getElementById("foriPhone").setAttribute("class", "show");	
	}
	else if (navigator.userAgent.indexOf("iPhone") == -1) {
		document.body.setAttribute("class", "noiPhone");
		var pgLinks = document.getElementById("content").getElementsByTagName("A");
		for (var i=0; i<pgLinks.length; i++) {
			if (pgLinks[i].getAttribute("href").indexOf("http://") == 0) pgLinks[i].setAttribute("target", "_blank");
		}
	}
	else {
		setOrientation();
		setTimeout(scrollTo, 0, 0, 1);
	}
}

if (window.addEventListener) {
	window.addEventListener('orientationchange', function() {
		setOrientation();
	}, false);
	window.addEventListener('resize', function() {
		setOrientation();
	}, false);
	window.addEventListener('load', function() {
		doLoad();
	}, false);
}
else if (window.attachEvent) {
	window.attachEvent('onload', function() {
		doLoad();
	});
}
