trans1 = new Image(); trans1.src = "/watl/images/popup/trans.png";
point1 = new Image(); point1.src = "/watl/images/popup/point.png";

var XOFFSET=2;//1
var YOFFSET=4;//20
var tip;
var w3=document.getElementById&&!document.all;
var ie=document.all;

function setToolTip() {
	
   if (w3) {tip=document.getElementById("tbx").style;}
	else if (ie) {tip=document.all.tbx.style;}
	tip.visibility="visible";
	tip.display="none";
}

function get_mouse(e) {
	var x = 0;
	var y = 0;
	if (!e) var e = window.event;
	
	if(e.pageX || e.pageY) {
		x = e.pageX;
		y = e.pageY;                             
	} else if (e.clientX || e.clientY) {
		x = e.clientX + document.body.scrollLeft
						+ document.documentElement.scrollLeft;
		y = e.clientY + document.body.scrollTop
						+ document.documentElement.scrollTop;
	}

	//not picking up ff event.
	//var x=(w3)?e.pageX:event.x+document.body.scrollLeft+10;
	//var y=(w3)?e.pageY:event.y+document.body.scrollTop-10;

	tip.left=x+XOFFSET;
	tip.top=y+YOFFSET;
}

window.onLoad=addEvent(window, 'load', pageInit);

function pageInit() {
	// the following call also calls "loadForecastMenu()" upon completion.
	loadDateDropDown("/fwo/IDY00070/frost.datetimes.js");
	setToolTip(); 
	// onmousemove - Mozilla
	if(w3) document.onmousemove=get_mouse;
}


function hidePopup() {
	tip.display="none";
}

function popupFrost(stn_name, stn_id, lat, lon, min_temp, elevation, vhigh_duration, high_duration, med_duration, low_duration) { 
	var riskLevel;
	var riskClass;
	if(min_temp <= -5.0) { riskLevel = "Severe"; riskClass = "risk-e"; }
	else if(min_temp <= -2.0) { riskLevel = "High"; riskClass = "risk-h"; }
	else if(min_temp <= 1.0) { riskLevel = "Medium"; riskClass = "risk-m"; }
	else if(min_temp <= 4.0) { riskLevel = "Low"; riskClass = "risk-l"; }
	else { riskLevel = "Very Low";  riskClass = "risk-n"; }
	if(min_temp < 0) min_temp = "&minus;" + min_temp.substring(1);
	else min_temp = "+" + min_temp;
	
	if (vhigh_duration == 0) vhigh_duration = "-";
	else if(vhigh_duration > 1) vhigh_duration += " hours";
	else vhigh_duration += " hour";
	if (high_duration == 0) high_duration = "-";
	else if(high_duration > 1) high_duration += " hours";
	else high_duration += " hour";
	if (med_duration == 0) med_duration = "-";
	else if(med_duration > 1) med_duration += " hours";
	else med_duration += " hour";
	if (low_duration == 0) low_duration = "-";
	else if(low_duration > 1) low_duration += " hours";
	else low_duration += " hour";
	
	var content="<div><img src=\"/watl/images/popup/point.png\" class=\"handle\" /></div><div class=\"shadow\"><table width=\"100%\" class=\"table-text\"><tr><th colspan=\"2\"><span class="+riskClass+">"+stn_name+"</span></th></tr>";
	content += "<tr class=\"small\"><td colspan=\"2\"><b>Lat: </b>"+lat+"<b>Lon: </b>"+lon+"<b>Elev: </b>"+Math.round(elevation)+"m</td></tr>";
	content += "<tr><td>Minimum temperature</td><td><span class=\"min\">"+min_temp+"&deg;C</span></td></tr>";
	content += "<tr><td><span class=\"risk-e\">&nbsp;</span> Hours below&nbsp; &minus;5&deg;C</td><td>"+vhigh_duration+"</td></tr>";
	content += "<tr><td><span class=\"risk-h\">&nbsp;</span> Hours below&nbsp; &minus;2&deg;C</td><td>"+high_duration+"</td></tr>";
	content += "<tr><td><span class=\"risk-m\">&nbsp;</span> Hours below&nbsp; +1&deg;C</td><td>"+med_duration+"</td></tr>";
	content += "<tr><td><span class=\"risk-l\">&nbsp;</span> Hours below&nbsp; +4&deg;C</td><td>"+low_duration+"</td></tr>";
	content += "</table></div>";

	document.onmousemove=get_mouse;
	if(w3){document.getElementById("tbx").innerHTML=content; tip.display='';}
	if(ie){document.all("tbx").innerHTML=content;tip.display='';}
}

// Preload certain global 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/wa/map_mini_wa.gif";
minimap6 = new Image();		minimap6.src = "/watl/images/maps/map_mini/nt/map_mini_nt.gif";
minimap7 = new Image();		minimap7.src = "/watl/images/maps/map_mini/qld/map_mini_qld.gif";

function getXMLHttpObject() {
	var xmlHttp = null;
	try {
		// Firefox, IE 7, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	} catch (e) {
		// Internet Explorer 6 and below
		try {
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				xmlHttp=new ActiveXObject("Msxml3.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;
}

function loadDateDropDown(file) {
	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);
			
			// populate district drop down
			document.getElementById('date').options.length = nSteps;
			for(i = 0; i < nSteps; i++) {
				document.getElementById('date').options[i] = new Option(FrostDisplay[i]);
				document.getElementById('date').options[i].value = FrostDateTimes[i];
			}
			document.getElementById('date').selectedIndex = 0;
			loadForecastModel();
		}
	}
	xmlHttp.open("GET",file,true);
	xmlHttp.send(null);
}

function loadUsemapFile(file) {
	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
                        document.getElementById('usemapInclude').innerHTML = xmlHttp.responseText;
                        document.getElementById('frosttab').tabber.tabShow(1);
                        setTimeout('document.getElementById(\'frosttab\').tabber.tabShow(0)', 1);
		}
	}
	xmlHttp.open("GET",file,true);
	xmlHttp.send(null);
}

function loadTableFile(file) {
	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
			document.getElementById('frost-table').innerHTML = xmlHttp.responseText;
		}
	}
	xmlHttp.open("GET",file,true);
	xmlHttp.send(null);
}

function loadForecastChanges(menu, source) {
	var url;
	var p;
	var i;
	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
			p = eval("(" + xmlHttp.responseText + ")");

			// load file which holds the Map area coordinates for the zoomed area, if viewing at state level.
			if(p.usemapInclude != "") {
				loadUsemapFile(p.usemapInclude);
			}
			
			// load image and usemap
			operaPreload = new Image();
			operaPreload.src = p.imgSrc;
			document.getElementById('forecastImg').src=p.imgSrc;
			//document.getElementById('forecastImg').useMap=p.usemap;  // old method
			// attempting to remove and set usemap attribute to deal with Opera bug.
			document.getElementById('forecastImg').removeAttribute('usemap');
			document.getElementById('forecastImg').setAttribute('usemap',p.usemap,0);//the third parameter is an IE workaround to make the attribute case insensitive
			
			document.getElementById('updatedImg').src=p.updatedImgSrc;
			
			if(source == "date") {					
				// earlier later button logic
				if(menu.selectedIndex <= 0) {
					document.getElementById('earlier').innerHTML = "<img src=\"/watl/images/symbols/earlier-0.gif\" alt=\"No earlier forecast available\" title=\"No earlier forecast available\" width=\"64\" height=\"11\" /><br />";
				} else  {
					document.getElementById('earlier').innerHTML = "<a href=\"javascript:func();\" onclick=\"loadForecastEarlier()\"><img src=\"/watl/images/symbols/earlier.gif\" alt=\"Previous day\" title=\"Previous day\" width=\"64\" height=\"11\" /></a><br />";
				}
				if(menu.selectedIndex >= 1) {
					document.getElementById('later').innerHTML = "<img src=\"/watl/images/symbols/later-0.gif\" alt=\"No later forecast available\" title=\"No later forecast available\" width=\"64\" height=\"11\" /><br />";
				} else {
					document.getElementById('later').innerHTML = "<a href=\"javascript:func();\" onclick=\"loadForecastLater()\"><img src=\"/watl/images/symbols/later.gif\" alt=\"Next day\" title=\"Next day\" width=\"64\" height=\"11\" /></a><br />";
				}
				if(p.state != "aus") {
					loadTableFile(p.tabDataFile);
				}
			}

			// top date change
			document.getElementById('dt_display').innerHTML = "Frost potential for: PM " + p.eveningDate + " - AM " + p.morningDate;

			// mini map logic
			if(source == "state" || source == "model") {
				if(p.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>";
				   document.getElementById('frost-table').innerHTML = "<p class=\"nodata\">No table available for this location.<br/>Please select a state or territory map to view a table.</p>";
				} else {
					document.getElementById('maplocator').innerHTML =  "<p><a href=\"javascript:func()\" onclick=\"zoomOutFull()\"><img src=\"/watl/images/maps/map_mini/"+p.state+"/map_mini_"+p.state+".gif\" alt=\"Current view "+p.displayStateScreen+", click to zoom out\" width=\"71\" height=\"65\" title=\"Current view "+p.displayStateScreen+", 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>";
					loadTableFile(p.tabDataFile);
				}
			}

			if(source == "model") {
				// prod code
				document.getElementById('prodCodeDisplay').innerHTML = "<b>Product Code:</b> "+p.prodCode;
				// top title change
				if(p.model=="frost") {
					document.getElementById('toptitle').innerHTML = "Frost Potential";
				} else {
					document.getElementById('toptitle').innerHTML = ""; 
				}
			}
		}
	}
	
	var modelRadioChoice = getModelChoice();

        url = "frost_loader.jsp?model="+ modelRadioChoice +"&date=" + menu.value + "&state=" + document.FrostReload.state.value;

        url += '&' + Math.floor(Math.random() * 11) + '=' + Math.random();
	
        xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

function getModelChoice() {
/*	if(document.FrostReload.modela.checked) {
		return "frost";
	} else if(document.FrostReload.modelb.checked) {
		return "sheep";
	} else if(document.FrostReload.modelc.checked) {
		return "downy";
	} else if(document.FrostReload.modeld.checked) {
		return "brown";
	} else {
		return "";
	}
*/
	return "frost";
}

function loadForecastEarlier() {
/*	if(document.FrostReload.date.selectedIndex > 0) {
		// set new index
		document.FrostReload.date.selectedIndex = document.FrostReload.date.selectedIndex - 1;
		loadForecastChanges(document.FrostReload.date, "date");
	}*/

    document.getElementById('date').selectedIndex = 0;
    loadForecastChanges(document.getElementById('date'), 'date');
}

function loadForecastLater() {
/*	if(document.FrostReload.date.selectedIndex < 4) {
		// set new index
		document.FrostReload.date.selectedIndex = document.FrostReload.date.selectedIndex + 1;
		loadForecastChanges(document.FrostReload.date, "date");
	}*/

    document.getElementById('date').selectedIndex = 1;
    loadForecastChanges(document.getElementById('date'), 'date');
}

function loadForecastModel() {
    loadForecastChanges(document.FrostReload.date, "model");
}

function loadForecastState() {
    loadForecastChanges(document.FrostReload.date, "state");
}

function zoomOutFull() {
	document.getElementById('state').selectedIndex = 0;
	loadForecastChanges(document.FrostReload.date, "state");
}

function zoomToState(stateValue) {
	var i;
	for(i = 0; i < document.getElementById('state').options.length; i++) {
		if(document.getElementById('state').options[i].value == stateValue) {
			break;
		}
	}
	document.getElementById('state').selectedIndex = i;
	loadForecastState();
}

function func() { } // empty method to handle image button clicks

