﻿var domain = document.domain;

google.load("maps", "2.x");

var map, icon_thing, icon_place, icon_food, icon_what, icon_shop, icon_thing_small, icon_place_small, icon_food_small, icon_what_small, icon_shop_small, curInfoMarker;

function initialize(productId) {
	map = new google.maps.Map2(document.getElementById("googleMap"));

	map.addControl(new google.maps.SmallMapControl());
	map.addControl(new google.maps.ScaleControl());
	map.enableScrollWheelZoom();

	icon_thing = new google.maps.Icon();
	icon_thing.image = "/UserFiles/Image/map/marker_thing.png";
	icon_thing.shadow = "/UserFiles/Image/map/shadow.png";
	icon_thing.transparent = "/UserFiles/Image/map/marker_trans.png";
	icon_thing.iconSize = new google.maps.Size(21, 35);
	icon_thing.shadowSize = new google.maps.Size(37, 34);
	icon_thing.iconAnchor = new google.maps.Point(10, 34);
	icon_thing.infoWindowAnchor = new google.maps.Point(10, 34);
	icon_thing.infoShadowAnchor = new google.maps.Point(18, 25);

	icon_place = new google.maps.Icon(icon_thing);
	icon_place.image = "/UserFiles/Image/map/marker_place.png";

	icon_food = new google.maps.Icon(icon_thing);
	icon_food.image = "/UserFiles/Image/map/marker_food.png";

	icon_what = new google.maps.Icon(icon_thing);
	icon_what.image = "/UserFiles/Image/map/marker_what.png";

	icon_shop = new google.maps.Icon(icon_thing);
	icon_shop.image = "/UserFiles/Image/map/marker_shop.png";

	icon_thing_small = new google.maps.Icon(icon_thing);
	icon_thing_small.image = "/UserFiles/Image/map/marker_thing_small.png";
	icon_thing_small.shadow = "/UserFiles/Image/map/shadow_small.png";
	icon_thing_small.transparent = "/UserFiles/Image/map/marker_trans_small.png";
	icon_thing_small.iconSize = new google.maps.Size(17, 29);
	icon_thing_small.shadowSize = new google.maps.Size(24, 22);
	icon_thing_small.iconAnchor = new google.maps.Point(8, 27);
	icon_thing_small.infoWindowAnchor = new google.maps.Point(8, 27);
	icon_thing_small.infoShadowAnchor = new google.maps.Point(14, 20);

	icon_place_small = new google.maps.Icon(icon_thing_small);
	icon_place_small.image = "/UserFiles/Image/map/marker_place_small.png";

	icon_food_small = new google.maps.Icon(icon_thing_small);
	icon_food_small.image = "/UserFiles/Image/map/marker_food_small.png";

	icon_what_small = new google.maps.Icon(icon_thing_small);
	icon_what_small.image = "/UserFiles/Image/map/marker_what_small.png";

	icon_shop_small = new google.maps.Icon(icon_thing_small);
	icon_shop_small.image = "/UserFiles/Image/map/marker_shop_small.png";

	$(function() {
	    $.get("http://" + domain + "/cwndesign/walesinstyle/webservice/getproduct.aspx?pid=" + productId, function (data) {
			var productLattitude = $("productLattitude", data).text();
			var productLongitude = $("productLongitude", data).text();

			var point = new google.maps.LatLng(productLattitude,productLongitude);

			map.setCenter(point, 10);

			var marker;

			var categoryId = $("categoryId", data).text();

			switch (categoryId) {
				case "23":
					marker = new google.maps.Marker(point, icon_food);
					break;
				case "22":
					marker = new google.maps.Marker(point, icon_place);
					break;
				case "24":
					marker = new google.maps.Marker(point, icon_thing);
					break;
				case "25":
					marker = new google.maps.Marker(point, icon_what);
					break;
	            case "27":
	                marker = new google.maps.Marker(point, icon_shop);
	                break;
			}

			var productName = $("productName", data).text();
			var productSummary = $("productSummary", data).text();
			var productAddressLine1 = $("productAddressLine1", data).text();
			var productAddressPostCode = $("productAddressPostCode", data).text();
			var productUrl = $("productUrl", data).text();

			marker.title = productName;
			marker.body = "<div class=\"map_balloon\">\n<div class=\"map_balloon_title\">\n<a href=\"" + productUrl + "\">" + productName + "</a>\n</div>\n<div class=\"map_balloon_desc\">\n<a href=\"" + productUrl + "\">" + productSummary + "</a>\n</div>\n<div class=\"map_balloon_links\">\n<a target=\"_blank\" href=\"http://maps.google.co.uk/?daddr=" + productAddressLine1 + "+" + productAddressPostCode + "&f=d\">Get directions</a>\n</div>\n</div>";
			curInfoMarker = marker;
			map.addOverlay(marker);

			marker.openInfoWindowHtml(marker.body);

			google.maps.Event.addListener(marker, "click", function() {
				marker.openInfoWindowHtml(marker.body);
			});

			$(function() {
			    $.get("http://" + domain + "/cwndesign/walesinstyle/webservice/getclosestproducts.aspx?pid=" + productId, function (xml) {
					$("productContent", xml).each(function() {
						var productLattitude = $("productLattitude", this).text();
						var productLongitude = $("productLongitude", this).text();

						var point = new google.maps.LatLng(productLattitude,productLongitude);

						var marker;

						var categoryId = $("categoryId", this).text();

						switch (categoryId) {
							case "23":
								marker = new google.maps.Marker(point, icon_food_small);
								break;
							case "22":
								marker = new google.maps.Marker(point, icon_place_small);
								break;
							case "24":
								marker = new google.maps.Marker(point, icon_thing_small);
								break;
							case "25":
								marker = new google.maps.Marker(point, icon_what_small);
								break;
				            case "27":
				                marker = new google.maps.Marker(point, icon_shop_small);
				                break;
						}

						var productName = $("productName", this).text();
						var productSummary = $("productSummary", this).text();
						var productAddressLine1 = $("productAddressLine1", this).text();
						var productAddressPostCode = $("productAddressPostCode", this).text();
						var productUrl = $("productUrl", this).text();

						marker.title = productName;
						marker.body = "<div class=\"map_balloon\">\n<div class=\"map_balloon_title\">\n<a href=\"" + productUrl + "\">" + productName + "</a>\n</div>\n<div class=\"map_balloon_desc\">\n<a href=\"" + productUrl + "\">" + productSummary + "</a>\n</div>\n<div class=\"map_balloon_links\">\n<a href=\"" + productUrl + "\">Further details</a>\n&nbsp;|&nbsp;<a target=\"_blank\" href=\"http://maps.google.co.uk/?daddr=" + productAddressLine1 + "+" + productAddressPostCode + "&f=d\">Get directions</a>\n</div>\n</div>";
						map.addOverlay(marker);

						google.maps.Event.addListener(marker, "click", function() {
							marker.openInfoWindowHtml(marker.body);
						});
					});
				});
			});
		});
	})
}

//google.setOnLoadCallback(initialize);

function resetPosition() {
	map.panTo(curInfoMarker.getLatLng());
}
