if (LINE == null) {
  var LINE = {};
}

LINE.maps = {
  setup: function() {
    if (GBrowserIsCompatible()) {

      var sizes = {'small': {
                             imagePath: "/images/markers/small/",
                             shadow: "/images/markers/small/Shadow.png",
                             iconSize: new GSize(15, 23),
                             shadowSize: new GSize(25, 23),
                             iconAnchor: new GPoint(7, 23),
                             infoWindowAnchor: new GPoint(7, 23)
                             },
                   'medium': {
                             imagePath: "/images/markers/medium/",
                             shadow: "/images/markers/medium/Shadow.png",
                             iconSize: new GSize(21, 32),
                             shadowSize: new GSize(37, 32),
                             iconAnchor: new GPoint(10, 32),
                             infoWindowAnchor: new GPoint(10, 32)
                             },
                   'large': {
                             imagePath: "/images/markers/large/",
                             shadow: "/images/markers/large/Shadow.png",
                             iconSize: new GSize(26, 38),
                             shadowSize: new GSize(48, 38),
                             iconAnchor: new GPoint(17, 38),
                             infoWindowAnchor: new GPoint(17, 38)
                             }
                   };
      LINE.maps.icons = {};

      var icon = new GIcon();
      icon.image = "/images/markers/active/Active.png";
      icon.shadow = "/images/markers/active/Shadow.png";
      icon.iconSize = new GSize(22, 32);
      icon.shadowSize = new GSize(39, 32);
      icon.iconAnchor = new GPoint(11, 32);
      icon.infoWindowAnchor = new GPoint(11, 32);
      LINE.maps.icons.active = icon;

      // make a custom map pin for each colour & size
      for (size in sizes) {
        LINE.maps.icons[size] = [];
        for (var i=0; i < 16; i++) {
          var icon = new GIcon();
          icon.image = sizes[size].imagePath+i+".png";
          icon.shadow = sizes[size].shadow;
          icon.iconSize = sizes[size].iconSize;
          icon.shadowSize = sizes[size].shadowSize;
          icon.iconAnchor = sizes[size].iconAnchor;
          icon.infoWindowAnchor = sizes[size].infoWindowAnchor;
          LINE.maps.icons[size].push(icon);
        }
      }
      
      // setup the map & add any pins

      map_div = document.getElementById("google_map");

      // IF WE CAN'T FIND THE MAP DIV THEN RETURN WITHOUT DOING ANYTHING MORE
      if(!map_div) {
        return;
      }

      LINE.maps.map = new GMap2(map_div);
      LINE.maps.map.addControl(new GSmallMapControl());
      LINE.maps.map.addControl(new GMenuMapTypeControl());
      LINE.maps.geocoder = new GClientGeocoder();
      LINE.maps.markers = [];
      LINE.maps.bounds = new GLatLngBounds();
      if (LINE.maps.listings) {
        for (var i in LINE.maps.listings) {
          LINE.maps.addMarker(LINE.maps.listings[i]);
        }
      }
      
    }
  },
  addGeocodedMarker: function(listinginfo) {
    var needle;
    if (listinginfo.postcode) {
      needle = listinginfo.postcode;
    } else {
      needle = listinginfo.address;
    }
    LINE.maps.geocoder.getLatLng(needle,
    function(latlng) {
      if (latlng) {
       LINE.maps.addMarkerCallback(latlng,listinginfo);
      }
    });
  },
  addMarker: function(listinginfo) {
    if (null == listinginfo.lat || null == listinginfo.lng) {
      LINE.maps.addGeocodedMarker(listinginfo);
    } else {
      var latlng = new GLatLng(listinginfo.lat,listinginfo.lng);
      if (latlng.lat() < 50) {
        return LINE.maps.addGeocodedMarker(listinginfo);
      }
      LINE.maps.addMarkerCallback(latlng,listinginfo);
    }
  },
  addMarkerCallback: function(latlng,listinginfo) {
    LINE.maps.bounds.extend(latlng);
    var zoomlevel = LINE.maps.map.getBoundsZoomLevel(LINE.maps.bounds);
    if (zoomlevel > LINE.maps.constants.maxZoomLevel) {
      zoomlevel = LINE.maps.constants.maxZoomLevel;
    }
    LINE.maps.map.setCenter(LINE.maps.bounds.getCenter(), zoomlevel);
    var icon = false;
    if (listinginfo.markerSize != null && listinginfo.markerIndex != null ) {
      icon = LINE.maps.icons[listinginfo.markerSize][listinginfo.markerIndex];
    } else if (listinginfo.marker == 'active') {
      icon = LINE.maps.icons.active;
    }
    if (icon) {
      marker = new GMarker(latlng, icon);
    } else {
      marker = new GMarker(latlng);
    }
    LINE.maps.markers.push(marker);
    LINE.maps.map.addOverlay(marker);
    GEvent.addListener(marker, "click", function() {
      var myHtml;
      var ratingHtml = '';
      var priceHtml = '';
      if (listinginfo.rating) {
        ratingHtml = '<p class="rating"><span class="rating star'+listinginfo.rating+'">' + listinginfo.rating + ' Stars</span></p>';
      }
      if (listinginfo.min_price) {
        priceHtml = '<p class="price">£'+listinginfo.min_price+' - '+listinginfo.max_price+' '+listinginfo.tariff_type+'</p>';
      }

      // ADD THE HTML MARKUP IN REVERSE - EASIER TO EXCLUDE PARTS

      // ADD THE BASIC INFO
      myHtml = ratingHtml+priceHtml+"<p>"+listinginfo.address+"</p>";

      // ADD THE TITLE
      if(listinginfo.url) {
        // DETERMINE IF IT NEEDS TO BE OPENED IN A NEW URL
        var target = '';
        if(listinginfo.url.substring(0,4) == "http") {
          target = ' target="_blank"';
        }
        myHtml = "<h3><a href='"+listinginfo.url+"'" + target + ">"+listinginfo.title+"</a></h3>" + myHtml;
      } else {
        myHtml = "<h3>"+listinginfo.title+"</h3>" + myHtml;
      }

      // ADD THE IMAGE
      if(listinginfo.img) {
        if(listinginfo.url) {
          myHtml = "<a href='"+listinginfo.url+"'><img src='"+listinginfo.img+"' width='74' height='74' /></a>" + myHtml;
        } else {
          myHtml = "<img src='"+listinginfo.img+"' width='74' height='74' />" + myHtml;
        }
      }

      // ADD THE CONTAINER
      myHtml = "<div class='infobubble clearfix'>" + myHtml + "</div>";

      LINE.maps.map.openInfoWindowHtml(latlng, myHtml);
    });
  },
  constants: {
    maxZoomLevel: 12
  }
}

$(document).ready(function(){
 /*jQuery.noConflict();*/
 if (!document.getElementById('map_tab') && !LINE.maps.map) {
   LINE.maps.setup();
 }
});
