﻿var maxZoom = 10;

registerLoadHandler(window, OnLoad);

function registerLoadHandler(windowObject, handler) {
	var node = windowObject;
	if (node.addEventListener) {
		node.addEventListener("load", handler, false);
	} else if (node.attachEvent) {
		node.attachEvent("onload", handler);
	} else {
		node['onload'] = handler;
	}
	return true;
}

function registerUnloadHandler(handler) {
	var node = window;
	if (node.addEventListener) {
		node.addEventListener("unload", handler, false);
	} else if (node.attachEvent) {
		node.attachEvent("onunload", handler);
	} else {
		node['onunload'] = handler;
	}
	return true;
}

function OnLoad() {
	initMap();
}


function initMap() {
	var map = document.getElementById("map_canvas");
	if (map == null) return;

	var lat = map.attributes["lat"].value;
	var lng = map.attributes["lng"].value;
	var company = map.attributes["company"].value;
	var address = map.attributes["address"].value;

	if (GBrowserIsCompatible()) {
		var map = new GMap2(map);
		map.setCenter(new GLatLng(lat, lng), 4);
		//map.setCenter(new GLatLng(40.59623, -73.974066), 8);
		map.addControl(new GLargeMapControl3D());
		map.addControl(new GMapTypeControl());
		map.addControl(new ZoomToNyControl());

		GMapType.prototype.getMaximumResolution = function(latlng) {
			return maxZoom;
		}

		var blueIcon = new GIcon(G_DEFAULT_ICON);
		blueIcon.iconSize = new GSize(29, 27);
		blueIcon.image = "img/nyr-shield-transparent.gif";
		markerOptions = { icon: blueIcon};

		var geocoder = new GClientGeocoder();
		var timeout = 1000;
		var zipIndex = 0;
		for (var i = 0; i < counts.length; i++) 
		{
			var loc = counts[i]
//			console.log(loc.City);

			point = new GLatLng(loc.Lat, loc.Lng);

			markerOptions.title = loc.NumberOfSubscribers + " blueshirts";
			var marker = new GMarker(point, markerOptions);
			map.addOverlay(marker);

			GEvent.addListener(marker, 'click', showAddress(loc.City, map, loc.NumberOfSubscribers));

		}
		registerUnloadHandler(GUnload);
	}
}

(function(f) {
	window.setTimeout = f(window.setTimeout);
	window.setInterval = f(window.setInterval);
})(function(f) { return function(c, t) { var a = [].slice.call(arguments, 2); return f(function() { c.apply(this, a) }, t) } });


function getAddress(geocoder, zip, map, markerOptions, fansCount) {
	geocoder.getLocations(zip, showMarker(zip, map, markerOptions, fansCount));
}

function showMarker(zip, map, markerOptions, fansCount) {
	return (function(response) {
		if (response && response.Status.code == 200) {
			place = response.Placemark[0];
			point = new GLatLng(place.Point.coordinates[1], place.Point.coordinates[0]);

			fansCount = zips[zip];

			markerOptions.title = fansCount.toString() + " blueshirts";
			var marker = new GMarker(point, markerOptions);
			map.addOverlay(marker);

			GEvent.addListener(marker, 'click', showAddress(zip, map, place, fansCount));
		}
	});
}


function showAddress(city, map, fansCount) {
	var fansCountMsg = (fansCount > 1) ? fansCount + " BLUESHIRTS" : "1 BLUESHIRT";
	
	return (function(latlng) {
		map.openInfoWindowHtml(latlng,
   "<div>" +
    "<div style='width:80px; float:left'><img style='width:70px; height:70px;'" +
     "src='http://images.askmen.com/sports/business_100/pictures_100/100b_sports_business.JPG'/>" +
    "</div>" +
    "<div style='float:right; padding-right:15px; width:150px; font-weight:bold;'>" +
     "<div style='font-size:16px; font-family:Arial; color:#002D57;'>" + city + "</div>" +
     "<div style='padding-top:10px; font-size:12px; font-family:Arial; color: black;'>" + fansCountMsg + "</div>" +
   "</div></div>",
   { maxWidth: 260 });
	});
}

function calculateFansCount(zip) {
    var fansCount = 0;
    for (borough in boroughs) {
        if (boroughs[borough] == boroughs[zip] && zips[borough] != null) {
            fansCount += zips[borough];
        }
    }
    return fansCount;
}

var states = {
	"AL": "Alabama",
	"AK": "Alaska",
	"AZ": "Arizona",
	"AR": "Arkansas",
	"CA": "California",
	"CO": "Colorado",
	"CT": "Connecticut",
	"DE": "Delaware",
	"DC": "District Of Columbia",
	"FL": "Florida",
	"GA": "Georgia",
	"HI": "Hawaii",
	"ID": "Idaho",
	"IL": "Illinois",
	"IN": "Indiana",
	"IA": "Iowa",
	"KS": "Kansas",
	"KY": "Kentucky",
	"LA": "Louisiana",
	"ME": "Maine",
	"MD": "Maryland",
	"MA": "Massachusetts",
	"MI": "Michigan",
	"MN": "Minnesota",
	"MS": "Mississippi",
	"MO": "Missouri",
	"MT": "Montana",
	"NE": "Nebraska",
	"NV": "Nevada",
	"NH": "New Hampshire",
	"NJ": "New Jersey",
	"NM": "New Mexico",
	"NY": "New York",
	"NC": "North Carolina",
	"ND": "North Dakota",
	"OH": "Ohio",
	"OK": "Oklahoma",
	"OR": "Oregon",
	"PA": "Pennsylvania",
	"PR": "Puerto Rico",
	"RI": "Rhode Island",
	"SC": "South Carolina",
	"SD": "South Dakota",
	"TN": "Tennessee",
	"TX": "Texas",
	"VI": "US Virgin Islands",
	"UT": "Utah",
	"VT": "Vermont",
	"VA": "Virginia",
	"WA": "Washington",
	"WV": "West Virginia",
	"WI": "Wisconsin",
	"WY": "Wyoming"
};

/************************/
function ZoomToNyControl() {
}

// To "subclass" the GControl, we set the prototype object to
// an instance of the GControl object
ZoomToNyControl.prototype = new GControl();

// Creates a one DIV for each of the buttons and places them in a container
// DIV which is returned as our control element. We add the control to
// to the map container and return the element for the map class to
// position properly.
ZoomToNyControl.prototype.initialize = function(map) {
  var container = document.createElement("div");

  var zoomInDiv = document.createElement("div");
  this.setButtonStyle_(zoomInDiv);
  container.appendChild(zoomInDiv);
  zoomInDiv.appendChild(document.createTextNode("Show New York"));
  GEvent.addDomListener(zoomInDiv, "click", function() {
	map.setCenter(new GLatLng(40.750800, -73.996122), 10);
  });

  map.getContainer().appendChild(container);
  return container;
}

ZoomToNyControl.prototype.getDefaultPosition = function() {
  return new GControlPosition(G_ANCHOR_BOTTOM_RIGHT, new GSize(7, 12));
}

// Sets the proper CSS for the given button element.
ZoomToNyControl.prototype.setButtonStyle_ = function(button) {
  button.style.textDecoration = "underline";
  button.style.color = "#0000cc";
  button.style.backgroundColor = "white";
  button.style.font = "small Arial";
  button.style.border = "1px solid black";
  button.style.padding = "2px";
  button.style.marginBottom = "3px";
  button.style.textAlign = "center";
  button.style.width = "6em";
  button.style.cursor = "pointer";
  button.style.width = "120px";
}

