// JavaScript Document


//the Castpoints that have been created, but not yet been submitted to the database
var cachedCastpoints = new Array();
	//0 = the name of the Castpoint
	//1 = the description of the Castpoint
	//2 = latitude of the location of the Castpoint
	//3 = longitude of the location of the Castpoint


//the innerHTML content of the listPoints_layer
var listLayerContent;

//the innerHTML content of the editPoints_layer
var editPointsContent;




//set a Castpoint at the given point in the cachedCastpoints array 
//then call buildEditCPForm to allow user to enter values for the new Castpoint
function initializeCastpoint(point) {
	

	var newCastpoint = new Array();
	newCastpoint[0] = point;
	newCastpoint[1] = "no description entered";
	newCastpoint[2] = point.lat();
	newCastpoint[3] = point.lng();
	
	cachedCastpoints.push(newCastpoint);
	
	cleanStoredPoints();
	
	//set the content of the listPoints_layer
	buildCPEditForm(point);
	


}







//creates a new name/description pair and a hidden location field in the form editCastpoint_form
//requires that a DIV called "editPoint_layer" exists in the HTML of the display page INSIDE the FORM
//called editCastpoint_form
function buildCPEditForm(point) {

	var cp_layer = document.getElementById("createPoint_layer");
	var ep_layer = document.getElementById("editPoint_layer");
	var lp_layer = document.getElementById("listPoints_layer");
	
	cp_layer.style.display = "none";
	ep_layer.style.display = "";
	lp_layer.style.display = "none";
		
	
	for(var i=0;i < cachedCastpoints.length;i++) {
		if(cachedCastpoints[i][2] == point.lat() && cachedCastpoints[i][3] == point.lng()) {
			var edit_location    = "(" + cachedCastpoints[i][2] + "," + cachedCastpoints[i][3] + ")";
			var edit_name        = cachedCastpoints[i][0];
			var edit_description = cachedCastpoints[i][1];
		}
	}
	
	
	editPointsContent = "<input name='ep_location' type='hidden' value='" + edit_location + "'>";
	editPointsContent += "Change name of Castpoint<br><input name='ep_name' type='text' value='" + edit_name + "'>";
	editPointsContent += "<br><br>Enter a description for your new Castpoint:<br><TEXTAREA name='ep_description' cols='15' rows='10'>" + edit_description + "</TEXTAREA>";
	editPointsContent += "<input name=submit type=button value='Keep This Castpoint' onClick='javascript:updateCastpoint();'><br>";
	editPointsContent += "<input name=cancel type=button value='Discard Castpoint' onClick=\"javascript:removeCastpoint(" + point + ");\"><br>";

	//set the innerHTML of the editPoints_layer to match the editPointContent variable
	ep_layer.innerHTML = editPointsContent;


}









//hide a castpoint form section - hides the particular section of the form corresponding to the supplied point by
//hiding the div with the given point as its ID
function buildCPList() {
	
	if(cachedCastpoints.length > 0) {
	
	var cp_layer = document.getElementById("createPoint_layer");
	var ep_layer = document.getElementById("editPoint_layer");
	var lp_layer = document.getElementById("listPoints_layer");
	
	cp_layer.style.display = "none";
	ep_layer.style.display = "none";
	lp_layer.style.display = "";
	lp_layer.innerHTML     = "";
	
	listPointsContent = "";
	
	for(var i=0;i < cachedCastpoints.length;i++) {
		
		var location = "(" + cachedCastpoints[i][2] + "," + cachedCastpoints[i][3] + ")";
		var real_location = new GLatLng(cachedCastpoints[i][2],cachedCastpoints[i][3]);
		
		
		listPointsContent += "<!--BEGIN CPLIST ITEM: " + location + "-->";
		listPointsContent += cachedCastpoints[i][0];
		listPointsContent += "<br><font size=2>";
		listPointsContent += "<a href='#' onClick=\"selectCastpoint('" + location + "');\">select</a> | ";
		listPointsContent += "<a href='#' onClick=\"buildCPEditForm('" + location + "');\">edit</a> | "; 
		listPointsContent += "<a href='#' onClick=\"removeCastpoint('" + real_location + "');\">delete</a>";
		listPointsContent += "</font>";
		listPointsContent += "<!--END CPLIST ITEM: " + location + "-->";
		listPointsContent += "<br><br>";
	}

	listPointsContent += "<input name=commit_castpoints type=button value='Define These Castpoints' onClick='javascript:commitCastpoints();'>";
	
	} else {
		
		var cp_layer = document.getElementById("createPoint_layer");
		var ep_layer = document.getElementById("editPoint_layer");
		var lp_layer = document.getElementById("listPoints_layer");
	
		cp_layer.style.display = "none";
		ep_layer.style.display = "none";
		lp_layer.style.display = "";
		lp_layer.innerHTML     = "";
		
		listPointsContent = "";	
	}

}








//adds the given Castpoint data to the serialized PHP array in the given hidden DIV called  serializedCastpoints
function cacheNewCastpoint() {

	var castpoint_name        = document.createCastpoint_form.cp_name.value;
	var castpoint_description = document.createCastpoint_form.cp_description.value;
	var castpoint_point       = document.createCastpoint_form.cp_location.value;	

	var new_castpoint = new Array(castpoint_name,castpoint_description,castpoint_point);

	cachedCastpoints.push(new_castpoint);

	
	//set the content of the listPoints_layer
	buildCPList();
	
	//set the innerHTML of the listPoints_layer to match the listPointsContent variable
	var lp_layer = document.getElementById("listPoints_layer");
	lp_layer.innerHTML = listPointsContent;
	
}







//updates the Castpoint that corresponds with the point in the editCastpoint_form.cp_location field
function updateCastpoint() {
	
	var castpoint_name        = document.editCastpoint_form.ep_name.value;
	var castpoint_description = document.editCastpoint_form.ep_description.value;
	var castpoint_point       = document.editCastpoint_form.ep_location.value;	

	for(var i=0;i < cachedCastpoints.length;i++) {
		if("("+cachedCastpoints[i][2]+","+cachedCastpoints[i][3]+")" == castpoint_point) {
			cachedCastpoints[i][0] = castpoint_name;
			cachedCastpoints[i][1] = castpoint_description;
			
		}
	}
	
	//set the content of listPoints_layer
	buildCPList();
	
	//set the innerHTML of the listPoints_layer to match the listPointsContent variable
	var lp_layer = document.getElementById("listPoints_layer");
	lp_layer.innerHTML = listPointsContent;
	
}






//highlights an entry in the list of stored Castpoints based on the given point
function highlightCastpoint(point) {
	
	//set the content of listPoints_layer
	buildCPList();
	
	var list     = listPointsContent;
	var new_list = "";
	
	var begin_tag = "<!--BEGIN CPLIST ITEM: " + point + "-->";
	var end_tag   = "<!--END CPLIST ITEM: " + point + "-->";
	
	var begin_highlight = "<DIV style='background-color:#FFFF99; layer-background-color:#FFFF99;'>";
	var end_highlight   = "</DIV>";
	
	
	new_list = list.replace(begin_tag,begin_highlight);
	new_list = new_list.replace(end_tag,end_highlight);
	
	listPointsContent = new_list;
	
	//set the innerHTML of the listPoints_layer to match the listPointsContent variable
	var lp_layer = document.getElementById("listPoints_layer");
	lp_layer.innerHTML = listPointsContent;
	
}





//pan the map to the given point and highlight the point in the list
function selectCastpoint(point) {

	var panto_point = point.replace("(","");
	panto_point     = panto_point.replace(")","");
	panto_point     = panto_point.split(",");
	
	var panto_lat = panto_point[0];
	var panto_lng = panto_point[1];

	var panto = new GLatLng(panto_lat,panto_lng);
	
	highlightCastpoint(point);
	map.panTo(panto);


}












//pan the map to the given point and highlight the point in the list
function selectResultPoint(point) {

	var panto_point = point.replace("(","");
	panto_point     = panto_point.replace(")","");
	panto_point     = panto_point.split(",");
	
	var panto_lat = panto_point[0];
	var panto_lng = panto_point[1];

	var panto = new GLatLng(panto_lat,panto_lng);

	map.panTo(panto);


}











//remove any storedPoint that has not been named.
//requires that a global array storedPoints be defined somewhere in the Javascript
function cleanStoredPoints() {

	var isamatch;
	
	for(var i=0;i < storedPoints.length;i++) {
	
		isamatch = false;
	
		for(var j=0;j < cachedCastpoints.length;j++) {
		
			if(storedPoints[i].lat() == cachedCastpoints[j][2] && storedPoints[i].lng() == cachedCastpoints[i][3]) {
				isamatch = true;
			}
		
		}
	
		if(isamatch == false) {
			storedPoints.splice(i,1);
		}
	
	}
	
	
}











//delete the Castpoint associated with the given point
function removeCastpoint(point) {
	
	for(var i=0;i < cachedCastpoints.length;i++) {
		if(cachedCastpoints[i][2] == point.lat() && cachedCastpoints[i][3] == point.lng()) {
			cachedCastpoints.splice(i,1);
			releaseStoredPoint(point);
			makeBoundingBox();
			drawMapFeatures(true,false,false);
		}
	}
	
	//set the content of the listPoints_layer
	buildCPList();
	
	
	//set the innerHTML of the listPoints_layer to match the listPointsContent variable
	var lp_layer = document.getElementById("listPoints_layer");
	lp_layer.innerHTML = listPointsContent;
	
}







//sends the contents of the cachedCasstpoints array to the Castpoint manager, //
//which in turn stores the Castpoints in the database						  //
function commitCastpoints() {

	
	for(var i=0;i < cachedCastpoints.length;i++) {
			
		var saveToSessionRequest  = "/AJAX/addCPToSession.php";
			
		var cap_name  = escape(cachedCastpoints[i][0]);
		var cap_desc  = escape(cachedCastpoints[i][1]);
		var cap_lat = escape(cachedCastpoints[i][2]);
		var cap_lng = escape(cachedCastpoints[i][3]);
		
		saveToSessionRequest     += "?castpoint_name=" + cap_name;
		saveToSessionRequest     += "&castpoint_description=" + cap_desc;
		saveToSessionRequest     += "&castpoint_lat=" + cap_lat;
		saveToSessionRequest     += "&castpoint_lng=" + cap_lng;
		sendRequest(saveToSessionRequest);



	}
		
		alert(responseValue);

	window.location = "edit_castpoint.php";

}
//////////////////////////////////////////////////////////////////////////////////////

