var gMaps = {

addMarker : false,
map : 0,
geocoder : 0,
marker : false,
mode : 0,

init: function(mapid,mode) {
 gMaps.mode = mode;
 if(mode == 'marking'){
  gMaps.counter = 0;
  gMaps.geocoder = new GClientGeocoder();
  } 
  gMaps.map = new GMap2(document.getElementById(mapid));
  gMaps.map.addControl(new GLargeMapControl());
  gMaps.ensureDefaults();

  },


ensureDefaults: function(){
 if(gMaps.mode == 'marking'){
  gMaps.map.clearOverlays();
  gMaps.marker = false;
  gMaps.counter = 0;
  
  document.getElementById('btn_deletemarker').disabled = 'disabled';
  $(document.getElementById('gMap_step3')).hide("fast");
  $(document.getElementById('gMap_step2')).hide("fast");
  $(document.getElementById('gMap_step1')).show("slow");
   
  //automatically initialise the clickable placement.
  gMaps.processAddMarker('placed','');
  }

  gMaps.map.setCenter(new GLatLng(51.590936,-1.851196), 12);
  gMaps.map.savePosition();
},   


/* ====================== EDITING FUNCTIONS --> */

/* check for marker already present; move processing to relevant function. */
processAddMarker: function(type,data){
 if(gMaps.marker != false){
  if(!confirm("You have already marked a location. If you would like to delete it and select another one, click OK. Otherwise, click Cancel.")){
   return;
  }
  else{
   gMaps.deleteMarker();
   //now carry on...
  } 
 } 
 if(type == 'address'){
  gMaps.addMarkerByAddress(data);
 }
 else if(type == 'placed'){
  gMaps.addMarkerByPlaced();
 }
 
 document.getElementById('btn_deletemarker').disabled = '';
},
 

/* add a marker at the location of the users mouse click. */
addMarkerByPlaced: function(){
 if(gMaps.marker == false){
 var myEventListener = GEvent.bind(gMaps.map, "click", this, function(overlay, latlng) {
  GEvent.removeListener(myEventListener);
    if (gMaps.counter < 1) { //counter only allows the placing of one marker.
      if (latlng) {
        gMaps.marker = new GMarker(latlng, {draggable:true});
        gMaps.map.addOverlay(gMaps.marker);
        gMaps.counter++;
      } else if (overlay instanceof GMarker) {
        gMaps.removeOverlay(marker)
      }
    } else {
      GEvent.removeListener(myEventListener);
    }
    $(document.getElementById('gMap_step1')).hide("slow");
  $(document.getElementById('gMap_step2')).show("slow");
  });
  } 
},


/* geocode the address provided and add a marker for it. */
addMarkerByAddress: function(address) {
  gMaps.geocoder.getLatLng(
    address,
    function(point) {
      if (!point) {
        alert(address + " not found.");
      } else {
        gMaps.map.setCenter(point, 16);
        gMaps.marker = new GMarker(point, {draggable:true});
        gMaps.map.addOverlay(gMaps.marker);
		gMaps.counter++;
        $(document.getElementById('gMap_step1')).hide("slow");
        $(document.getElementById('gMap_step2')).show("slow");
      }
    }
  );
},

goToStep3: function(){
  $(document.getElementById('gMap_step2')).hide("slow");
  $(document.getElementById('gMap_step3')).show("slow");
},  

/* Delete any placed marker. */
deleteMarker: function(){
  gMaps.ensureDefaults();
},


submitSelection: function(){
 document.getElementById('inputgMaps_geocode').value = gMaps.makeDirectionsPoint(gMaps.marker.getLatLng());
}, 


/* ======================================== Viewing functions --> */

/* remove brackets from a GMap-provided latlong, and create link to google directions. */
makeDirectionsPoint: function(point){
 var dpoint = point.toString();
 dpoint = dpoint.split('(');
 dpoint = dpoint[1].split(')');
 dpoint = dpoint[0];
 return dpoint;
},

/* use provided lat, lng and html to place marker and show information bubble. */
displayLLPoint: function(lat,lng,html){
 gMaps.map.clearOverlays();
 latlng = new GLatLng(lat,lng);
 gMaps.map.setCenter(latlng, 12);
 gMaps.marker = new GMarker(latlng);
 gMaps.map.addOverlay(gMaps.marker);
 html = '<h3>' + html + '</h3><strong>Get Directions:</strong><br /><a href="http://maps.google.co.uk/maps?hl=en&q=to%20' + gMaps.makeDirectionsPoint(latlng) + '&um=1&ie=UTF-8&sa=N&tab=wl&z=12" rel="external">To Here</a> &bull; <a href="http://maps.google.co.uk/maps?hl=en&q=from%20' + gMaps.makeDirectionsPoint(latlng) + '&um=1&ie=UTF-8&sa=N&tab=wl&z=14" rel="external">From Here</a>';
 gMaps.marker.openInfoWindowHtml(html);
 gMaps.marker.bindInfoWindowHtml(html); //to make it persistent.
 
},


checkResize: function(){
 setTimeout("gMaps.map.checkResize();gMaps.map.returnToSavedPosition()",500);
} 
  

} //end of object


