/* TODO make array access nicer
0 lat
1 lng
2 link
3 title
4 image
5 desc
TODO detatch from prototype
*/

var Map = {
	currentItemUrl: null,
	targetFrame: null,
	divId: null,
	lat: null,
	lng: null,
	zoom: null,
	zoomControlType: 'large',
	typeName: null,
	includeMapTypeControl: true,
	includeOverviewControl: true,
	includeScaleControl: true,
	
	init: function() {
		Map.normalIcon = new GIcon();
    Map.normalIcon.image = '/images/maps/small_pin.png';
    Map.normalIcon.shadow = '/images/maps/small_shadow.png';
    Map.normalIcon.iconSize = new GSize(17, 36);
    Map.normalIcon.shadowSize = new GSize(43, 36);
    Map.normalIcon.iconAnchor = new GPoint(9, 36);

    Map.mutedIcon = new GIcon();
    Map.mutedIcon.image = '/images/maps/small_grey_pin.png';
    Map.mutedIcon.shadow = '/images/maps/small_shadow.png';
    Map.mutedIcon.iconSize = new GSize(17, 36);
    Map.mutedIcon.shadowSize = new GSize(43, 36);
    Map.mutedIcon.iconAnchor = new GPoint(9, 36);

    Map.clusterIcon = new GIcon();
    Map.clusterIcon.image = '/images/maps/large_pin.png';
    Map.clusterIcon.shadow = '/images/maps/large_shadow.png';
    Map.clusterIcon.iconSize = new GSize( 28, 62 );
    Map.clusterIcon.shadowSize = new GSize( 76, 62 );
    Map.clusterIcon.iconAnchor = new GPoint( 13, 61 );
		
		Map.gmap = new GMap2(document.getElementById(Map.divId));
		Map.gmap.setCenter(new GLatLng(Map.lat, Map.lng), Map.zoom);

    mapTypes = Map.gmap.getMapTypes();
    mapTypeName = Map.typeName.toLowerCase()
   	for (var index = 0; index < mapTypes.length; ++index) {
      if (mapTypes[index].getName().toLowerCase() == mapTypeName) {
 				Map.gmap.setMapType(mapTypes[index]);
			}
 	  }
 	  
		Map.gmap.setCenter(new GLatLng(Map.lat, Map.lng), Map.zoom);

    Map.clusterer = new Clusterer(Map.gmap);
		Map.clusterer.icon = Map.clusterIcon;
	},
	
	addAllMarkers: function() {
		var is_current;
		for (var index = 0; index < Map.locData.length; ++index) {
			is_current = Map.locData[index][2/*link*/] == Map.currentItemUrl;
			if (!Map.currentItemUrl || is_current) Map.addMarker(index, Map.normalIcon, !is_current);
			else Map.addMarker(index, Map.mutedIcon, true);
		} 
	},
	
	addMarker: function(index, icon, clickable) {
		var marker = new GMarker(new GLatLng(Map.locData[index][0/*lat*/],Map.locData[index][1/*lng*/]), icon);
    Map.clusterer.AddMarker(marker, null);
    GEvent.addListener(marker, "mouseover", function() {
			Map.gmap.addOverlay(new MapHover(index, icon, clickable));
		});
	},
	
	defaultSettings: function() { 
	  if (Map.divId==null) return;
		Map.init();
	  
	  if (Map.zoomControlType == 'small') Map.gmap.addControl(new GSmallZoomControl());
	  if (Map.zoomControlType == 'medium') Map.gmap.addControl(new GSmallMapControl());
	  if (Map.zoomControlType == 'large') Map.gmap.addControl(new GLargeMapControl());
	  
	  
	  if (Map.includeMapTypeControl) Map.gmap.addControl(new GMapTypeControl());
	  if (Map.includeScaleControl) Map.gmap.addControl(new GScaleControl());
	  if (Map.includeOverviewControl) Map.gmap.addControl(new GOverviewMapControl(new GSize(115,115)));
		Map.clusterer.maxVisibleMarkers = 40;
		Map.clusterer.minMarkersPerCluster = 5;
		Map.clusterer.gridSize = 7;
		Map.addAllMarkers();
	},
	
	itemShowSettings: function() {
    if (Map.divId==null) return;
		Map.init();
	  Map.gmap.addControl(new GLargeMapControl());
	  Map.gmap.addControl(new GMapTypeControl());
		Map.clusterer.maxVisibleMarkers = 30;
		Map.clusterer.minMarkersPerCluster = 7;
		Map.clusterer.gridSize = 9;
		Map.addAllMarkers();
	},
	
	itemEditSettings: function() {
		if (Map.divId==null) return;
		Map.init();
	  Map.gmap.addControl(new GLargeMapControl());
	  Map.gmap.addControl(new GMapTypeControl());
		Map.gmarker = new GMarker(new GLatLng(Map.lat, Map.lng), {draggable: true, icon: Map.normalIcon});
    Map.gmap.addOverlay(Map.gmarker);
		GEvent.addListener(Map.gmarker, "dragend", function() {
			$("map_latitude").value = Map.gmarker.getPoint().lat()
		  $("map_longitude").value = Map.gmarker.getPoint().lng()
		});
		GEvent.addListener(Map.gmap, "zoomend", function(oldLevel, newLevel) {
	   	$("map_zoom").value = newLevel
		});
		GEvent.addListener(Map.gmap, "maptypechanged", function() {
	   	$("map_type").value = Map.gmap.getCurrentMapType().getName()
		});
	}
}


/* Tie in to clusterer */
Clusterer.Click = function () {
	MapHover.removeCurrent();
	Map.gmap.setCenter(this.marker.getPoint());
	Map.gmap.zoomIn();
}

Clusterer.MouseOver = function ( cluster ) {
	Map.gmap.addOverlay(new MapHover(cluster, Map.clusterIcon, false));
}


/* MapHover is our custom GMaps GOverlay */
function MapHover(index_or_cluster, icon, clickable) {
	if (typeof index_or_cluster == 'number') {
		this.index_ = index_or_cluster;
		this.cluster_ = null;
	} else {
		this.index_ = null;
		this.cluster_ = index_or_cluster;
	}
	this.icon_ = icon;
	this.clickable_ = clickable;
	MapHover.removeCurrent();
	MapHover.curHoverOverlay = this;
}

MapHover.removeCurrent = function() {
	if (MapHover.curHoverOverlay) Map.gmap.removeOverlay(MapHover.curHoverOverlay);
	MapHover.curHoverOverlay = null;
}

MapHover.pngImage = function(src, href, width, height) {
	if (/MSIE/.test(navigator.userAgent)) {
		h = '<span style="display:inline-block; width:' + width + '; height:' + height + '; filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'' + src + '\', sizingMethod=\'scale\')" />'
		h += '<image src="/images/layout/spacer.gif" width="100%" height="100%" />';
		h += '</span>';
	} else {
		h = '<image src="' + src + '" style="width:' + width + '; height:' + height + ';" /></a>';
		if (href && Map.targetFrame) {
			h = '<a href="' + href + '" target="' + Map.targetFrame + '">' + h + '</a>';
		} else if (href) {
			h = '<a href="' + href + '">' + h + '</a>';
		}
	}
	return h;
}

MapHover.Click = function() {
	/* Alternate click -- remove if you get pngImage hrefs working in IE6 */
	if (Map.targetFrame) {
		window.open(Map.locData[this.index_][2], Map.targetFrame, '');
	} else {
		redirect_to(Map.locData[this.index_][2]);
	}
}

MapHover.prototype = new GOverlay();

MapHover.prototype.initialize = function(map) {

	if (this.cluster_) {
		var pt = map.fromLatLngToDivPixel(this.cluster_.marker.getPoint());
	} else {
  	var pt = map.fromLatLngToDivPixel(new GLatLng(Map.locData[this.index_][0/*lat*/], Map.locData[this.index_][1/*lng*/]));
	}

  var mainDiv = document.createElement("div");
  mainDiv.style.position = "absolute";
  mainDiv.style.left = pt.x - 15 + "px";
	mainDiv.style.top = pt.y - 20 + "px";
	h = '<div style="margin-top: 2px; margin-left: 2px; background: #333333;">'
	h += '<div style="position: relative; top: -2px; left: -2px; border: 1px solid; padding: 4px; background: white;">'
	if (this.cluster_) {
	  mainDiv.style.width = "140px";
		h += '<div style="margin-left: 25px;">' + this.cluster_.markers.length + ' items hidden.<div style="font-size: 0.9em;color: blue">Click ' + MapHover.pngImage(this.icon_.image, false, '0.7em', '1.1em') + ' to zoom.</div></div>';
	} else {
	  mainDiv.style.width = "175px";
		h += '<div style="text-indent: 25px; margin-top: 0; margin-bottom: 2px; font-size: 1.2em; font-weight: bold;">' + Map.locData[this.index_][3/*title*/] + '</div>'
		if (Map.locData[this.index_][4/*image*/]) h += "<img style='position: relative; float: left; margin-right: 4px; margin-bottom: 2px;' height=62 width=62 src=\"" + Map.locData[this.index_][2/*link*/] + "/thumb.gif\" />";
		h += Map.locData[this.index_][5/*desc*/];
		if (this.clickable_) h += '<div style="font-size: 0.9em;color: blue">For more info, click on ' + MapHover.pngImage(this.icon_.image, false, '0.7em', '1.1em') + ' above.</div>';
		h += '<div style="clear: both"></div>';
	}
	h += '</div></div>'
	mainDiv.innerHTML = h
  map.getPane(G_MAP_FLOAT_PANE).appendChild(mainDiv);

  var shadowDiv = document.createElement("div");
  shadowDiv.style.position = "absolute";
	shadowDiv.style.left = pt.x - this.icon_.iconAnchor.x + "px";
	shadowDiv.style.top = pt.y - this.icon_.iconAnchor.y + "px";
  shadowDiv.style.width = this.icon_.shadowSize.width + "px";
  shadowDiv.style.height = this.icon_.shadowSize.height + "px";
	shadowDiv.innerHTML = MapHover.pngImage(this.icon_.shadow, href, this.icon_.shadowSize.width + 'px', this.icon_.shadowSize.height + 'px');
  map.getPane(G_MAP_FLOAT_PANE).appendChild(shadowDiv);

  var safetyNetDiv = document.createElement("div");
  safetyNetDiv.style.position = "absolute";
	safetyNetDiv.style.left = pt.x - 200 + "px";
	safetyNetDiv.style.top = pt.y - 200 + "px";
  safetyNetDiv.style.width = "400px";
  safetyNetDiv.style.height = "400px";
	safetyNetDiv.innerHTML = '<image src="/images/layout/spacer.gif" width="100%" height="100%" />';
  map.getPane(G_MAP_FLOAT_PANE).appendChild(safetyNetDiv);

  var markerDiv = document.createElement("div");
  markerDiv.style.position = "absolute";
	markerDiv.style.left = pt.x - this.icon_.iconAnchor.x + "px";
	markerDiv.style.top = pt.y - this.icon_.iconAnchor.y + "px";
  markerDiv.style.width = this.icon_.iconSize.width + "px";
  markerDiv.style.height = this.icon_.iconSize.height + "px";
	var href = null;
	if (this.clickable_) href = Map.locData[this.index_][2];
	markerDiv.innerHTML = MapHover.pngImage(this.icon_.image, href, this.icon_.iconSize.width + 'px', this.icon_.iconSize.height + 'px');
  map.getPane(G_MAP_FLOAT_PANE).appendChild(markerDiv);

  this.map_ = map;
  this.mainDiv_ = mainDiv;
  this.shadowDiv_ = shadowDiv;
  this.safetyNetDiv_ = safetyNetDiv;
  this.markerDiv_ = markerDiv;

	safetyNetDiv.onmouseover = MapHover.removeCurrent;
	if (this.clickable_) href = markerDiv.onclick = MapHover.Click.bindAsEventListener(this);
	if (this.cluster_) markerDiv.onclick = Clusterer.Click.bindAsEventListener(this.cluster_);
}

MapHover.prototype.remove = function() {
  this.mainDiv_.parentNode.removeChild(this.mainDiv_);
  this.shadowDiv_.parentNode.removeChild(this.shadowDiv_);
  this.safetyNetDiv_.parentNode.removeChild(this.safetyNetDiv_);
  this.markerDiv_.parentNode.removeChild(this.markerDiv_);
}

MapHover.prototype.copy = function() {
  return new MapHover(this.index_);
}

MapHover.prototype.redraw = function(force) {
}