function mapObj (id){
    if (GBrowserIsCompatible()) {
		this.id = id;
		this.dist = 5;
		this.en_pts = '';
		this.en_lvl = '';
		this.en_lat = 0;
		this.en_lng = 0;
	//	alert (document.getElementById(id));
		this.map = new GMap2(document.getElementById(id));
		
		//var mapType = map.getMapTypes(); 
		
		
		
		this.bounds = new GLatLngBounds(); 
		
		this.map.setCenter(new GLatLng(39.436193, -95.361328), this.dist);
	//	this.map.setCenter(this.bounds.getCenter(), this.dist);
		
		this.map.addControl(new GSmallMapControl());
		
		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.mapGetProx=mapGetProx;
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;
mapObj.prototype.mapESN=mapESN;
mapObj.prototype.mapEN=mapEN;
mapObj.prototype.mapLAP=mapLAP;
mapObj.prototype.mapPP=mapPP;

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 < 3) zoom = 3;
	this.map.setZoom(zoom);
	//this.map.setCenter(this.bounds.getCenter());

}

function mapPlotMarker(lat, lng, icon, id, info, lid){
	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.html = info;
	marker.lid = lid;
	GEvent.addListener(marker, "mouseover", this.mapListenerBox);
	GEvent.addListener(marker, "mouseout", this.mapListenerBoxClose);
	GEvent.addListener(marker, "click", this.mapListenerLoad);
//	GEvent.bind(marker, "click", this, this.mapListenerBox);

	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();

	//alert (top+" "+right+" "+bottom+" "+left);
	
	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 xOffset = Math.floor( ( ( lng - left ) / (right - left) ) * this.width );
	
	var obj = document.createElement("span");
//	obj.id = this.mid+"1";
	
	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);
	
	//var close = document.getElementById("pUC");
	//close.pid = this.mid+"1";
	//close.onclick = close_onclick;
	
}

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

function mapListenerLoad (){
	var iframe = document.getElementById("iframe");		
	//alert ("travel2.php?lid="+this.lid);
	
	var ifr = document.createElement("iframe");
	ifr.style.width = "800px";
	ifr.style.height = "250px";
	ifr.scrolling = "auto";
	ifr.frameBorder = "0";
	ifr.style.overflowY = "hidden";
	ifr.src = "travel2.php?lid="+this.lid;
	
	iframe.removeChild(iframe.firstChild);
	iframe.appendChild(ifr);
	
}

function mapGetProx (lat, lng){
	var request_onreadystatechange = function () { self.mapPlotMarker(self); };
	(function (){
		this.request = ajaxObject();
		this.request.open("GET", "distProx.php?lat="+this.lat+"&lon="+this.lon+"&d="+this.dist, true);
		var self = this.request;
		this.request.onreadystatechange = request_onreadystatechange;
		this.request.send(null);
	})();
}

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 "start":
		default:
			icon.iconSize = new GSize(16, 16);
			icon.iconAnchor = new GPoint(8, 8);
			icon.infoWindowAnchor = new GPoint(8, 8);	
			icon.image = "http://www.markrobot.com/pics/travel/"+schema+".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;
}

// Line add point
function mapLAP (lat, lng){
	
	var nlat = Math.floor ( lat * 1e5 );
	var nlng = Math.floor ( lng * 1e5 );
	
	var dlat = nlat - this.en_lat;
	var dlng = nlng - this.en_lng;

	this.en_lat = nlat;
	this.en_lng = nlng;
	
	this.en_pts += this.mapESN(dlat) + this.mapESN(dlng);
	this.en_lvl += this.mapEN(3);
		
}


// print polyline
function mapPP (){
	
//	alert (this.en_pts);
//	alert (this.en_lvl);

	var pts = this.en_pts;
	var lvl = this.en_lvl;

	this.en_pts = "";
	this.en_lvl = "";
	this.en_lat = 0;
	this.en_lng = 0;
	
    var pl = new GPolyline.fromEncoded({color: "#0000FF",
                                      weight: 3,
                                      points: pts,
                                      levels: lvl,
                                      zoomFactor: 16,
                                      numLevels: 4
                                     });
	this.map.addOverlay(pl);

}

// Stolen from google
// Encode a signed number in the encode format.
function mapESN(num) {
  var s_num = num << 1;

  if (num < 0) {
    s_num = ~(s_num);
  }

  return(mapEN(s_num));
}

// Encode an unsigned number in the encode format.
function mapEN(num) {
  var encodeString = "";

  while (num >= 0x20) {
    encodeString += (String.fromCharCode((0x20 | (num & 0x1f)) + 63));
    num >>= 5;
  }

  encodeString += (String.fromCharCode(num + 63));
  return encodeString;
}