var maps = [];

swa.gmap = function() {
	var _namespace = "swa.gmap";
	return { };
}();

swa.gmap.util = function() {
	var _namespace = "swa.gmap.util";
	return {
	
		

		geocoder: new GClientGeocoder(),

		getIconByType: function(markerType) {
			var icon = new GIcon(G_DEFAULT_ICON);
			icon.iconSize = new GSize(32, 32);
			icon.shadowSize = new GSize(59, 32);

			return new GIcon(G_DEFAULT_ICON);
		},

		getLatLngForAddress: function(address, callback) {
			if (this.geocoder) {
				this.geocoder.getLatLng(address, callback);
			}
		},

		addMarkerWithInfoWindow: function(map, point, name, address) {
			var marker = new GMarker(point);
			map.addOverlay(marker);
			$("#gmaps_infoWindow .iwAddressHiddenInput").val(address); // necessary workaround for ie and hidden input fields
			marker.bindInfoWindowHtml($('#gmaps_infoWindow').html().replace(/IW_ADDRESS/g, address).replace(/IW_NAME/g, name), {maxWidth:250});
		},
		
		switchTabs: function(currentTab, nameOfTabToShow) {
					currentTab.parent().hide();
					currentTab.parent().siblings(nameOfTabToShow).show();
					for (var i in maps) {
						// currently no way to know which map has been clicked here, so let's just update them all...
						maps[i].updateInfoWindow();
					}
		}

		

	};
}();


swa.gmap.Map = function(params) {
	params = params || {};
	this.map = null;
	this.mapCanvas = params.mapCanvas;
	this.mapContainer =  params.mapContainer;
	this.address = params.address;
	this.lat = params.lat;
	this.lng = params.lng;
	this.name = params.name || "";
	this.zoom = params.zoom || 13;
	this.smallMap = params.smallMap || false;
	this.runOnInit = params.runOnInit;
	this.streetView = null;
	this.streetViewOn = false;

	this.runOnFailure = function() {
		this.mapContainer.hide();
		return false;
	};

	this.init = function() {

		if (!GBrowserIsCompatible()) {
			this.runOnFailure();
		}

		this.map = new GMap2(this.mapCanvas); // pass in the DOM element, not the jquery element
		maps.push(this.map);

		if (this.address && this.address != "") {
			var me = this;
			swa.gmap.util.getLatLngForAddress(this.address, function(point) {
				me.showPoint(point);
			});
		} else if (this.lat && this.lng) {
			this.address = "" + this.lat + ", " + this.lng;
			this.name = this.address;
			this.showPoint(new GLatLng(this.lat, this.lng));
		} else {
			this.runOnFailure();
		}

		this.smallMap = true;

		this.setUI();
	};

	this.setUI = function() {
		this.map.addMapType(G_NORMAL_MAP);
		this.map.addMapType(G_SATELLITE_MAP);
		this.map.addMapType(G_HYBRID_MAP);

		this.map.enableScrollWheelZoom();
		new GKeyboardHandler(this.map);

		if (this.smallMap) {
			this.map.addControl(new GMenuMapTypeControl());
			this.map.addControl(new GSmallZoomControl3D());
		} else {
			this.map.addControl(new GMapTypeControl());
			this.map.addControl(new GLargeMapControl3D());
		}
		//this.streetView = new swa.gmap.StreetView(this.map, this.smallMap);
	};

	this.showPoint = function(point) {
		if (!point) {
			this.runOnFailure();
		}
		swa.gmap.util.addMarkerWithInfoWindow(this.map, point, this.name, this.address);
		this.map.setCenter(point, this.zoom);

		if (this.runOnInit) {
			this.runOnInit();
			this.runOnInit = null;
		}
	};

	this.addVenue = function(venue) {
		if (!venue) {
			return false;
		}
		if (venue.lat && venue.lng) {
			swa.gmap.util.addVenueMarkerForReviews(this.map, venue);
		} else if (venue.address) {
			swa.gmap.util.addVenueMarkerWithAddress(this.map, venue);
		}
	};
	this.init();
};

swa.gmap.StreetView = function(map, smallMap) {
	this.streetViewClient = null;
	this.map = map;
	this.streetViewOn = false;
	this.overlay = null;
	this.marker = null;
	this.panorama = null;
	this.panoSmallNode = null;
	this.panoLargeNode = null;
	this.button = null;
	var me = this;
	var myMap = this.map;

	this.init = function() {
		this.streetViewClient = new GStreetviewClient();
		this.overlay = new GStreetviewOverlay();
		this.addStreetViewGuyMarker(new GLatLng(0, 0));
		this.panoSmallNode = $("#streetViewBubblePanorama").get(0);
		if (smallMap) {
			this.panoLargeNode = $("#streetViewFullPanorama_small").get(0);
		} else {
			this.panoLargeNode = $("#streetViewFullPanorama_large").get(0);
		}
		this.createStreetViewControl();
	};

	this.createStreetViewControl = function() {
		function StreetViewControl() {
		}

		StreetViewControl.prototype = new GControl();

		StreetViewControl.prototype.initialize = function() {
			var container = document.createElement("div");
			me.button = $($("#streetViewButton").html());
			me.button.appendTo(container).click(function () {
				me.toggleStreetView();
			});
			myMap.getContainer().appendChild(container);
			return container;
		};

		StreetViewControl.prototype.getDefaultPosition = function() {
			if (smallMap) {
				return new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(83, 7));
			} else {
				return new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(190, 7));
			}
		};

		this.map.addControl(new StreetViewControl());
	};

	this.toggleStreetView = function() {
		if (!this.streetViewOn) {
			this.button.children("#streetViewButtonInner").css({"font-weight": "bold", "border-color": "rgb(52, 86, 132) rgb(108, 157, 223) rgb(108, 157, 223) rgb(52, 86, 132)"});
			this.map.addOverlay(this.overlay);
			this.marker.setLatLng(this.map.getCenter());
			this.map.addOverlay(this.marker);
			GEvent.trigger(this.marker, "click");
			this.streetViewOn = true;
		} else {
			this.button.children("#streetViewButtonInner").css({"font-weight": "normal", "border-color": "white rgb(176, 176, 176) rgb(176, 176, 176) white"});
			this.map.removeOverlay(this.overlay);
			this.map.removeOverlay(this.marker);
			this.streetViewOn = false;
		}
	};

	this.addStreetViewGuyMarker = function(point) {
		var guyIcon = new GIcon();
		guyIcon.image = "images/man_arrow-0.png";
		guyIcon.transparent = "images/man-pick.png";
		guyIcon.imageMap = [
			26,13, 30,14, 32,28, 27,28, 28,36, 18,35, 18,27, 16,26,
			16,20, 16,14, 19,13, 22,8
		];
		guyIcon.iconSize = new GSize(49, 52);
		guyIcon.iconAnchor = new GPoint(25, 35);  // near base of guy's feet
		guyIcon.infoWindowAnchor = new GPoint(25, 5);  // top of guy's head

		this.marker = new GMarker(point, {icon: guyIcon, draggable: true});

		GEvent.addListener(this.marker, "dragend", function() {
			me.updateLocation();
		});
		GEvent.addListener(this.marker, "click", function() {
			me.updateLocation();
		});
	};

	this.updateLocation = function() {
		this.streetViewClient.getNearestPanorama(this.marker.getLatLng(), function(response) {
			me.onResponse(response);
		});
	};

	this.onResponse = function(response) {
		if (response.code == 200) {
			var latlng = new GLatLng(response.Location.lat, response.Location.lng);
			this.marker.setLatLng(latlng);
			this.openPanoramaBubble();
		} else {
			this.map.closeInfoWindow();
		}
	};

	this.openPanoramaBubble = function() {
		this.panorama = new GStreetviewPanorama(this.panoSmallNode);
		this.panorama.setLocationAndPOV(this.marker.getLatLng(), null);

		GEvent.addListener(this.panorama, "initialized", function(location) {
			me.onNewLocation(location.latlng);
		});
		GEvent.addListener(this.panorama, "yawchanged", function(yaw) {
			me.onYawChange(yaw);
		});

		window.setTimeout(function() {
			me.openInfoWindow();
		}, 5);
	};

	this.openInfoWindow = function() {
		this.marker.openInfoWindow(this.panoSmallNode, {maxContent: this.panoLargeNode, maxTitle: "Street View"});
		var iw = this.map.getInfoWindow();
		GEvent.addListener(iw, "maximizeend", function() {
			me.panorama.setContainer(me.panoLargeNode);
			window.setTimeout(function() {
				me.panorama.checkResize();
			}, 5);
		});
	};

	this.onYawChange = function(yaw) {
		var GUY_NUM_ICONS = 16;
		var GUY_ANGULAR_RES = 360 / GUY_NUM_ICONS;
		if (yaw < 0) {
			yaw += 360;
		}
		var guyImageNum = Math.round(yaw / GUY_ANGULAR_RES) % GUY_NUM_ICONS;
		var guyImageUrl = "images/man_arrow-" + guyImageNum + ".png";
		this.marker.setImage(guyImageUrl);
	};
	this.onNewLocation = function(latlng) {
		this.marker.setLatLng(latlng);
	};
	this.init();
};

// run page specific scripts when loaded
//alert(gmaps_page_initialize);
//if (typeof gmaps_page_initialize == "function") {
//	gmaps_page_initialize();
//}

function renderMap(address, name, canvas, container,city) {

	gmaps_page_initialize(address, name, canvas, container,city);
}

       

