/// <reference path="jquery.js" />
/// <reference path="http://www.google.com/jsapi?key=ABQIAAAA0Kmueb3wVLHqnaNBSwQ-NRQkSsWcx1b4Sk3o6TxC_TgE-u3iTxQQ4BHiBqhSAvLKLHHL9OTXZ30OJQ" />
var geoXml;
var Icon;
var slider;
var sliderRange = 245;
var search_string = '';
var pest_type = '';
var region = '';
var country = '';
var pub_date = '1';

function init_mapper_page()
{
	// DOM method for binding an event.
	if (window.addEventListener) 
	{
		window.addEventListener("load", initMap, false);
	}	
	
   	// IE exclusive method for binding an event.
   	else if (window.attachEvent)
   	{ 
		window.attachEvent("onload", initMap);
  	}


	// Enable use of google maps API.
   	google.load("maps", "2");
	google.setOnLoadCallback(initMap); 
	initMap();
}

function sliderInit() 
{
	slider = YAHOO.widget.Slider.getHorizSlider("slider-bg", "slider-thumb", 0, sliderRange);
	slider.subscribe("change", sliderValueChange); 
}

function sliderValueChange(currentValue)
{
	var dateDiff2 = (sliderRange - currentValue);
	<!-- Execute updateDate aynchronously, on return control moves to updateStartDate. --> 
	DWREngine._execute(_cfscriptLocation, null, 'updateDate', dateDiff2, updateStartDate);
}
	
<!-- The inner HTML is modified to display the new date range. --> 	
function updateStartDate(result)
{
	document.getElementById("date_range").innerHTML = result;
	buildMap();
}

function resetParameters()
{
//	search_string = '';
//	pest_type = '';
//	region = '';
//	country = '';
}	

function resetUIParameters()
{
	document.getElementById("search_param").value = '';
	document.getElementById("typeOfPestSelected").value = '';
	document.getElementById("regionSelect").value = '';
	document.getElementById("countrySelect").value = '';
}	


function buildMap()
{
	user_id_obj = document.getElementById("search_user_id");
	if(user_id_obj == null)
		user_id = '-'
	else
		user_id = user_id_obj.value;
	

	DWREngine._execute(_cfscriptLocation, null, 'searchFeedDatabase', search_string, pest_type, region, country, pub_date, user_id, buildMapResults);
	document.getElementById("panel_details_content").style.background = "#ffffff";
	document.getElementById("missing_geo_results").innerHTML = "";
	document.getElementById("panel_details_header_content").innerHTML = "<div style='height:15px;'><table height='15px'><tr><td width='15px'> <img src='images/mozilla_blu.gif' style='width: 12px;'/></td><td><span class=custom_message style='vertical-aign:middle'>Search in progress...</span></td></table></div>";
}	

function search()
{
	search_string = document.getElementById("search_param").value;
	buildMap();
	resetUIParameters();
	document.getElementById("search_param").value = search_string;
	resetParameters();
}	

function search_pest()
{
	pest_type = document.getElementById("typeOfPestSelected").value;
	buildMap();
	resetUIParameters();
	document.getElementById("typeOfPestSelected").value = pest_type;
	resetParameters();
}	

function search_pub_date()
{
	pub_date = document.getElementById("dateRange").value;
	buildMap();
	resetUIParameters();
	document.getElementById("dateRange").value = pub_date;
	resetParameters();
}	



function search_region()
{
	region = document.getElementById("regionSelect").value;
	buildMap();
	resetUIParameters();
	document.getElementById("regionSelect").value = region;
	resetParameters();
}		

function search_country()
{
	country = document.getElementById("countrySelect").value;
	buildMap();
	resetUIParameters();
	document.getElementById("countrySelect").value = country;
	resetParameters();
}	

<!-- The inner HTML is modified to display the new date range. --> 	
function buildMapResults(result)
{
	var detail = "";
	var feedsMissingGeo = "";
	var geoMissingItemCount = 0;
	var itemCount = 0;
	var bounds = new GLatLngBounds();
	
 	feedsMissingGeo = "<table width='1008px'>";
	document.getElementById("panel_details_header_content").innerHTML = "<table width='1006px' class=custom_header_cell id ='details_header'><tr><td class=custom_header_cell width='50px'>&nbsp;Type</td><td class=custom_header_cell width='103px'>&nbsp;Published Date</td><td class=custom_header_cell width='304px'>&nbsp;Title</td><td class=custom_header_cell>&nbsp;Description</td></tr></table>";

	if(map != null)
 	map.clearOverlays();

	for(i=0; i<result.length; i++)
	{
		if(result[i].GEO_PRESENT_FLAG == 1)
		{
			var point = new GLatLng(result[i].LAT, result[i].LONG);
			var description = result[i].DESCRIPTION;
			if(description.length > 200)
			{
				for(j=100; j>=0;j--)
				{
					if(description.charAt(j)==' ') break;
				}

				description = description.substr(0, j) + "... ";
			}

			detail = "";
			detail = detail + "<div class=custom_tip>";
			detail = detail + "<b>Type:&nbsp;</b>" + result[i].TYPE.toUpperCase() + "<br>";
			detail = detail + "<b>Published Date:&nbsp;</b>" + result[i].PUB_DATE.substr(0,10) + "<br>";
			detail = detail + "<b>Title:&nbsp;</b>" + result[i].TITLE + "<br>";
			detail = detail + "<b>Description:&nbsp;</b>" + description + "<br>";
			detail = detail + "<b><a target='_blank' style='font-size: 7pt;' href=" + result[i].LINK + "> More Details</a>";
			detail = detail +  "</div>"
			var iType = result[i].TYPE;

			var marker = createInfoMarker(point,iType,detail);
			bounds.extend(point);
			if(map != null)
				map.addOverlay(marker);
			itemCount++;
		}
		else
		{
			var description = result[i].DESCRIPTION;
			if(description.length > 100)
			{
				for(j=100; j>=0;j--)
				{
					if(description.charAt(j)==' ') break;
				}

				description = description.substr(0, j) + "... ";
			}


			feedsMissingGeo = feedsMissingGeo + "<tr>";
			feedsMissingGeo = feedsMissingGeo + "<td class=custom_cell width='47px'>";
			feedsMissingGeo = feedsMissingGeo + result[i].TYPE.toUpperCase();
			feedsMissingGeo = feedsMissingGeo + "</td>";
			feedsMissingGeo = feedsMissingGeo + "<td class=custom_cell width='100px'>";
			feedsMissingGeo = feedsMissingGeo + result[i].PUB_DATE.substr(0,10);
			feedsMissingGeo = feedsMissingGeo + "</td>";
			feedsMissingGeo = feedsMissingGeo + "<td class=custom_cell width='300px'>";
			feedsMissingGeo = feedsMissingGeo + "<a target='_blank' style='font-size: 7pt;' href=" + result[i].LINK + ">"  + result[i].TITLE + "</a>";
			feedsMissingGeo = feedsMissingGeo + "</td>";
			feedsMissingGeo = feedsMissingGeo + "<td class=custom_cell>";
			feedsMissingGeo = feedsMissingGeo + description;
			feedsMissingGeo = feedsMissingGeo + "</td>";
			feedsMissingGeo = feedsMissingGeo + "</tr>";
			geoMissingItemCount++;
		}
	}
	
	if(geoMissingItemCount!=0)
	{
		feedsMissingGeo = feedsMissingGeo + "</table>";
		document.getElementById("panel_details_content").style.background = "#FEFFAB";
	}
	else
	{
		feedsMissingGeo = "";
		document.getElementById("details_header").style.visibility = "hidden";
	}
	
	if(geoMissingItemCount == 0 && itemCount == 0)
	{
		feedsMissingGeo = "<div class=custom_tip>Search Results: No results found for the given criteria.</div>";
		if(map != null)
			map.setCenter(new GLatLng(10.200000, 8.100000), 2);
	}
	else
	{
		if(map != null && bounds!=null)
			map.setCenter(bounds.getCenter()); 
	}
	
	document.getElementById("missing_geo_results").innerHTML = feedsMissingGeo;
	result = null;
}


// Function to initialize the map if browser is compatible.
function initMap()
{
	if(map == null) 
	{
		map = new GMap(document.getElementById("maps"));
		map.addControl(new GLargeMapControl());
		map.addControl(new GMapTypeControl());
		map.setCenter(new GLatLng(10.200000, 8.100000), 2, G_NORMAL_MAP);
		buildMap();
		google.setOnLoadCallback(buildMap);
	}

}

function createInfoMarker(point, iType , detail ) 
{
	Icon = new GIcon(G_DEFAULT_ICON);

	if (iType == "insect")
	{
		Icon.image = "http://pestmapper.org/version2/images/1.png";
	}
	else if (iType == "plant")
	{
		Icon.image = "http://pestmapper.org/version2/images/2.png";
	}
	else if (iType == "weed")
	{
		Icon.image = "http://pestmapper.org/version2/images/3.png";
	}
	else
	{
		Icon.image = "http://pestmapper.org/version2/images/4.png";
	}
   
	Icon.iconSize = new GSize(11, 20);
	Icon.shadowSize = new GSize(34, 22);
	Icon.iconAnchor = new GPoint(11, 11);
	Icon.infoWindowAnchor = new GPoint(5, 2);
					
	var marker = new GMarker(point , Icon);
				 
	GEvent.addListener(marker, "mouseover", function(){marker.openInfoWindowHtml(detail);});
	GEvent.addListener(marker, "click", function(){marker.openInfoWindowHtml(detail);});
	return marker;
}