var HGZ = {
  settings: {
    infoWindowContainerClass: "HGZ GMap info container",
    infoWindowImageClass: "HGZ GMap info image",
    containerClass: "HGZ GMap container",
    imageClass: "HGZ GMap image thumb",
    infoWindowProperties: {
      pixelOffset: new GSize(17,17)
    }
  },
  map: null,
  MapImage: function (id, user, baseUrl, coords, title, dimensions) {
    this.id = id;
    this.user = user;
    this.baseUrl = baseUrl;
    this.coords = coords;
    this.title = title;
    this.dimensions = dimensions;
  },
  setImages: function(images) {
    this.images = images;
  },
  addImagesToMap: function(map) {
    this.map = map;
    for (var i in this.images) {
      var image = this.images[i];
      // image.coords = this.tempFakeCoords();
      var baseUrl = "http://farm" + image.farm + ".static.flickr.com/" + image.server + "/" + image.id + "_" + image.secret;
      var coords = new GLatLng(image.coords.lat, image.coords.lng);
      this.map.addOverlay(new HGZ.MapImage(image.id, image.user, baseUrl, coords, image.title, image.dimensions));
    }
  },
  tempFakeCoords: function() {
    var bounds = this.map.getBounds();
    var southWest = bounds.getSouthWest();
    var northEast = bounds.getNorthEast();
    var lngDelta = (northEast.lng() - southWest.lng());
    var latDelta = (northEast.lat() - southWest.lat());
    var lngRand = lngDelta * Math.random();
    var latRand = latDelta * Math.random();
    return new GLatLng(northEast.lat() - latRand, northEast.lng() - lngRand);
  }
};

HGZ.MapImage.prototype = new GControl();

HGZ.MapImage.prototype.initialize = function(map) {
  var self = this;
  var container = document.createElement("div");
  container.style.position = "absolute";
  container.className = HGZ.settings.containerClass;
  var image = document.createElement("img");
  image.src = self.getSrcUrl("s");
  image.className = HGZ.settings.imageClass;
  container.appendChild(image);
  image.onclick = function () {
    if (HGZ.currentContainer && HGZ.currentContainer != container) {
      HGZ.currentContainer.className = HGZ.settings.containerClass;
      HGZ.currentContainer.style.zIndex = 0;
      HGZ.currentContainer.zoomed = false;
    }
    HGZ.currentContainer = container;
    container.style.zIndex = 1;
    map.openInfoWindowHtml(self.coords, '<div class="'+ HGZ.settings.infoWindowContainerClass +'"><a href="' + self.getLink() + '"><img src="' + self.getSrcUrl("m") + '" class="' + HGZ.settings.infoWindowImageClass + '" alt="" /><span>' + self.title + '</span></a></div>', HGZ.settings.infoWindowProperties);
  };
  
  map.getPane(G_MAP_MAP_PANE).appendChild(container);

  this.map = map;
  this.container = container;
}

HGZ.MapImage.prototype.getSrcUrl = function(size) {
  return this.baseUrl + "_" + size + ".jpg";
}

HGZ.MapImage.prototype.getLink = function() {
  return "http://humanglobalerzufall.de/wp-content/plugins/falbum/wp/album.php?show=recent&photo=" + this.id;
}

HGZ.MapImage.prototype.remove = function() {
  this.container.parentNode.removeChild(this.div_);
}

HGZ.MapImage.prototype.copy = function() {
  return new HGZ.MapImage(this.id, this.user, this.baseUrl, this.coords, this.title, this.dimensions);
}

HGZ.MapImage.prototype.redraw = function(force) {
  if (!force) return;

  var coords = this.map.fromLatLngToDivPixel(this.coords);
  this.container.style.left = coords.x + "px";
  this.container.style.top = coords.y + "px";
}
