
// This Document Defines the objects that will appear on the map and control the tags and their information

// the createMarker function takes the latitude and longitude, the name and info and makes a google marker object
function createMarker(latlng, name, info, type) {
	    
	//set marker image 
	var mapIcon = new GIcon(); 
	mapIcon.image = "http://www.campussublet.com/images/"+type+'.png';
	mapIcon.iconSize = new GSize(43, 29);
    mapIcon.iconAnchor = new GPoint(6, 20);
	mapIcon.infoWindowAnchor = new GPoint(5, 1);

	
	//generate new marker
      var marker = new GMarker(latlng, {icon:mapIcon});
    // add name to marker
	  marker.value = name;
	//add event to display pop up Info Window
	GEvent.addListener(marker,"mouseover", function() {
        var myHtml = "<b>" + name + "</b><br/>" + info;
        map.openInfoWindowHtml(latlng, myHtml);
      });
	
	//return the marker to calling function
    return marker;
  }
                   

function getNearby(URI) {
	geocoder.getLatLng(
    address,
    function(point) { 
      if (!point) {
        alert(address + " not found");
      } else {

		updateLonLatOnPage(point);
			
		}
    }
  );
}

 function loadXMLString(txt) {
     if (window.DOMParser) {
         parser=new DOMParser();
         xmlDoc=parser.parseFromString(txt,"text/xml");
     }
     else // Internet Explorer
     {
         xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
         xmlDoc.async="false";
         xmlDoc.loadXML(txt); 
     }
     return xmlDoc.documentElement;
 }
 
 
function processNearby(node){
	var placeType = node.value;
	//var ajax = new Ajax(); // now it's a local variable
	//ajax.doGet("http://www.campussublet.com/listing/nearByProcessor.php?cLat=40.7927985&cLon=-73.6307407&placeType=pizza", doStuff, "object");
	//loadData("http://www.campussublet.com/listing/nearByProcessor.php?cLat=40.7927985&cLon=-73.6307407&placeType=pizza");
	//alert ("the lat on:"+cLat + "," + cLon);
	//http://www.campussublet.com/listing/nearByProcessor.php?cLat=40.7927985&cLon=-73.6307407&placeType=pizza
	new Ajax.Request('http://www.campussublet.com/listing/nearByProcessor.php?cLat='+cLat+'&cLon='+cLon+'&placeType='+placeType,
  {
    method:'get',
    onSuccess: function(transport){
      var response = transport.responseXML || "no response text";
	
	 	  
	var allTuples = response.getElementsByTagName("location");
	for (var i = 0; i< 4;i++){ //allTuples.length
		////////////////////parse out two points, seperate glatlng coordinages below
				var pstring = new String(response.getElementsByTagName('point')[i].firstChild.nodeValue);
				//alert(pstring);
				var parray = pstring.split(',');
				
				var lat = parray[0];
					//alert(parray[0]);
				var lon = parray[1];
					//alert(parray[1]);
			  	var a = response.getElementsByTagName('address')[i].firstChild.nodeValue;
			  	var n = response.getElementsByTagName('name')[i].firstChild.nodeValue;
				
				//alert( lat+ "\n" + lon + "\n" +a+ "\n"+n);
			   	var point = new GLatLng(lon,lat);
				
				var newTextNode = document.createTextNode(n+", "+a);
				var newBr = document.createElement('br');
				
				//place on page
				document.getElementById(placeType+"_content").appendChild(newTextNode);	
				document.getElementById(placeType+"_content").appendChild(newBr);	
				
			


				//place in map
			   	map.addOverlay(createMarker(point, n,a, placeType));
	 
	 
	 			// since remove from map will not be implemented, i will remove the check box instead
				//node.parentNode.removeChild(node);
				node.style.visibility = 'hidden';
        }
		
				//unhide 
				document.getElementById(placeType+"_content_div").style.visibility = 'visible';
				//slide insertion down - Scriptaculous effect
				$(placeType+"_content_div").hide();
				new Effect.SlideDown(placeType+"_content_div");
				
			
	  
    },
    onFailure: function(){ alert('Something went wrong...') }
  });

	
	
	//mapdata
	//if checked
	if(node.checked){
		// get long off page
		cLon = document.getElementById("cLon").innerHTML ;
		//get lat off page
		clat = document.getElementById("cLat").innerHTML ;
		//get nearby results by submitting point to Google API
	
	
	//test point remove when done with nearby ///////////////////////////////////////////////////////////
     // var point = new GLatLng(cLat-.0004,     cLon-.0009);
	// map.addOverlay(createMarker(point, "test", "info"));
	
	
	} // end if checked
	//else unchecked hide contents and remove tags
	else{ 
			
				//alert("not checked");
		// hide contents of this_content
		//remove tags
	}
}

// this function will be used to place the school on the map
function placeSchool(schoolName,sLon,sLat,sWebsite){
	
	}

function initializeMap(lat,lon){
	setGlobalCenter();
   if (GBrowserIsCompatible()) {
         map = new GMap2(document.getElementById("map_canvas"));
        map.setCenter(new GLatLng(cLat,cLon), 13);
		var name = "Listing# " + document.getElementById("LID").innerHTML;
		var info= document.getElementById("Street").innerHTML;
		var point = new GLatLng(cLat, cLon);
		map.addOverlay(createMarker(point,name, info,'home'));
        map.setUIToDefault();
      }

}


function setGlobalCenter(){
	if (document.getElementById("map_canvas")){
		cLat = parseFloat(document.getElementById("cLat").firstChild.nodeValue );
		cLon = parseFloat(document.getElementById("cLon").firstChild.nodeValue );
	}
}


//make global geocoder object to referecnce throughout app
var geocoder = new GClientGeocoder();

// makes a global latitude and longitude variable for the center object/point
var cLat;
var cLon;
//make map global
var map;
var xmlDoc;

function initialize() {
	initializeMap(323,232);
}
window.onload = initialize;






var geoXml;