var init_lat = '46.4250791183536';
var init_lng = '-95.64559936523438';
var init_zoom = 9;

/************************* google API *************************/


/********************************************************/
var _shd_icon = new GIcon(G_DEFAULT_ICON);
_shd_icon.shadow = gBaseURL + "gmap/icons/shadow50.png";
_shd_icon.iconSize = new GSize(19, 32);
_shd_icon.shadowSize = new GSize(37, 32);
_shd_icon.iconAnchor = new GPoint(9, 32);
_shd_icon.infoWindowAnchor = new GPoint(9, 2);
_shd_icon.infoShadowAnchor = new GPoint(18, 25);


/************************* ClusterMapMarker *************************/

ClusterMapMarker.prototype._id;
ClusterMapMarker.prototype._geo;
ClusterMapMarker.prototype._mkr;

function ClusterMapMarker(lat, lng, id, title, divhtml) {
    var img = "";
	img = gBaseURL + "gmap/icons/red-dot.png";
    
	if (arguments.length > 1) {
		var icn = new GIcon(_shd_icon);
		icn.image = img;
		this._id = id;
		this._geo = new GLatLng(lat, lng);
		this._infoVis = false; //infow window visible
		var mrkTitle = unescape(title);
		this._mkr = new GMarker(this._geo, { icon:icn, title:mrkTitle });
		
	    var ref_img = document.getElementById("trvl_" + id);
		if (ref_img) {
			ref_img.src = img;
		}
		var ref = this._mkr;
		if (divhtml != null) {


			GEvent.addListener(this._mkr, "click", function() { 
			    if (!this._infoVis) {
	                ref.openInfoWindowHtml(divhtml);
			        this._infoVis = true;
			    } 
			    else {
			        ref.closeInfoWindow();
			}});
			GEvent.addListener(this._mkr, "infowindowclose", function() {
		        this._infoVis = false;
		    });
			
		}

		
	} else {
		this._geo = new GLatLng(0, 0);
		if (icn != null) {
		    icn.image = img;
	    }
	}
	
	/******************** methods ********************/
	
	this.mergeClusterMapMarker = function(mkr) {
		return new ClusterMapMarkerGroup(this, mkr);
	}
	
	this.addToMap = function(map) {
		if (this._mkr != null) {
			map.addOverlay(this._mkr);
		}
	}
	
	this.getLatLng = function() {
		return this._geo;
	}
	
	this.getSize = function() {
		if (this._mkr) {
			return this._mkr.getIcon().iconSize;
		} else {
			return new GSize(0,0);
		}
	}
	this.getImage = function() {
		if (this._mkr) {
			return this._mkr.getIcon().image;
		} else {
			return gBaseURL + "gmap/icons/shim.gif";
		}
	}
	this.getTitle = function() {
		if (this._mkr) {
			return this._mkr.getTitle();
		} else {
			return "unset";
		}
	}
	
	this.getId = function() {
	    return this._id;
    }
}

/************************* ClusterMapMarkerGroup *************************/
function ClusterMapMarkerGroup(mkr1, mkr2) {
	this._child_markers = new Array(2);
	this._child_markers[0] = mkr1;
	this._child_markers[1] = mkr2;
	
	var geo1 = mkr1.getLatLng();
	var geo2 = mkr2.getLatLng();
	this._lat = (parseFloat(geo1.lat()) + parseFloat(geo2.lat()))/2;
	this._lng = (parseFloat(geo1.lng()) + parseFloat(geo2.lng()))/2;
		
	var icn = new GIcon(_shd_icon);
	
	
	this._mkr = new GMarker(new GLatLng(this._lat, this._lng), { icon:icn });
	this._final_mkr = null;
	
	
	
	/******************** methods ********************/
	
	this._addClusterMapMarker = function(mkr) {
		var geo = mkr.getLatLng();
		var ln = this._child_markers.length;
		this._lat = (parseFloat(geo.lat()) + (parseFloat(this._lat) * ln))/(ln+1);
		this._lng = (parseFloat(geo.lng()) + (parseFloat(this._lng) * ln))/(ln+1);
			
		this._mkr.setPoint(new GLatLng(this._lat, this._lng));
		this._child_markers[this._child_markers.length] = mkr;
	}
	
	this.mergeClusterMapMarker = function(mkr) {
		this._addClusterMapMarker(mkr);
		return this;
	}
	
	this.addToMap = function(map) {
		if (this._final_mkr == null) {
			var icn = new GIcon(_shd_icon);

			   
		    if (this._child_markers.length < 99) {
		        icn.image = gBaseURL + "gmap/icons/blue" + this._child_markers.length + ".png";
		    } else {
			    icn.image = gBaseURL + "gmap/icons/blue99plus.png";
		    }
		    var title = this._child_markers.length + " listings";

			
			var g = new GLatLng(this._lat, this._lng);
			this._final_mkr = new GMarker(g, { icon:icn, title:title });
		

			GEvent.addListener(this._final_mkr, "click", function() { 
				var z = map.getZoom(); 
				map.setCenter(g, z+2);
			});
			var ref = this._final_mkr;
			var ul = document.createElement("ul");
			ul.className = "ggl_info";
			for (var i = 0; i < this._child_markers.length; i++) {
				var li = document.createElement("li");
				
				
				var a = document.createElement("a");

				var id = this._child_markers[i].getId();
				var loc = 'listing.aspx?EntityId=' + id;
				a.setAttribute("href", loc);
				
				var img = new Image();
				img.className = "flg";
				img.src = this._child_markers[i].getImage();
				
				var span = document.createElement("span");
				span.innerHTML = this._child_markers[i].getTitle();
				
				a.appendChild(img);
				a.appendChild(span);
				
				li.appendChild(a);
				
				ul.appendChild(li);
			}
			
			var listct = this._child_markers.length;
				
			
		}
		map.addOverlay(this._final_mkr);
	}
	
	this.getLatLng = function() {
		return new GLatLng(this._lat, this._lng);
	}
	
	this.getSize = function() {
		if (this._mkr) {
			return this._mkr.getIcon().iconSize;
		} else {
			return new GSize(0,0);
		}
	}
}

/************************* ClusterMapMap *************************/
function ClusterMapMap(div, lat, lng, zm) {

	this._clustermap_markers = new Array();
	this._zoom_lvls = new Array();
	if (div) {this._map = new GMap2(div);
	this._map.setCenter(new GLatLng(lat, lng), zm);
    }
	
	/******************** methods ********************/
	
	this.addClusterMapMarker = function(mkr) {
		this._clustermap_markers[this._clustermap_markers.length] = mkr;
	}
	
	this.renderClusterMapMarkers = function() {
		var z = this._map.getZoom();
		var curr = this._zoom_lvls[z];
		if (curr == null) {
			curr = new Array();
			
			for (var i = 0; i < this._clustermap_markers.length; i++) {
				this._insertMarker(curr, this._clustermap_markers[i]);
			}
			this._zoom_lvls[z] = curr;
		}
		
		this._map.clearOverlays();
		for (var i = 0; i < curr.length; i++) {
			curr[i].addToMap(this._map, this._is_mini);
		}
		
	}
	
	this._insertMarker = function(arr, mkr) {
		var merged = false;
		for (var i = 0; i < arr.length; i++) {
			if (this._overlap(arr[i], mkr)) {
				arr[i] = arr[i].mergeClusterMapMarker(mkr);
				merged = true;
				break;
			}
		}
		if (!merged) {
				arr[arr.length] = mkr;
		}
	}
	
	this._overlap = function(mkr1, mkr2) {
		//return false;
		var sz1 = mkr1.getSize();
		
		var pt1 = this._map.fromLatLngToDivPixel(mkr1.getLatLng());
		var pt2 = this._map.fromLatLngToDivPixel(mkr2.getLatLng());
		
		return (sz1.width > Math.abs(pt1.x - pt2.x)) && (sz1.height > Math.abs(pt1.y - pt2.y));
	}
	
    this._map.addControl(new GLargeMapControl());
	this._map.addControl(new GMapTypeControl());
	
	var ref = this;
	GEvent.addListener(this._map, "zoomend", function() { 
		ref.renderClusterMapMarkers();
	});
	
}
