var centerLatitude = 38.898648;
var centerLongitude =  -77.037692;
var startZoom = 13;
var map;
// ====== Create a Client Geocoder ======
var geo = new GClientGeocoder();

// Lat/Lng Bounds
var bounds = new GLatLngBounds();

function initGoogleMap() {
	return initGoogleMap(startZoom);
}

function initGoogleMap(startZoom) {
	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById("map"));
		var location = new GLatLng(centerLatitude, centerLongitude);
		map.addControl(new GLargeMapControl());
		map.addControl(new GMapTypeControl());
		map.setCenter(location, startZoom);
	}
	return map;
}
// window.onload = init;

function addMarker(latitude, longitude) {
	var marker = new GMarker(new GLatLng(latitude, longitude));	
	map.addOverlay(marker);
	
	map.setCenter(new GLatLng(latitude, longitude), 15);
}

/*
Adds a Marker to the map with a click event to show information about a listing.
Accepts an associative array with the following values:
locationArray["Address"]
locationArray["City"]
locationArray["State"]
locationArray["PostalCode"]
locationArray["ListingKey"]
locationArray["ListingID"]
locationArray["ThumbnailURL"]
locationArray["ListPrice"]
locationArray["Beds"]
locationArray["Baths"]
locationArray["Subdivision"]
locationArray["MetroStation"]
locationArray["DistanceToMetro"]
locationArray["MetroLineImgURL"]
locationArray["Latitude"]
locationArray["Longitude"]
*/
function addMarker(locationArray)
{
	var latitude = locationArray['Latitude'];
	var longitude = locationArray['Longitude'];
	if ((latitude == null || latitude == '') ||
	   (longitude == null || longitude == ''))
	{
		// don't add the marker if there are no geo coordinates
		return;
	}
	var marker = new GMarker(new GLatLng(latitude, longitude));
	var lisKey = locationArray['ListingKey'];
	var address = locationArray['Address'];
	
	GEvent.addListener(marker, 'click', function() {
		// get the metro line images
		var metroLines = locationArray["MetroLineImgURL"];
		var metroLinesArr = metroLines.split(",");
		var metroLinesImages = "";
		for (i = 0; i < metroLinesArr.length; i++)
		{
			metroLinesImages += '&nbsp;<img src="' + metroLinesArr[i] + '" align="bottom">';			
		}
		var html = '<table><tr><td rowspan="2" align="left" valign="top"><img src="' + locationArray["ThumbnailURL"] + '" width="50" height="40"></td>' + 
		'<td align="left" valign="top" style="padding-left: 5px;border-bottom:1px solid #708090; solid black;" class="mapInfoText"><a href="?lis_key=' + lisKey + '">' + address + '</a><br>' + 
			locationArray["City"] + ', ' + locationArray["State"] + ' ' + locationArray["PostalCode"] + '</td>' + 
			'</tr><tr>' + 
			'<td align="left" valign="top" class="mapInfoText">' + 
			'<span class="mapInfoLabel">List Price:</span> $' + numberFormat(locationArray["ListPrice"], 2) + 
			'&nbsp;<span class="mapInfoLabel">Listing ID:</span> ' + locationArray["ListingID"] + '<br>' + 
			'<span class="mapInfoLabel">Beds:</span> ' + locationArray["Beds"] + 
			'&nbsp;<span class="mapInfoLabel">Baths:</span> ' + locationArray["Baths"] + 
			'&nbsp;<span class="mapInfoLabel">Subdivision:</span> ' + locationArray["Subdivision"] + '<br>' + 
			'<span class="mapInfoLabel">Distance To Metro:</span> ' + numberFormat(locationArray["DistanceToMetro"], '2') + ' miles<br>' + 
			'<span class="mapInfoLabel">Metro Station:</span> ' + locationArray["MetroStation"] + metroLinesImages + '<br>' + 
			// '<span class="mapInfoLabel">Latitude:</span> ' + locationArray["Latitude"] + 
			// '&nbsp;<span class="mapInfoLabel">Longitude</span>: ' + locationArray["Longitude"] + '<br>' + 
		//'<a href="/search/?lis_key=' + lisKey + '">View Listing Details</a>' + 
		'</td></tr></table>';
		
		marker.openInfoWindowHtml(html);
	});
	
	map.addOverlay(marker);
	
	bounds.extend(marker.getPoint());
	map.setZoom(map.getBoundsZoomLevel(bounds));
	map.setCenter(bounds.getCenter());
}

function addMarkerNew(locationArray)
{
    var latitude = locationArray['Latitude'];
    var longitude = locationArray['Longitude'];
    if ((latitude == null || latitude == '') ||
       (longitude == null || longitude == ''))
    {
        // don't add the marker if there are no geo coordinates
        return;
    }
    var marker = new GMarker(new GLatLng(latitude, longitude));
    var permalink = locationArray['PermaLink'];
    var address = locationArray['Address'];

    GEvent.addListener(marker, 'click', function() {
        // get the metro line images

		if (locationArray["MetroLineImgURL"] != null && locationArray["MetroLineImgURL"] != '') {
        	var metroLines = locationArray["MetroLineImgURL"];
        	var metroLinesArr = metroLines.split(",");
        	var metroLinesImages = "";
		
        	for (i = 0; i < metroLinesArr.length; i++)
       	 {
            	metroLinesImages += '&nbsp;<img src="' + metroLinesArr[i] + '" align="bottom">';
       	 }
		}
        var html = '<table><tr><td rowspan="2" align="left" valign="top"><img src="' + locationArray["ThumbnailURL"] + '" width="50" height="40"></td>' +
        '<td align="left" valign="top" style="padding-left: 5px;border-bottom:1px solid #708090; solid black;" class="mapInfoText"><a href="' + permalink + '">' + address + '</a><br>' +
            locationArray["City"] + ', ' + locationArray["State"] + ' ' + locationArray["PostalCode"] + '</td>' +
            '</tr><tr>' +
            '<td align="left" valign="top" class="mapInfoText">' +
            '<span class="mapInfoLabel">List Price:</span> $' + numberFormat(locationArray["ListPrice"], 2) +
            '&nbsp;<span class="mapInfoLabel">Listing ID:</span> ' + locationArray["ListingID"] + '<br>' +
            '<span class="mapInfoLabel">Beds:</span> ' + locationArray["Beds"] +
            '&nbsp;<span class="mapInfoLabel">Baths:</span> ' + locationArray["Baths"] +
            '&nbsp;<span class="mapInfoLabel">Subdivision:</span> ' + locationArray["Subdivision"] + '<br>' ;
 
			if (locationArray["DistanceToMetro"] != null && locationArray["DistanceToMetro"] != '' ) {
           html = html +  '<span class="mapInfoLabel">Distance To Metro:</span> ' + numberFormat(locationArray["DistanceToMetro"], '2') + ' miles<br>' +
            '<span class="mapInfoLabel">Metro Station:</span> ' + locationArray["MetroStation"] + metroLinesImages + '<br>';
		}
		
		html = html  + '</td></tr></table>';

        marker.openInfoWindowHtml(html);
    });

    map.addOverlay(marker);

//    bounds.extend(marker.getPoint());
  //  map.setZoom(map.getBoundsZoomLevel(bounds));
   // map.setCenter(bounds.getCenter());
}





function addLocation(address) {
	addLocation(address, true);
}

function addLocation(address, centerOnLocation)
{
	addLocation(address, centerOnLocation, null);
}

function addLocation(address, centerOnLocation, id)
{
	if (isArray(address))
	{
		addMarker(address);
	}
	else
	{		
		geo.getLatLng(
				address,    
				function(point) {
				if (!point) {
					//alert(address + " not found");      
				} else {        
					map.setCenter(point, 13);
				var marker = new GMarker(point);        
				map.addOverlay(marker);        
				}    
			}
		);		

	}
}

function addLocationNew(address, centerOnLocation, id)
{
    if (isArray(address))
    {
        addMarkerNew(address);
    }
    else
    {
        geo.getLatLng(
                address,
                function(point) {
                if (!point) {
                    //alert(address + " not found");
                } else {
                    map.setCenter(point, 13);
                var marker = new GMarker(point);
                map.addOverlay(marker);
                }
            }
        );

    }
}




function showAddress(address) {  
	geo.getLatLng(
		address,    
		function(point) {
		if (!point) {
			// alert(address + " not found");      
		} else {        
			map.setCenter(point, 13);
		var marker = new GMarker(point);        
		map.addOverlay(marker);        
		marker.openInfoWindowHtml(address);      
		}    
	}
	);
}

function drawMetroLines(map) {
	// Drawing Orange line
	var polyline = new GPolyline( [
			new GLatLng(38.877693, -77.271562),
			new GLatLng(38.883015, -77.228939),
			new GLatLng(38.90067, -77.189394),
			new GLatLng(38.885841, -77.157177),
			new GLatLng(38.882071, -77.111845),
			new GLatLng(38.883104, -77.103177),
			new GLatLng(38.886373, -77.096963),
			new GLatLng(38.891499, -77.08391),
			new GLatLng(38.896595, -77.071460), // Rosslyn
			new GLatLng(38.900599, -77.050273),
			new GLatLng(38.901311, -77.03981),
			new GLatLng(38.901316, -77.033652),
			new GLatLng(38.898303, -77.028099),
			new GLatLng(38.893757, -77.028218),
			new GLatLng(38.888022, -77.028232),
			new GLatLng(38.884775, -77.021964),
			new GLatLng(38.884958, -77.01586),
			new GLatLng(38.884968, -77.005137),
			new GLatLng(38.884124, -76.995334),
			new GLatLng(38.880841, -76.985721),
			new GLatLng(38.88594, -76.977485),
			new GLatLng(38.898284, -76.948042),
			new GLatLng(38.907734, -76.936177),
			new GLatLng(38.91652, -76.915427),
			new GLatLng(38.934411, -76.890988),
			new GLatLng(38.947674, -76.872144) ], "#FF6600", 8, 1);
	map.addOverlay(polyline);

	// Drawing Red line
	var polyline = new GPolyline( [ new GLatLng(39.119819, -77.164921),
			new GLatLng(39.084215, -77.146424),
			new GLatLng(39.062359, -77.121113),
			new GLatLng(39.048043, -77.113131),
			new GLatLng(39.029158, -77.10415),
			new GLatLng(38.999947, -77.097253),
			new GLatLng(38.984282, -77.094431),
			new GLatLng(38.960744, -77.085969),
			new GLatLng(38.947808, -77.079615),
			new GLatLng(38.94362, -77.063511),
			new GLatLng(38.934703, -77.058226),
			new GLatLng(38.924999, -77.052648),
			new GLatLng(38.909499, -77.04362),
			new GLatLng(38.903192, -77.039766),
			new GLatLng(38.898303, -77.028099),
			new GLatLng(38.89834, -77.021851),
			new GLatLng(38.896084, -77.016643),
			new GLatLng(38.897723, -77.006745),
			new GLatLng(38.910916, -77.00185),
			new GLatLng(38.920741, -76.995984),
			new GLatLng(38.933234, -76.994544),
			new GLatLng(38.951777, -77.002174),
			new GLatLng(38.975532, -77.017834),
			new GLatLng(38.993841, -77.031321),
			new GLatLng(39.015413, -77.042953),
			new GLatLng(39.038558, -77.051098),
			new GLatLng(39.061713, -77.05341) ], "#ff0033", 8, 1);
	map.addOverlay(polyline);

	// Drawing Green line
	var polyline = new GPolyline( [ new GLatLng(38.826995, -76.912134),
			new GLatLng(38.843891, -76.932022),
			new GLatLng(38.851187, -76.956565),
			new GLatLng(38.840974, -76.97536),
			new GLatLng(38.845334, -76.98817),
			new GLatLng(38.862072, -76.995648),
			new GLatLng(38.876588, -77.005086),
			new GLatLng(38.876221, -77.017491),
			new GLatLng(38.884775, -77.021964),
			new GLatLng(38.893893, -77.021902),
			new GLatLng(38.89834, -77.021851),
			new GLatLng(38.905604, -77.022256),
			new GLatLng(38.912919, -77.022194),
			new GLatLng(38.916489, -77.028938),
			new GLatLng(38.928672, -77.032775),
			new GLatLng(38.936077, -77.024728),
			new GLatLng(38.951777, -77.002174),
			new GLatLng(38.954931, -76.969881),
			new GLatLng(38.965276, -76.956182),
			new GLatLng(38.978523, -76.928432),
			new GLatLng(39.011036, -76.911362) ], "#339933", 8, 1);
	map.addOverlay(polyline);

	// Drawing Blue line Franconia - Rosslyn
	var polyline = new GPolyline( [ new GLatLng(38.766129, -77.168797),
			new GLatLng(38.799193, -77.129407),
			new GLatLng(38.806474, -77.061115),
			new GLatLng(38.814009, -77.053763),
			new GLatLng(38.852985, -77.043805),
			new GLatLng(38.85779, -77.050589),
			new GLatLng(38.863045, -77.059507),
			new GLatLng(38.869349, -77.054013),
			new GLatLng(38.884574, -77.063108),
			new GLatLng(38.896595, -77.071460) ], "#0000CC", 8, 1); // Rosslyn
	map.addOverlay(polyline);

	// Drawing Blue line half width Rosslyn - Stadium Armory
	var polyline = new GPolyline( [
			new GLatLng(38.896595, -77.071460), // Rosslyn
			new GLatLng(38.900599, -77.050273),
			new GLatLng(38.901311, -77.03981),
			new GLatLng(38.901316, -77.033652),
			new GLatLng(38.898303, -77.028099),
			new GLatLng(38.893757, -77.028218),
			new GLatLng(38.888022, -77.028232),
			new GLatLng(38.884775, -77.021964),
			new GLatLng(38.884958, -77.01586),
			new GLatLng(38.884968, -77.005137),
			new GLatLng(38.884124, -76.995334),
			new GLatLng(38.880841, -76.985721),
			new GLatLng(38.88594, -76.977485) ], "#0000CC", 3, 1);
	map.addOverlay(polyline);

	// Drawing Blue line Stadium Armory - Largo
	var polyline = new GPolyline( [ new GLatLng(38.88594, -76.977485),
			new GLatLng(38.890488, -76.938291),
			new GLatLng(38.889757, -76.913382),
			new GLatLng(38.886713, -76.893592), new GLatLng(38.8913, -76.8682),
			new GLatLng(38.9008, -76.8449) ], "#0000CC", 10, 1);
	map.addOverlay(polyline);

	// Drawing Yellow line Huntington - King St
	var polyline = new GPolyline( [ new GLatLng(38.793841, -77.075301),
			new GLatLng(38.800313, -77.071173),
			new GLatLng(38.806474, -77.061115) ], "#FFFF00", 8, 1);
	map.addOverlay(polyline);

	// Drawing Yellow line King St. - Pentagon half width
	var polyline = new GPolyline( [ new GLatLng(38.806474, -77.061115),
			new GLatLng(38.814009, -77.053763),
			new GLatLng(38.852985, -77.043805),
			new GLatLng(38.85779, -77.050589),
			new GLatLng(38.863045, -77.059507),
			new GLatLng(38.869349, -77.054013) ], "#FFFF00", 3, 1);
	map.addOverlay(polyline);

	// Drawing Yellow line Pentagon - Lenfant
	var polyline = new GPolyline( [ new GLatLng(38.869349, -77.05401),
			new GLatLng(38.884775, -77.021964) ], "#FFFF00", 8, 1);
	map.addOverlay(polyline);

	// Drawing Yellow line Lenfant - Mt Vernon half width
	// Added extension to Fort Totten effective 12/31/2006

	var polyline = new GPolyline( [ new GLatLng(38.884775, -77.021964),
			new GLatLng(38.893893, -77.021902),
			new GLatLng(38.89834, -77.021851),
			new GLatLng(38.905604, -77.022256),
			new GLatLng(38.912919, -77.022194),
			new GLatLng(38.916489, -77.028938),
			new GLatLng(38.928672, -77.032775),
			new GLatLng(38.936077, -77.024728),
			new GLatLng(38.951777, -77.002174) ], "#FFFF00", 3, 1);
	map.addOverlay(polyline);// after
}

function formatNumber(num,prefix){   
	prefix = prefix || '';   
	num += '';   
	var splitStr = num.split('.');   
	var splitLeft = splitStr[0];   
	var splitRight = splitStr.length > 1 ? '.' + splitStr[1] : '';   
	var regx = /(\d+)(\d{3})/;   
	while (regx.test(splitLeft)) {      
		splitLeft = splitLeft.replace(regx, '$1' + ',' + '$2');   
	}
	return prefix + splitLeft + splitRight;
}

function isArray(testObject) {
	return testObject && 
		!(testObject.propertyIsEnumerable('length')) && 
		typeof testObject === 'object' && 
		typeof testObject.length === 'number';
}

var thousand_sep = ",";
var decimal_point = ".";
var default_precision = 2;

function numberFormat(aFloat, aPrecision){
	try {
		precision = default_precision;
		if(!isNaN(aPrecision))
		if(Math.abs(aPrecision)<=10)
		precision = aPrecision;
	} catch(e) {
		precision = default_precision;
	}
	try {
		number = parseFloat(aFloat+'');
		if(isNaN(number))
		return "NaN";
	} catch(e) {
		return "NaN";
	}

	number = Math.round(number * Math.pow(10, precision)) / Math.pow(10,precision);
	integerpart = '' + ((number<0) ? Math.ceil(number) :
	Math.floor(number));
	decimalpart = Math.abs(Math.round((number - integerpart)*(Math.pow(10,precision))));
	if(decimalpart<10)
	{
		decimalpart="0"+decimalpart;
	}
	if(decimalpart==0)
	{
		decimalpart="00";
	}
	var buff = "";
	for(j=-1, i=integerpart.length; i>=0; i--, j++){
		if((j%3) == 0 && j>1)
		buff = thousand_sep + buff;
		buff = integerpart.charAt(i) + buff;
	}
	if(precision>0)
	{
		return buff+decimal_point+decimalpart;
	}
	return buff;
}
