//calling page MUST call initMap function, passing div tag as param
//calling page must include a form named form1, that calls saveRoute() once all other fields are validated
//form1 must also contain a hidden field to store the final list of points in, thePointList
/**************************************************/


/*
Store the services as GMarkers in an array
The following vars will be accessible:
name
serviceID
catArray -> ID of service types, matches the toggle button values in the map control panel
url
address
city
state
zip
phone
email

*/
var serviceMarkerArray = new Array();

var routePointIcon = new GIcon();
routePointIcon.iconSize = new GSize(43,63);
routePointIcon.iconAnchor = new GPoint(21,63);
routePointIcon.image = "/tripplanner/i/mapImages/icons/lodging1.gif";


//function addServiceToMap(sID,latitude,longitude,name,catID)
function addServiceToMap(sID,latitude,longitude,name,catID,url,address,city,state,zip,phone,email,image)
{
	//loop through service array and check to see if we've already added this service. If so, we are adding it again under a second category, so just update catArray
	var added = false;
	for(var k = 0;k < serviceMarkerArray.length;k++)
	{
		if(serviceMarkerArray[k].serviceID == sID)
		{
			added = true;
			//add new category and break
			serviceMarkerArray[k].catArray.push(catID);
			break;
		}
	}
	
	if(!added)//create the new marker, map it
	{
		var marker = new GMarker(new GLatLng(latitude,longitude),{icon:routePointIcon});
	
		marker.serviceID = sID;
		marker.name = name;
		marker.address = address;
		marker.city = city;
		marker.state = state;
		marker.zip = zip;
		
		if(url.indexOf("http:") == -1 && url != '')//add the HTTP://
		{
			marker.url = 'http://'+url;
		}
		else
		{
			marker.url = url;
		}
		
		
		
		marker.phone = phone;
		marker.email = email;
		marker.image = image;
		
		var catArray = new Array();
		catArray.push(catID);
		marker.catArray = catArray;
		serviceMarkerArray.push(marker);
		
		//when they click the marker, open the editForm
		GEvent.addListener(marker, "click",function()
		{
			openInfoWindow(this.serviceID);
			
		});
		
		map.addOverlay(marker);
		marker.hide();
	}
	
}
/**
pop a GMap info window for the given serviceID
*/
function openInfoWindow(sID,sectionID)
{
	for(var k = 0;k < serviceMarkerArray.length;k++)
	{
		if(serviceMarkerArray[k].serviceID == sID)
		{
			var theSMarker = serviceMarkerArray[k];
			var theGLL = theSMarker.getPoint();
			
			var contactHTML = theSMarker.address+'<br />'+theSMarker.city+', '+theSMarker.state+' '+theSMarker.zip+'<br /><a href="'+theSMarker.url+'" target="_blank">'+theSMarker.url+'</a><br /><a href="mailto:'+theSMarker.email+'">'+theSMarker.email+'</a><br />'+theSMarker.phone;

				
			
			//.infoBox class is defined in gmap.css
			var htmlText = '<div class="infoBox">';
			if(theSMarker.image != '')
			{
				htmlText += '<img src="/logos/'+theSMarker.image+'" alt="" style="margin-right:5px;" align="left" />';
			}
			
			htmlText += theSMarker.name+'<br />'+contactHTML+'<p><a href="javascript:zoomToStreet('+theGLL.lat()+','+theGLL.lng()+');">zoom to street</a><br /><a href="javascript:addToTripPlanner('+sID+');">add to trip planner</a><br /><a href="/sitePages/tripPlanner.cfm?action=serviceDetails&serviceID='+sID+'&sectionID='+sectionID+'" target="_blank">click for details</a></div>';
			map.openInfoWindowHtml(theGLL,htmlText);
			map.setCenter(theGLL);
			break;
		}
	}
}



/*
getServiceMarker to return the service Marker instance
*/

function getServiceMarker(serviceID)
{
	for(var k = 0;k < serviceMarkerArray.length;k++)
	{
		if(serviceMarkerArray[k].serviceID == serviceID)
		{
			return serviceMarkerArray[k];
			break;
		}
	}
}

/*
event handler for onclick service checkboxes, from within the control panel

*/

function choseService(serviceTypeID)
{
	
		currentServiceTypeID = serviceTypeID;
		openTabById("searchResults");
}


/*
show services in this category; hide all others
*/

function showCategory(SERVICETYPEID)
{
	//make sure SERVICETYPEID != 0	
	if(SERVICETYPEID != 0)
	{
		
		//First, turn off ALL the markers
		for(var k = 0;k < serviceMarkerArray.length;k++)
		{
			serviceMarkerArray[k].hide();
		}


		//update the global current type variable
		currentServiceTypeID = SERVICETYPEID;
		//close any open windows
		map.closeInfoWindow();
		//store the services that should be displayed in this temp array, so we can upate the map view when done
		var tmpGLLArray = new Array();
		
		//use a counter to determine which icon to use
		var imageCounter = 1;
		var imageIconName = "";
		
		for(var k=0; k < typeObjectArray.length;k++)//find the service type icon
		{
			if(typeObjectArray[k].id == SERVICETYPEID)
			{
				
				imageIconName = typeObjectArray[k].iconName;
				//alert(imageIconName);
				break;
			}
		}


		

		//startRow identifies the beginning index of the marker array. 
		var startRow = 1;
		
		
		for(var k = 0;k < serviceMarkerArray.length;k++)
		{
			var showThisMarker = false;
			for(var x = 0;x < serviceMarkerArray[k].catArray.length; x++)
			{
				if(serviceMarkerArray[k].catArray[x] == SERVICETYPEID)
				{
					showThisMarker = true;
					break;
				}
			}
			
			if(showThisMarker)
			{
				if(imageCounter > 25)
				imageCounter = 1;
				tmpGLLArray.push(serviceMarkerArray[k].getLatLng());
				//update the image so this service is displaying the correct icon
				serviceMarkerArray[k].setImage("/tripplanner/i/mapImages/icons/"+imageIconName+imageCounter+".gif");
				//alert(serviceMarkerArray[k].catArray+' :: '+imageIconName+imageCounter);
				imageCounter++;
				serviceMarkerArray[k].show();
			}
			else{serviceMarkerArray[k].hide();}
			
		}
		//update the map view using our tmp array
		//Maintain full-section view, allow user to control zoom
		//zoomToBestView(tmpGLLArray);
		
		//update the description DIV, using the AJAX call in dhtml.js
		loadServices(currentSectionID,SERVICETYPEID);
	}//END OF IF IT != 0
}


function hideServiceMarkers()
{
	//alert('calling hideServiceMarkers');
	for(var k = 0;k < serviceMarkerArray.length;k++)
	{
		serviceMarkerArray[k].hide();
	}
}
