//
var map;
var geocoder = null;
var addressMarker;
var listings;
var pointersArray = [];
var listingAddressesAndDetails = [];
var addresses = [];
var numGeocoded = 0;
var bounds = null;
var strLatLng = "";
var containerDiv = null;
var isLoaded = false;
var mapDiv = null;
var html = null;
var cityId = null;
var gmarkers = [];
var i=0;
var mapIcon;
var mapIconNew;
var mapIconMouseOver;
var sterlingMapIcon;
var mapIconNewOpenHouse;
var mapIconBO;
var IWMarker;
var mapCenterLat = null;
var mapCenterLng = null;
var isCenterMapByDefaultCity;
var centerMapByCityId;
var isMapZoomIn;
var previousEventHandle;
var previousMarker;

function geocodeAll() 
{
	isCenterMapByDefaultCity = document.getElementById('isCenterMapByDefaultCityHidden').value;
	centerMapByCityId = document.getElementById('centerMapByCityIdHidden').value;
	isMapZoomIn = document.getElementById('isMapZoomInHidden').value;
	
	mapIcon = document.getElementById('sterlingMapIcon').value;
	mapIconNew = document.getElementById('sterlingMapIcon_NewHome').value;
	mapIconNewOpenHouse = document.getElementById('sterlingMapIcon_OpenHouse').value;
	mapIconBO = document.getElementById('sterlingMapIcon_BO').value;
	mapIconMouseOver = document.getElementById('sterlingMapIcon_MouseOver').value;
	mapDiv.style.background = '#fff';
	mapDiv.style.cursor = '';
	
	var i = listings.indexOf('*');
	if (i > 0)
	{	
		addresses = listings.split('*');
		if (numGeocoded < addresses.length) 
		{
			listingAddressesAndDetails = addresses[numGeocoded].split('^');
			var mapDisplayDetails = listingAddressesAndDetails[1].split('~');
			html = mapDisplayDetails[0];
			cityId = mapDisplayDetails[1];
			sterlingMapIcon = mapIcon;
			geocoder.getLocations(listingAddressesAndDetails[0], addressResolved);
		}
		
		if (numGeocoded == addresses.length) 
		{
			document.getElementById("loading").style["display"] = "none";
			bounds = new GLatLngBounds();
			for (var i = 0; i < pointersArray.length; i++) 
			{
				bounds.extend(new GLatLng(pointersArray[i].lat, pointersArray[i].lng));
			}
			var latSpan = bounds.toSpan().lat();
			if (isCenterMapByDefaultCity == "1")
			{
				map.setCenter(new GLatLng(mapCenterLat,mapCenterLng), map.getBoundsZoomLevel(bounds));
			}
			else
			{
				map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds));
			}
			if (isMapZoomIn == "1")
			{
				map.zoomIn();
			}
			//map.enableGoogleBar();
		}
	} 
}
function geoCodeAddress() 
{
	listings = document.getElementById(arguments[0]).value;
	addresses = listings.split('|');
	listingAddressesAndDetails = addresses[0].split('^')
	var listingInfoHTML = listingAddressesAndDetails[1].split('~');
	html = listingInfoHTML[0];
	showAddress(listingAddressesAndDetails[0],html,arguments[1]);
}
function addressResolved(response) 
{
	var delay = 0;
	
    if (response.Status.code == 620) 
    {
		delay = 50;
    } 
    else 
    {
		if (response.Status.code == 200) 
		{
			place = response.Placemark[0];
			point = new GLatLng(place.Point.coordinates[1],
								place.Point.coordinates[0]);
			pointersArray.push({"lat":point.lat(),"lng":point.lng()});	
			if (strLatLng.length==0)
			{
				strLatLng = point;	
			}
			map.setCenter(strLatLng, 10); 
			if (isCenterMapByDefaultCity == "1" && cityId == centerMapByCityId)
			{
				mapCenterLat = point.lat();
				mapCenterLng = point.lng();
			}
			marker = createMarker(point,html,sterlingMapIcon,cityId);
			map.addOverlay(marker); 
		}
		numGeocoded += 1;
    }
    window.setTimeout(geocodeAll, delay);
}
function showAddress(addr,html,sterlingMapIconImageUrl) 
{
	if (geocoder) 
    {
		geocoder.getLatLng
		(
			addr,
			function(point) 
			{
				if (!point) 
				{
					//alert(addr + " not found");
				} 
				else 
				{
					var shadowIcon = document.getElementById('sterlingShadowMapIcon').value;
					var sterlingMapIcon = new GIcon(G_DEFAULT_ICON);
					sterlingMapIcon.image = sterlingMapIconImageUrl;
					sterlingMapIcon.shadow = shadowIcon;
					sterlingMapIcon.shadowSize = new GSize(0, 0); 
					markerOptions = { icon:sterlingMapIcon };
							
					map.setCenter(point, 13);
					var marker = new GMarker(point,markerOptions);
					map.addOverlay(marker);
					marker.openInfoWindowHtml(html);
					IWMarker = marker;
				}
			}
		);
    }
}
//
function searchByCity()
{
	var url = "ByVendorSearch.aspx?data=st:p|pt:res|at:ci|ct:"+arguments[0]+"|bd:0|ba:0|pp:0-0|htv:0|cn:0|iress:m|fh:0|ss:0|bo:0|alllt:1";
	window.location.href = url;
}
function createMarker(point,html,sterlingMapIconImageUrl,cityId) 
{
	var shadowIcon = document.getElementById('sterlingShadowMapIcon').value;
	var sterlingMapIcon = new GIcon(G_DEFAULT_ICON);
	sterlingMapIcon.image = sterlingMapIconImageUrl;
	sterlingMapIcon.iconSize = new GSize(35, 35); 
	sterlingMapIcon.shadow = shadowIcon;
	sterlingMapIcon.shadowSize = new GSize(0, 0); 
	//
	var sterlingMapIconMouseOver = new GIcon(G_DEFAULT_ICON);
	sterlingMapIconMouseOver.image = mapIconMouseOver;
	sterlingMapIconMouseOver.iconSize = new GSize(35, 35); 
	sterlingMapIconMouseOver.shadow = shadowIcon;
	sterlingMapIconMouseOver.shadowSize = new GSize(0, 0); 
	
	markerOptions = { icon:sterlingMapIcon };
			
	var marker = new GMarker(point,markerOptions);
	marker.title = "Address";
	
	GEvent.addListener(marker, "click", function() {
	searchByCity(cityId);
	});
	
	GEvent.addListener(marker, "mouseover", function() {
		if(previousMarker)
		{
			previousMarker.setImage(sterlingMapIconImageUrl);
		}
        marker.setImage(mapIconMouseOver);
        previousMarker = marker;
    });
    
    
	GEvent.addListener(marker, "mouseover", function() {
	map.getPane(G_MAP_FLOAT_SHADOW_PANE).style.visibility = 'hidden'; 
	marker.openInfoWindowHtml(html,{maxWidth:250});
	});
	   
  	gmarkers[i] = marker;
	i++;
	return marker;
}

function getAbsX(elt) 
{ 
	return (elt.x) ? elt.x : getAbsPos(elt,"Left"); 
}
function getAbsY(elt) 
{ 
	return (elt.y) ? elt.y : getAbsPos(elt,"Top"); 
}

function getAbsPos(elt,which) 
{
	iPos = 0;
	while (elt != null) 
	{
		iPos += elt["offset" + which];
		elt = elt.offsetParent;
	}
	return iPos;
}
function getAbsX(elt) 
{ 
	return (elt.x) ? elt.x : getAbsPos(elt,"Left"); 
}
function getAbsY(elt) 
{ 
	return (elt.y) ? elt.y : getAbsPos(elt,"Top"); 
}

function getAbsPos(elt,which) 
{
	iPos = 0;
	while (elt != null) 
	{
		iPos += elt["offset" + which];
		elt = elt.offsetParent;
	}
	return iPos;
}
function load() 
{
	if (GBrowserIsCompatible()) 
    {	
		document.getElementById("loading").style["pixelLeft"] = getAbsX(document.getElementById("map"));
		document.getElementById("loading").style["pixelTop"] = getAbsY(document.getElementById("map"));	
		listings = document.getElementById("addressHidden").value;
		map = new GMap2(document.getElementById("map"));
		map.addControl(new GSmallMapControl());
		map.addControl(new GMapTypeControl());
		map.addMapType(G_PHYSICAL_MAP);
		map.setMapType(G_PHYSICAL_MAP);
		
		geocoder = new GClientGeocoder();
		geocoder.setCache(null);
		if (!isLoaded) 
		{
			mapDiv = document.getElementById('map');
			var div = document.createElement('div');
			div.className = 'message';
			div.innerHTML = 'Loading...';
			div.style.left = (500/2 - 53) + 'px';
			div.style.top = 500/2 + 'px'; 
			mapDiv.appendChild(div);
			window.setTimeout(geocodeAll, 50);
		}
    }
}




