﻿//http://localhost:4498 key: ABQIAAAAgBOy8tE1U8vChLGx_e81vhRy-UwLJ2FVvdUDDTc_4jv5wwdw6hQLx9aE-Htm7x0P0paygZcL0OBbvQ
//http://driverwebsites.com' key: 'ABQIAAAAgBOy8tE1U8vChLGx_e81vhQ12KYbfpp549SRnt_SPwL5TQbY8xTBQ-rLcudfEPdvuCYF_8R2K5_2xg

var map = null;
var geocoder = new GClientGeocoder();
var jsonData = null;
var blueIcon = null;
var markers = [];
var mcOptions = { gridSize: 40, maxZoom: 15 };
var raceTot = 0;
var bisTot = 0;
var hCardTot = 0;

function initialize() {
	if (GBrowserIsCompatible()) {
		//alert("test");
		map = new GMap2(document.getElementById("map_canvas"));

		map.setCenter(new GLatLng(39.55, -97.7), 4);
		map.setUIToDefault();

		setPrefs();

		getMapData();

	}
}

function getMapData() {
	var hCardCount = 0;
	$.getJSON('/portfolio/getMapData.aspx', function(json) {
		jsonData = json;
		for (var i = 0; i < jsonData.clients.length; i++) {
			if (hCardCount == 0) {
				addMarker(jsonData.clients[i].portId, jsonData.clients[i].picName, jsonData.clients[i].portSite, jsonData.clients[i].portName, jsonData.clients[i].portAddress, jsonData.clients[i].portLat, jsonData.clients[i].portLng, jsonData.clients[i].portLive, jsonData.clients[i].portType);
			}
			if (jsonData.clients[i].portType == "4") {				//check for hero card and only add a marker once
				hCardCount++;
				if (hCardCount == 2)
					hCardCount = 0;
			}
		}
		var markerCluster = new MarkerClusterer(map, markers, mcOptions);

		$("#legend1 .count").text(" (" + raceTot + ")");
		$("#legend2 .count").text(" (" + bisTot + ")");
		//$("#legend4 .count").text(" (" + hCardTot + ")");
	});
}

function setPrefs() {
	// Create our "raceIcon" marker icon
	var raceIcon = new GIcon(G_DEFAULT_ICON);
	raceIcon.image = "/img/map/red-pushpin.png";
	raceIcon.shadow = "/img/map/pushpin_shadow.png";
	raceIcon.iconSize = new GSize(32, 35);
	raceIcon.shadowSize = new GSize(52, 35);
	// Set up our GMarkerOptions object literal
	raceMarkerOpts = { icon: raceIcon };

	var bisIcon = new GIcon(G_DEFAULT_ICON);
	bisIcon.image = "/img/map/blue-pushpin.png";
	bisIcon.shadow = "/img/map/pushpin_shadow.png";
	bisIcon.iconSize = new GSize(32, 35);
	bisIcon.shadowSize = new GSize(52, 35);
	// Set up our GMarkerOptions object literal
	bisMarkerOpts = { icon: bisIcon };

	var hCardIcon = new GIcon(G_DEFAULT_ICON);
	hCardIcon.image = "/img/map/grn-pushpin.png";
	hCardIcon.shadow = "/img/map/pushpin_shadow.png";
	hCardIcon.iconSize = new GSize(32, 35);
	hCardIcon.shadowSize = new GSize(52, 35);
	// Set up our GMarkerOptions object literal
	hCardMarkerOpts = { icon: hCardIcon };
}

function addMarker(portId, picName, portSite, portName, portAddress, portLat, portLng, portLive, portType) {
	if (!portLat) {
		//alert(portId + portAddress + " not found");
		addCoords(portId, portAddress);
	} else {
		//alert(portId);
		var randomNum = Math.random() * .002; 			//move marker slightly to prevent overlapping
		var randomNum2 = Math.random() * -.002; 			//move marker slightly to prevent overlapping
		var point1 = parseFloat(portLat) + randomNum;
		var point2 = parseFloat(portLng) + randomNum2;
		//var point = new GLatLng(point1, point2);
		var latlng = new GLatLng(point1, point2);
		//var marker = new GMarker(point, raceMarkerOpts);

		var marker = new GMarker(latlng, raceMarkerOpts);

		if (portType == "1") {
			raceTot++;
		}

		if (portType == "2") {
			bisTot++;
			marker = new GMarker(latlng, bisMarkerOpts);
		}

		if (portType == "4") {
			hCardTot++;
			marker = new GMarker(latlng, hCardMarkerOpts);
		}

		marker.value = portId;
		GEvent.addListener(marker, "click", function() {
			var myHtml = buildHtmlInfo(portId, picName, portSite, portName, portAddress, portLive, portType);
			//map.openInfoWindowHtml(point, myHtml);
			map.openInfoWindowHtml(latlng, myHtml);
		});
		//map.addOverlay(marker);
		markers.push(marker);
	}
}

function buildHtmlInfo(portId, picName, portSite, portName, portAddress, portLive, portType) {

	if (picName)
		picName = "<div id='mapSiteThumb_" + portId + "' class='mapSiteThumb'><img alt='" + portName + "' src='/img/portfolio/t/" + picName + "' /></div>";
	else
		picName = "";

	if (portLive == "True" && portType != "4")
		portSite = "<div><a target='_blank' href='http://www." + portSite + "'>Visit Website</a></div>";
	else
		portSite = "";

	if (portType == "4") {
		portName = portName.replace("- Front", "<a href='JavaScript:void(0);' onclick='swapHCard(0, " + portId + ")'>Front</a> | <a href='JavaScript:void(0);' onclick='swapHCard(1, " + portId + ")'>Back</a>");
	}

	portName = "<div><strong>" + portName + "</strong></div>";

	portAddress = "<div>" + portAddress + "</div>";

	return "<div class='mapInfoWrap'>" + picName + portName + portAddress + portSite + "</div>";
}

function swapHCard(side, thumbId) {
	var thumbSrc = $("#mapSiteThumb_" + thumbId + " img").attr("src");
	if (side == "0") {
		thumbSrc = thumbSrc.replace("back", "front");
	}
	if (side == "1") {
		thumbSrc = thumbSrc.replace("front", "back");
	}
	$("#mapSiteThumb_" + thumbId + " img").animate({
		opacity: 0.25
	}, 600, function() {
		$(this).attr("src", thumbSrc);
		$("#mapSiteThumb_" + thumbId + " img").animate({
			opacity: 1.0
		}, 600);
	});
}

function addCoords(id, address) {

	var newLat = null;
	var newLng = null;

	address = address.replace(",", "");
	address = address.replace(" ", "+");

	geocoder.getLatLng(
    address,
    function(point) {
    	if (!point) {
    		//alert(address + " not found");
    	} else {
    		var marker = new GMarker(point);
    		newLat = marker.getLatLng().lat();
    		newLng = marker.getLatLng().lng();
    		processCoords(newLat, newLng);
    	}
    }
  );

	function processCoords(newLat, newLng) {
		//var addNewCoords = confirm("Adding: " + address + " (" + id + ")\n Latitude: " + newLat + "\n Longitude: " + newLng);
		//if (addNewCoords) {
		$.post("/portfolio/addLatLng.aspx?a=" + id + "&b=" + newLat + "&c=" + newLng, function(data) {
			//alert("added....maybe");
		});
		//}
		//else {
		//	alert("skipped");
		//}
	}
}


$(document).ready(function() {
	//$("body").attr("onload", "initialize()");

	//$("body").attr("onunload", "GUnload()");
});
