// Javascript Code-Beside for Virtual Earth Side Map (homebuying, homedetail, refinesearch, agenthome)

var resultsMapControl = null;
var resultHomes = new Array();
var resultPushPins = new Array();

var selectedHome = null;
var selectedHomeID = "";

var initMap = false;

function pageLoad()
{	
	LoadMyMap();
		
	RefreshMap();
}

function InitSelectedHomeID()
{	
	var homeElements = document.getElementsByTagName("selectedhome");
	if(homeElements.length > 0)
	{
		selectedHomeID = homeElements[0].getAttribute("homepagesid");
		selectedHome = new VELatLong(homeElements[0].getAttribute("latitude"), homeElements[0].getAttribute("longitude"));
	}
}
		
function RefreshMap()
{	
	InitSelectedHomeID();
	
	var homeElements = document.getElementsByTagName('home');
	
	for(var i=homeElements.length-1; i >= 0; i--)
	{
		AddHome(
			homeElements[i].getAttribute('homepagesid'),
			homeElements[i].getAttribute('resultnumber'),
			homeElements[i].getAttribute('latitude'),
			homeElements[i].getAttribute('longitude'),			
			homeElements[i].getAttribute('isfeatured')=='True'?true:false	
		);
	}
	
	if (homeElements.length-1 <= 0)
	{
	    CenterOnLatLongandZoom();
	}
	
	//add address center point if available
	AddAddressCenterPoint();
	
	//move matching home to top of list
	if(selectedHomeID != "")
	{
		for(var i=0; i < resultPushPins.length; i++)
		{
			var resultPushPin = resultPushPins[i];
			
			if(resultPushPin.ID == selectedHomeID)
			{
				var tempPushPin = resultPushPins[resultPushPins.length-1];
				resultPushPins[resultPushPins.length-1] = resultPushPin;
				resultPushPins[i] = tempPushPin;
			}
		}
	}
	
	DrawPushPins();
	
	
	
}

function LoadMyMap()
{
	var mapDiv = document.getElementById('resultsMap');

	if(mapDiv != null)
	{
		//check to see if we have a valid map control
		if(resultsMapControl == null)
		{
			//verify that we have a valid map
			var resultsMapDiv = document.getElementById('resultsMap');
			if(resultsMapDiv == null)
			{
				return;
			}		
		
			resultsMapControl = new VEMap('resultsMap');          
			resultsMapControl.LoadMap();
			resultsMapControl.SetMapStyle('h');
			resultsMapControl.HideDashboard();
			
			initMap = true;
		}
	}
}

function DrawPushPins()
{
	if(resultsMapControl == null)
	{
		return;
	}

	resultsMapControl.DeleteAllPushpins();
		
	//cycle through pushpins and refresh map
	for(i = 0; i < resultPushPins.length; i++)
	{
		var pushpin = resultPushPins[i];
		
		VEPushpin.ShowDetailOnMouseOver = false;
		
		if(pushpin.ID != "")
		{		
			resultsMapControl.AddPushpin(pushpin);
			
			if(resultHomes.length > 1)
			{
				var imgElementParent = document.getElementById(pushpin.ID);
				
				//check if its featured
				//if(pushpin.getAttribute('direct') == true)
				if(pushpin.Details == 'DirectLink')
				{
					imgElementParent.href = './HomeDetail.aspx?hpid='+pushpin.ID;
				}
				else
				{
					imgElementParent.setAttribute("hpid", pushpin.ID);
					imgElementParent.onclick = function(){ ViewDetails(this);interstitial(0,true); };
				}			
				
				var imgElement = imgElementParent.childNodes[0];
				if (pushpin.ID == 'centerpoint')
				{
				    imgElement.setAttribute("alt", addressLocation);
				    imgElement.setAttribute("title", addressLocation);
			    }
			    else
			    {
				    imgElement.setAttribute("alt", "Click To View Details");
				    imgElement.setAttribute("title", "Click To View Details");
				}
				
			}
		}
	}
	
	if(initMap && resultHomes.length > 1)
	{
		resultsMapControl.SetMapView(resultHomes);
		initMap = false;
	}
	
	//init map after refresh
	if(selectedHome != null)
	{		
		
		if(resultHomes.length > 1)
		{
			resultsMapControl.PanToLatLong(selectedHome);
		}
		else
		{
			resultsMapControl.SetCenterAndZoom(selectedHome, 15);
		}
	}
	else if(resultHomes.length > 1)
	{
		resultsMapControl.SetMapView(resultHomes);
	}
	else if((typeof LocationLat != 'undefined') && (typeof LocationLong != 'undefined')
	        && LocationLat != '' && LocationLong != '')
	{
	    var agentCenter = new VELatLong(LocationLat, LocationLong);
	    resultsMapControl.SetCenterAndZoom(agentCenter, 11); 
	}
	
	
	//clear pushpins array
	resultPushPins = new Array();
	resultHomes = new Array();
	selectedHome = null;
	selectedHomeID = "";
}

function AddAddressCenterPoint()
{
    try
    {
        if (null != addressLocation)
        {
            if ((null != addressLocationLat) && (null != addressLocationLong))
            {
                var latlon = new VELatLong(addressLocationLat, addressLocationLong);
            
                var pin;
        	
                pin = new VEPushpin('centerpoint', latlon, 'img/marker.png', null, null, null);

                resultPushPins.push(pin);
            }
        }
    }
    catch(e)
    {
        return 1;
    }
}

function CenterOnLatLongandZoom()
{
//    try
//    {
//        mapCenterLat = mapCenterLocationLat;
//        mapCenterLon = mapCenterLocationLong;
//        
//        if (resultsMapControl && mapCenterLat && mapCenterLon)
//        {
//            var latlon = new VELatLong(mapCenterLat.value, mapCenterLon.value);
//            resultsMapControl.SetCenterAndZoom(latlon, 11);
//        }
//    }
    try
    {
        if (null != mapCenterLocation)
        {
            if ((null != mapCenterLocationLat) && (null != mapCenterLocationLong))
            {
                if (('' != mapCenterLocationLat) && ('' != mapCenterLocationLong))
                {
		            if(resultsMapControl != null)
	                {
	                    //center and zoom to the city level = 11
		                var latlon = new VELatLong(mapCenterLocationLat, mapCenterLocationLong);
		                resultsMapControl.SetCenterAndZoom(latlon, 11);
	                }
                }
            }
        }
    }
    catch(e)
    {
        return 1;
    }
    
        
}

function ViewDetails(senderElement)
{
	var hpID = senderElement.getAttribute("hpid");

	var inputButtons = document.getElementsByTagName("input");
	
	for(var i = 0; i < inputButtons.length; i++)
	{
		var inputHPID = ""+inputButtons[i].getAttribute("hpid");
		
		if(hpID == inputHPID)
		{
			inputButtons[i].click();
			return;
		}		
	}
}


function CenterMap(lat, lon)
{
	if(resultsMapControl != null)
	{
		var latlon = new VELatLong(lat, lon);
		resultsMapControl.PanToLatLong(latlon);
	}
}

function AddHome(id, number, lat, lon, isFeatured)
{
	var isSelected = (id == selectedHomeID);
	
	//alert('id:'+id+', number:'+number+', lat:'+lat+', lon'+lon);
	var latlon = new VELatLong(lat, lon);
		
	var pin;
	
	if(isSelected)
	{   
		if(isFeatured)
		{
			pin = new VEPushpin(id, latlon, 'img/map_icon_clear.gif', null, null, 'mapIcon FeaturedHomeIcon');
		}
		else
		{
		    pin = new VEPushpin(id, latlon, 'img/homeicon.gif', null, null, 'mapIcon SelectedHomeBlankIcon');
		}
	}
	else
	{
		var iconCSS = isFeatured?'FeaturedHomeBlankIcon':'HomeBlankIcon';
		
		if(number == -1)
		{
			pin = new VEPushpin(id, latlon, 'img/map_icon_featuredhome.gif', null, null, 'mapIcon '+iconCSS);
		}
		else
		{
			pin = new VEPushpin(id, latlon, 'icons/numberpushpins/result'+number+'_icon.gif', null, null, 'mapIcon '+iconCSS);
		}
	}

	if(isFeatured)
	{
		pin.Details = 'DirectLink';
	}
	
	//verify that the id isnt already on the list
	findAndDeleteHomesWithID(id);
	
	//add home to lists
	resultHomes.push(latlon);
	resultPushPins.push(pin);

}

function zoomIn()
{
	if(resultsMapControl != null)
	{
		resultsMapControl.ZoomIn();
	}
}

function zoomOut()
{
	if(resultsMapControl != null)
	{
		resultsMapControl.ZoomOut();
	}
}

function findAndDeleteHomesWithID(id)
{
	for(var i = 0; i < resultPushPins.length; i++)
	{
		var resultHome = resultPushPins[i];
		
		if(resultHome.ID == id)
		{
			resultHomes.splice(i, 1);
			resultPushPins.splice(i, 1);
			return;
		}
	}
}

