function mapObj (id, lat, lon){
	if (GBrowserIsCompatible()) {
		
		var body = document.getElementsByTagName('body');
		body[0].onunload = function (){
			GUnload();
		}
		
		this.id = id;
		this.lat = lat;
		this.lon = lon;
		this.dist = 10;
		this.map = new GMap2(document.getElementById(id));
		this.bounds = new GLatLngBounds(); 
	
		this.map.addControl(new GSmallMapControl());
		this.map.setCenter(new GLatLng(this.lat, this.lon), this.dist);
	
		this.map.id = id;
		GEvent.addListener(this.map, "movestart", function(){ 
			if (document.getElementById(this.id+"1")){
				var obj = document.getElementById(this.id+"1");
				var pObj = document.getElementById(this.id);
				pObj.removeChild(obj); 
			}
		});
		GEvent.addListener(this.map, "zoomend", function(){ 
			if (document.getElementById(this.id+"1")){
				var obj = document.getElementById(this.id+"1");
				var pObj = document.getElementById(this.id);
				pObj.removeChild(obj); 
			}
		});
	}
}

mapObj.prototype.mapPrintMap=mapPrintMap;
mapObj.prototype.mapPlotMarker=mapPlotMarker;
mapObj.prototype.mapSetDist=mapSetDist;
mapObj.prototype.mapSetIcon=mapSetIcon;
mapObj.prototype.mapListenerBox=mapListenerBox;
mapObj.prototype.mapListenerBoxClose=mapListenerBoxClose;
mapObj.prototype.mapListenerLoad=mapListenerLoad;
mapObj.prototype.mapClearOverlay=mapClearOverlay;

function mapPrintMap(){
	
	var loc = new GLatLng(this.lat, this.lon);
	var icon = this.mapSetIcon("myloc", 0);
	var marker = new GMarker(loc, icon)
	this.bounds.extend(marker.getPoint());
	this.map.addOverlay(marker);
	var zoom = this.map.getBoundsZoomLevel(this.bounds);
	if (zoom > 15) zoom = 15;
	this.map.setZoom(zoom);
	this.map.setCenter(this.bounds.getCenter());

}

function mapPlotMarker(lat, lng, icon, id, info){
	var obj = this.id;
	var loc = new GLatLng(lat, lng);
	var icon = this.mapSetIcon(icon, id);
	var marker = new GMarker(loc, icon);
	var bounds = this.map.getBounds();
	marker.width = parseInt(document.getElementById(this.id).style.width);
	marker.height = parseInt(document.getElementById(this.id).style.height);
	marker.mid = this.id;
	marker.map = this.map;
	marker.lid = id;
	marker.html = info;
	GEvent.addListener(marker, "mouseover", this.mapListenerBox);
	GEvent.addListener(marker, "mouseout", this.mapListenerBoxClose);
	GEvent.addListener(marker, "click", this.mapListenerLoad);

	this.bounds.extend(marker.getPoint());
	this.map.addOverlay(marker);
}

function mapListenerBox (){
	
	var close_onclick = function (){ 
		var obj = document.getElementById(this.pid);
		obj.parentNode.removeChild(obj);
	};
	
	if (document.getElementById(this.mid+"1")){
		var oldObj = document.getElementById(this.mid+"1");
		oldObj.parentNode.removeChild(oldObj);
	}
	
	var oldObj = document.getElementById(this.mid+"1");
	var bounds = this.map.getBounds();

	var right = bounds.getNorthEast().lng();
	var top = bounds.getNorthEast().lat();
	var left = bounds.getSouthWest().lng();
	var bottom = bounds.getSouthWest().lat();

	var lat = this.getPoint().lat();
	var lng = this.getPoint().lng();
	if (right > left){
		var xOffset = Math.floor( ( ( lng - left ) / ( right - left ) ) * this.width );
	} else {
		var l2 = 180 - left;
		if (lng < 0){
			var lng2 =  Number(l2) + 180 + Number(lng);
		} else {
			var lng2 =  lng - left;
		}
		var tw = Number(l2) + 180 + Number(right);
		var xOffset = Math.floor( ( lng2 / tw ) * this.width );
	}
	
	var yOffset = Math.floor( ( ( lat - top ) / (bottom - top) ) * this.height );
	
	var obj = document.createElement("span");
	
	obj.style.position = "absolute";
	obj.style.top = (yOffset-15) + "px";
	obj.style.left = (xOffset+10) + "px";
	obj.innerHTML = '<div style="border: 3px solid #555555; background-color: #eeeeee;">'+this.html+ '</div>';
	obj.id = "mappopup";
	obj = mapPNGConvert(obj);
	document.getElementById(this.mid).appendChild(obj);
}

function mapListenerBoxClose (){
	var popup = document.getElementById("mappopup");
	if (popup) popup.parentNode.removeChild(popup);
}

function mapListenerLoad (){
	
	for (var i=0; i<10; i++){
		var obj = document.getElementById("r"+(i+1));
		if (obj){
			if (this.lid == i+1){
				obj.style.backgroundColor = "#e8edfd";
			} else {
				obj.style.backgroundColor = "#ffffff";
			}
		}
	}
	
	document.location.replace("#"+this.lid);
	
	
}

function mapClearOverlay(){
	this.map.clearOverlays();
}

function mapSetDist (dist){
	this.dist = dist;
}

function mapSetIcon (schema, id){

	// Create our "tiny" marker icon
	var icon = new GIcon();

	switch (schema){
		case "myloc":
			icon.iconSize = new GSize(16, 15);
			icon.iconAnchor = new GPoint(8, 7);
			icon.infoWindowAnchor = new GPoint(8, 7);	
			icon.image = "http://www.nacba.org/attorneyfinder/images/markers/myloc.png";
			break;
		case "new":
			icon.iconSize = new GSize(20, 20);
			icon.iconAnchor = new GPoint(10, 10);
			icon.infoWindowAnchor = new GPoint(10, 10);	
			icon.image = "http://www.nacba.org/attorneyfinder/images/markers/new"+id+".png";
			break;
	}
	
	return icon;
}

function mapPNGConvert (info){
	
	var arVersion = navigator.appVersion.split("MSIE")
	var version = parseFloat(arVersion[1])

	if ((version >= 5.5) && (document.body.filters)){
		
		var imgs = info.getElementsByTagName ("img");
		
		for(var i=0; i<imgs.length; i++){
			var img = imgs[i];
			var imgName = img.src.toUpperCase();
			if (imgName.substring(imgName.length-3, imgName.length) == "PNG"){
				var imgID = (img.id) ? "id='" + img.id + "' " : "";
				var imgClass = (img.className) ? "class='" + img.className + "' " : "";
				var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' "
				var imgStyle = "display:inline-block;" + img.style.cssText ;
				if (img.align == "left") imgStyle = "float:left;" + imgStyle;
				if (img.align == "right") imgStyle = "float:right;" + imgStyle;
				if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle;
				var strNewHTML = "<span " + imgID + imgClass
				+ " style=\"" + "width:" + img.parentElement.width + "px; height:" + img.parentElement.height + "px;" + imgStyle + ";"
				+ "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
				+ "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>";
				img.outerHTML = strNewHTML;
				i = i-1;
			}
		}
	}
	
	return info;
}

function mapDirSimple (lat, lng, dirto) {
	if (GBrowserIsCompatible()) {
		var map = new GMap2(document.getElementById("map"));
		map.addControl(new GSmallMapControl());
		map.setCenter(new GLatLng(lat, lng), 15);
		if (dirto != false){
			var directionsPanel = document.getElementById("directions");
			var directions = new GDirections(map, directionsPanel);
			directions.load(dirto);
		} else {
			var loc = new GLatLng(lat, lng);
			map.addOverlay(new GMarker(loc));
		}
	}
}