// JavaScript Document


	var map; //Map being manipulated
	//var mgr; //Marker manager
	var ZoomLevel;
	var visCenterLat = "40.0506548";
	var visCenterLng = "-76.2549233";
	
	var arrAllPins = new Array();
	//Set up our groups of push pins
	arrAllPins[0] = new pushPinGroup("dining", "/maps/images/map_icon_dining.png", "/maps/images/map_icon_dining_shadow.png", "/maps/images/icon_dining_f2.gif", "/maps/images/icon_dining.gif", 31, 22, -1, 20, 46, 31, 18, 25);
	arrAllPins[1] = new pushPinGroup("lodging", "/maps/images/map_icon_lodging.png", "/maps/images/map_icon_lodging_shadow.png", "/maps/images/icon_lodging_f2.gif", "/maps/images/icon_lodging.gif", 23, 26, 0, 28, 46, 31, 18, 25);
	arrAllPins[2] = new pushPinGroup("shopping", "/maps/images/map_icon_shopping.png", "/maps/images/map_icon_shopping_shadow.png", "/maps/images/icon_shopping_f2.gif", "/maps/images/icon_shopping.gif", 22, 30, 4, 33, 46, 31, 18, 25);
	arrAllPins[3] = new pushPinGroup("things_to_do", "/maps/images/map_icon_to_do.png", "/maps/images/map_icon_to_do_shadow.png", "/maps/images/icon_to_do_f2.gif", "/maps/images/icon_to_do.gif", 27, 23, -1, 24, 46, 31, 18, 25);
	var eventsGroupID = 4;
	arrAllPins[eventsGroupID] = new pushPinGroup("events", "/maps/images/map_icon_events.png", "/maps/images/map_icon_events_shadow.png", "/maps/images/icon_events_f2.gif", "/maps/images/icon_events.gif", 24, 24, 24, -1, 46, 31, 18, 25);
	var travePlanGroupID = 5;
	arrAllPins[travePlanGroupID] = new pushPinGroup("travel plan", "/maps/images/map_plan.png", "/maps/images/map_icon_plan_shadow.png", "/maps/images/icon_plan_f2.gif", "/maps/images/icon_plan.gif", 25, 25, 0, 0, 46, 31, 18, 25);
	var informationGroupID = 6;
	arrAllPins[informationGroupID] = new pushPinGroup("info", "/maps/images/map_icon_info.png", "/maps/images/map_icon_info_shadow.png", "", "", 23, 27, 12, 27, 46, 31, 18, 25);
	
	

	//Create a generic icon for the map.  This is extended functionality which is not required.
	var baseIcon = new GIcon();
	baseIcon.shadow = "/maps/images/shadow.png";
	baseIcon.iconSize = new GSize(24, 30);
	baseIcon.shadowSize = new GSize(46, 31);
	baseIcon.iconAnchor = new GPoint(9, 34);
	baseIcon.infoWindowAnchor = new GPoint(9, 2);
	baseIcon.infoShadowAnchor = new GPoint(18, 25);

	//********************************************
	//----- Stop page scrolling if wheel over map ----
	//********************************************
	function wheelevent(e)
	{
		if (!e) e = window.event;
		if (e.preventDefault) e.preventDefault();
		e.returnValue = false;
	}

	//********************************************
	//*
	//********************************************
	function pushPinGroup(name, image, shadow, extIcon, extIconDepressed, imageWidth, imageHeight, iconAnchorX, iconAnchorY, shadowWidth, shadowHeight, shadowAnchorX, shadowAnchorY) {
		this.reqIS = null;
		this.GroupName = name;
		this.Image = image;
		this.imageWidth = imageWidth;
		this.imageHeight = imageHeight;
		this.iconAnchorX = iconAnchorX;
		this.iconAnchorY = iconAnchorY;
		this.shadowWidth = shadowWidth;
		this.shadowHeight = shadowHeight;
		this.shadowAnchorX  = shadowAnchorX;
		this.shadowAnchorY = shadowAnchorY;
		this.Shadow = shadow;
		this.markers = new Array();    //parallels arrMarkerInfo array
		this.arrMarkerInfo = new Array(); //parallels markers array
		this.displayed = false;
		this.addedToMap = false;
		this.externalIcon = extIcon;
		this.externalIconDepressed = extIconDepressed;		
	}
	//********************************************
	//*
	//********************************************
	function objMarker(title, icon, subType, lat, lng, gPoint, shortInfo, longInfo, url, id, add1, add2, city, state, zip, phone, tollfreephone, evtDate, evtTime) {
		this.id = id;
		this.title = title;
		this.icon = icon;
		this.subType = subType;
		this.point = gPoint;
		this.lat = lat;
		this.lng = lng;
		this.displayed = true; //based on user subType checkbox status checked = displayed
		this.add1 = add1;
		this.add2 = add2;
		this.city = city;
		this.state = state;
		this.zip = zip;
		this.phone = phone;
		this.tollfreephone = tollfreephone;
		this.evtDate = evtDate;
		this.evtTime = evtTime;
		
		//now obtained dynamically this.shortInfo = shortInfo;
		//now obtained dynamically this.longInfo = longInfo;
		//now obtained dynamically this.url = url;
	}
	
	
		
	//********************************************
	//*
	//********************************************
	function changeMap(strInput){ //strInput format  "lat,lng,zoom"
		//Change location of map center and zoom level
		var arrOptions=strInput.split(",");
		if(arrOptions.length >= 3){
			map.setCenter(new GLatLng(arrOptions[0],arrOptions[1]), parseInt(arrOptions[2]));
			document.getElementById('MapTitle').innerHTML = arrOptions[3];
		}
	}

	//********************************************
	//*
	//********************************************
	function toggleIconGroup(pinGroupId){
		//Add pins to map for first use if not currently on map/in marker manager
		if (arrAllPins[pinGroupId].displayed == false)
			toggleIconGroupOn(pinGroupId);
		else //Toggle current pin group
			toggleIconGroupOff(pinGroupId);
	}
	
	//********************************************
	//*
	//********************************************
	function toggleIconGroupOn(pinGroupId){
		//if not currently displayed and not added to map
		if (arrAllPins[pinGroupId].displayed == false && arrAllPins[pinGroupId].addedToMap == false){
			//add icons/listings to map
			getListingsUsingXML(arrAllPins[pinGroupId].GroupName, pinGroupId);
			arrAllPins[pinGroupId].addedToMap = true;
			arrAllPins[pinGroupId].displayed = true;
		} else{ //display pin group
			for (var i = 0; i < arrAllPins[pinGroupId].markers.length; i++) {
				var marker = arrAllPins[pinGroupId].markers[i];
				var markerInfo = arrAllPins[pinGroupId].arrMarkerInfo[i];
				if (markerInfo.displayed == true && marker.isHidden() && arrAllPins[pinGroupId].displayed == false) {
					//Show markers on map
				 	marker.show();
					markerInfo.displayed = true;
				} 
			}
			
		}
		arrAllPins[pinGroupId].displayed = true;
		//reset the icon
		if (document.getElementById("icon_"+pinGroupId)){
			document.getElementById("icon_"+pinGroupId).src = arrAllPins[pinGroupId].externalIconDepressed;
		}
		
	}

	//********************************************
	//*
	//********************************************
	function toggleIconGroupOff(pinGroupId){			
		for (var i = 0; i < arrAllPins[pinGroupId].markers.length; i++) {
			var marker = arrAllPins[pinGroupId].markers[i];
			if (marker.isHidden() == false){//need to hide if member is being viewed from member page and view TP was clicked && arrAllPins[pinGroupId].displayed == true){
				//Hide markers on map
				marker.hide();
				marker.closeInfoWindow();
				//do not change info status as that only tracks users checkbox request
				
			}
		}
		arrAllPins[pinGroupId].displayed = false;
		//reset the icon
		document.getElementById("icon_"+pinGroupId).src = arrAllPins[pinGroupId].externalIcon;
	}
	//********************************************
	//*
	//********************************************
	function toggleIconGroupSubTypes(pinGroupId, subType, boolShow, parentDivID){
		
		//this Assumes all icons have been added to map
		for (var i = 0; i < arrAllPins[pinGroupId].markers.length; i++) {
			var marker = arrAllPins[pinGroupId].markers[i];
			var markerInfo = arrAllPins[pinGroupId].arrMarkerInfo[i];
			
			if(markerInfo.subType.indexOf(subType) != -1){				
				if(boolShow){
					//bkw may need to check if group is displayed before calling marker.show()
					//need to set displayed to true so marker will be shown.
					markerInfo.displayed = true; 
					marker.show();
				} else {
					//special logic just in case the member is listed in more than one sub type category
					// Logic checks ids of check boxes which are checked and verify that those ids are contained in the subType which should be a comma separated list of items
					var boolNotInOtherSubType = true;
					
					if (markerInfo.subType.length > subType.length + 2){ //try to limit the amount of checks. marker subtype may have a comma or two
					
						var ckboxes = document.getElementById(parentDivID).getElementsByTagName('input');				
						for(j=0; j<ckboxes.length && boolNotInOtherSubType == true; j++){

							if (ckboxes[j].type=="checkbox" && ckboxes[j].checked == true && markerInfo.subType.toLowerCase().indexOf(ckboxes[j].id.toLowerCase()) != -1){								
								boolNotInOtherSubType = false;
							}
						}
					}
					
					if (boolNotInOtherSubType){
						markerInfo.displayed = false;
						marker.hide();
						marker.closeInfoWindow();
					}
				}
			}
		}
	}
	
	//********************************************
	//*
	//********************************************
	function selectAllSubTypes(groupID, parentDivID){
		var ckboxes = document.getElementById(parentDivID).getElementsByTagName('input');
		for (var i = 0; i < ckboxes.length; i++){
			//if (doc[i].id.indexOf('subtype_0_'+groupID) == 0){
				if(ckboxes[i].checked == false){
					ckboxes[i].checked = true;
					toggleIconGroupSubTypes(groupID, ckboxes[i].id, true, parentDivID)
				}
			//}
		}
	}
	
	//********************************************
	//*
	//********************************************
	function deselectAllSubTypes(groupID, parentDivID){
		var ckboxes = document.getElementById(parentDivID).getElementsByTagName('input');
		for (var i = 0; i < ckboxes.length; i++){
			//if (doc[i].id.indexOf('subtype_0_'+groupID) == 0){
				if(ckboxes[i].checked == true){
					ckboxes[i].checked = false;
					toggleIconGroupSubTypes(groupID, ckboxes[i].id, false, parentDivID)
				}
			//}
		}
	}
	
	//********************************************
	//*
	//********************************************
	function resetEvents(){
		for (var i = 0; i < arrAllPins[eventsGroupID].markers.length; i++) {
			map.removeOverlay(arrAllPins[eventsGroupID].markers[i]);
		}
		arrAllPins[eventsGroupID].markers.length = 0;
		
		//var mgrOptions = { borderPadding: 1, maxZoom: 17, trackMarkers: false };
		//mgr = new MarkerManager(map, mgrOptions);
		//There is no way to remove markers using the marker manager. Can I add the events markers in some other way?
		
		getListingsUsingXML(arrAllPins[eventsGroupID].GroupName, eventsGroupID);
		arrAllPins[eventsGroupID].addedToMap = true;
		arrAllPins[eventsGroupID].displayed = true;
	}
	
	//********************************************
	//*
	//********************************************
	function resetTravelPlannerMarkers(){
		for (var i = 0; i < arrAllPins[travePlanGroupID].markers.length; i++) {
			map.removeOverlay(arrAllPins[travePlanGroupID].markers[i]);
		}
		arrAllPins[travePlanGroupID].markers.length = 0;

		
		getListingsUsingXML(arrAllPins[travePlanGroupID].GroupName, travePlanGroupID);
		arrAllPins[travePlanGroupID].addedToMap = true;
		arrAllPins[travePlanGroupID].displayed = true;
	}
	
	//********************************************
	//*
	//********************************************
	function hideHiddenMarkers(){ //This is not used since marker manager is not currently used
		//This function is called when marger manager fires a changed event from either a mgr refresh or zoom change.
		for (var j = 0; j < arrAllPins.length; j++) {
			if (arrAllPins[j].displayed == false) {
				for (var i = 0; i < arrAllPins[j].markers.length; i++) {
					//Remove pins from map
					arrAllPins[j].markers[i].hide();
				} //else show action not needed
			}	
		}
	}
	  
	//********************************************
	//*
	//********************************************
	function setInitialMapLocation(zoom){
		ZoomLevel = zoom
		//Check the querystring for default map location and zoom level
		if (inLat.length > 0 || inLng.length > 0){
			//center on input lat lng if it exists
			map.setCenter(new GLatLng(inLat, inLng), inZoom);
		} else if(inZip.length > 0 || (inAddress.length > 0 && inCity.length > 0 && inState.length > 0)){
			//center map on address if it exists call google to geocode
			var inputAddresses = new Array(3);
			inputAddresses[0] = inAddress + "," + inCity + "," + inState + " " + inZip;
			inputAddresses[1] = inAddress + "," + inCity + "," + inState;
			inputAddresses[2] = inZip;
			
			centerOnAddressPoint(inputAddresses, 0, ZoomLevel);
		} else if (inMemberID > 0 && inMemLat != 0 && inMemLng != 0){
			//center on specified member
			map.setCenter(new GLatLng(inMemLat, inMemLng), inZoom);
		} else {
			//Center map on visitors center
			map.setCenter(new GLatLng(visCenterLat, visCenterLng), ZoomLevel);
		}		
	}


	//********************************************
	//*
	//********************************************
	function addPointToMarkerList(lat, lng, title, arrObjAllMarkers, subType, sectionID, ID, add1, add2, city, state, zip, phone, tollfreephone, evtDate, evtTime){
		var point = new GLatLng(lat, lng);
		var icon = new GIcon(baseIcon);
		icon.image = arrObjAllMarkers[sectionID].Image;
		icon.iconSize = new GSize(arrObjAllMarkers[sectionID].imageWidth, arrObjAllMarkers[sectionID].imageHeight);
		icon.iconAnchor = new GPoint(arrObjAllMarkers[sectionID].iconAnchorX, arrObjAllMarkers[sectionID].iconAnchorY);
		icon.shadow = arrObjAllMarkers[sectionID].Shadow;
		icon.shadowSize = new GSize(arrObjAllMarkers[sectionID].shadowWidth, arrObjAllMarkers[sectionID].shadowHeight);
		icon.infoShadowAnchor = new GPoint(arrObjAllMarkers[sectionID].shadowAnchorX, arrObjAllMarkers[sectionID].shadowAnchorY);

		//Add the marker
		var marker = new GMarker(point, {icon:icon, title:title.replace(/<(br) \/+(>)/g, " ")});
		GEvent.addListener(marker, "click", function() {	
			if(sectionID == eventsGroupID) { //events
				getEventInfoUsingXML(marker,ID);
			} else if(subType == "tp_user") {//travel planner user entry
				//tp user info and event basic info
				getTPUserAddressInfoUsingXML(marker,ID); 
			} else if(subType == "tp_member") {//travel planner member entry
				getMemberInfoUsingXML(marker,ID, false); 
			} else if(ID > 0) {//members
				getMemberInfoUsingXML(marker,ID, true);
			}
		});
		
		//bkw note that this will put two instances of a map request specifying a member id in the array. I may need to make sure member id does not exist in list before pushing

		
		arrObjAllMarkers[sectionID].markers.push(marker);
		
		var strMarkerInfo = new objMarker(title, icon, subType, lat, lng, point, "", "", "", ID, add1, add2, city, state, zip, phone, tollfreephone, evtDate, evtTime) 
		arrObjAllMarkers[sectionID].arrMarkerInfo.push(strMarkerInfo);
	}

	//********************************************
	//*
	//********************************************
	function addMarkerGroupToMap(arrObjAllMarkers, sectionID){
		//alert('adding ' + arrObjAllMarkers[sectionID].markers.length + ' markers');
		//mgr.addMarkers(arrObjAllMarkers[sectionID].markers, 5, 17);			
		//refresh and display the markers via the manager
		//mgr.refresh(); 	
				
		for(var i=0;i< arrObjAllMarkers[sectionID].markers.length;i++){
			map.addOverlay(arrObjAllMarkers[sectionID].markers[i])		
		}
				
		document.getElementById("loadStatus").innerHTML = "Loaded marker group containing " + arrObjAllMarkers[sectionID].markers.length + "  markers"
				
				
	}

	//********************************************
	//********************************************
	//Geocoder functions
	//********************************************
	//********************************************
	var arrTryAddresses;

	function centerOnAddressPoint(tryAddresses, index) {
		arrTryAddresses = tryAddresses;
		//Get the lat and long. When done, call back function is called with valid/invalid point
		var geocoder = new GClientGeocoder();
		geocoder.getLatLng(tryAddresses[index], centerOnAddressPointCallBack);
	}
	
	//********************************************
	//*
	//********************************************
	var arrGeocoderTryAddresses = new Array(4);
	var intGeocoderAddressCnt = 0;
	var boolGeocoderRunning = false;
	var intGeocoderLat = 0;
	var intGeocoderLng = 0;
	
	function geocodeAddress(add1, add2, city, state, zip) {
		//right now I am not using this set of geocoding functions due to the complications of waiting for google to return the result asyncronously
		
		arrGeocoderTryAddresses[0] = add1 + "," + add2 + "," + city + "," + state + " " + zip;
		arrGeocoderTryAddresses[1] = add1 + "," + city + "," + state + " " + zip;
		arrGeocoderTryAddresses[2] = add2 + "," + city + "," + state + " " + zip;
		arrGeocoderTryAddresses[3] = city + "," + state + " " + zip;
		
		intGeocoderLat = 0;
		intGeocoderLng = 0;
		boolGeocoderRunning = true;
		geocoderAddressTry(0); //kick of the geocode
		//need to wait for the geocoder to do its thing
	}
	function geocoderAddressTry(index) {
		//Get the lat and long. When done, store the lat lng values
		alert("here 1");
		intGeocoderAddressCnt = index;
		var geocoder = new GClientGeocoder();
		geocoder.getLatLng(arrGeocoderTryAddresses[index], geocoderAddressCallBack);
	}
	function geocoderAddressCallBack(point){
		alert("here 2");
		if (!point) {
			if (intGeocoderAddressCnt == arrGeocoderTryAddresses.length){
				//alert("Unable to map address.");
				boolGeocoderRunning = false;
			} else {
				//Try the next address
				intGeocoderAddressCnt++;
				geocoderAddressTry(intGeocoderAddressCnt);
			}
		} else {
			//Point Found
			//update the global vars
			intGeocoderLat = point.lat()
			intGeocoderLng = point.lng();

			boolGeocoderRunning = false;
		}
	}
	
	
	
	//********************************************
	//*
	//********************************************
	function centerOnAddressPointCallBack(point){
	  if (!point) {
		if (intAddressCnt == arrTryAddresses.length){
			//No more addresses to try, use the visitors center
			map.setCenter(new GLatLng(visCenterLat, visCenterLng), ZoomLevel);
		} else {
			//Try the next address
			intAddressFromCnt++;
			centerOnAddressPoint(arrTryAddresses, intAddressCnt);
		}
	  } else {
		  //Point Found
		  map.setCenter(point, ZoomLevel);		  
	  }
	}


	//********************************************
	//********************************************
	//* Travel Planner frunctions
	//********************************************
	//********************************************




