// GMaps class for all our processing needs

function GMaps(){
    this.map = new GMap(document.getElementById("map"));
	//this.map.setMapType(G_SATELLITE_MAP);
	this.map.centerAndZoom(new GPoint( -116.48659944, 51.3962001), 9);
    this.map.addControl(new GLargeMapControl());
    this.map.addControl(new GScaleControl());
    this.map.addControl(new GMapTypeControl());

    this.markers = new Array();
  	//this.loadXMLMarkers();


    // Create a map point marker.
   // During edit mode add extra functionality to the marker to allow repositioning.
   this.createMarker = function (point, id, description){
       var marker = new GMarker( point );
       //Set some marker instance variables
       marker.id = id;
      marker.html = description;
	//	marker.html = description;
	  GEvent.addListener(marker, "click", function() {
          marker.openInfoWindowHtml(marker.html);
      });

	
	   this.map.addOverlay(marker);
	   return marker;
    }

	this.createMarker( new GPoint(-116.48659944, 51.3962001),'1','Stephen Creek Guest Cabin'  );



  //Display the marker
  this.displayMarker = function( marker, clicked ){
       this.map.addOverlay(marker);
       marker.visible = true;

       if (clicked == 1){ this.triggerMarker(marker); }
  }


  // Return the marker using it's point id
  this.getMarkerByID = function ( id ){
       return this.markers[id];
  }



   // Basic toString function so you can determine the class
   this.toString = function(){
        return "WorldWeb GMaps Class";
   }

   // Have software perform a marker Click
   this.triggerMarker = function ( marker ){
        GEvent.trigger(marker,"click");
   }

   // This function is called from the webpage when a point that already
   // Exists on the map is selected from the drop box.
   // Recenter and show the infoWindow
   this.triggerPoint = function ( objSelect ){
        var value = objSelect.options[objSelect.selectedIndex].value;
        if ( objSelect.selectedIndex > 0 ){
              var marker = this.getMarkerByID(parseFloat(value));
		//alert(marker);

        //      this.map.closeInfoWindow();
         //     this.map.recenterOrPanToLatLng(marker.point);
              this.triggerMarker(     marker );
        }
    }


};
  
  //Load the XML markers from the URL
  GMaps.prototype.loadXMLMarkers = function(){
       this.map.clearOverlays();
       this.markers = new Array();

       xmlURL = '/u/mapXML.phtml';

       var parent = this;
       var request = GXmlHttp.create();
       request.open('GET',xmlURL,true);
       request.onreadystatechange = function(){
               if(request.readyState == 4){
                    var xmlDoc = GXml.parse(request.responseText);
                    var markers = xmlDoc.documentElement.getElementsByTagName("marker");
                    for (var i=0; i < markers.length; i++){
                         var marker = markers[i];
                         var point = new GPoint(parseFloat(marker.getAttribute("lng")), parseFloat(marker.getAttribute("lat")));
                         var id = parseFloat(marker.getAttribute("id"));
                         var clicked =  parseFloat(marker.getAttribute("clicked"));
                         var description =  marker.getAttribute("desc");
                    
						//Create the point and display it if necessary
                        //Also add it to the markers array
                         var marker = parent.createMarker(point, id, description);
  						 parent.displayMarker( marker, clicked );

                         parent.markers[id] = marker;

                     }

                }
       }
       request.send(null);
   }

// Function for starting GMaps Class
var gmap;
function showMap(){
    gmap = new GMaps();
}

