// MGeocoder() GMaps API extension copyright 2005 Mikel Maron (mikel_maron@yahoo.com)
// http://brainoff.com/gmaps/mgeocoder.html
// free for non-commercial use
//  use must also comply with licensing on geocoder.us and brainoff.com/geocoder

function MGeocoder(){}
MGeocoder.prototype.initialize=function(map) {
    this.map = map;
    this.xmlhttp = false;
    this.request = false;
    //    this.addButton(map);
    //    this.addForm(map);
}
MGeocoder.prototype.addButton=function(map) {
    this.button=map.ownerDocument.createElement('div');
    this.button.m = this;
    this.button.id='MGeocoderButton';
    this.button.style.position='absolute';
    this.button.style.right='0px';
    this.button.style.bottom='20px';
    this.button.style.backgroundColor='#f2efe9';
    this.button.style.zIndex=35500;
    this.button.style.fontFamily='arial';
    //this.button.style.fontWeight='bold';
    this.button.style.borderStyle='solid';
    this.button.style.borderWidth='1px';
    this.button.innerHTML = "<input type=\"button\" value=\"Find your Location\" href=\"#\" onclick=\"this.parentNode.m.showhide()\">";
    this.setOpacity(this.button,75);
    map.div.parentNode.appendChild(this.button);
    
}
MGeocoder.prototype.addForm=function(map) {
    this.frm=map.ownerDocument.createElement('div');
    this.frm.m = this;
    this.frm.id = 'MGeocoderFormDiv';
    this.frm.style.position='relative';
    this.frm.style.backgroundColor='#f2efe9';
    this.frm.style.zIndex=35501;
    this.frm.style.visibility="hidden";
    this.frm.style.display="none";
    this.frm.style.padding='20px';
    this.frm.style.fontSize='10px';
    this.frm.style.fontFamily='arial';
    this.frm.innerHTML = '<span style="color:red; font-size:12px;" id="MGeocoderError"></span><br/><br/><input type=\"button\" value=\"hide\" href="#" style="position:absolute;right:0px;top:0px;" onclick="this.parentNode.m.showhide()"><br/><b>Enter a Full US Address</b><form id="MGeocoderFormUs" onSubmit="this.parentNode.m.callgeocoder(\'us\',this.addr.value,null,null); return false"><input name="addr" id="geocoder_addr" size="80"/><br/><input type="Submit" value="Lookup US Address"/></form>';
    this.setOpacity(this.frm,95);
    map.div.parentNode.appendChild(this.frm);
}
MGeocoder.prototype.setOpacity=function(d,b) {
  if(b<0){b=0;}  if(b>=100){b=100;}
  var c=b/100;
  if(typeof(d.style.filter)=='string'){d.style.filter='alpha(opacity:'+b+')';}
  if(typeof(d.style.KHTMLOpacity)=='string'){d.style.KHTMLOpacity=c;}
  if(typeof(d.style.MozOpacity)=='string'){d.style.MozOpacity=c;}
  if(typeof(d.style.opacity)=='string'){d.style.opacity=c;}
}
MGeocoder.prototype.showhide=function() {
    e = this.frm;
    if (e.style.visibility == "hidden") {
        e.style.visibility = "visible";
        e.style.display = "block";
        var addr = document.getElementById("geocoder_addr");
        addr.focus();

    } else {
        e.style.visibility = "hidden";
        e.style.display = "none";
    }
}

MGeocoder.prototype.callgeocoder=function(type,addr,city,country) {
    if (this.request != false)  { return; }
    this.request = type;
    var errNode = document.getElementById("MGeocoderError");
    if(errNode) {
        errNode.innerHTML = "Querying...";
    }
    if (! this.xmlhttp) {
        this.loadReq();
    }
    if (this.request == "us") {
        url = "http://geocoder.us/service/rest/?address=" + encodeURI(addr);
    } else {
        url = "http://brainoff.com/geocoder/rest/?city=" + encodeURI(city+","+country);
    }
    this.addr = addr;
    this.xmlhttp.open("GET","proxy.php?"+url,true);
    //    this.xmlhttp.onreadystatechange=mgeocoder_cb;

    var geoc = this;
    this.xmlhttp.onreadystatechange= function() {
        geoc.callback();
    }
    this.xmlhttp.send(null);
}

MGeocoder.prototype.loadReq = function() {
    /*@cc_on @*/
    /*@if (@_jscript_version >= 5)
     // JScript gives us Conditional compilation, we can cope with old IE versions.
     // and security blocked creation of the objects.
 try {
  this.xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
 } catch (e) {
  try {
   this.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  } catch (E) {
   this.xmlhttp = false;
  }
 }
@end @*/
    if (!this.xmlhttp && typeof XMLHttpRequest!='undefined') {
        this.xmlhttp = new XMLHttpRequest();
    }
}

function mgeocoder_cb() {
    e = document.getElementById("MGeocoderFormDiv");
    e.m.callback();
}

MGeocoder.prototype.getNodeValue = function(obj, nodeName) {
    var st = "";
    if (obj.hasChildNodes()) {
        var i = 0;
        while ((st == "") && (i < obj.childNodes.length)) {
            st = (obj.childNodes[i].nodeName == nodeName) ? obj.childNodes[i].firstChild.nodeValue : this.getNodeValue(obj.childNodes[i], nodeName);
            i++;
        }
    }
    return st;
}

MGeocoder.prototype.callback = function() {
  	if (this.xmlhttp.readyState==4) {
		lat = false; lon = false;
		if (this.xmlhttp.status == "200") {
            if (this.xmlhttp.responseXML && this.xmlhttp.responseXML.hasChildNodes()) {
				try {
                    lat = this.getNodeValue(this.xmlhttp.responseXML, "geo:lat");
                    lon = this.getNodeValue(this.xmlhttp.responseXML, "geo:long");
				} catch(e) {
				}
			}
		}
        
		if (lat != false && lon != false) {
			var errNode = document.getElementById("MGeocoderError");
            if(errNode) {
                errNode.innerHTML = "";
            }
            //			this.showhide();
			if (this.request == "us") { z = 3; }
			else { z = 8; }

            var point = new GPoint(lon, lat);
            var marker = new GMarker(point);
            map.addOverlay(marker);
            this.map.centerAndZoom(point,z);
            marker.openInfoWindowHtml("<B>" + this.addr.replace(/\,/ig, "<br>") + "</b>");
		} else {
            var errNode = document.getElementById("MGeocoderError");
            if(errNode) {
                errNode.innerHTML = "Lookup Failed";
            }
		}
		this.request = false;
	}
}

GMap.prototype.addMGeocoder=function(a) {
    a.initialize(this);
}
