/*****************
Trip planner related functions and variables.
Add/remove, map icons and markers, trip planner management, marker and map management

*****************************/




var TPObjectArray = new Array();

var SearchResultsObjectArray = new Array();
var SearchResultsMarkerArray = 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 clearTPArrays()
{
	TPObjectArray = new Array();
	TPMarkerArray = new Array();
}


function isListingInTripPlanner(item_proxy,category)
{
	var wellIsIt = false;
	for(var i=0;i < TPObjectArray.length;i++)
	{
		if(TPObjectArray[i].item_proxy == item_proxy && TPObjectArray[i].category == category)
		{
			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,item_proxy,imagePath,contactHTML,curRow,category)
{
	
	//alert('adding item to tp object array: '+name);
	//make sure this item has not already been added
	//use tpCategory argument to determine icon to use once ready
	var newObject = new Object();
	
	newObject.item_name = name;
	newObject.item_proxy = item_proxy;
	newObject.category = category;
	newObject.imagePath = imagePath;
	newObject.contactHTML = contactHTML;
	
	newObject.lat = lat
	newObject.lon = lon;
	newObject.name = name;
	newObject.imageNum = curRow;
	
	TPObjectArray.push(newObject);
}


function addSearchMarker(Gmarker)
{
	SearchResultsMarkerArray.push(Gmarker);
}
/*store search results*/
function addItemToSearchResults(lat,lon,name,item_proxy,imagePath,contactHTML,curRow,category)
{
	
	//make sure this item has not already been added
	//use tpCategory argument to determine icon to use once ready
	var newObject = new Object();
	
	newObject.item_proxy = item_proxy;
	newObject.category = category;
	newObject.imagePath = imagePath;
	newObject.contactHTML = contactHTML;
	
	newObject.lat = lat
	newObject.lon = lon;
	newObject.name = name;
	newObject.imageNum = curRow;
	
	TPObjectArray.push(newObject);
	
}


/*******************************************************/
/*	Trip Planner updating functions  */

var addWindow = 0;
function addToTripPlanner(theID,theCAT)
{
	

	if(addWindow)
	{
		if(!addWindow.closed)
		{
			addWindow.close();
		}
	}
	addWindow = open("/addToTripPlanner.cfm?id="+theID+"&category="+theCAT,'addWindow',"toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=1,copyhistory=no,width=200,height=125,top=150,left=150"); 
	addWindow.focus();
	
	if(typeof updateTripPlanner != "undefined")
	{
		setTimeout("updateTripPlanner()",1000);
	}
}

function removeFromTripPlanner(theID,theCAT)
{
		
	if(addWindow)
	{
		if(!addWindow.closed)
		{
			addWindow.close();
		}
	}
	addWindow=window.open("/remove_item.cfm?id="+theID+"&cat="+theCAT,"addWindow","toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=1,copyhistory=no,width=200,height=125,top=150,left=150"); 
	addWindow.focus();
	
	if(typeof updateTripPlanner != "undefined")
	{
		setTimeout("updateTripPlanner()",1000);
	}
}



/*
loop through the TPObjectArray and map the TP
*/

function mapTPMarkers()
{
	//remove the current markers
	removeTPMarkers();
	
	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});
		
		newTPMarker.item_proxy = TPObjectArray[i].item_proxy;
		newTPMarker.category = TPObjectArray[i].category;
		newTPMarker.imagePath = TPObjectArray[i].imagePath;
		newTPMarker.contactHTML = TPObjectArray[i].contactHTML;
		
		newTPMarker.lat = TPObjectArray[i].lat
		newTPMarker.lon = TPObjectArray[i].lon;
		newTPMarker.item_name = TPObjectArray[i].name;

		
		
		
		tempGLLArray.push(newGLL);
		var imageIconName = '';
		var imageNum = TPObjectArray[i].imageNum;
		
		
		map.addOverlay(newTPMarker);
		TPMarkerArray.push(newTPMarker);
		
		  if(TPObjectArray[i].category.toLowerCase() == 'pubs')
		  {
			  imageIconName = 'dining';
		  }
		  else if(TPObjectArray[i].category.toLowerCase() == 'activities')
		  {
			  imageIconName = 'attractions';
		  }
		  else if(TPObjectArray[i].category.toLowerCase() == 'campgrounds')
		  {
			  imageIconName = 'camping';
		  }
		  else {imageIconName = TPObjectArray[i].category;}
		
		
		
		newTPMarker.setImage("i/mapImages/icons/"+imageIconName+imageNum+".gif");
		
		newTPMarker.serviceID = TPObjectArray[i].serviceID;
		
		//when they click the marker, open the editForm
		GEvent.addListener(newTPMarker, "click",function()
		{
			listingWindow(this.lon,this.lat,this.item_name,this.item_proxy,this.imagePath,this.contactHTML,this.category);
		});
	}
	
	zoomToBestView(tempGLLArray);
}


/****************/
function hideAllMarkers()
{
	
	removeTPMarkers();
	
	for(var k=0;k < SearchResultsMarkerArray.length; k++)
	{
		SearchResultsMarkerArray[k].hide();
	}

}

function showSearchResultMarkers()
{
	//adjust map view and then call the print function
	var tempGLLArray = new Array();
		
	for(var k=0;k < SearchResultsMarkerArray.length; k++)
	{
		SearchResultsMarkerArray[k].show();
	}

}

function 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();
}

/*
given an array of GLatLng objects, center and zoom the map
*/

function zoomToBestView(arrayGLL)
{
	if(arrayGLL.length)
	{
		var minLat = 90;
		var maxLat = -90;
		var minLon = 180;
		var maxLon = -180;
		for(var k=0; k < arrayGLL.length;k++)
		{
			if(arrayGLL[k].lat() < minLat){minLat = arrayGLL[k].lat();}
			if(arrayGLL[k].lat() > maxLat){maxLat = arrayGLL[k].lat();}
			if(arrayGLL[k].lng() < minLon){minLon = arrayGLL[k].lng();}
			if(arrayGLL[k].lng() > maxLon){maxLon = arrayGLL[k].lng();}
		}
		
		var GLLBounds = new GLatLngBounds(new GLatLng(minLat,minLon),new GLatLng(maxLat,maxLon));
		
		map.setCenter(GLLBounds.getCenter(),map.getBoundsZoomLevel(GLLBounds));
	}
	
}



function createMarker(item_name,item_proxy,longitude,latitude,imageNum,imagePath,contactHTML,category,iconname)
{
	  
	  var point = new GLatLng(latitude,longitude);
	  var marker = new GMarker(point,{icon:tpIcon});
	 	  
	  map.addOverlay(marker);
	  	  
	 
	  if(iconname.toLowerCase() == 'activities')
	  {
		  iconname = 'attractions';
	  }
	  else if(iconname.toLowerCase() == 'campgrounds')
	  {
		  iconname = 'camping';
	  }
	  
      marker.setImage("i/mapImages/icons/"+iconname.toLowerCase()+imageNum+".gif");
	 

	 //STORE THE MARKER FOR MAPPING
     addSearchMarker(marker);
	 
		 
	 GEvent.addListener(marker, "click", function()
	 {
	 	listingWindow(longitude,latitude,item_name,item_proxy,imagePath,contactHTML,category);
	 });
}


function zoomToStreet(longitude,latitude)
{
	var point = new GLatLng(latitude,longitude);
	map.setCenter(point,15);
}
