renderGoogleMap = function( pc,title )
{
  var geocoder;
  var map;
  
  geocoder = new google.maps.Geocoder();
  
  geocoder.geocode( { 'address':pc },function( results,status )
  {
    if ( status == google.maps.GeocoderStatus.OK )
    {
      var marker = new google.maps.Marker
      (
        {
          position: results[0].geometry.location,
          title: title
        }
      );
      
      var myOptions = {
        zoom: 13,
        center: results[0].geometry.location,
        mapTypeId: google.maps.MapTypeId.ROADMAP
      }
      
      map = new google.maps.Map( document.getElementById('gMapCanvas'),myOptions );
      
      marker.setMap( map );
    }
    else
    {
      alert( 'Loading Google Map failed, please refresh page.');
    }
  } );
}

renderBranchLocatorGoogleMap = function( JSONdata,canvasID )
{
  JSONdata = ( typeof( JSONdata ) != 'undefined' ) ? JSONdata : '';
  canvasID = ( typeof( canvasID ) != 'undefined' ) ? canvasID : 'gMapCanvas';
  
  if ( JSONdata != '' )
  {
    // Initialize Map centered on Norwich
    var args =
    {
      zoom: 8,
      center: new google.maps.LatLng( 52.6289,1.3017 ),
      mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    var map = new google.maps.Map( document.getElementById( canvasID ),args );
    
    // Initialise our ideal View Boundaries for setting appropriate Map Center and Zoom level
    var bounds = new google.maps.LatLngBounds();
    
    // Instantiate an Info Window to hold Branch details...
    var infowindow = new google.maps.InfoWindow(
    {
      content: '...'
    } );
    
    // Plot Markers from JSON data...
    $.get( JSONdata,function( data,fetchResult )
    {
      if ( fetchResult == 'success' )
      {
        var json = JSON.parse( data );
        
        // Loop through returned listings...
        for ( var i=0; i<json.branches.length; i++ )
        {
          var b = json.branches[i];
          
          // Use listing data to plot Marker
          var marker = new google.maps.Marker
          (
            {
              title: b.title,
              url: b.url,
              position: new google.maps.LatLng( b.latitude,b.longitude ),
              infowindow: b.address
            }
          );
          
          marker.setMap( map );
          bounds.extend( marker.getPosition() );
          
          google.maps.event.addListener( marker,'mouseover',function()
          {
            infowindow.setContent( this.infowindow );
            infowindow.open( map,this );
          } );
          
          google.maps.event.addListener( marker,'click',function()
          {
            window.location = this.url;
          } );
        }
        
        // Center/zoom the map based on the bounds
        map.fitBounds( bounds );
        map.setCenter( bounds.getCenter() );
      }
      else
      {
        alert( 'Loading Google Map failed, please refresh page.' );
      }
    },'text' );
  }
}

renderMarkerTweakGoogleMap = function( JSONdata,canvasID )
{
  JSONdata = ( typeof( JSONdata ) != 'undefined' ) ? JSONdata : '';
  canvasID = ( typeof( canvasID ) != 'undefined' ) ? canvasID : 'gTweakMapCanvas';
  
  if ( JSONdata != '' )
  {
    // Initialize Map centered on Norwich
    var args =
    {
      zoom: 8,
      center: new google.maps.LatLng( 52.6289,1.3017 ),
      mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    var map = new google.maps.Map( document.getElementById( canvasID ),args );
    
    // Initialise our ideal View Boundaries for setting appropriate Map Center and Zoom level
    var bounds = new google.maps.LatLngBounds();
    
    // Plot Markers from JSON data...
    $.get( JSONdata,function( data,fetchResult )
    {
      if ( fetchResult == 'success' )
      {
        var json = JSON.parse( data );
        var square;
        var coords = new Array();
        
        // Loop through returned markers...
        for ( var i=0; i<json.markers.length; i++ )
        {
          var m = json.markers[i];
          var point = new google.maps.LatLng( m.latitude,m.longitude );
          
          // Use marker data to plot coord
          coords.push( point );
          
          bounds.extend( point );
        }
        
        square = new google.maps.Polygon({
          paths: coords,
          strokeColor: '#FF0000',
          strokeOpacity: 0.8,
          strokeWeight: 1,
          fillColor: '#FF0000',
          fillOpacity: 0.35
        });
        
        square.setMap( map );
        
        // Center/zoom the map based on the bounds
        map.fitBounds( bounds );
        map.setCenter( bounds.getCenter() );
      }
      else
      {
        alert( 'Loading Google Map failed, please refresh page.' );
      }
    },'text' );
  }
}

