


//<![CDATA[
	Event.observe(window, 'load', init, false);

//*** Status codes from geocoder results ***//
var map = null;
var mapurl = 'gudfud/editmap.php';//url of ajax server code
var newwindow =null; //xternal gmap directions window
var defaultzoom=13;

var map_type_control='small';
var map_control='small';
var map_type='normal';
var map_latt='39.0074';
var map_long='-77.0403';
var map_zoom='10';
var show_scale=0;
var map_markers=[];

/*
var MARKER_ID=0;
var MARKER_SHOW=1;
var MARKER_LATITUDE=2;
var MARKER_LONGITUDE=3;
var MARKER_CONTENTS=4;
var MARKER_ZOOM=5;
var MARKER_OBJECT=6;
*/

var status=[];
status[G_GEO_SUCCESS]            = "Found";
status[G_GEO_MISSING_ADDRESS]    = "Missing Address";
status[G_GEO_UNKNOWN_ADDRESS]    = "Unknown Address";
status[G_GEO_UNAVAILABLE_ADDRESS]= "Unavailable Address";
status[G_GEO_BAD_KEY]            = "Bad Key";
status[G_GEO_TOO_MANY_QUERIES]   = "Too Many Queries";
status[G_GEO_SERVER_ERROR]       = "Server Error"


	//called onLoad()
	function init()
	{
		//addEvent(window, "unload", GUnload);
		Event.observe(window, "unload", GUnload);
		//GUnload();
		
		map = new GMap2($("mapcontainer"));

		//set the controls
		set_map_type_control(map_type_control);
		set_map_control(map_control);
		set_map_scale_control(show_scale);
		
		load_map(map_latt, map_long, map_zoom);
		set_map_type(map_type);
		
		//map_markers[0]= showamarker(90,34.0538,-118.463, 13,'<div class=\'markertitle\'>Hamlet Brentwood</div><div class=\'markeraddress\' id=\'markeraddress\'>11648 San Vicente Blvd.<BR />Los Angeles, CA 90049</div><div class=\'markergetdir\'>Get Directions to here from:<BR><input id=\'fromaddress\' type=text /><input type=button value=\'Go\' onclick=\'popup_directions()\'>', map);
		map_markers[0]= showamarker(109,39.1151,-77.198, 13,'<div class=\'markertitle\'>Hamlet Gaithersburg</DIV><div class=\'markeraddress\' id=\'markeraddress\'>9811 Washingtonian Blvd.<BR />Gaithersburg, MD 20878</DIV><div class=\'markergetdir\'>Get Directions to here from:<BR><input id=\'fromaddress\' type=text /><input type=button value=\'Go\' onclick=\'popup_directions()\'>', map);
	}
	
//this function checks any other conditions on the editted information 
//before calling save_changes();
function check_more_changes(obj)
{
	//we need to update marker array with new contents...
	marker=get_marker_by_id(obj.id.match(/\d+/));
	if(marker)
	{
		marker.contents=obj.innerHTML;
		marker.openInfoWindowHtml();
	}
	return true;
}

//this function returns the url that any editted div on this page should be AJAXed to
function get_edit_url()
{
	return mapurl;
}

//makes the html div when the admin double clicks on something to edit
function make_edit_div(obj)
{
	insert_edit_textarea(4,60);
}

//this makes any other changes after an edit has been completed
function finish_edit_complete(obj)
{
	
}	
	
function onMarker(obj, event)
{
	for(i=0; i<map_markers.length; i++)
	{
		if(Position.within($("marker_"+map_markers[i].markerid),Event.pointerX(event),Event.pointerY(event)))
		{
			obj=$("marker_"+map_markers[i].markerid);
			return obj;
		}
	}
	return false;
}	



	
	

function get_marker_by_id(markerid)
{
	for(i=0; i<map_markers.length; i++)
	{
		if(map_markers[i].markerid==markerid)
		{
			return map_markers[i];
		}
	}
}

function go_to()
{
	show_address_dialog();
}




function show_control_type_dialog()
{
	title="Settings";
	msg="Choose which type of map control you <BR>would like: <Select id='controltype'><Option value='small'>Small</Option><Option value='large'>Large</Option><Option value='zoomonly'>Zoom Only</Option><Option value='none'>None</Option></Select>";
	contents=msg+'</li>\
		<li class="contextmenuitem" style="text-align:center">\
		<Input type=button onclick="update_control_type(); close_dialog(this); " value="ok"></li>';
	dialog(contents, title);
}

function update_scale_control()
{
	if(show_scale) show_scale=0;
	else show_scale=1;
	set_map_scale_control(show_scale);
	do_ajax(mapurl, 'modxid='+modxid+'&do=show_scale&value='+show_scale,
					function(t){},
					function(t){notice("Error", "There was an error connecting to the server.  Please check your connection and try again.")}
					);	
}

function update_control_type()
{
	set_map_control($F("controltype"));
	do_ajax(mapurl, 'modxid='+modxid+'&do=setcontroltype&controltype='+escape($F("controltype")),
					function(t){},
					function(t){notice("Error", "There was an error connecting to the server.  Please check your connection and try again.")}
					);	
}

	//** the map controls **/
	
	var gudfud_control=null;
	function set_map_control(control)
	{
		//first make sure there are no other controls on the map
		map.removeControl(gudfud_control);
		//now add new one
		if(control == 'zoomonly')
		{
			gudfud_control=new GSmallZoomControl();
		}
		else if(control == 'large')
		{
			gudfud_control=new GLargeMapControl();
		}
		else if(control == 'small')
		{
			gudfud_control=new GSmallMapControl();
		}
		if(control != 'none')
		{
			map.addControl(gudfud_control);
		}
	}
	
	var gudfud_type_control=null;
	function set_map_type_control(control)
	{
		map.removeControl(gudfud_type_control);
		if(control == 'large')
		{
			gudfud_type_control=new GMapTypeControl(0);
		}
		else if(control == 'small')
		{
			gudfud_type_control=new GMapTypeControl(1);
		}
		if(control!='none')
		{
			map.addControl(gudfud_type_control);
		}
	}
	
	var gudfud_scale_control=null;
	function set_map_scale_control(show)
	{
		map.removeControl(gudfud_scale_control);
		if(show)
		{
			gudfud_scale_control=new GScaleControl();
			map.addControl(gudfud_scale_control);
		}
		else 
		{
			gudfud_scale_control=null;
		}
	}
	
	
	function set_map_type(maptype) 
	{
		if(maptype=="normal")
		{
			maptype= G_NORMAL_MAP;
		}
		else if(maptype=="hybrid")
		{
			maptype = G_HYBRID_MAP;
		}
		else if(maptype=="satellite")
		{
			maptype = G_SATELLITE_MAP;
		}

		//alert("map type: "+maptype);
		map.setMapType(maptype);
	}
	


	var geocoder = new GClientGeocoder();
	//var bounds = new GLatLngBounds();





    function load_map(maplatt, maplong, zoom) {
    	//alert("calling : map.setCenter(new GLatLng("+maplatt+","+maplong+"),"+zoom+");");
    	//alert("Im nor broken");
      if (GBrowserIsCompatible()) {
        //map = new GMap2($("mapcontainer"));
        //map.centerAndZoom(new GPoint(maplatt, maplong), 4);
        point = new GLatLng(maplatt,maplong);
        map.setCenter(point);
        map.setZoom(Number(zoom)); //number is needed for some reason
      }
    }
    
    function show_marker(latti,longi,contents) {
    	point = new GLatLng(latti,longi);
    	var marker = new GMarker(point);
		marker.openInfoWindowHtml(contents);
		map.addOverlay(marker);
   }
    
/***********AJAX Functions*********************/
function add_marker(title, contents, latitude, longitude)
{	
	do_ajax(mapurl, 'modxid='+modxid+'&do=addmarker&title='+escape(title)+"&contents="+escape(contents)+"&latitude="+latitude+"&longitude="+longitude+"&zoom="+defaultzoom,
					function(t){add_marker_complete(t, contents, latitude, longitude);},
					function(t){notice("Error", "There was an error connecting to the server.  Please check your connection and try again.")}
					);	
}

function add_marker_complete(t, contents, latitude, longitude)
{
		pagechanged=1;
		markerid=t.responseText;
		marker=new GudfudMarker(markerid, latitude, longitude, defaultzoom, contents, {draggable: true});
		map_markers[map_markers.length]=marker;
		marker.print_marker();	
		add_marker_ids();
	  zoom_to_marker(marker);
}	

function delete_marker(id)
{	
	id=id.match(/\d+/);
	marker=get_marker_by_id(id);
	do_ajax(mapurl, 'modxid='+modxid+'&do=deletemarker&markerid='+id,
					function(t){delete_marker_complete(marker);},
					function(t){notice("Error", "There was an error connecting to the server.  Please check your connection and try again.")}
					);	
}

function delete_marker_complete(marker)
{
	pagechanged=1;
	map.closeInfoWindow();
	map.removeOverlay(marker.gmarker);
	map_markers=$A(map_markers).without(marker);
}

function set_map_view()
{
		center=map.getCenter();
		
		do_ajax(mapurl, 'modxid='+modxid+'&do=setmapview&latitude='+center.lat()+"&longitude="+center.lng()+"&zoom="+map.getZoom()+"&maptype="+map.getCurrentMapType().getName(),
					function(t){set_map_view_complete();},
					function(t){notice("Error", "There was an error connecting to the server.  Please check your connection and try again.")}
					);
}

function set_map_view_complete()
{
	pagechanged=1;
	notice("The current view has been set to be the default view<BR\> users see when they load this page");
}

function set_marker_zoom(markerid)
{
		markerid=markerid.match(/\d+/);
		marker=get_marker_by_id(markerid);
		do_ajax(mapurl, 'markerid='+markerid+'&do=setmarkerzoom&zoom='+map.getZoom(),
					function(t){set_marker_zoom_complete(marker, map.getZoom());},
					function(t){notice("Error", "There was an error connecting to the server.  Please check your connection and try again.")}
					);
}

function set_marker_zoom_complete(marker, zoom)
{
	pagechanged=1;
	marker.zoom=zoom;
}

function move_marker(marker, latitude, longitude)
{
		do_ajax(mapurl, 'markerid='+marker.markerid+'&do=movemarker&latitude='+latitude+"&longitude="+longitude+"&zoom="+map.getZoom(),
					function(t){move_marker_complete(marker, latitude, longitude);},
					function(t){notice("Error", "There was an error connecting to the server.  Please check your connection and try again.")}
					);
}

function move_marker_complete(marker, latitude, longitude)
{
	pagechanged=1;
	marker.latitude=latitude;
	marker.longitude=longitude;
}
/********End AJAX FUNCTIONS********************************/  
    
 
    function replace_breaks_with_commas(s)
		{
			if (s.replace)
		  {
		  	s = s.replace(/\<br[^\>]*\>/gi, ", ");
		  }
		
		  return s;
		}
		//opens a popup page with directions.
		function popup_directions()
		{
			//alert("here");
			var fromaddress=$F("fromaddress");
			if(fromaddress.length)
			{		
				var url = "http://maps.google.com/maps?f=d&q=" + fromaddress + " to " + replace_breaks_with_commas($("markeraddress").innerHTML);
				newwindow = window.open( url , "directions", "height=600,width=800,left=100,top=100,resizable=yes,scrollbars=yes,toolbar=yes,status=yes");
				if (window.focus) {newwindow.focus()};
			}
		}

		
		function center_on_marker(markerid)
		{
			for(i=0; i<map_markers.length; i++)
			{
				if(map_markers[i][MARKER_ID]==markerid)
				{
					point = new GLatLng(map_markers[i][MARKER_LATITUDE],map_markers[i][MARKER_LONGITUDE]);
					map.setCenter(point, map_markers[i][MARKER_ZOOM]);
				}
			}
		}
		
		function zoom_to_marker(marker, point, zoom)
		{
				if(map.getInfoWindow().isHidden())
				{
					if(map.getZoom()<zoom)
					{
						//map.setZoom(marker.zoom);
						//point = new GLatLng(latitude,longitude);
						map.setCenter(point, zoom);
					}
					
				}
				else
				{
					map.closeInfoWindow();
					//point = new GLatLng(latitude,longitude);
					if(map.getZoom()<zoom)
					{
						map.setCenter(point, zoom);
						//map.setZoom(marker.zoom);
					}
					else
					{
						map.setCenter(point);
					}
				}
		
		}
	//function initializemarkers	
			
  	//var GudfudMarker = Class.create( 
  	//var GudfudMarker = GMarker.extend( 
  	
  	function showthemarkers()
  	{
  	//	map_markers[0] = showthemarkers(94,34.0904,-118.393, 13,'<div class=\'markertitle\'>Hamlet Sunset</DIV><div class=\'markeraddress\' id=\'markeraddress\'>9201 Sunset Blvd.<BR />West Hollywood, CA 90069</DIV><div class=\'markergetdir\'>Get Directions to here from:<BR><input id=\'fromaddress\' type=text /><input type=button value=\'Go\' onclick=\'popup_directions()\'>', {});
//map_markers[1] = showthemarkers(93,34.1517,-118.449, 13,'<div class=\'markertitle\'>Hamlet Sherman Oaks</DIV><div class=\'markeraddress\' id=\'markeraddress\'>4419 Van Nuys Blvd.<BR />Sherman Oaks, CA 91403</DIV><div class=\'markergetdir\'>Get Directions to here from:<BR><input id=\'fromaddress\' type=text /><input type=button value=\'Go\' onclick=\'popup_directions()\'>', {draggable: false});
//map_markers[2] = showthemarkers(92,34.0281,-118.429, 13,'<div class=\'markertitle\'>Hamlet Sepulveda</DIV><div class=\'markeraddress\' id=\'markeraddress\'>2927 Sepulveda Blvd.<BR />Los Angeles, CA 90046</DIV><div class=\'markergetdir\'>Get Directions to here from:<BR><input id=\'fromaddress\' type=text /><input type=button value=\'Go\' onclick=\'popup_directions()\'>', {draggable: false});
//map_markers[3] = showthemarkers(91,34.1499,-118.132, 13,'<div class=\'markertitle\'>Hamlet Pasadena</DIV><div class=\'markeraddress\' id=\'markeraddress\'>214 S. Lake Ave.<BR />Pasadena, CA 91101</DIV><div class=\'markergetdir\'>Get Directions to here from:<BR><input id=\'fromaddress\' type=text /><input type=button value=\'Go\' onclick=\'popup_directions()\'>', {draggable: false});
//map_markers[4] = showthemarkers(90,34.0538,-118.463, 13,'<div class=\'markertitle\'>Hamlet Brentwood</DIV><div class=\'markeraddress\' id=\'markeraddress\'>11648 San Vicente Blvd.<BR />Los Angeles, CA 90049</DIV><div class=\'markergetdir\'>Get Directions to here from:<BR><input id=\'fromaddress\' type=text /><input type=button value=\'Go\' onclick=\'popup_directions()\'>', {draggable: false});
  			
  			points0 = new GLatLng(34.0904,-118.393);
			points1 = new GLatLng(34.1517,-118.449);
			points2 = new GLatLng(34.0281,-118.429);
			points3 = new GLatLng(34.1499,-118.132);
			points4 = new GLatLng(34.0538,-118.463);
			
      	var gmarker0 = new GMarker(points0);
      	var gmarker1 = new GMarker(points1);
      	var gmarker2 = new GMarker(points2);
      	var gmarker3 = new GMarker(points3);
      	var gmarker4 = new GMarker(points4);
      	
      	
      	divid0="marker_"+0+"_contents";
      	divid1="marker_"+1+"_contents";
      	divid2="marker_"+2+"_contents";
      	divid3="marker_"+3+"_contents";
      	divid4="marker_"+4+"_contents";
      	
    		infocontents0="<div id="+divid0+"><div class='markertitle'>Hamlet Sunset</DIV><div class='markeraddress' id='markeraddress'>9201 Sunset Blvd.<BR />West Hollywood, CA 90069</DIV><div class='markergetdir'>Get Directions to here from:<BR><input id='fromaddress' type=text /><input type=button value='Go' onclick='popup_directions()'></div>";
    		infocontents1="<div id="+divid1+"><div class='markertitle'>Hamlet Sherman Oaks</DIV><div class='markeraddress' id='markeraddress'>4419 Van Nuys Blvd.<BR />Sherman Oaks, CA 91403</DIV><div class='markergetdir'>Get Directions to here from:<BR><input id='fromaddress' type=text /><input type=button value='Go' onclick='popup_directions()'></div>";
    		infocontents2="<div id="+divid2+"><div class='markertitle'>Hamlet Sepulveda</DIV><div class='markeraddress' id='markeraddress'>2927 Sepulveda Blvd.<BR />Los Angeles, CA 90046</DIV><div class='markergetdir'>Get Directions to here from:<BR><input id='fromaddress' type=text /><input type=button value='Go' onclick='popup_directions()'></div>";
    		infocontents3="<div id="+divid3+"><div class='markertitle'>Hamlet Pasadena</DIV><div class='markeraddress' id='markeraddress'>214 S. Lake Ave.<BR />Pasadena, CA 91101</DIV><div class='markergetdir'>Get Directions to here from:<BR><input id='fromaddress' type=text /><input type=button value='Go' onclick='popup_directions()'></div>";
    		infocontents4="<div id="+divid4+"><div class='markertitle'>Hamlet Brentwood</DIV><div class='markeraddress' id='markeraddress'>11648 San Vicente Blvd.<BR />Los Angeles, CA 90049</DIV><div class='markergetdir'>Get Directions to here from:<BR><input id='fromaddress' type=text /><input type=button value='Go' onclick='popup_directions()'></div>";
    		
    		//gmarker.openInfoWindowHtml(infocontents);
    		map.addOverlay(gmarker0);
    		map.addOverlay(gmarker1);
    		map.addOverlay(gmarker2);
    		map.addOverlay(gmarker3);
    		map.addOverlay(gmarker4);

    		
    		//add_marker_id(markerid);
    		
			//var newmarker=$A(document.getElementsByTagName("img")).find(function(image){return(image.src=="http://www.google.com/intl/en_us/mapfiles/marker.png" && !image.id)})[0];
   		//newmarker.id="marker_"+0;
			GEvent.addListener(gmarker0, "click", function(event)
			{
					zoom_to_marker(gmarker0, points0, 13); 
					gmarker0.openInfoWindowHtml(infocontents0);
			});
			GEvent.addListener(gmarker1, "click", function(event)
			{
					zoom_to_marker(gmarker1, points1, 13); 
					gmarker1.openInfoWindowHtml(infocontents1);
			});
			GEvent.addListener(gmarker2, "click", function(event)
			{
					zoom_to_marker(gmarker2, points2, 13); 
					gmarker2.openInfoWindowHtml(infocontents2);
			});
			GEvent.addListener(gmarker3, "click", function(event)
			{
					zoom_to_marker(gmarker3, points3, 13); 
					gmarker3.openInfoWindowHtml(infocontents3);
			});
			GEvent.addListener(gmarker4, "click", function(event)
			{
					zoom_to_marker(gmarker4, points4, 13); 
					gmarker4.openInfoWindowHtml(infocontents4);
			});
			
				
		return gmarker0;

  	}
  	

  	
  function add_marker_id(markerid)
   		{
   			
   			
   			//alert("doing marker"+markerid);
				newmarker=$A(document.getElementsByTagName("img")).find(function(image){return(image.src=="http://www.google.com/intl/en_us/mapfiles/marker.png" && !image.id)})[0];
   			newmarker.id="marker_"+markerid;

 	 		}
  

    function searchAddress() 
    {
    	Element.show($("message"));
    	//$("message").style.visibility = "visible";
    	$("message").innerHTML = "searching";
    	search_string = $("address").value;
    	//search_string = $("address").value+" "+$("zipcode").value;
    	//alert("search string" + search_string);
		geocoder.getLocations(search_string, 
		function(result) 
		{
			if(result)
			{
				$("message").innerHTML = status[result.Status.code]+"<br>";
	   		   	if(result.Status.code == G_GEO_SUCCESS) 
	   		   	{
			   		  $("message").innerHTML = "If needed, please adjust the position of the marker";
			   		  // 		$("latt").value=result.Placemark[0].Point.coordinates[1];
							//$("long").value=result.Placemark[0].Point.coordinates[0];
							
							//set the center of the map
							defaultcontent="<div class='markertitle'>"+ $F("markername")+"</DIV>";
							defaultcontent+="<div class='markeraddress' id='markeraddress'>"+ replace_with_breaks($F("address"))+"</DIV>";
							defaultcontent+="<div class='markergetdir'>Get Directions to here from:<BR>";
							defaultcontent+="<input id='fromaddress' type=text />";
							defaultcontent+="<input type=button value='Go' onclick='popup_directions()'>"; 
							add_marker($("markername").value, defaultcontent, result.Placemark[0].Point.coordinates[1], result.Placemark[0].Point.coordinates[0]);	
							close_dialog("address");
	   		   	}
	   		  	else
	   		  	{
	   		  		notice(status[result.Status.code]);	
	   		  	}
	   		}
	   		else
	   		{
	   			$("message").innerHTML = "Please enter an address to search";
	   		}
   		}
   	);
	}
	
function add_marker_ids()
{
	pane=map.getPane(G_MAP_MARKER_MOUSE_TARGET_PANE);
	marker=pane.firstChild;
	i=0;
	while(marker)
	{
		marker.id="marker_"+map_markers[i].markerid;
		marker=marker.nextSibling;
		i++;
	}
}	





function showamarker(divid, latit, longit, zoom, contents, map)
  	{
  	//	map_markers[0] = showthemarkers(94,34.0904,-118.393, 13,'<div class=\'markertitle\'>Hamlet Sunset</DIV><div class=\'markeraddress\' id=\'markeraddress\'>9201 Sunset Blvd.<BR />West Hollywood, CA 90069</DIV><div class=\'markergetdir\'>Get Directions to here from:<BR><input id=\'fromaddress\' type=text /><input type=button value=\'Go\' onclick=\'popup_directions()\'>', {});
	//map_markers[1] = showthemarkers(93,34.1517,-118.449, 13,'<div class=\'markertitle\'>Hamlet Sherman Oaks</DIV><div class=\'markeraddress\' id=\'markeraddress\'>4419 Van Nuys Blvd.<BR />Sherman Oaks, CA 91403</DIV><div class=\'markergetdir\'>Get Directions to here from:<BR><input id=\'fromaddress\' type=text /><input type=button value=\'Go\' onclick=\'popup_directions()\'>', {draggable: false});
	//map_markers[2] = showthemarkers(92,34.0281,-118.429, 13,'<div class=\'markertitle\'>Hamlet Sepulveda</DIV><div class=\'markeraddress\' id=\'markeraddress\'>2927 Sepulveda Blvd.<BR />Los Angeles, CA 90046</DIV><div class=\'markergetdir\'>Get Directions to here from:<BR><input id=\'fromaddress\' type=text /><input type=button value=\'Go\' onclick=\'popup_directions()\'>', {draggable: false});
	//map_markers[3] = showthemarkers(91,34.1499,-118.132, 13,'<div class=\'markertitle\'>Hamlet Pasadena</DIV><div class=\'markeraddress\' id=\'markeraddress\'>214 S. Lake Ave.<BR />Pasadena, CA 91101</DIV><div class=\'markergetdir\'>Get Directions to here from:<BR><input id=\'fromaddress\' type=text /><input type=button value=\'Go\' onclick=\'popup_directions()\'>', {draggable: false});
	//map_markers[4] = showthemarkers(90,34.0538,-118.463, 13,'<div class=\'markertitle\'>Hamlet Brentwood</DIV><div class=\'markeraddress\' id=\'markeraddress\'>11648 San Vicente Blvd.<BR />Los Angeles, CA 90049</DIV><div class=\'markergetdir\'>Get Directions to here from:<BR><input id=\'fromaddress\' type=text /><input type=button value=\'Go\' onclick=\'popup_directions()\'>', {draggable: false});
  			
  			point = new GLatLng(latit, longit);
      	var gmarker = new GMarker(point);
      	divid="marker_"+divid+"_contents";
    		infocontents="<div id="+divid+"><div class='markertitle'>"+contents+"</div>";
    		map.addOverlay(gmarker);
    		
			//var newmarker=$A(document.getElementsByTagName("img")).find(function(image){return(image.src=="http://www.google.com/intl/en_us/mapfiles/marker.png" && !image.id)})[0];
   		//newmarker.id="marker_"+0;
			GEvent.addListener(gmarker, "click", function(event)
			{
					zoom_to_marker(gmarker, point, zoom); 
					gmarker.openInfoWindowHtml(infocontents);
			});
		zoom_to_marker(gmarker, point, zoom); 
		return gmarker;

  	}
