﻿var bounds;
var lat;
var lng;
var point;
var html;
var label;

var myIcon = new GIcon();
myIcon.image = "http://d3oc9cjcc60qxw.cloudfront.net/images/pages/search-results/google-marker.png";
myIcon.iconSize = new GSize(16, 14);
myIcon.shadowSize = new GSize(20, 17);
myIcon.iconAnchor = new GPoint(5, 14);
myIcon.infoWindowAnchor = new GPoint(7, 4);

var myStateIcon = new GIcon();
myStateIcon.image = "http://d3oc9cjcc60qxw.cloudfront.net/images/objects/cityscape3.png";
myStateIcon.iconSize = new GSize(24, 24);
myStateIcon.shadowSize = new GSize(28, 27);
myStateIcon.iconAnchor = new GPoint(8, 24);
myStateIcon.infoWindowAnchor = new GPoint(7, 4);

GPolygon.prototype.Contains = function(point) {
	var j = 0;
	var oddNodes = false;
	var x = point.lng();
	var y = point.lat();
	for (var i = 0; i < this.getVertexCount(); i++) {
		j++;
		if (j == this.getVertexCount()) { j = 0; }
		if (((this.getVertex(i).lat() < y) && (this.getVertex(j).lat() >= y)) || ((this.getVertex(j).lat() < y) && (this.getVertex(i).lat() >= y))) {
			if (this.getVertex(i).lng() + (y - this.getVertex(i).lat()) / (this.getVertex(j).lat() - this.getVertex(i).lat()) * (this.getVertex(j).lng() - this.getVertex(i).lng()) < x) {
				oddNodes = !oddNodes
			}
		}
	}
	return oddNodes;
}

window.onbeforeunload = _gobul;

function load() {
	var d;



	d = null;
	eval("d = document.getElementById('gmap')");
	if (d == null)
		return;
	setTimeout(lmstatemap, 700 + 0);

}

var m_loadedstatemap = false;
var mapstatemap = null;
var polyldctstatemap = 0;
var polyldintstatemap = 1;
var polyldtmtstatemap = 0;
var me_timeoutstatemap = 0;
var movingstatemap = false;
var _iwcstatemap = false;

var side_bar_html = "";
var gmarkers = [];
var htmls = [];
var i = 0;

function createMarker(point, name, html) {
	var marker = new GMarker(point, myIcon);
	GEvent.addListener(marker, "click", function() {
		marker.openInfoWindowHtml(html);
	});

	bounds.extend(point);
	
	gmarkers[i] = marker;
	htmls[i] = html;
	i++;
	return marker;
}


// ===== Start with an empty GLatLngBounds object =====     
var bounds = new GLatLngBounds();


function doLstatemap() {
	m_loadedstatemap = true;
	polyldctstatemap = 0;
	polyldtmtstatemap = setTimeout(ldpolysstatemap, 100);

}

function ds_iwcstatemap() {
	_iwcstatemap = false;
}
function lmstatemap() {
	if (GBrowserIsCompatible()) {
		mapstatemap = new GMap2(document.getElementById("gmap"));
		mapstatemap.addControl(new GMapTypeControl());
		mapstatemap.addControl(new GLargeMapControl());

		GEvent.addListener(mapstatemap, "moveend", function() {
			if (m_loadedstatemap)
				return; // <-- load only 1x
			var devstatemap = .1;
			clearTimeout(me_timeoutstatemap);
			me_timeoutstatemap = setTimeout(doLstatemap, 500);

		});

		var minlat = 360;
		var maxlat = -360;
		var minlon = 360;
		var maxlon = -360;

		for (var x = 0; x < polygonsstatemap.length; x++) {
			var b = polygonsstatemap[x].getBounds();
			if (b == null)
				return;
			if (b.getSouthWest().lat() < minlat) minlat = b.getSouthWest().lat();
			if (b.getSouthWest().lng() < minlon) minlon = b.getSouthWest().lng();
			if (b.getNorthEast().lat() > maxlat) maxlat = b.getNorthEast().lat();
			if (b.getNorthEast().lng() > maxlon) maxlon = b.getNorthEast().lng();
		}



		var prebnds = new GLatLngBounds(new GLatLng(minlat, minlon), new GLatLng(maxlat, maxlon));

		var prezm = mapstatemap.getBoundsZoomLevel(prebnds);
		if (prezm == 0) prezm = 5;
		mapstatemap.setCenter(prebnds.getCenter(), prezm);
		mapstatemap.setMapType(G_NORMAL_MAP);

		for (var z = 0; z < gmarkers.length; z++) {
			mapstatemap.addOverlay(gmarkers[z]);
		}

		mapstatemap.setCenter(bounds.getCenter(), mapstatemap.getBoundsZoomLevel(bounds) - 1);
	}
}

function ldpolysstatemap() {
	if (polyldctstatemap < polygonsstatemap.length) {
		mapstatemap.addOverlay(polygonsstatemap[polyldctstatemap]);
		if (polyldctstatemap < polygonsstatemap.length - 1) {
			polyldctstatemap++;
			polyldtmtstatemap = setTimeout(ldpolysstatemap, polyldintstatemap);
		}
	}
}

function _gobul() {
	if (mapstatemap == null)
		return;
	mapstatemap.clearOverlays();
}

function _gul() {
	GUnload();
}

function pushPin(latitude, longitude, listingid, city, state, address, beds, baths) {
	lat = parseFloat(latitude); lng = parseFloat(longitude);
	point = new GLatLng(lat, lng);
	htmlBB = '';
	if (beds > 0 || baths > 0) {
		htmlBB = '<b>' + beds + '</b> Bed / <b>' + baths + '</b> Bath<br />';
	}
	var url = '/' + state + '/' + city.replace(" ", "-") + '/' + listingid + '/' + address.replace(" ", "-") + '/';
	html = '<div>' + '<a href="' + url + '">' + address + '</a><br />' + htmlBB + '</div>';
	var marker = createMarker(point, label, html);
}

var isMapOpen = false;

function showMapWindow() {
	if ((isMapOpen == undefined) || (isMapOpen == null)) return;

	//if box is not set to open then don't do anything
	if (isMapOpen == false) return;

	//var alphaListingID = $("#HiddenAlphaListingID").val();

	// set the properties of the overlay box, the left and top positions
	$('.showOnMapBox').css({
		display: 'block',
		left: ($(window).width() - $('.showOnMapBox').width()) / 2,
		top: ($(window).height() - $('.showOnMapBox').height()) / 2 - 20 + $(window).scrollTop(),
		position: 'absolute'
	});
	// set the window background for the overlay. i.e the body becomes darker
	$('.bgCover').css({ display: 'block', width: $(document).width(), height: $(document).height() });
}

function showMap(lat, lng, address, isapproximate) {
	//set status to open
	isMapOpen = true;
	$("#map-title")[0].innerText = address + (isapproximate ? " (APPROXIMATE)" : "");
	$("#map-image")[0].src = "http://maps.google.com/maps/api/staticmap?center=" + lat + "," + lng + "&zoom=15&size=400x300&maptype=roadmap\&markers=color:red|label:C|" + lat + "," + lng + "&sensor=false&key=ABQIAAAAP5aoKAzxHjxfLye81d7HXRT2yXp_ZAY8_ufC3CFXhHIE1NvwkxSYdYrVm29hPWET-q08N-myUmoKAg"
	
	showMapWindow();



	$('.bgCover').css({ opacity: 0 }).animate({ opacity: 0.5, backgroundColor: '#000' });
	// dont follow the link : so return false.
	return false;
}

function closeMapOverlay() {
	//set status to closed
	isMapOpen = false;
	$("#map-image")[0].src = "/images/pixel.gif";
	$('.showOnMapBox').css('display', 'none');

	// now animate the background to fade out to opacity 0
	// and then hide it after the animation is complete.
	$('.bgCover').animate({ opacity: 0 }, null, null, function() { $(this).hide(); });
}


