var estMapInfo;
var chainMap;
var estMap;
var theaterMarkers = [];
var allMarkers = [];

function initialize_map() {
	if ((GBrowserIsCompatible() == false) || (typeof estMapInfo == "undefined")) return;

	// Initialize any maps present
	if (typeof estMap != "undefined") {
		// Load the establishment map with green P locations and the establishment itself
		estMap = createProfileMapObject("estMap");
		if (typeof gDirections == "undefined") {
			loadProfileMapMarkers(estMap);
			loadGreenP(estMap);
		}
	}
	if (typeof chainMap != "undefined") {
		$("#tabCell").bind('tabsshow', function(event, ui) {
			if (ui.panel.id != "tabChain") return true;
			if (chainMap == null) {
				chainMap = createProfileMapObject("chainLocations");
				chainMap.checkResize();
				loadProfileChainLocations(chainMap);
			}
		});
   }
}

function createProfileMapObject(htmlId) {
	var map = new GMap2( $("#"+htmlId).get(0) );
	map.addControl(new GSmallMapControl());
	
	if (typeof gDirections != "undefined") return map;
	
	var location = new GLatLng(estMapInfo.lat, estMapInfo.lon);
	map.setCenter(location, 14);
	
	return map;
}

function loadProfileMapMarkers(map) {

	var location = new GLatLng(estMapInfo.lat, estMapInfo.lon);
	var marker = new GMarker(location);
	allMarkers.push(marker);
	
	var tooltip = "<div><div style='text-align:center'><b>"+estMapInfo.name+"</b></div>";
	tooltip += "<span style='font-size: 10px;'>"+estMapInfo.addr+"</span></div>";
	marker.tooltip = new Tooltip(marker, tooltip, 0);
	marker.tooltip.initialize(map);

	GEvent.addListener (marker, 'mouseover', profileMapHover);
	GEvent.addListener (marker, 'mouseout', profileMapUnHover);
	
	map.addOverlay(marker);
}

function loadGreenP(map) {
	if (typeof greenP == "undefined") return;

	var gpIcon = new GIcon();
	gpIcon.image = "images/gpIcon.png";
	gpIcon.iconSize = new GSize(24, 38);
	gpIcon.iconAnchor = new GPoint(10, 36);
	gpIcon.shadow = "images/gpshadow.png";
	gpIcon.shadowSize = new GSize(37, 34);
	gpIcon.infoShadowAnchor = new GPoint(18, 25);
	gpIcon.infoWindowAnchor = new GPoint(1, 1);

	for (var i=0; i<greenP.length; i++) {

		var marker = new GMarker( new GLatLng(greenP[i].lat, greenP[i].lon), gpIcon );
		allMarkers.push(marker);
		var type = (greenP[i].type == 0) ? "Garage" : "Surface";
		var tooltip = "<div><b>"+greenP[i].addr+"</b><br/>Type: "+type+"</div>";
		marker.tooltip = new Tooltip(marker, tooltip, 0);
		marker.tooltip.initialize(map);

		GEvent.addListener (marker, 'mouseover', profileMapHover);
		GEvent.addListener (marker, 'mouseout', profileMapUnHover);
		
		map.addOverlay(marker);
	}
}

function zoomMapTo(map, markerArray) {
	var boundry = new GLatLngBounds();
	for (var i=0; i<markerArray.length; i++) {
		boundry.extend(markerArray[i].getLatLng());
	}
	var zoomLevel = map.getBoundsZoomLevel(boundry);
	var centerPoint = boundry.getCenter();
	map.setCenter(centerPoint, zoomLevel);
}

function loadProfileChainLocations(map) {

	var boundry = new GLatLngBounds();
	for (var i=0; i<chainLoc.length; i++) {

		var gpIcon = new GIcon();
		gpIcon.image = "images/location_pt/"+chainLoc[i].index+".png";
		gpIcon.iconSize = new GSize(24, 38);
		gpIcon.iconAnchor = new GPoint(10, 36);
		gpIcon.shadow = "images/gpshadow.png";
		gpIcon.shadowSize = new GSize(37, 34);
		gpIcon.infoShadowAnchor = new GPoint(18, 25);
		gpIcon.infoWindowAnchor = new GPoint(1, 1);
	
		var point = new GLatLng(chainLoc[i].lat, chainLoc[i].lon);
		var marker = new GMarker(point, gpIcon);
		var tooltip = "<div><div style='text-align:center'><b>"+chainLoc[i].name+"</b></div>";
		tooltip += "<span style='font-size: 10px;'>"+chainLoc[i].addr+"</span></div>";
		marker.tooltip = new Tooltip(marker, tooltip, 0);
		marker.tooltip.initialize(map);

		GEvent.addListener (marker, 'mouseover', profileMapHover);
		GEvent.addListener (marker, 'mouseout', profileMapUnHover);
		
		map.addOverlay(marker);

		boundry.extend(point);
	}
	var zoomLevel = map.getBoundsZoomLevel(boundry) - 1;
	var centerPoint = boundry.getCenter();
	map.setCenter(centerPoint, zoomLevel);
}

function profileMapHover() {
	this.tooltip.show();
	this.tooltip.redraw(true);
}

function profileMapUnHover() {
	this.tooltip.hide();
}

function loadTheaterLocations(map) {
	if (typeof theaterMapInfo == "undefined") return;

	var gpIcon = new GIcon();
	gpIcon.image = "images/movies/gmIcon_movie.png";
	gpIcon.iconSize = new GSize(24, 38);
	gpIcon.iconAnchor = new GPoint(10, 36);
	gpIcon.shadow = "images/gpshadow.png";
	gpIcon.shadowSize = new GSize(37, 34);
	gpIcon.infoShadowAnchor = new GPoint(18, 25);
	gpIcon.infoWindowAnchor = new GPoint(1, 1);

	map.checkResize();
	for (var i=0; i<theaterMapInfo.length; i++) {

		var latLon = new GLatLng(theaterMapInfo[i].lat, theaterMapInfo[i].lon);
		var marker = new GMarker( latLon, gpIcon );
		theaterMarkers.push(marker);
		var tooltip = "<div><b>"+theaterMapInfo[i].name+"</b><br/>"+theaterMapInfo[i].addr+"</div>";
		marker.tooltip = new Tooltip(marker, tooltip, 0);
		marker.tooltip.initialize(map);

		GEvent.addListener(marker, 'mouseover', profileMapHover);
		GEvent.addListener(marker, 'mouseout', profileMapUnHover);
		
		map.addOverlay(marker);
	}
	var combinedArray = [];
	for (var i=0; i<allMarkers.length; i++) {
		combinedArray.push(allMarkers[i]);
		if (typeof theaterMarkers[i] != "undefined") {
			combinedArray.push(theaterMarkers[i]);
		}
	}
	zoomMapTo(estMap, combinedArray);
}

function unloadTheaterLocations(map) {
	map.checkResize();
	for (var i=0; i<theaterMarkers.length; i++) {
		map.removeOverlay(theaterMarkers[i]);
	}
	theaterMarkers = [];
	estMap.returnToSavedPosition();
}

