/*!
	\file Map_Widget.js
	\author Alex McClung, SSAI
	
	Contains the basic JavaScript functions that the map widget (Map_Widget.swf) needs and uses.
	
	JavaScript functions that are called from ActionScript:
	-------------------------------------------------------
		SWF_Loaded() - called when the SWF (NOT the map image) is finished loading
		
		MapImage_Loaded() - called when the map image is finished loading
		
		UpdateForm(n,s,w,e) - called when a new area is selected in the widget; intended to let JavaScript update a form
							  somewhere on the page with the new lat/long values
							  
		ClearRegion() - called when the 'clear' button is pressed; intended to let JavaScript clear the lat/long
					  values for the selected region in the form
		
		
	
	
	ActionScript functions that can be called from JavaScript:
	----------------------------------------------------------
		LoadMapImage(url) - Loads an image to use as the widget's map
		
		SelectRegion(n,s,w,e) - Select a new region using lat/long coordinates
		
		MouseEvent(e) - can be called when the user rolls over (or out of) the widget's stage area; 'e' should take on
						one of two values: "over" or "out"; used to show/hide the UI buttons
		
		SetTitle(t) - sets the title that is displayed at the bottom of the widget (default: "Data Browser")
		
		HideHelpIcon() - hides the help icon in the upper-right corner
		
		Resize(width,height) - resizes the widget to a new width and height; to be used when EMBEDDING the widget
							   on the stage with a size that's different than the standard 480x240
*/


/***********************************************
********* STEP 1: Determine widget size ********
***********************************************/

/********* Determine Widget Dimensions *********/
var DEFAULT_WIDGET_WIDTH  = 480;
var DEFAULT_WIDGET_HEIGHT = 240;
var map_widget = -1;
var map_widget_id = "neo_map_widget";

// Option 1: User supplies an aspect ratio
//			 widget height and width are derived from the aspect ratio:
//				if the user supplies a width, the height is derived from it using the ratio,
//				if the user supplies a height, the width is derived from it using the ratio,
//				if the user supplies neither, the height is derived from the default width and the aspect ratio
if(widget_aspect_ratio != null)
{
	if(widget_width != null)
		var widget_height = Math.round(widget_width / widget_aspect_ratio);
	else if(widget_height != null)
		var widget_width = Math.round(widget_height * widget_aspect_ratio);
	else
	{
		var widget_width = DEFAULT_WIDGET_WIDTH;
		var widget_height = Math.round(widget_width / widget_aspect_ratio);
	}
}
// Option 2: The user provides height and/or width, but no aspect ratio
//			 widget dimensions and aspect ratio are derived from the user-defined dimension(s), using default values for undefined properties
//				if the user supplies a width and height, the aspect ratio is derived
//				if the user supplies a width and no height, the aspect ratio is derived using the default height
//				if the user supplies a height and no width, the aspect ratio is derived using the default width
//				otherwise, default values are used for the height and width, and the aspect ratio is derived from them
else
{
	if(widget_width == null)
		var widget_width = DEFAULT_WIDGET_WIDTH;
		
	if(widget_height == null)
		var widget_height = DEFAULT_WIDGET_HEIGHT;
	
	var widget_aspect_ratio = widget_width / widget_height;
}

/***********************************************
* STEP 2: Write the HTML for embedding the SWF *
***********************************************/

// First make sure the user has a NEO-compatable browser
/*if(user_platform < 0 || user_browser < 0 || !accepted_platforms[user_platform][user_browser])
{
	switch(platforms[user_platform])
	{
		case "OS X": document.write('<img src="/images/map_widget/static/browsers_osx.gif" alt="" width="'+widget_width+'" border="0" />'); break;
		case "Windows": document.write('<img src="/images/map_widget/static/browsers_windows.gif" alt="" width="'+widget_width+'" border="0" />'); break;
		case "Linux": document.write('<img src="/images/map_widget/static/browsers_linux.gif" alt="" width="'+widget_width+'"  border="0" />'); break;
		case "Unix": document.write('<img src="/images/map_widget/static/browsers_unix.gif" alt="" width="'+widget_width+'" border="0" />'); break;
		
		default: document.write('<img src="/images/browsers_unknown.gif" alt="" width="'+widget_width+'" border="0" />'); break;
	}
}
else if(!hasFlash8) // Now make sure they have the flash 8 player (swf_detector.js should be included before including this script)
{
	document.write("<a href='http://www.macromedia.com/go/getflashplayer' target='_blank'><img src='/images/map_widget/static/getflashplayer.gif' alt='Get Flash Player 8' border='0' width='" + widget_width + "'/></a>");
}
else // in the clear: plop in the HTML for the widget
{
	var swf_str = '\
	<object  width="' + widget_width + '" height="' + widget_height + '" id="' + map_widget_id + '" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0">\
	  <param name="allowScriptAccess" value="sameDomain" />\
	  <param name="movie" value="/common/swf/Map_Widget.swf" />\
	  <param name="quality" value="high" />\
	  <param name="bgcolor" value="#ffffff" />\
	  <embed width="' + widget_width + '" height="' + widget_height + '" name="' + map_widget_id + '" src="/common/swf/Map_Widget.swf" quality="high" bgcolor="#ffffff" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />\
	</object>';
	
	//alert(swf_str);
	//document.write(swf_str);
}*/


/***********************************************
********** STEP 3: Get widget Instance *********
***********************************************/

/*!
	\brief Retrieves a reference to an embedded swf object
	\param movie_name: the id (name) of the swf object to retrieve
	\returns HTMLElement Object
*/
function GetMovie(movie_name)
{
	var isIE = navigator.appName.indexOf("Microsoft") != -1;
	return (isIE) ? window[movie_name] : document[movie_name];
}

/**************************************************************\
|**** STEP 4: Create Callback Functions for the SWF to Use ****|
\**************************************************************/

/*!
	\brief Called by ActionScript when the SWF is finished loading.
	This function will load the current month's blue marble image. You can override this function
	by re-defining it after you include this .js file. You should do this only if you want to 
	load a different background image or change the title string at the bottom of the swf.
*/
function SWF_Loaded()
{
	// Get the map widget instance and give it some mouseover functionality
	map_widget = GetMovie(map_widget_id);
	map_widget.onmouseover = WidgetMouseOver;
	map_widget.onmouseout = WidgetMouseOut;
	
	// Figure out what month it is
	//var d = new Date();
	//var int_m = d.getMonth()+1;
	
	// This is just a default image to load if in case this is not overridden
	var fn = "/images/map_widget/bluemarble/08x5400x2700.jpg";
	
	// Tell the map widget what its dimensions are
	map_widget.Resize(widget_width, widget_height);
	
	// Load the new image
	map_widget.LoadMapImage(fn);
}


/*!
	\brief Called by ActionScript when the map image is loaded
	\param width - the original width of the image that was loaded into the widget
	\param height - the original height of the image that was loaded ingo the widget
*/
function MapImage_Loaded(width, height)
{
	// Figure out the aspect ratio and resize the widget if needed
	widget_aspect_ratio = width / height;
	widget_height = widget_width / widget_aspect_ratio;
	
	// Resize the widget element
	map_widget.style.height = widget_height + "px";
}

/*!
	\brief Loads a new image in the widget
	\param url - String  URL of the new image's location
	\returns nothing
	
	This is not actually called by the widget itself.
*/
function LoadMapImage(url)
{
	map_widget.LoadMapImage(url);
}

/*!
	\brief Called when the user drags a rectangle in the widget window; new north, south, west, and east values are filled in the regional search inputs
	\returns nothing
*/
function UpdateForm(n, s, w, e)
{
	// Get the form and fill in the values
	var form = get_form('mapForm');
	if(!form)
		return false;
	
	form.maxLat.value = !isNaN(n) ? n : "";
	form.minLat.value = !isNaN(s) ? s : "";;
	form.minLon.value = !isNaN(w) ? w : "";;
	form.maxLon.value = !isNaN(e) ? e : "";;
	
	// Select 'Regional Search' from the search parameters drop-down menu
	var f = get_form(widget_form_name);
	var c = f.coverage;
	c.selectedIndex = 1;
	
	// Call to ChangeCoverage to update the form - this is defined in NEO.js
	ChangeCoverage();
	
	// Call to ChangeDates to update form, also in NEO.js
	ChangeDates();
}

/*!
	\brief Called when the user clears the rubber-band box in the widget
	\returns nothing
*/
function ClearRegion() {
	var form = get_form('mapForm');
	if(form) {
		form.reset();
	}
	ChangeCoverage();
}

/*!
	\brief Sends user-entered north, south, west, and east regional search values to the map
	\returns nothing
*/
function UpdateMap()
{
	var form = get_form('mapForm');
	if(!form)
		return false;
	
	var n = form.maxLat.value;
	var s = form.minLat.value;
	var w = form.minLon.value;
	var e = form.maxLon.value;
	
	map_widget.SelectRegion(n,s,w,e);
}

/*!
	\brief Mouse over event
	\returns nothing
*/
function WidgetMouseOver()
{
	map_widget.MouseEvent("over");
}

/*!
	\brief Mouse out event
	\returns nothing
*/
function WidgetMouseOut()
{
	map_widget.MouseEvent("out");
}