// Preload certain images

earlier1 = new Image();	earlier1.src = "/watl/images/symbols/earlier.gif";

earlier0 = new Image();	earlier0.src = "/watl/images/symbols/earlier-0.gif";

later1 = new Image(); later1.src = "/watl/images/symbols/later.gif";

later0 = new Image(); later0.src = "/watl/images/symbols/later-0.gif";



minimap0 = new Image();		minimap0.src = "/watl/images/maps/map_mini/map_mini_aus.gif";

minimap1 = new Image();		minimap1.src = "/watl/images/maps/map_mini/nsw/map_mini_nsw.gif";

minimap2 = new Image();		minimap2.src = "/watl/images/maps/map_mini/vic/map_mini_vic.gif";

minimap3 = new Image();		minimap3.src = "/watl/images/maps/map_mini/tas/map_mini_tas.gif";

minimap4 = new Image();		minimap4.src = "/watl/images/maps/map_mini/sa/map_mini_sa.gif";

minimap5 = new Image();		minimap5.src = "/watl/images/maps/map_mini/wa1/map_mini_wa1.gif";

minimap6 = new Image();		minimap6.src = "/watl/images/maps/map_mini/wa2/map_mini_wa2.gif";

minimap7 = new Image();		minimap7.src = "/watl/images/maps/map_mini/nt/map_mini_nt.gif";

minimap8 = new Image();		minimap8.src = "/watl/images/maps/map_mini/qld1/map_mini_qld1.gif";

minimap9 = new Image();		minimap9.src = "/watl/images/maps/map_mini/qld2/map_mini_qld2.gif";



//listener functions

function modelChange() {

	stop();

	theStep = 0;

	loadJSLoopParameters();

}



function stateChange() {

	stop();

	theStep = document.getElementById('time').selectedIndex;

	loadJSLoopParameters();

}



function timeChange() {

	stop();

	theStep = document.getElementById('time').selectedIndex;

	loadJSLoopParameters();

}





function zoomSelect(state) {

	stop();

	var i;

	for(i = 0; i < document.getElementById('state').options.length; i++) {

		if(document.getElementById('state').options[i].value == state) {

			break;

		}

	}

	document.getElementById('state').selectedIndex = i;

	theStep = document.getElementById('time').selectedIndex;

	loadJSLoopParameters();

}



function zoomOutFull() {

	stop();

	document.getElementById('state').selectedIndex = 0;

	theStep = document.getElementById('time').selectedIndex;

	loadJSLoopParameters();

}





// Asynchronous Image loading

var theImageNames;

var nImages;

var theStep = 0;



var sliderEl;

var inputEl;

var s;



function loadJSLoopParameters() {

	var url;

	var p;

	var i;

	var state = document.getElementById('state').value;

	var stateScreen;

	var xmlHttp = getXMLHttpObject();

	xmlHttp.onreadystatechange = function() {

		// 0 - not initialized;    1 - request has been set up;    2 - sent;    3 - in process;    4 - complete

		if(xmlHttp.readyState==4) {

			// read in HTTP response

			eval(xmlHttp.responseText);



			// Define Animation related Javascript variables

			theImageNames = new Array();

			// Create the image name array for the loop

			if(getModelChoice() == "IDYAA005") {

				for(i = 0; i < IDYAA005nImages; i++) {

					theImageNames[i] = "/fwo/IDY00060/IDYAA005." + state + "." + IDYAA005LoopDateTimes[i] + ".png";

				}

				nImages = IDYAA005nImages;

			} else {

				for(i = 0; i < IDYEC003nImages; i++) {

					theImageNames[i] = "/fwo/IDY00060/IDYEC003." + state + "." + IDYEC003LoopDateTimes[i] + ".png";

				}

				nImages = IDYEC003nImages;

			}

			// check incase the declaration from the Javascript file fails.

			if(nImages == null)

			{

				nImages = 0;

			}



			// image src

			//document.getElementById('animation').src=theImageNames[theStep];

			document.getElementById('animation').useMap="";



			// populate district drop down

			document.getElementById('time').options.length = nImages;

			if(getModelChoice() == "IDYAA005") {
				nImages = 9;
				document.getElementById('time').options.length = nImages;

				for(i = 0; i < 9; i++) {

					document.getElementById('time').options[i] = new Option(IDYAA005LoopDisplay[i]);

					document.getElementById('time').options[i].value = IDYAA005LoopDateTimes[i];

				}

			} else {

				for(i = 0; i < nImages; i++) {

					document.getElementById('time').options[i] = new Option(IDYEC003LoopDisplay[i]);

					document.getElementById('time').options[i].value = IDYEC003LoopDateTimes[i];

				}

			}

			document.getElementById('time').selectedIndex = theStep;



			// Load the full state name.

			if(state == "aus") {

				stateScreen = "Australia";

				document.getElementById('animation').useMap="#stateMap";

			} else if(state == "vic") stateScreen = "Victoria";

			else if(state == "nsw") stateScreen = "New South Wales";

			else if(state == "qld1") stateScreen = "Queensland - Northern";

			else if(state == "qld2") stateScreen = "Queensland - Southern";

			else if(state == "nt") stateScreen = "Northern Territory";

			else if(state == "wa1") stateScreen = "Western Australia - Northern";

			else if(state == "wa2") stateScreen = "Western Australia - Southern";

			else if(state == "sa") stateScreen = "South Australia";

			else if(state == "tas") stateScreen = "Tasmania";

			else stateScreen = "";



			// mini map logic

			if(state == "aus") {

				document.getElementById('maplocator').innerHTML = "<p><img src=\"/watl/images/maps/map_mini/map_mini_aus.gif\" alt=\"Current view Australia\" width=\"71\" height=\"65\" title=\"Current view Australia\" /></p>";

			} else {

				document.getElementById('maplocator').innerHTML =  "<p><a href=\"javascript:func()\" onclick=\"zoomOutFull()\"><img src=\"/watl/images/maps/map_mini/"+state+"/map_mini_"+state+".gif\" alt=\"Current view "+stateScreen+", click to zoom out\" width=\"71\" height=\"65\" title=\"Current view "+stateScreen+", click to zoom out\" /></a></p>";

				document.getElementById('maplocator').innerHTML += "<p><a href=\"javascript:func()\" onclick=\"zoomOutFull()\"><img src=\"/watl/images/symbols/zoom-out.gif\" alt=\"Click for wider view\" width=\"13\" height=\"15\" align=\"middle\" />Zoom out</a></p>";

			}





			//TODO: clear slider funky business

			document.getElementById('slider-1').innerHTML = "";

			document.getElementById('slider-1').innerHTML = "<input class=\"slider-input\" id=\"slider-input-1\" name=\"slider-input-1\"  />";

			// Set slider variables.

			sliderEl = document.getElementById ? document.getElementById("slider-1") : null;

			inputEl = document.getElementById("slider-input-1");

			s = new Slider(sliderEl, inputEl);

			s.setMinimum(1);

			s.setMaximum(nImages);

			s.setBlockIncrement(1);

			s.setValue(theStep+1);

			s.onchange = function () {

				if(slider_interrupt==1) {

					stop();

					display_current_image_from_slider(s.getValue()-1);

				}

			};

			s.onvaluechange = function () { }

			window.onresize = function () {

				s.recalculate();

			};





			if(getModelChoice() == "IDYAA005") {

				document.getElementById('loop_row').style.display = "";

				launch();

			}

			else {

				document.getElementById('loop_row').style.display = "none";

				launch_EC();

			}





		}

	}





	url = "/fwo/IDY00060/" + getModelChoice() + "." + document.getElementById('state').value +".datetimes.js";

	xmlHttp.open("GET",url,true);

	xmlHttp.send(null);

}



function getModelChoice() {

	if(document.forms.control_form.modelCodea.checked) {

		return "IDYAA005";

	} else if(document.forms.control_form.modelCodeb.checked) {

		return "IDYEC003";

	} else {

		return "";

	}

}





function getXMLHttpObject() {

	var xmlHttp = null;

	try {

		// Firefox, Opera 8.0+, Safari

		xmlHttp=new XMLHttpRequest();

	} catch (e) {

		// Internet Explorer

		try {

			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");

		} catch (e) {

			try {

				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");

			} catch (e) {

				alert("Your web browser appears to be too old to display these pages. Please update your browser.");

				return false;

			}

		}

	}

	return xmlHttp;

}

