///////////////////////////////////
//////////////////////////////////
// All rights reserved to Amadot(ZBN) Ltd. (c) 
// Google Maps handling version 2.0
// search the map
// last update -  19/02/11
/////////////////////////////////
MAPS.Search = {};
MAPS.Search.searcher = null; //the search object
MAPS.Search.marker = null;  //the marker which shows the search result
MAPS.Search.markerOptions = 
{
	icon: MAPS.Icons.arrow.mapIcon //setting the icon for the marker
}
MAPS.Search.eventListener = null;
MAPS.Search.searchMenuDivColapseObject = null;

MAPS.Search.search = function()
{
	var what2search = document.forms.searchForm.what2search.value.replace(/^\s+|\s+$/, ''); 
	if (what2search == '')
	{
		alert('Please search for something...');
		return;	
	}
	
	var searchType = '';
	
	///////////////////////////////////////////////////////
	// map search
	if (document.forms.searchForm.searchType[0].checked)
	{
		searchType = document.forms.searchForm.searchType[0].value;
		if (City.name === "paris")
			what2search += ",Paris,France";
		else if (City.name === "barcelona")
			what2search += ",Barcelona,Spain";
		else if (City.name === "Israel")
			what2search += ",Israel";
		
		MAPS.Search.searcher.getLatLng(what2search, function(point)
		{
			if (!point)
			{
				alert(what2search + ' was not found!');
				return;	
			}
			
			if (MAPS.Search.marker != null)
			{
				google.maps.Event.clearListeners(MAPS.Search.marker,"click");
				MAPS.mapObj.removeOverlay(MAPS.Search.marker);
				MAPS.Search.marker = null;
			}
			
			MAPS.Search.marker = new google.maps.Marker(point,MAPS.Search.markerOptions);
			MAPS.mapObj.addOverlay(MAPS.Search.marker);
			MAPS.Search.marker.openInfoWindowHtml(what2search);
			
			MAPS.mapObj.setCenter(point,MAPS.mapObj.getZoom());
			//add the event listener
			google.maps.Event.addListener(MAPS.Search.marker,"click",function()
			{
				MAPS.mapObj.removeOverlay(MAPS.Search.marker);	
			});
			
		});
		
		return false;
	}
	
	//////////////////////////////////////////////////////////////////////////
	// marker search
	searchType = document.forms.searchForm.searchType[0].value;	
	var googleMarker = null;
	var resultsArr = MAPS.Markers.searchMarker(what2search);
	if (resultsArr.length == 0)
		alert(what2search + ' was not found');
	else //places found
	{
		var html = "<h4>"+resultsArr.length+" Markers found: <a href='javascript:MAPS.Forms.searchResults.hide();' class='resultsClose'>(Close)</a></h4>";
		html += "<div id='searchResultsDet'>";
		for (var i=0;i<resultsArr.length;i++)
		{
			var num = resultsArr[i].num;
			html += "<h5 class='resultsNumbers'>"+(i+1)+".<a href='javascript:MAPS.Markers.arr["+num+"].showInfo()' class='resultsLinks'> " + resultsArr[i].name+"</a></h5><br style='line-height:5px;'/>";
		}
		html += "</div>";
		document.getElementById(MAPS.DivNames.searchResults).innerHTML = html;
		MAPS.Forms.searchResults.show();
		MAPS.Search.searchMenuDivColapseObject.calcSizes();
		MAPS.Search.searchMenuDivColapseObject.expand();
	}
	
	return false;
}

