var MapService = {
	homeLat: null,
	homeLng: null,
	map: null,
	directions: null,
	parameters: "",
	markers: null,
	zoom: 10,
	page: 1,
	maxPage: 1,
	center: null,
	shopIcon: null,
	popUpIndex: 0,
	type: 'n',
	initialize: function(lat, lng, parameters) {
		MapService.shopIcon = new GIcon();
		MapService.shopIcon.shadow = "/img/tp_images/maps_tp/gm_pin_tire_shadow.png"; MapService.shopIcon.iconSize = new GSize(51, 61); MapService.shopIcon.shadowSize = new GSize(64, 61); MapService.shopIcon.iconAnchor = new GPoint(25, 51); MapService.shopIcon.infoWindowAnchor = new GPoint(37, 13);
		MapService.parameters = parameters; MapService.homeLat = lat; MapService.homeLng = lng; MapService.maxPage = Math.ceil(storedata.length/10.0);
		MapService.map = new GMap2($('#gm_map_map')[0]);
		MapService.map.setCenter(new GLatLng(lat,lng), 0);
		MapService.map.enableScrollWheelZoom();
		MapService.map.addControl(new GScaleControl(), new GControlPosition(G_ANCHOR_BOTTOM_LEFT, new GSize(80,10)));

		var bounds = new GLatLngBounds();
		var adjLat = null; stAdjLat = null; if (lat > storedata[0].lat) {adjLat = lat + .0025; stAdjLat = storedata[0].lat - .0025;} else {adjLat = lat - .0025; stAdjLat = storedata[0].lat + .0025;}
		var adjLng = null; stAdjLng = null; if (lng < storedata[0].lng) {adjLng = lng - .0025; stAdjLng = storedata[0].lng + .0025;} else {adjLng = lng + .0025; stAdjLng = storedata[0].lng - .0025;}
		bounds.extend(new GLatLng(adjLat, adjLng));
		bounds.extend(new GLatLng(stAdjLat, stAdjLng));
		MapService.zoom = MapService.map.getBoundsZoomLevel(bounds);
		MapService.map.setZoom(MapService.zoom);
		MapService.center = bounds.getCenter();
		MapService.map.setCenter(MapService.center); MapService.setZoom(MapService.zoom);

		GEvent.addListener(MapService.map, 'zoomend', function (oldValue, newValue) { MapService.setZoom(newValue); } );

		var baseIcon = new GIcon(); baseIcon.image = "/img/tp_images/maps_tp/gm_pin_house.png"; baseIcon.shadow = "/img/tp_images/maps_tp/gm_pin_house_shadow.png"; baseIcon.iconSize = new GSize(39, 46); baseIcon.shadowSize = new GSize(58, 46); baseIcon.iconAnchor = new GPoint(19, 46); baseIcon.infoWindowAnchor = new GPoint(30, 6);
		MapService.map.addOverlay(new GMarker(new GLatLng(lat, lng), { icon:baseIcon }));

	},
	createOverlay: function(index) {
		var numberedIcon = new GIcon(MapService.shopIcon);
		numberedIcon.image = "/img/tp_images/maps_tp/gm_pin_tire" + storedata[index].imageIndex + ".png";
	  	var shopMarker = new GMarker(new GLatLng(storedata[i].lat, storedata[i].lng), { icon:numberedIcon });
		shopMarker.storeIndex = index;
		GEvent.addListener(shopMarker, "click", function() { MapService.showPopUp(this.storeIndex); });
		MapService.map.addOverlay(shopMarker);
	},
	changeMapType: function(type) {
		var list = $(".gm_mapViewsMatrix > li");
		list.removeClass("on");
		if(type == 'n') {
			MapService.map.setMapType(G_NORMAL_MAP);
			$(".gm_mapViewsMatrix > li:eq(0)").addClass("on");
		}
		else if(type == 'h') {
			MapService.map.setMapType(G_HYBRID_MAP);
			$(".gm_mapViewsMatrix > li:eq(1)").addClass("on");
		}
		else if(type == 's') {
			MapService.map.setMapType(G_SATELLITE_MAP);
			$(".gm_mapViewsMatrix > li:eq(2)").addClass("on");
		}
		MapService.type = type;
	},
	showPopUp: function(index) {
		var popUp = $("#gm_map_popup");
		var currentZoom = MapService.map.getZoom();
		popUp.hide();

		$("#storeStreetLink").hide();
		var panoClient = new GStreetviewClient();
		var iconPoint = new GLatLng(storedata[index].lat, storedata[index].lng);
		panoClient.getNearestPanorama(iconPoint, MapService.showStreetViewLink);

		var popClass = "gm_map_popup";
		if (storedata[index].name == "Tires Plus Licensee Store")
			popClass = "gm_map_popup_total";
		else if (storedata[index].name == "Wheel Works Store")
			popClass = "gm_map_popup_wheel";
		else if (storedata[index].name == "Hibdon Tires Plus Store")
			popClass = "gm_map_popup_hibdon";
		else if (storedata[index].name == "Michel Tires Plus Store")
			popClass = "gm_map_popup_michel";

		popUp.attr("class", popClass);

		var pixels = MapService.map.getCurrentMapType().getProjection().fromLatLngToPixel(iconPoint,currentZoom);
		var point = MapService.map.getCurrentMapType().getProjection().fromPixelToLatLng(new GPoint(parseInt(pixels.x-150),pixels.y), currentZoom);
		MapService.map.panTo(new GLatLng(point.lat(), point.lng()));
		$("#storeName").text(storedata[index].name);
		$("#storeNum").text(storedata[index].num);
		$("#storeNumber").val(storedata[index].num);
		$("#zipcode_hidden").val(storedata[index].zip);
		$("#storeAdd1").text(storedata[index].add1);
		$("#storeAdd2").text(storedata[index].add2);
		$("#storePhone").text(storedata[index].phone);
	//	$("#storeDirLink").attr("href", "/locate/getDirections.action?" + MapService.parameters + "&storeNumber=" + storedata[index].num);
	//	$("#storeAppLink").attr("href", "./appointment/selectStore.jsp?storeNumber=" + storedata[index].num);
		$("#storeDirLink").attr("href", '#');
		$("#storeAppLink").attr("href", '#');
		$("#storeYourShopLink").attr("href", '#');
		$("#storeHours").html(storedata[index].hours);
		$("#storeStreetLink").click(function () { MapService.showStreetView(index); return false;});
		if (storedata[index].app == 1) $("#storeApp").show(); else $("#storeApp").hide();
		MapService.popUpIndex = index;
		popUp.fadeIn();
	},
	showStreetViewLink: function (panoData) {
		if (panoData != null && panoData.code == 200)
			$("#storeStreetLink").show();
   	},
	focusOn: function(index) {
		MapService.map.setCenter(new GLatLng(storedata[index].lat, storedata[index].lng), 13);
		MapService.showPopUp(index);
	},
	closePopUp: function () {
		$("#gm_map_popup").fadeOut();
	},
	setZoom: function(value) {
		Ext.getCmp('gmapSlider').setValue(value);
	},
	resetCenter: function() {
		MapService.map.setCenter(MapService.center, MapService.zoom);
	},
	showStreetView: function(index) {
		$("#gm_streetview_popup").show();
		var loc = new GLatLng(storedata[index].lat, storedata[index].lng)
		panoramaOptions = { latlng:loc };
		var myPano = new GStreetviewPanorama($("#gm_streeview_panel")[0], panoramaOptions);
		GEvent.addListener(myPano, "error", MapService.handleNoFlash);
	},
	prevPage: function() {
		if (MapService.page <= 1)
			return;
		else {
			$("#page" + MapService.page).hide();
			MapService.page--;
			$("#page" + MapService.page).show();
			$("#nextPage").show();
			$("#pageSeparator").show();
		}
		if (MapService.page == 1) {
			$("#prevPage").hide();
			$("#pageSeparator").hide();
		}
		$("#pageNumber").text(MapService.page);
	},
	nextPage: function() {
		if (MapService.page >= MapService.maxPage)
			return;
		else {
			$("#page" + MapService.page).hide();
			MapService.page++;
			$("#page" + MapService.page).show();
			$("#prevPage").show();
			$("#pageSeparator").show();
		}
		if (MapService.page == MapService.maxPage) {
			$("#nextPage").hide();
			$("#pageSeparator").hide();
		}
		$("#pageNumber").text(MapService.page);
	},
	setPage: function(page) {
		$("#page" + MapService.page).hide();
		MapService.page = page;
		$("#page" + MapService.page).show();
		$("#prevPage").show();
		$("#pageSeparator").show();
		if (MapService.page == MapService.maxPage) {
			$("#nextPage").hide();
			$("#pageSeparator").hide();
		}
		$("#pageNumber").text(MapService.page);
	},
	initializePrint: function (centerLat, centerLng, zoom, popUp, pIndex, page, isLargeMode, maptype) {
		if (maptype != 'n')
			MapService.changeMapType(maptype);
		MapService.map.setCenter(new GLatLng(centerLat, centerLng), zoom);
		if (popUp == 1)
			MapService.showPopUp(pIndex);
		if (!isLargeMode && page > 1 && page <= MapService.maxPage)
			MapService.setPage(page);
	},
	print: function (parameters) {
		var popupParams = "&popUp=0";
		if ($("#gm_map_popup").is(":visible"))
			popupParams = "&popUp=1&pIndex=" + MapService.popUpIndex;
		window.open("/locate/displayMap.action?" + parameters + "&print=1&zoom=" + MapService.map.getZoom() + "&centerLat=" + MapService.map.getCenter().lat() + "&centerLng=" + MapService.map.getCenter().lng() + popupParams + "&p=" + $("#pageNumber").text() + "&maptype=" + MapService.type,'', "menubar=yes,toolbar=yes,scrollbars=yes,width=900,height=680,resizable=yes");
	},
	handleNoFlash: function (errorCode) {
		if (errorCode == 603) {
			alert("In order to use Street View you must have Adobe Flash installed.  Please visit http://get.adobe.com/flashplayer/.");
			return;
		}
	}
};
