if (!Array.indexOf){
  // define indexOf for arrays in IE
  Array.prototype.indexOf = function(obj){
      for(var i=0; i<this.length; i++){
          if(this[i]==obj){
              return i;
          }
      }
      return -1;
  }
}

GoogleMap = function() {}
GoogleMap.groups = {}
GoogleMap.markers = function() {
  var result = {}
  
  for(var gkey in GoogleMap.groups) {
    result = $.extend(result, GoogleMap.groups[gkey].markersById)
  }
  
  return result
}

GoogleMap.markerIds = function() {
  var result = []
  
  for(var gkey in GoogleMap.groups) {
    markers = GoogleMap.groups[gkey].markersById
    for(var key in markers) {
      result.push(key)
    }
  }
  
  return result
}

GoogleMap.nextMarker = function(current_key) {
  var markers = GoogleMap.markerIds()
  var index   = markers.indexOf(current_key)
  var length  = markers.length;
  
  if (index >= 0) {
    return ((index == (length - 1)) ? markers[0] : markers[index + 1]);
  }
}

GoogleMap.previousMarker = function(current_key) {
  var markers = GoogleMap.markerIds()
  var index   = markers.indexOf(current_key)
  var length  = markers.length;
  
  if (index >= 0) {
    return ((index > 0) ? markers[index - 1] : markers[length - 1]);
  }  
}

GoogleMap.showInfoWindowFor = function(key) {
  var markers = GoogleMap.markers()
  var marker  = markers[key]
  
  if (marker) {
    GEvent.trigger(marker, 'click');
  }
}

GoogleMap.bindInfoWindowsToMarkers = function() {
  var marker
  for(var gkey in GoogleMap.groups) {
    var markers = GoogleMap.groups[gkey].markersById
    for(var key in markers) {
      marker  = markers[key]
      var element = $('#' + key)
      if (element) {
        element.width(300)
        // standard window
        // marker.bindInfoWindow(element[0])
        
        // extinfowindow
        marker.element = element;
        element.find('div.d').truncate({max_length: 550});
        
        element.find('a.previousMarker').attr('href',
          "javascript:GoogleMap.showInfoWindowFor(GoogleMap.previousMarker('" + key + "'))"
        )
        
        element.find('a.nextMarker').attr('href',
          "javascript:GoogleMap.showInfoWindowFor(GoogleMap.nextMarker('" + key + "'))"
        )

        GEvent.addListener(marker, 'click', function() { 
          var html = this.element.html();
          this.openExtInfoWindow(map, "balloon", html, {
            beakOffset: 0,
            paddingX: 100,
            paddingY: 100
          });
        }); // end addListener  
        
      } // end if
    } // end for key
  } // end for gkey
} // end function

// JS helper functions for YM4R

function addListenerToMarker(marker,callback,options) {
	GEvent.addListener(marker, "click", function() {
	  (callback)(marker, options)
    // marker.openInfoWindowHtml(info,options);
	});
	
	return marker;
}

function addInfoWindowToMarker(marker,info,options){
	GEvent.addListener(marker, "click", function() {
	  marker.openInfoWindowHtml(info,options);
	});
	return marker;
}

function addInfoWindowTabsToMarker(marker,info,options){
     GEvent.addListener(marker, "click", function() {marker.openInfoWindowTabsHtml(info,options);});
     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,zoom) {
  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,zoom);
} 

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,manual_zoom) {
  var center = bounds.getCenter();
  var zoom   = this.getBoundsZoomLevel(bounds);
  var max    = 15
      zoom   = zoom > max ? max : zoom
  
  if (manual_zoom) zoom = manual_zoom;
  this.setCenter(center, zoom)
}

//For full screen mode
function setWindowDims(elem) {
    if (window.innerWidth){
	elem.style.height = (window.innerHeight) + 'px;';
	elem.style.width = (window.innerWidth) + 'px;';
    }else if (document.body.clientWidth){
	elem.style.width = (document.body.clientWidth) + 'px';
	elem.style.height = (document.body.clientHeight) + 'px';
    }
}

ManagedMarker = function(markers,minZoom,maxZoom) {
    this.markers = markers;
    this.minZoom = minZoom;
    this.maxZoom = maxZoom;
}

//Add the markers and refresh
function addMarkersToManager(manager,managedMarkers){
    for(var i = 0, length = managedMarkers.length; i < length;i++) {
	mm = managedMarkers[i];
	manager.addMarkers(mm.markers,mm.minZoom,mm.maxZoom);
    }
    manager.refresh();
    return manager;
}


var INVISIBLE = new GLatLng(0,0); //almost always invisible

if(self.Event && Event.observe){
    Event.observe(window, 'unload', GUnload);
}else{
    window.onunload = GUnload;
}
