﻿//<![CDATA[

var true_imagePath = "App_Themes/default/art/";

$(document).ready(function() {
	if (GBrowserIsCompatible()) {
		var showrooms = {
			calgary: { lat: 51.069208, lng: -113.993255, zoom: 15 },
			canmore: { lat: 51.089266, lng: -115.348955, zoom: 15 },
			invermere: { lat: 50.516697, lng: -116.007579, zoom: 15 },
			scottsdale: { lat: 33.614145, lng: -111.910422, zoom: 15, image: true_imagePath + 'map_markerAffinity.png' },
			vancouver: { lat: 49.2657047, lng: -123.1120661, zoom: 15, image: true_imagePath + 'map_markerV6B.png' }
			//tucson: { lat: 32.227832, lng: -110.943784, zoom: 13, image: true_imagePath + 'map_markerAffinity.png' }
		};

		var showroom = showrooms[showroomName] || showrooms.calgary;
		var point = new GLatLng(showroom.lat, showroom.lng);
		var holder = $("span.js-map").get(0);
		if (holder) {
			map = new GMap2(holder);
			map.addControl(new LGZoomControl());
			map.addControl(new LGMapTypeControl());
			map.setCenter(point, showroom.zoom);

			var baseIcon = new GIcon();
			baseIcon.image = true_imagePath + 'map_markerLegacy.png';
			baseIcon.shadow = true_imagePath + 'map_markerShadow.png';
			baseIcon.iconSize = new GSize(95, 58);
			baseIcon.shadowSize = new GSize(134, 58);
			baseIcon.iconAnchor = new GPoint(47, 58);
			baseIcon.infoWindowAnchor = new GPoint(47, 2);

			var options;
			var m = null;
			for (var p in showrooms) {
				options = { icon: new GIcon(baseIcon) };
				options.icon.image = showrooms[p].image || baseIcon.image;
				var marker = new GMarker(new GLatLng(showrooms[p].lat, showrooms[p].lng), options);
				map.addOverlay(marker);
				if (showrooms[p] == showroom) {
					var m = marker;
					var popHtm = $('div.js-mapPopup');
					if (popHtm.length > 0) {
						var f = function() { m.openInfoWindowHtml(popHtm.html()); };
						GEvent.addListener(m, "click", f);
						f();
					}
				}
			}
		}
	}
});

function LGControl() { };

LGControl.prototype = new GControl();
LGControl.prototype._addButton = function(p_container, p_image, p_clickDelegate) {
    var anchor = document.createElement("a");
    $(anchor).attr("href", "javascript:;");
    $(anchor).html("<img src='" + true_imagePath + p_image + "' />");
    p_container.appendChild(anchor);

    GEvent.addDomListener(anchor, "click", p_clickDelegate);
}

function LGZoomControl() { };

LGZoomControl.prototype = new LGControl();

LGZoomControl.prototype.initialize = function(map) {
    var container = document.createElement("div");

    this._addButton(container, "map_buttonZoomOut.gif", function() { map.zoomOut(); return false; });
    this._addButton(container, "map_buttonZoomIn.gif", function() { map.zoomIn(); return false; });
    map.getContainer().appendChild(container);

    return container;
};

LGZoomControl.prototype.getDefaultPosition = function() {
    return new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(4, 4));
};

function LGMapTypeControl() {
};

LGMapTypeControl.prototype = new LGControl();

LGMapTypeControl.prototype.initialize = function(map) {
    var container = document.createElement("div");

    this._addButton(container, "map_buttonMap.gif", function() { map.setMapType(G_NORMAL_MAP); return false; });
    this._addButton(container, "map_buttonSatellite.gif", function() { map.setMapType(G_SATELLITE_MAP); return false; });
    this._addButton(container, "map_buttonHybrid.gif", function() { map.setMapType(G_HYBRID_MAP); return false; });
    map.getContainer().appendChild(container);

    return container;
};

LGMapTypeControl.prototype.getDefaultPosition = function() {
    return new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(4, 4));
};

//]]>