var map = null;
var selectedLat;
var selectedLong;
var pointMarkers = new HashLookUp();
var mash = null;
function ContentMarker(content, marker) {
	this.marker = marker;
	this.inOverlay = false;
	this.geoCodedContent = content;
	this.setMarker = function (marker) {
		this.marker = marker;
	};
	this.getMarker = function () {
		return marker;
	};
	this.setGeoCodedContent = function (content) {
		this.geoCodedContent = content;
	};
	this.getGeoCodedContent = function () {
		return this.geoCodedContent;
	};
	this.isInOverlay = function () {
		return this.inOverlay;
	};
	this.setInOverlay = function (inoverlay) {
		 this.inOverlay=inoverlay;
	};
	
	this.openInfoWindows = function () {
		var infoTabs = new HashLookUp();
		var count = 0;
		for (url in this.geoCodedContent.items) {
			infoTabs.addLookUp(infoTabs.length, this.geoCodedContent.items[url]);
		}
		this.marker.openInfoWindowTabsHtml(infoTabs.items);
	};
}
function HashLookUp() {
	this.length = 0;
	this.items = new Array();
	this.removeLookUp = function (key) {
		var value;
		if (typeof (this.items[key]) != "undefined") {
			this.length--;
			var value = this.items[key];
			delete this.items[key];
		}
		return value;
	};
	this.getLookUp = function (key) {
		return this.items[key];
	};
	this.getAtIndex = function (index) {
		return this.items[index];
	};
	this.addLookUp = function (key, value) {
		if (typeof (value) != "undefined") {
			this.length++;
			this.items[key] = value;
		}
		return value;
	};
	this.hasLookUp = function (key) {
		return typeof (this.items[key]) != "undefined";
	};
}
function loadGeoMap(nodename,zoom,lat,long) {
	eval(mash);
	if (map) {
		return;
	}
	
	var mapNode = document.getElementById(nodename);
	if (!mapNode) {
		return;
	}
	if (GBrowserIsCompatible()) {
		var defaultIcon = new GIcon();
		defaultIcon.iconSize = new GSize(16, 16);
		defaultIcon.shadowSize = new GSize(24, 16);
		defaultIcon.iconAnchor = new GPoint(8, 16);
		defaultIcon.infoWindowAnchor = new GPoint(8, 0);
		map = new GMap2(document.getElementById(nodename));
		map.addControl(new GLargeMapControl());
		map.addControl(new GMapTypeControl()); // map, sattelite, hybrid
		map.setCenter(new GLatLng(lat, long), zoom);
	}
}
function centerMap(lat,long,zoom){
	if(!map){
	 return;
	}
	map.setCenter(new GLatLng(lat, long), zoom);
	
}

function addGeoCodedContent(url, title, author, lat, long) {
	var point = new GLatLng(lat, long);
	if (!pointMarkers.hasLookUp(point.toString())) {
		var marker = new GMarker(point);
		var contents = new HashLookUp();
		pointMarkers.addLookUp(point.toString(), new ContentMarker(contents, marker));
	}
	var contentMark = pointMarkers.getLookUp(point.toString());
	var geoCodedContents = contentMark.getGeoCodedContent();
	var message = " <a href='" + url + "'> <b>" + title + "</b></a><br/>" + author ;
	if (geoCodedContents.hasLookUp(url.toString())) {
		geoCodedContents.removeLookUp(url);
	}
	geoCodedContents.addLookUp(url, new GInfoWindowTab(title.split(" ")[0] + " ...", message));
	addContentMarkOnMap(contentMark);
}
function addPointMarkers(geomash) {
	for (var i = 0; i < geomash.length; i++) {
		if (!geomash[i]) {
			continue;
		}
		addGeoCodedContent(geomash[i].url, geomash[i].title, geomash[i].author, geomash[i].text, geomash[i].lat, geomash[i].long);
	}
}
function addContentMarkOnMap(contentMarker) {
	if (!map) {
		return;
	}
	if(contentMarker.isInOverlay())
		return ;
	if (GBrowserIsCompatible()) {
		contentMarker.setInOverlay(true);		
		GEvent.bind(contentMarker.getMarker(), "click", contentMarker, contentMarker.openInfoWindows);
		map.addOverlay(contentMarker.getMarker());
	}
}
function addMarkers() {
	if (GBrowserIsCompatible()) {
		for (contentMarker in pointMarkers.items) {
			addContentMarkOnMap(pointMarkers.items[contentMarker]);
		}
	}
}

