/*****************
Trip planner related functions and variables.
Add/remove, map icons and markers, trip planner management, marker and map management

*****************************/


var iconWidth = 43;
var iconHeight = 63;

var tpIcon = new GIcon();
tpIcon.iconSize = new GSize(43,63);
tpIcon.iconAnchor = new GPoint(21,63);


var TPObjectArray = new Array();

//store the markers in this array so they can be removed quickly
var TPMarkerArray = new Array();
/*
returns Boolean
used to maintain unique TP items

*/
function isListingInTripPlanner(serviceID)
{
	var wellIsIt = false;
	for(var i=0;i < TPObjectArray.length;i++)
	{
		if(TPObjectArray[i].sectionID == currentSectionID && TPObjectArray[i].serviceID == serviceID && TPObjectArray[i].serviceTypeID == currentServiceTypeID)
		{
			wellIsIt = true;
			return true;
		}
	}
	if(!wellIsIt)
	{
		return false;
	}
}

function hasTripPlannerItems()
{
	return TPObjectArray.length;//0 items = false, anything else = true
}

/*
add a new YMarker object to the TPArray

*/
function addItemToTPObjectArray(lat,lon,name,sectionID,serviceID,typeID,curRow)
{
	
	//make sure this item has not already been added
	//use tpCategory argument to determine icon to use once ready
	var newObject = new Object();
	
	newObject.sectionID = currentSectionID;
	newObject.serviceTypeID = typeID;
	newObject.serviceID = serviceID;
	
	newObject.lat = lat
	newObject.lon = lon;
	newObject.name = name;
	newObject.imageNum = curRow;	
	
	TPObjectArray.push(newObject);
	
}

function removeTPObject(serviceID)
{
	var tmpArray = new Array();//store all values in this array EXCEPT for the match
	for(var i=0;i < TPObjectArray.length;i++)
	{
		if(TPObjectArray[i].sectionID == currentSectionID && TPObjectArray[i].serviceID == serviceID && TPObjectArray[i].serviceTypeID == currentServiceTypeID)
		{
			//we found the item, DON'T DO ANYthing
		}
		else//add this item, we want to keep it
		{
			tmpArray.push(TPObjectArray[i]);
		}
	}
	//reset the TP Array, so it now contains one less value
	TPObjectArray = tmpArray;
}


/*******************************************************/
/*	Trip Planner updating functions  */


function addToTripPlanner(serviceID)
{
	closeInfoWindows();
	
	if(!isListingInTripPlanner(serviceID))//only add if NOT already added
	{
		var xmlHttp;
		try
			{    // Firefox, Opera 8.0+, Safari    
					xmlHttp=new XMLHttpRequest();    }
		  catch (e)
			{    // Internet Explorer    
				try
			  {      xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");}
			catch (e)
			  {      try
				{        xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");}
			  catch (e)
				{
					document.location = 'addToTripPlanner.cfm?serviceID='+serviceID+'&amp;serviceTypeID='+currentServiceTypeID+'&amp;sectionID='+currentSectionID;
				}
			  }
			}
			xmlHttp.onreadystatechange=function()
			  {
			  if(xmlHttp.readyState==4)
				{
					reloadPage();
				}
			  }
			var queryURL = 'CFHandlers/addToTripPlanner.cfm?serviceID='+serviceID+'&serviceTypeID='+currentServiceTypeID+'&sectionID='+currentSectionID;
			xmlHttp.open("GET",queryURL,true);
			xmlHttp.send(null);
	}
}

function removeFromTripPlanner(sectionID,serviceID,theTypeID)
{
	if(confirm('Are you sure you want to remove this item from your trip planner?'))
	{
		closeInfoWindows();
		//update the local TPObject array
		removeTPObject(sectionID,serviceID,theTypeID);
		var xmlHttp;
		try
		{    // Firefox, Opera 8.0+, Safari    
			xmlHttp=new XMLHttpRequest();
		}
		catch (e)
		{    // Internet Explorer    
			try
			{
				xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
			}
			catch (e)
			{
				try
				{
					xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
				}
				catch (e)
				{
					document.location = 'remove_item.cfm?serviceID='+serviceID+'&serviceTypeID='+theTypeID+'&sectionID='+currentSectionID;
				}
			}
		}
		//nothing was caught, proceed
		xmlHttp.onreadystatechange=function()
		{
			if(xmlHttp.readyState==4)
			{
				//query was sucessful, call update to refresh TP DIV = updateTripPlanner();
				reloadPage();
			}
		}
		
		var queryURL = 'CFHandlers/remove_item.cfm?serviceID='+serviceID+'&serviceTypeID='+theTypeID+'&sectionID='+sectionID;
		xmlHttp.open("GET",queryURL,true);
		xmlHttp.send(null);
	}//end of if confirm
}

/*
loop through the TPObjectArray and map the TP
*/

function mapTPMarkers()
{
	/*
	Looping through Array of objects, create markes and map them
	
	newObject.sectionID = currentSectionID;
	newObject.serviceTypeID = currentServiceTypeID;
	newObject.serviceID = serviceID;
	
	newObject.lat = theServiceMarker.getLatLng().lat();
	newObject.lon = theServiceMarker.getLatLng().lng();
	newObject.name = theServiceMarker.name;
	*/
	
	//alert('calling mapTPMarkers');
	var tempGLLArray = new Array();
	
	for(var i=0;i < TPObjectArray.length;i++)
	{
		var newGLL = new GLatLng(TPObjectArray[i].lat,TPObjectArray[i].lon);
		var newTPMarker = new GMarker(newGLL,{icon:tpIcon});
		tempGLLArray.push(newGLL);
		var imageIconName = "";
		var imageNum = TPObjectArray[i].imageNum;
		
		for(var k=0; k < typeObjectArray.length;k++)//find the service type icon
		{			
			if(typeObjectArray[k].id == TPObjectArray[i].serviceTypeID)
			{
				imageIconName = typeObjectArray[k].iconName;
				
				break;
			}
		}

		map.addOverlay(newTPMarker);
		TPMarkerArray.push(newTPMarker);
		newTPMarker.setImage("/tripplanner/i/mapImages/icons/"+imageIconName+imageNum+".gif");
		
		newTPMarker.serviceID = TPObjectArray[i].serviceID;
		
		//when they click the marker, open the editForm
		GEvent.addListener(newTPMarker, "click",function()
		{
			openInfoWindow(this.serviceID);
			
		});
	}
	
	//Keep full-section view
	//zoomToBestView(tempGLLArray);

}

function removeTPMarkers()
{
	//alert('calling removeTPMarkers');
	for(var i=0;i < TPMarkerArray.length;i++)
	{
		map.removeOverlay(TPMarkerArray[i]);
	}
}

function printTripPlanner()
{
	//adjust map view and then call the print function
	var tempGLLArray = new Array();
	
	for(var i=0;i < TPObjectArray.length;i++)
	{
		var newGLL = new GLatLng(TPObjectArray[i].lat,TPObjectArray[i].lon);
		tempGLLArray.push(newGLL);
	}

	zoomToBestView(tempGLLArray);
	print();
}
