Allocate.map = {
  initialise : function(opt) {
    jQuery.extend(this,opt||{});
    if(this.initialised) {
      google.maps.event.trigger(this.map, 'resize');
      return;
    }
    
    var myOptions = {
      zoom: this.zoom,
      center: this.location,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    this.map = map = new google.maps.Map(document.getElementById('map-canvas'), myOptions);

    //attempt to geolocate if we are in a location uri
    //split the uri
    var cur = this;
    var split_uri = window.location.toString().split('/');
    if(split_uri[5] == 'location' && split_uri[7]) {
      // try to geolocate
      var sane_addr = split_uri.slice(6).reverse().join(', ').replace('_',' ');
      cur.geocoder.geocode({
          address : sane_addr,
          region : 'uk'
        },
        function(results,status) {
          if (status == google.maps.GeocoderStatus.OK) {
            var bc = results[0].geometry.viewport;
            cur.map.fitBounds(bc);
            if(!split_uri[8]) {
              cur.map.setZoom(cur.map.getZoom()-1);
            }
          }
        }
      );
    }

    var obj = this;
    jQuery.getJSON('/en/list_json', function(data) {
      for (var i=0; i < data.length; i++) {
        var hm = new google.maps.Marker({
          position: new google.maps.LatLng(parseFloat(data[i][2]),parseFloat(data[i][3])),
          map: map,
          clickable: true,
          uri : 'http://www.test.com',
          icon: "/portal/theindependents/assets/images/map_hotel_red.png"
        });
        obj.infowin(hm,data[i]);
      };
    });
    this.initialised  = true;
    
  },
  location : new google.maps.LatLng(52.626008,-3.262695),
  geocoder : new google.maps.Geocoder(),
  infowin : function(target,data) {
    var infowindow = new google.maps.InfoWindow({ 
      content: data[0] + "<br/><a href='/"+ data[1]+ "'>View more information</a>"
    });
    var obj = this;
    google.maps.event.addListener(target, 'click', function() {
      if(obj.open_window) {
        obj.open_window.close();
      }
      obj.open_window = infowindow;
      infowindow.open(map,target);
    });
  },
  target : 'map-canvas',
  initialised : false,
  zoom : 6,
  map : null
};

