/**
 * @author john
 */
var renderMarkerAndCache = function(o) {
	var iObj = o;
	if(iObj.properties.location == false) {
		return;
	}
	
	var rm = function(point) {
		if(point) {
			var offsety = (Math.random() - 0.5) / 25;
			var offsetx = (Math.random() - 0.5) / 25;
			var offsetpoint = new GLatLng((point.y + offsety), (point.x + offsetx));
			var marker = new GMarker(offsetpoint, iObj.properties.icon);
			if(!iObj.properties.location.havegeoinfo) {
				cacheGeo(point, iObj.properties.location.locstring);
				iObj.properties.location.havegeoinfo = true;
				iObj.properties.location.lat = point.y;
				iObj.properties.location.lon = point.x;
			}
			iObj.marker = marker;
			marker.parent = iObj;
			
			GEvent.addListener(marker, "click", iObj.iconClick);
			GEvent.addListener(marker, "mouseover", iObj.onMouseOver);
			GEvent.addListener(marker, "mouseout", iObj.onMouseOut);
			
			iObj.properties.distanceFromSearch = getDist(searchCenter['lon'], searchCenter['lat'], offsetpoint.x, offsetpoint.y);
			
			map.addOverlay(marker);
			if(iObj.properties.geoWait) {
				delete(iObj.properties.geoWait);
				iObj.iconClick();
			}
		} else {
			iObj.properties.location = false;
		}
	}
	if(!iObj.properties.location.havegeoinfo) {
		geocoder.getLatLng(iObj.properties.location.locstring, rm);
	} else {
		var pt = new GLatLng(iObj.properties.location.lat, iObj.properties.location.lon);
		rm(pt);
		delete pt;
	}
}

var getDist = function(x1, y1, x2, y2) {
	var xdiff = x1-x2;
	var ydiff = y1-y2;
	return Math.sqrt((xdiff * xdiff) + (ydiff * ydiff));
}

var highlighter = function() {
	this.hl = function(trObj) {
		trObj.style.borderColor = '#FFFFC5';
	}
	
	this.uhl = function(trObj) {
		trObj.style.borderColor = '#DDDDCA';
	}
}

var HL = new highlighter();

var showPreviewBubble = function(marker, content) {
	var previewDiv = document.getElementById("previewPopup");
	var previewC = document.getElementById("previewContent");
	var TlcLatLng = map.fromContainerPixelToLatLng(new GPoint(0,0), true);
	var TlcDivPixel = map.fromLatLngToDivPixel(TlcLatLng);
	var pointDivPixel = map.fromLatLngToDivPixel(marker.getPoint());
	
	var apt = subGPoints(pointDivPixel, TlcDivPixel);
	
	var mapdiv = document.getElementById("map");
	
	xcor = findX(mapdiv) + apt.x - 125;
	ycor = findY(mapdiv) + apt.y - 123;
	
	if(xcor < 10) {
		xcor = 10;
	}
	
	previewDiv.style.display = 'block';
	previewDiv.style.left = xcor + "px";
	previewDiv.style.top = ycor + "px";
	previewC.innerHTML = content;
}

function hidePreviewBubble() {
	document.getElementById("previewPopup").style.display = "none";
}

function subGPoints(a,b) {
	return new GPoint(a.x-b.x, a.y-b.y);
}

function setElementValue(elementid, value) {
	var e = document.getElementById(elementid);
	var a;
	if(e) {
		switch(e.tagName) {
			case "SELECT":
				selectValue(e, value);
				if(e.onchange) {
					e.onchange();
				}
				break;
			default:
				e.value = value;
				break;
		}
	}
}

function findX(obj) {
	var curleft = 0;
	if(obj.offsetParent) {
		while(obj.offsetParent) {
			curleft += obj.offsetLeft;
			obj = obj.offsetParent;
		}
	}
	else if(obj.x)
		curleft += obj.x;
	return curleft;
}

function findY(obj) {
	var curtop = 0;
	if(obj.offsetParent) {
		while(obj.offsetParent) {
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if(obj.y)
		curtop += obj.y;
	return curtop;
}

var bgcolors = ['grey','lightgrey'];