// 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";
minimap10 = new Image();		minimap10.src = "/watl/images/maps/map_mini/nsw-syd/map_mini_nsw-syd.gif";
minimap11 = new Image();		minimap11.src = "/watl/images/maps/map_mini/vic-mel/map_mini_vic-mel.gif";
minimap12 = new Image();		minimap12.src = "/watl/images/maps/map_mini/tas-hob/map_mini_tas-hob.gif";
minimap13 = new Image();		minimap13.src = "/watl/images/maps/map_mini/sa-adel/map_mini_sa-adel.gif";
minimap14 = new Image();		minimap14.src = "/watl/images/maps/map_mini/wa-per/map_mini_wa-per.gif";
minimap15 = new Image();		minimap15.src = "/watl/images/maps/map_mini/nt-dar/map_mini_nt-dar.gif";
minimap16 = new Image();		minimap16.src = "/watl/images/maps/map_mini/qld-bris/map_mini_qld-bris.gif";


//listener functions
function modelChange() {
	stop();
	theStep = 0;
	loadJSLoopParameters();
}

function stateChange() {
	stop();
	theStep = document.getElementById('time').selectedIndex;
	document.getElementById('local').selectedIndex = 0;
	loadJSLoopParameters();
}

function localChange() {
	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 zoomLocalSelect(local) {
	stop();
	var i;
	for(i = 0; i < document.getElementById('local').options.length; i++) {
		if(document.getElementById('local').options[i].value == local) {
			break;
		}
	}
	document.getElementById('local').selectedIndex = i;
	theStep = document.getElementById('time').selectedIndex;
	loadJSLoopParameters();
}


function zoomOutFull() {
	stop();
	document.getElementById('state').selectedIndex = 0;
	document.getElementById('local').selectedIndex = 0;
	theStep = document.getElementById('time').selectedIndex;
	loadJSLoopParameters();
}

function zoomOutLocal() {
	stop();
	document.getElementById('local').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 local = document.getElementById('local').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() == "IDYMA005") {
				document.getElementById('local').disabled = "";
				for(i = 0; i < IDYMA005nImages; i++) {
					theImageNames[i] = "/fwo/IDY00061/IDYMA005." + (local==""||local==null?state:local) + "." + IDYMA005LoopDateTimes[i] + ".png";
				}
				nImages = IDYMA005nImages;
			} else if(getModelChoice() == "IDYMA005day2") {
				document.getElementById('local').disabled = "";
				for(i = 0; i < IDYMA005nImages; i++) {
					theImageNames[i] = "/fwo/IDY00061/IDYMA005." + (local==""||local==null?state:local) + "." + IDYMA005LoopDateTimes[i] + ".png";
				}
				nImages = IDYMA005nImages;
			}
			else {
				document.getElementById('local').selectedIndex = 0;
				local = document.getElementById('local').value;
				document.getElementById('local').disabled = "disabled";
				for(i = 0; i < IDYEC003nImages; i++) {
					theImageNames[i] = "/fwo/IDY00061/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 drop down
			document.getElementById('time').options.length = nImages;
			if(getModelChoice() == "IDYMA005") {
				for(i = 0; i < nImages; i++) {
					document.getElementById('time').options[i] = new Option(IDYMA005LoopDisplay[i]);
					document.getElementById('time').options[i].value = IDYMA005LoopDateTimes[i];
				}
			} else if(getModelChoice() == "IDYMA005day2") {
				for(i = 0; i < nImages; i++) {
					document.getElementById('time').options[i] = new Option(IDYMA005LoopDisplay[i]);
					document.getElementById('time').options[i].value = IDYMA005LoopDateTimes[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;
			
			var allowLocalZoom = false;
			// Load the full location name.
			if(local == "" || local == null) {
				if(getModelChoice() == "IDYMA005" || getModelChoice() == "IDYMA005day2") {
					allowLocalZoom = true;
				} else {
					localWatersArea(0,0,0,0, '', 'aus');
				}
				if(state == "aus") {
					stateScreen = "Australia";
					document.getElementById('animation').useMap="#stateMap";
					localWatersArea(0,0,0,0, '', 'aus');
				} else if(state == "vic") { stateScreen = "Victoria"; if(allowLocalZoom) localWatersArea(228,116,327,217, 'javascript:zoomLocalSelect(\'vic-mel\')', 'Melbourne'); }
				else if(state == "nsw") { stateScreen = "New South Wales"; if(allowLocalZoom) localWatersArea(269,218,307,257, 'javascript:zoomLocalSelect(\'nsw-syd\')', 'Sydney'); }
				else if(state == "qld1") { stateScreen = "Queensland - Northern"; if(allowLocalZoom) localWatersArea(0,0,0,0, '', ''); }
				else if(state == "qld2") { stateScreen = "Queensland - Southern"; if(allowLocalZoom) localWatersArea(366,269,425,332, 'javascript:zoomLocalSelect(\'qld-bris\')', 'Brisbane'); }
				else if(state == "nt") { stateScreen = "Northern Territory"; if(allowLocalZoom) localWatersArea(171,124,256,186, 'javascript:zoomLocalSelect(\'nt-dar\')', 'Darwin'); }
				else if(state == "wa1") { stateScreen = "Western Australia - Northern"; if(allowLocalZoom) localWatersArea(0,0,0,0, '#', ''); }
				else if(state == "wa2") { stateScreen = "Western Australia - Southern"; if(allowLocalZoom) localWatersArea(144,250,204,304, 'javascript:zoomLocalSelect(\'wa-per\')', 'Perth'); }
				else if(state == "sa") { stateScreen = "South Australia"; if(allowLocalZoom) localWatersArea(411,230,504,328, 'javascript:zoomLocalSelect(\'sa-adel\')', 'Adelaide'); }
				else if(state == "tas") { stateScreen = "Tasmania"; if(allowLocalZoom) localWatersArea(263,324,357,423, 'javascript:zoomLocalSelect(\'tas-hob\')', 'Hobart'); }
				else stateScreen = "";
			} else {
				localWatersArea(0,0,0,0, '', '');
				if(local == "vic-mel") { 
					stateScreen = "Victoria, Melbourne";
					document.getElementById('state').selectedIndex = 7;
				}
				else if(local == "nsw-syd") {
					stateScreen = "New South Wales, Sydney";
					document.getElementById('state').selectedIndex = 1;
				}
				else if(local == "qld-bris") {
 					stateScreen = "Queensland, Brisbane";
					document.getElementById('state').selectedIndex = 4;
				}
				else if(local == "nt-dar") {
 					stateScreen = "Northern Territory, Darwin";
					document.getElementById('state').selectedIndex = 2;
				}
				else if(local == "wa-per") {
 					stateScreen = "Western Australia, Perth";
					document.getElementById('state').selectedIndex = 9;
				}
				else if(local == "sa-adel") {
 					stateScreen = "South Australia, Adelaide";
					document.getElementById('state').selectedIndex = 5;
				}
				else if(local == "tas-hob") {
 					stateScreen = "Tasmania, Hobart";
					document.getElementById('state').selectedIndex = 6;
				}
				else stateScreen = "";
			}
			
			// mini map logic
			if(local == "" || local == null) {
				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>";
				}
			} else {
				document.getElementById('maplocator').innerHTML =  "<p><a href=\"javascript:func()\" onclick=\"zoomOutLocal()\"><img src=\"/watl/images/maps/map_mini/"+local+"/map_mini_"+local+".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=\"zoomOutLocal()\"><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() == "IDYMA005" || getModelChoice() == "IDYMA005day2" ) {
				document.getElementById('loop_row').style.display = "";
				launch();
			}
			else {
				document.getElementById('loop_row').style.display = "none";
				launch_EC();
			}
			

		}
	}
	if(getModelChoice() != "IDYEC003") {
		if(local == "" || local == null) {	
			url = "/fwo/IDY00061/" + getModelChoice() + "." + document.getElementById('state').value +".datetimes.js";
		} else {
			url = "/fwo/IDY00061/" + getModelChoice() + "." + document.getElementById('local').value +".datetimes.js";
		}
	} else {
		url = "/fwo/IDY00061/" + 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 "IDYMA005";
	} else if(document.forms.control_form.modelCodeb.checked) {
		return "IDYMA005day2";
	} else if(document.forms.control_form.modelCodec.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;
}

