﻿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);
	}
	
	$(document).ready(function() {
		if ($('dl.news dd a').length) {
			$('dl dt, dl dd').hover(function() {
					if (!$(this).hasClass('header')) {
						var thisEl = this.tagName.toLowerCase();
						var otherEl = (thisEl=='dt')? 'dd' : 'dt';
						var hoverIndex = $('dl ' + thisEl).index(this);
						$(this).addClass('hover');
						$(this).parents('dl').find(otherEl).eq(hoverIndex).addClass('hover');
					}
				}, function() {
					if (!$(this).hasClass('header')) {
						var thisEl = this.tagName.toLowerCase();
						var otherEl = (thisEl=='dt')? 'dd' : 'dt';
						var hoverIndex = $('dl ' + thisEl).index(this);
						$(this).removeClass('hover');
						$(this).parents('dl').find(otherEl).eq(hoverIndex).removeClass('hover');
					}
				});
			$('dl dt, dl dd').click(function() {
				if (!$(this).hasClass('header')) {
					var thisEl = this.tagName.toLowerCase();
					var hoverIndex = $('dl ' + thisEl).index(this);
					window.location = $(this).parents('dl').find('dd:eq(' + hoverIndex + ') a').attr('href');
				}
			});
		}
	});
}

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();
	});
}

