var mapCenterLat;
var mapCenterLong;
var oSchoolsJSON = null;
var minLat ;
var maxLat ;
var minLong ;
var maxLong;
var map;

//window.onload = function()
Event.onDOMReady(function() 
{
	//if ($('aSMap') != null) $('aSMap').setAttribute('href','javascript:Show("satellite")');
	if ($('aMap') != null) $('aMap').setAttribute('href','javascript:Show("map")');
	if ($('aPhoto') != null) $('aPhoto').setAttribute('href','javascript:Show("photo")');
});

function LoadMap(mapstyle)
{
	var point;
	var marker;
	map = new google.maps.Map2(document.getElementById("Map1"));
	setupIco();
	markerOptions = { icon:home_ico };
	
	if (lat == 0 && lon == 0)
	{
		if (citylat != 0 && citylon != 0)
		{
			point = new GLatLng(citylat,citylon);
			map.setCenter(point, 13);
			$('Err').style.display = 'block';
		}
		else
		{
			getCenter();
			point = new GLatLng(mapCenterLat,mapCenterLong);
			map.setCenter(point, 13);
			$('Err').style.display = 'block';
		}
	}else
	{
		point = new GLatLng(lat,lon);
		marker = new GMarker(point, markerOptions);
		map.setCenter(point, 13);
		map.setUIToDefault();
		map.addOverlay(marker); 
		
	}

	
/*
	var centerPoint;
	oMap = new VEMap('Map1');
	if (lat == 0 && lon == 0)
	{
		if (citylat != 0 && citylon != 0)
		{
			centerPoint = new VELatLong(citylat, citylon);
			$('Err').style.display = 'block';
		}
		else
		{
			getCenter();
			centerPoint = new VELatLong(mapCenterLat, mapCenterLong);
			$('Err').style.display = 'block';
		}
	}
	else 
		centerPoint = new VELatLong(lat,lon);
	try
	{
		if (token.length>0) { oMap.SetClientToken(token) };
		oMap.LoadMap(centerPoint, 12,mapstyle,false);
		oMap.HideFindControl();
		oMap.HideDashboard();
		minLat = GetBottomLat();
		maxLat = GetTopLat();
		minLong = GetLeftLong();
		maxLong = GetRightLong();
		oMap.AttachEvent('onclick', CloseSchoolBubble);
		var pushpin = oMap.vemapcontrol.AddPushpin('pushpin1', listing.lat, listing.lon, 1, 1,'listingIcon','',500);
		var loc = new VELatLong(listing.lat, listing.lon);
		if (loc != null && listing.lat != 0 && listing.lon != 0) 
		{
			schoollocs.push(loc);
		}
		
		if ($('chkSchools').checked) Show('schools');
		else DeleteSchoolPushPins();
	}
	catch(err)
	{
		oMap.HideFindControl();
		oMap.HideDashboard();
		oMap.AttachEvent('onclick', CloseSchoolBubble);
		var pushpin = oMap.vemapcontrol.AddPushpin('pushpin1', listing.lat, listing.lon, 1, 1,'listingIcon','',500);
		var loc = new VELatLong(listing.lat, listing.lon);
		if (loc != null && listing.lat != 0 && listing.lon != 0) 
		{
			schoollocs.push(loc);
		}
		
		if ($('chkSchools').checked) Show('schools');
		else DeleteSchoolPushPins();
	}*/
}
function loadLargerMap(listingid, domainid, jnxdir)
{
	var strurl = '';
	var style = ""; //oMap.GetMapStyle();
	
	//var searchparams = "&minLat=" + minLat + "&maxLat=" + maxLat + "&minLong=" + minLong + "&maxLong=" + maxLong ;
	//to make prop detail & larger map to have same schools, pass the server side lat lon range. there is a minor variation in the client side & server side values.
	var searchparams = "&minLat=" + minLat + "&maxLat=" + maxLat + "&minLong=" + minLong + "&maxLong=" + maxLong ;
	if (document.getElementById('chkSchools').checked)
		//strurl = '/propertyDetailMap.aspx?listingid=' + listingid + '&domainid=' + domainid + '&showschools=true&jnxdir=<%= MMRE.Web.Library.WebtopUtility.GetJnxVirtualDirectory() %>' + '&style=' + style;
		strurl = '/propertyDetailMap.aspx?listingid=' + listingid + '&domainid=' + domainid + '&showschools=true&style=' + style + '&jnxdir=' + jnxdir + searchparams;
	else
		//strurl = '/propertyDetailMap.aspx?listingid=' + listingid + '&domainid=' + domainid + '&showschools=false&jnxdir=<%= MMRE.Web.Library.WebtopUtility.GetJnxVirtualDirectory() %>' + '&style=' + style;
		strurl = '/propertyDetailMap.aspx?listingid=' + listingid + '&domainid=' + domainid + '&showschools=false&style=' + style + '&jnxdir=' + jnxdir + searchparams;
	
	var ltop = (screen.availHeight - 550)/2	;
	var lleft = (screen.availWidth - 920)/2 ;
	window.open(strurl,null,"top=" + ltop + ",left=" + lleft + ",height=550,width=920,status=yes,toolbar=no,menubar=no,location=no,scrollbars=yes,resizable=yes" );
}
function openFlyer(url){
	openPopup(url, 'yes', 'yes', 810, 640);
}

function openMortCalc(url){
	openPopup(url, 'no', 'no', 420, 320);
}
function openDirections(url)
{
	openPopup(url, 'yes', 'no', 920, 740);
}


function NewMapSearch()
{
	DeleteCookie("redirect","/");
	window.location.href = "mapsearch.aspx?mode=new";
}

function getCenter()
{
	var strHref = window.location.href;
	if(strHref.indexOf("prurealty") >= 0)
	{
		strState = "CA";
		mapCenterLat = 37.834948933372203;
		mapCenterLong = -121.752225568653;
	}
	else if(strHref.indexOf("prunevada") >= 0)
	{
		strState = "NV";
		mapCenterLat = 39.5245220474904;
		mapCenterLong = -119.81166770313;
	}
	else if(strHref.indexOf("prudentialtexasrealty") >= 0) //5
	{
		strState = "TX";
		mapCenterLat = 30.267631552169199;
		mapCenterLong = -97.742875702333905;
	}
	else if(strHref.indexOf("prudentialtexas") >= 0) //4
	{
		strState = "TX";
		mapCenterLat = 32.778091487269499;
		mapCenterLong = -96.795583105966401;
	}
	else if(strHref.indexOf("gotucson") >= 0)
	{
		strState = "AZ";
		mapCenterLat =  32.221513032183502;
		mapCenterLong = -110.969769256495;
	}
	else
	{
		strState = "CA";
		mapCenterLat = 37.834948933372203;
		mapCenterLong = -121.752225568653;
	}
	
}
var mapWidth = 280;
var mapHeight = 210;

function GetBoundLatLng(){
	var bounds = map.getBounds();
	var southWest = bounds.getSouthWest();
	var northEast = bounds.getNorthEast();
	minLat = southWest.lat();
	maxLat = northEast.lat();
	minLong = southWest.lng();
	maxLong = northEast.lng();
}
function GetBottomLat(){
	var pix = new VEPixel(mapWidth, mapHeight);
	var ll = oMap.PixelToLatLong(pix)
	return ll.Latitude
}
function GetLeftLong(){
	var pix = new VEPixel(0, 0);
	var ll = oMap.PixelToLatLong(pix)
	return ll.Longitude
}
function GetRightLong(){
	var pix = new VEPixel(mapWidth, mapHeight);
	var ll = oMap.PixelToLatLong(pix)
	return ll.Longitude
}
	
	
function getSchools()
{
	var schoolsurl = "/include/ajax/mapsearch/schoolList.aspx";	
	GetBoundLatLng();
	var searchparams = 'zipcode=' + zipcode + '&mode=small' + "&minLat=" + minLat + "&maxLat=" + maxLat + "&minLong=" + minLong + "&maxLong=" + maxLong;
	var myAjax = new Ajax.Request(
	schoolsurl, 
	{
		method: 'get', 
		parameters: searchparams, 
		onComplete: showSchoolsResponse
	});
}
function showSchoolsResponse(originalRequest)
{
	var response = originalRequest.responseText;
	if (response != null) 
	{
		oSchoolsJSON = eval('(' + response + ')');
	}
	if (response != '{}' && oSchoolsJSON != null && oSchoolsJSON != '')
	{
		AddSchoolPushPins();
	}
		
}