// JS helper functions for YM4R

function addInfoWindowToMarker(marker,info){
	GEvent.addListener(marker, "click", function() {marker.openInfoWindowHtml(info);});
	return marker;
}

function addInfoWindowTabsToMarker(marker,info){
     GEvent.addListener(marker, "click", function() {marker.openInfoWindowTabsHtml(info);});
     return marker;
}

function addPropertiesToLayer(layer,getTile,copyright,opacity,isPng){
    layer.getTileUrl = getTile;
    layer.getCopyright = copyright;
    layer.getOpacity = opacity;
    layer.isPng = isPng;
    return layer;
}

function addOptionsToIcon(icon,options){
    for(var k in options){
	icon[k] = options[k];
    }
    return icon;
}

function addCodeToFunction(func,code){
    if(func == undefined)
	return code;
    else{
	return function(){
	    func();
	    code();
	}
    }
}

function addGeocodingToMarker(marker,address){
    marker.orig_initialize = marker.initialize;
    orig_redraw = marker.redraw;
    marker.redraw = function(force){}; //empty the redraw method so no error when called by addOverlay.
    marker.initialize = function(map){
	new GClientGeocoder().getLatLng(address,
					function(latlng){
	    if(latlng){
		marker.redraw = orig_redraw;
		marker.orig_initialize(map); //init before setting point
		marker.setPoint(latlng);
	    }//do nothing
	});
    };
    return marker;
}

GMap2.prototype.centerAndZoomOnMarkers = function(markers) {
  var bounds = new GLatLngBounds(markers[0].getPoint(),
                                 markers[0].getPoint());
  for (var i=1, len = markers.length ; i<len; i++) {
    bounds.extend(markers[i].getPoint());
  }

  this.centerAndZoomOnBounds(bounds);
} 

GMap2.prototype.centerAndZoomOnPoints = function(points) {
  var bounds = new GLatLngBounds(points[0],
                                 points[0]);
  for (var i=1, len = points.length ; i<len; i++) {
    bounds.extend(points[i]);
  }

  this.centerAndZoomOnBounds(bounds);
} 

GMap2.prototype.centerAndZoomOnBounds = function(bounds) {
  var center = bounds.getCenter();
  this.setCenter(center, Math.min(18, this.getBoundsZoomLevel(bounds)));
}

var INVISIBLE = new GLatLng(0,0); //almost always invisible
window.onunload = GUnload;

// LALife marker classes - not related to YM4R

var LAMarkerLayer = function(){this.init.apply(this, arguments);} // can't be initialize -- er, thanks, Google Maps

LAMarkerLayer.prototype = {
    map: null,
    contentNode: null,
    expandedMarker: null,
    extent: null,
    minZoom: 8,
    maxZoom: 14,
    markers: [],
    init: function(extent, minZoom, maxZoom) {
      this.extent = extent;
      if (minZoom)
        this.minZoom = minZoom;
      if (maxZoom)
        this.maxZoom = maxZoom;
    },
    initialize: function(m) {
      this.map = m;
      this.contentNode = document.createElement("div");
      $(this.contentNode).css({left: "0px", top: "0px", position: "relative"});
      if ($.browser.msie)
        $(this.contentNode).css({zoom: "1"}); // must create layout for IE7
      $(this.map.getPane(G_MAP_MARKER_PANE)).append(this.contentNode);
    },
    remove: function() {
    },
    copy: function() {
      return new LAMarkerLayer(this.extent, this.minZoom, this.maxZoom);
    },
    redraw: function(force) {
      if ((this.maxZoom && this.map.getZoom() > this.maxZoom) ||
          (this.minZoom && this.map.getZoom() < this.minZoom)) {
        $(this.markers).each(function(){ this.element.style.display = "none"; });
      } else if (force) {
        $(this.markers).each(function(){
          this.element.style.display = "";
          this.redraw(true);
        });
      }
    },
    addMarker: function(marker, expand) {
      marker.placeInLayer(this);
      this.markers.push(marker);
      if (expand)
        marker.expand();
      return marker;
    },
    findMarkerByPos: function(pos) {
      var i;
      for (i=0; i < this.markers.length; i++){
        if (Math.abs(pos.x - this.markers[i].pos.x) < 0.000001 &&
            Math.abs(pos.y - this.markers[i].pos.y) < 0.000001) {
          return this.markers[i];
        }
      }
      return null;
    }
}

var LAMarker = function(){
  var bubble_src_params = [
    [
      ['/images/markers/bubn_t.gif', 71, 7],
      ['/images/markers/bubn_b.gif', 71, 20],
    ],
    [
      ['/images/markers/bubw_t.gif', 115, 7],
      ['/images/markers/bubw_b.gif', 115, 20],
    ],
    [
      ['/images/markers/bubx_t.gif', 215, 7],
      ['/images/markers/bubx_b.gif', 215, 20],
    ]
  ];

  this.markerLayer = null;
  this.topBubbleElement = null;
  this.contentElement = null;
  this.bottomBubbleElement = null;

  this.set_attributes = function(expanded, narrow, params) {
    this.element.className = (expanded ? "la_marker_expanded" : "la_marker");

    this.topBubbleElement.src = params[0][0];
    this.topBubbleElement.width = params[0][1];
    this.topBubbleElement.height = params[0][2];

    this.contentElement.className = (narrow ? "marker_content marker_narrow_content" : "marker_content");

    this.bottomBubbleElement.src = params[1][0];
    this.bottomBubbleElement.width = params[1][1];
    this.bottomBubbleElement.height = params[1][2];
  };

  this.set_condensed_attributes = function(narrow) {
    this.set_attributes(false, narrow, bubble_src_params[narrow ? 0 : 1]);
  };

  this.set_expanded_attributes = function() {
    this.set_attributes(true, false, bubble_src_params[2]);
  };

  this.create_bubble_elements = function(narrow) {
    this.topBubbleElement = document.createElement("img");
    this.contentElement = document.createElement("div");
    this.bottomBubbleElement = document.createElement("img");

    this.set_condensed_attributes(narrow);

    $(this.element).append(this.topBubbleElement);
    $(this.element).append(this.contentElement);
    $(this.element).append(this.bottomBubbleElement);
  };

  this.init.apply(this, arguments);
}

LAMarker.prototype = {
    pos: null,

    element: null,

    condensedContent: null,
    contentIsNarrow: false,

    expandedContent: null,
    expandedContentURL: null,
    expandedContentURLParams: null,

    init: function(p, content, options) {
      if (!options) options = {};
      this.pos = p;
      this.condensedContent = content;

      this.contentIsNarrow = options.contentIsNarrow;
      this.expandedContent = options.expandedContent;
      this.expandedContentURL = options.expandedContentURL;
      this.expandedContentURLParams = options.expandedContentURLParams;
    },
    placeInLayer: function(layer) {
      if (this.markerLayer == null && this.condensedContent) {
        this.markerLayer = layer;
        this.element = document.createElement("div");
        this.element.className = "la_marker";
        $(this.element).click(function(){m.expand()});

        this.create_bubble_elements(this.contentIsNarrow);

        $(this.markerLayer.contentNode).append(this.element);

        this.contentElement.innerHTML = this.condensedContent;
        var m = this; // close over this
        $(this.contentElement).children('.expm').click(function(){m.expand()});

        this.redraw(true);
      }
    },
    contract: function() {
      if (this.markerLayer && this.markerLayer.expandedMarker == this) {
        this.markerLayer.expandedMarker = null;
        $(this.contentElement).html(this.condensedContent);
        var m = this; // close over this
        $(this.contentElement).children('.expm').click(function(){m.expand()});
        this.set_condensed_attributes();
        this.redraw(true);
      }
    },
    expand: function(expanded_content, expanded_content_url, expanded_content_url_params) {
      if (this.markerLayer && this.markerLayer.expandedMarker != this) {
        if (this.markerLayer.expandedMarker) {
          this.markerLayer.expandedMarker.contract();
        }
        this.markerLayer.expandedMarker = this;
        if (expanded_content) {
          this.expandedContent = expanded_content;
        }
        if (expanded_content_url) {
          this.expandedContentURL = expanded_content_url;
        }
        if (expanded_content_url_params) {
          this.expandedContentURLParams = expanded_content_url_params;
        }
        if (this.expandedContentURL) {
          var m = this; // close over this
          $(this.contentElement).load(this.expandedContentURL, this.expandedContentURLParams,
                                      function(){$(m.contentElement).children('.conm').click(function(){m.expand()}); m.redraw(true);});
        } else if (this.expandedContent) {
          $(this.contentElement).html(this.expandedContent);
        }
        this.set_expanded_attributes();
        this.redraw(true);
        map.panTo(this.pos);
      }
    },
    redraw: function(force) {
      if (this.markerLayer && force) {
        var px = this.markerLayer.map.fromLatLngToDivPixel(this.pos);
        px.x -= 9;
        px.y -= this.element.clientHeight;
        $(this.element).css({left: px.x + "px", top: px.y + "px", position: "absolute"});
      }
    }
}
