var map = null;
var locationPin = null;  
var coordArray = null;
var shapeArray = Array();
var searchlat = 0;
var searchlon = 0;
function setMapCountry(theCountry)
{                  
	try
	{
		results = map.Find("",theCountry,null,null,0,1,false,false,true,true);
	}
    catch(e)
    {
	}
}

function setMapCountryWithCoords(theCountry)
{                  
	try
	{
		results = map.Find("",theCountry,null,null,0,1,false,false,true,true,countryFindCallback);
	}
    catch(e)
    {
	}
}


function getHighlightsForProperty(lat,lon)
{
	var tl = new VELatLong(parseFloat(parseFloat(lat) + 0.25),parseFloat(parseFloat(lon) - 0.25));
	// var tr = new VELatLong(parseFloat(parseFloat(lat) + 0.10),parseFloat(parseFloat(lon) + 0.10));
	// var bl = new VELatLong(parseFloat(parseFloat(lat) - 0.10),parseFloat(parseFloat(lon) - 0.10));
	var br = new VELatLong(parseFloat(parseFloat(lat) - 0.25),parseFloat(parseFloat(lon) + 0.25));
	// var rect = new VELatLongRectangle(tl,br,tr,bl);          
	searchlat = lat;
	searchlon = lon;
	setTimeout('retrySearch()',3000); 
}

function retrySearch()
{
	var tl = new VELatLong(parseFloat(parseFloat(searchlat) + 0.25),parseFloat(parseFloat(searchlon) - 0.25));
	var br = new VELatLong(parseFloat(parseFloat(searchlat) - 0.25),parseFloat(parseFloat(searchlon) + 0.25));
	var rect = new VELatLongRectangle(tl,br);  
	results = map.Find("cultural",map.GetMapView(),null,null,0,4,false,true,false,true,propertyHighlightSearchCallback);	
}             

function propertyHighlightSearchCallback(layer, resultsArray, places, hasMore, veErrorMessage)
{   
	var resultml = ""; // "<li><h2>Local Area Highlights</h2></li>";
	shapeArray = Array();
             
	if (resultsArray!= null)
	{
	  	$.each(resultsArray,
		  	function(intIndex, thing)
			{                                                                                           
				var linkText = "";
				if (thing.Name.length > 45)
				{
					linkText = thing.Name.substring(0,42) + "...";
				}              
				else
				{
					linkText = thing.Name;
				}
				resultml += "<li class=\"marker"+intIndex+"\"><a href=\"javascript:centerMapToHighlight("+intIndex+");\" onmouseover=\"showInfoForHighlight("+intIndex+");\" onmouseout=\"hideInfoForHighlight("+intIndex+");\" >"+linkText+"</a></li>";			
	            var lahPoint = new VELatLong(thing.LatLong.Latitude,thing.LatLong.Longitude);
	            var locationPin = new VEShape(VEShapeType.Pushpin,lahPoint);
	            locationPin.SetCustomIcon('/img/pins/pin_'+intIndex+'.png');
				locationPin.SetTitle(thing.Name);
				locationPin.SetDescription(thing.Description);
	            locationPin.SetMoreInfoURL(intIndex);			
				shapeArray.push(locationPin);
	            map.AddShape(locationPin);
			}
		);  
	}
	else
	{
	   resultml+= "<li>No highlights available for this area.</li>";  
	}
  
   $("#plahlist").html(resultml);
	
	var zoom = map.GetZoomLevel();     
    map.SetZoomLevel(zoom - 1);        
}

  
    
function highlightSearchCallback(layer, resultsArray, places, hasMore, veErrorMessage)
{                       
	var resultml = "<ul></li>";
	var jsTxt = "";
	shapeArray = Array();
	if (resultsArray != null)
	{
		$.each(resultsArray,
		  	function(intIndex, thing)
			{                                                                                           
				var linkText = "";
				if (thing.Name.length > 45)
				{
					linkText = thing.Name.substring(0,42) + "...";
				}              
				else
				{
					linkText = thing.Name;
				}
				resultml += "<li class=\"marker"+intIndex+"\"><a href=\"javascript:centerMapToHighlight("+intIndex+");\" onmouseover=\"showInfoForHighlight("+intIndex+");\" onmouseout=\"hideInfoForHighlight("+intIndex+");\" >"+linkText+"</a></li>";			
	            var lahPoint = new VELatLong(thing.LatLong.Latitude,thing.LatLong.Longitude);
	            var locationPin = new VEShape(VEShapeType.Pushpin,lahPoint);
	            locationPin.SetCustomIcon('/img/pins/pin_'+intIndex+'.png');
				locationPin.SetTitle(thing.Name);
				locationPin.SetDescription(thing.Description);
	            locationPin.SetMoreInfoURL(intIndex);			
				shapeArray.push(locationPin);
	            map.AddShape(locationPin);
			}
		); 
	}            
	resultml+="</ul>";
	 $("#columnOneR").html(resultml);
	
	var zoom = map.GetZoomLevel();     
	
    map.SetZoomLevel(zoom - 1);        
	
}

function centerMapToHighlight(num)         
{
	for(var i = 0;i<shapeArray.length;++i)
	{         
		if(shapeArray[i].GetMoreInfoURL() == num)
		{
			map.SetCenter(shapeArray[i].GetIconAnchor());
		}
	}
}

function showInfoForHighlight(num)
{
	for(var i = 0;i<shapeArray.length;++i)
	{         
		if(shapeArray[i].GetMoreInfoURL() == num)
		{
			map.ShowInfoBox(shapeArray[i]);
			//now check to see if we need to center the map?
		}
	}
}   

function hideInfoForHighlight(num)
{
	for(var i = 0;i<shapeArray.length;++i)
	{         
		if(shapeArray[i].GetMoreInfoURL() == num)
		{
			map.HideInfoBox(shapeArray[i]);
		}
	}
}


function countryFindCallback(layer, resultsArray, places, hasMore, veErrorMessage)
{       
	document.getElementById("coord").value = map.GetCenter();
}
 function GetMap()
 {
    map = new VEMap('myMap');
    map.SetDashboardSize(VEDashboardSize.Tiny);
	map.HideScalebar();
	map.onLoadMap = mapLoaded;
	map.LoadMap();
	if (coordArray == null)
	{            
		var lat = 37.0625;
		var lon = -95.677068;
		var startPoint = new VELatLong(lat, lon);
		map.SetCenterAndZoom(startPoint, 1);      		
	}
	else
	{
	   	var lat = coordArray[0];
		var lon = coordArray[1];
		var startPoint = new VELatLong(lat, lon);
		map.SetCenterAndZoom(startPoint, 12);
	} 
}   

 function GetLocalMap(lat,lon)
 {          
    map = new VEMap('localMap');
	map.SetDashboardSize(VEDashboardSize.Tiny);
	map.onLoadMap = addLahPoints;
	try
	{
		map.LoadMap();		
	}                 
	catch(e)
	{                      
		setTimeout('addLahPoints()',1000);
	}
    var startPoint = new VELatLong(lat, lon);
	try
	{   
		map.HideScalebar();
	    map.SetCenterAndZoom(startPoint, 11);		
	}
	catch(e)
    {
		
	}   
   	var locationPin = new VEShape(VEShapeType.Pushpin,startPoint);
	locationPin.SetCustomIcon("/img/pins/pin_sheraton.png");
	map.AddShape(locationPin);

 }

function GetLocalMapUser(lat,lon)
{          
	map = new VEMap('localMap');
	map.SetDashboardSize(VEDashboardSize.Tiny);
	map.HideScalebar();
	map.LoadMap();
    var startPoint = new VELatLong(lat, lon);
    map.SetCenterAndZoom(startPoint, 11);      
	var tl = new VELatLong(parseFloat(parseFloat(lat) + 0.25),parseFloat(parseFloat(lon) - 0.25));
	// var tr = new VELatLong(parseFloat(parseFloat(lat) + 0.10),parseFloat(parseFloat(lon) + 0.10));
	// var bl = new VELatLong(parseFloat(parseFloat(lat) - 0.10),parseFloat(parseFloat(lon) - 0.10));
	var br = new VELatLong(parseFloat(parseFloat(lat) - 0.25),parseFloat(parseFloat(lon) + 0.25));
	// var rect = new VELatLongRectangle(tl,br,tr,bl);
	var rect = new VELatLongRectangle(tl,br);
	results = map.Find("cultural",rect,null,null,0,12,false,true,false,true,highlightSearchCallback);
	var locationPin = new VEShape(VEShapeType.Pushpin,startPoint);
	locationPin.SetCustomIcon("/img/pins/pin_user.png");
	map.AddShape(locationPin);
 }

function GetMapHighlights()
 {
    map = new VEMap('myMap');
    map.SetDashboardSize(VEDashboardSize.Tiny);
	map.HideScalebar();
	map.onLoadMap = mapLoaded;
	map.LoadMap();
	if (coordArray == null)
	{            
		var lat = 37.0625;
		var lon = -95.677068;
		var startPoint = new VELatLong(lat, lon);
		map.SetCenterAndZoom(startPoint, 1);      		
	}
	else
	{
	   	var lat = coordArray[0];
		var lon = coordArray[1];
		var startPoint = new VELatLong(lat, lon);
		map.SetCenterAndZoom(startPoint, 12);
	}  
	map.AttachEvent("onclick", showLocationPin);
}  

function GetMapHighlightsEdit()
 {
    map = new VEMap('myMap');
    map.SetDashboardSize(VEDashboardSize.Tiny);
	map.HideScalebar();
	map.onLoadMap = mapLoadedEdit;
	map.LoadMap();
	if (coordArray == null)
	{            
		var lat = 37.0625;
		var lon = -95.677068;
		var startPoint = new VELatLong(lat, lon);
		map.SetCenterAndZoom(startPoint, 1);      		
	}
	else
	{
	   	var lat = coordArray[0];
		var lon = coordArray[1];
		var startPoint = new VELatLong(lat, lon);
		map.SetCenterAndZoom(startPoint, 12);
	}  
	map.AttachEvent("onclick", showLocationPin);
}     

                          
function showLocationPin(e)
{
	map.DeleteAllShapes();
	var pixel = new VEPixel(e.mapX - 10,e.mapY-16);
	var touchLocation = map.PixelToLatLong(pixel);
	locationPin = new VEShape(VEShapeType.Pushpin,touchLocation);
	locationPin.SetCustomIcon("/img/pins/pin_user.png");
	map.AddShape(locationPin);
	document.getElementById("coord").value = touchLocation;
	// document.getElementById("coord").value = map.GetCenter();
}  

function disableMap(e)
{
	return true;
}

function dropPinEdit(lat,lon)
{
	map.DeleteAllShapes();
	var touchLocation = new VELatLong(lat,lon);
	locationPin = new VEShape(VEShapeType.Pushpin,touchLocation);
	locationPin.SetCustomIcon("/img/pins/pin_user.png");
	map.AddShape(locationPin);
}   

function dropPin(lat,lon)
{
	map.DeleteAllShapes();
	var touchLocation = new VELatLong(lat,lon);
	locationPin = new VEShape(VEShapeType.Pushpin,touchLocation);
	locationPin.SetCustomIcon("/img/pins/pin_sheraton.png");
	map.AddShape(locationPin);
}   

function mapLoaded()
{   
	if (coordArray != null)
	{                                    
		var startPoint = new VELatLong(coordArray[0], coordArray[1]); 
	  	map.SetCenterAndZoom(startPoint, 12);
		dropPin(coordArray[0],coordArray[1]);
	}
}  

function mapLoadedEdit()
{   
	if (coordArray != null)
	{                                    
		var startPoint = new VELatLong(coordArray[0], coordArray[1]); 
	  	map.SetCenterAndZoom(startPoint, 12);
		dropPinEdit(coordArray[0],coordArray[1]);
	}
}
