/************************************************************************
Filename:   surf.js
Used By:    http://www.crh.noaa.gov/grr/marine/surf/
Author:     Steve Wallgren, ITO Grand Rapids, Michigan
Purpose:    This script provides the functions to do the popup forecasts

Version:    1.0 - 22 April 2009

Disclaimer: This software and related documentation was developed by
			National Weather Service employees in the course of their
			official duties. Pursuant to title 17, Section 105 of the
			United States Code this software is not subject to copyright
			protection and therefore may be used, copied, modified and
			distributed without fee or cost. Parties who develop software
			incorporating predominantly NWS developed software must
			include notice as required by title 17, section 403 of the
			United States Code.
			
			NWS provides no warranty, expressed or implied, as to the
			correctness of the furnished software or the suitability for
			any purpose. NWS assumes no responsibility, whatsoever, for
			its use by other parties, about its quality, reliability, or
			any other characteristic. The NWS may change this software
			to meet its mission needs or discontinue its use without
			prior notice.
			
			The software has been tested, but as with any complex software,
			there could be undetected errors. Users who find errors are
			requested to report them to the NWS (w-grr.webmaster@noaa.gov).
			The NWS has limited resources to assist non-NWS users and is
			not obligated to fix reported problems; however, we make an
			attempt to fix reported problems whenever possible.
************************************************************************/

// This function sets up all the advanced functionality of the page.
function initializePage()
{
	// Check browser capabilities, abort if the browser is
	// too ancient to deal with this code.
	if(!document.getElementById || !document.createTextNode)
	{
		return false;
	}
	
	// The popup should already be hidden, but this still
	// needs to be done so popup works on first mouseover.
	document.getElementById("popup").style.display = "none";

	// Set up hooks into the area nodes from the click map.
	clickHook("clickMason", "MIZ037", objMIZ037);
	clickHook("clickOceana", "MIZ043", objMIZ043);
	clickHook("clickMuskegon", "MIZ050", objMIZ050);
	clickHook("clickOttawa", "MIZ056", objMIZ056);
	clickHook("clickAllegan", "MIZ064", objMIZ064);
	clickHook("clickVanBuren", "MIZ071", objMIZ071);
	
	// Hook in the mouse follow function.
	document.onmousemove = mouseFollow;
	
	// Display instructions for the JavaScript enabled.
	document.getElementById("scriptOffInstructions").style.display = "none";
	document.getElementById("scriptOnInstructions").style.display = "block";
}

// Hook into clickmap area
function clickHook(areaID, zone, zonedata)
{
	var areaObj = document.getElementById(areaID);
	// Only hook if area exists.
	if(areaObj)
	{
		areaObj.onmouseover = function() { mouseOver(areaObj.title, zone, zonedata); };
		areaObj.onmouseout = mouseOut;
	}
	return areaObj;
}

// Display the appropriate forecast.
function mouseOver(county, zone, zonedata)
{
	var surfPopup = document.getElementById("popup");
	if( surfPopup.style.display == "none" )
	{
		document.getElementById("popupCounty").appendChild(document.createTextNode(county + " Recreational Beach Forecast"));
		document.getElementById("ripPopup").appendChild(document.createTextNode(zonedata.rip));
		document.getElementById("wavePopup").appendChild(document.createTextNode(zonedata.wave));
		document.getElementById("waterTPopup").appendChild(document.createTextNode(zonedata.waterT));
		document.getElementById("skyPopup").appendChild(document.createTextNode(zonedata.sky));
		document.getElementById("maxTPopup").appendChild(document.createTextNode(zonedata.maxT));
		document.getElementById("windPopup").appendChild(document.createTextNode(zonedata.wind));
		document.getElementById("uviPopup").appendChild(document.createTextNode(zonedata.uvi));
		surfPopup.style.display = "block";
	}
}

// Hide the forecast and reset the popup contents.
function mouseOut()
{
	document.getElementById("popup").style.display = "none";
	emptyNest(document.getElementById("popupCounty"));
	emptyNest(document.getElementById("ripPopup"));
	emptyNest(document.getElementById("wavePopup"));
	emptyNest(document.getElementById("waterTPopup"));
	emptyNest(document.getElementById("skyPopup"));
	emptyNest(document.getElementById("maxTPopup"));
	emptyNest(document.getElementById("windPopup"));
	emptyNest(document.getElementById("uviPopup"));
}

// Removes all children from a node
function emptyNest(parent)
{
	while(parent.hasChildNodes())
	{
		parent.removeChild(parent.lastChild);
	}
}

// Make popup div follow the mouse.
function mouseFollow(event)
{
	// Need this first line to handle IE's non-standard ways.
	if(!event) { var event = window.event; }
	var cordX = (parseInt(event.clientX) + 5);
	var cordY = (parseInt(event.clientY) + 5);
	var limitY = (parseInt(window.outerHeight) - 460);
	var surfPopup = document.getElementById("popup");
	if(cordY > limitY) { cordY = limitY; }
	surfPopup.style.top = cordY + "px";
	surfPopup.style.left = cordX + "px";
}

// This function adds a function onto the window's onload event handler,
// without overriding anything that was already there.
function addOnloadEventHandler(newHandler)
{
	var oldHandler = window.onload;
	if( typeof window.onload != 'function')
	{
		window.onload = newHandler;
	}
	else
	{
		window.onload = function() { oldHandler(); newHandler(); }
	}
}

// Put the page initializing function into the onload event handler.
addOnloadEventHandler(initializePage);