// Map balloon
var baseIcon,gmap=null,geocoder=null;
function mapinit()
{
	if(gmap) return;
	gmap = new GMap2(document.getElementById("mapbalmap"),{size:new GSize(300,240)});
	gmap.addControl(new GSmallMapControl());    
	gmap.addControl(new GScaleControl());    
	baseIcon = new GIcon();    
	baseIcon.image = "/sys/images/all/redmarker.png";    
	baseIcon.iconSize = new GSize(20, 20);    
	baseIcon.shadowSize = new GSize(20, 20);    
	baseIcon.iconAnchor = new GPoint(6, 20);    
	baseIcon.infoWindowAnchor = new GPoint(5, 1);    
	gmap.setCenter(new GLatLng(180,0));
	
	var dc=document.cookie;
	var pos=dc.indexOf("addr1=");
	if(pos>=0) {
		var end=dc.indexOf(";",pos);
		if(end<0) end=dc.length;
		document.mapbalform["addr1"].value=unescape(dc.substring(pos+6,end));
	}
}
function mapover(e,name,addr,city,st,zip,geo,p)
{
	if(geo && !p && typeof(geo)!="string") { p=geo; geo=''; }	// for backward compatability
	var str=addr;if(str.length)str+=", ";str+=city;if(st.length)str+=", "+st;if(zip.length)str+=" "+zip;
	mappop.over(e,{n:name,a:addr,c:city,s:st,z:zip,g:str,l:geo},p);
	mapinit();
}
function mapout() {	mappop.out() }
function mapclick()
{
	var d=mappop.newestData();
	if(!d) return;
	mappop.hide();
	map(d.n,d.a,d.c,d.s,d.z,d.l);
}
function mapshow(p)
{
	if(!p.data) return;
	if(wfpop.visible) p.parent=wfpop;
	document.getElementById("mapname").innerHTML=p.data.n;
	document.getElementById("mapaddr").innerHTML=p.data.g;
	document.getElementById("mapbalnotfound").style.top=-90;
	gmap.setCenter(new GLatLng(180,0));
	trynum=0;
	
	setTimeout("mapgeocode()",1);
}
function maphide(p) { p.parent=null; }
function mapdir()
{
	var saddr=escape(document.mapbalform["addr1"].value);
	var daddr=escape(mappop.data.g);
	if(typeof(mappop.data.l)=='string' && mappop.data.l.length) daddr+=escape(" @"+mappop.data.l);
	mappop.hide();
	document.cookie="addr1="+saddr+";expires=Fri, 31-Dec-2023 00:00:00 GMT";
	window.open("http://maps.google.com/?saddr="+saddr+"&daddr="+daddr,"Directions","titlebar=no,status=no,toolbar=no,menubar=no,resizable=yes");
}
function mapgeocode()
{
	if(!mappop.data) return;
	var lat=null;
	var lon=null;
	var lev=15;
	var geo=mappop.data.l;
	if(geo && geo.length) {
		geo=geo.split(",");
		if(geo.length>1) {
			lat=parseFloat(geo[0]);
			lon=parseFloat(geo[1]);
		}
		if(geo.length>2) lev=parseInt(geo[2]);
	}
	if(lat||lon) {
		placeMarker(lev,lat,lon);
	} else {
		if(!geocoder) geocoder=new GClientGeocoder();
		geocoder.getLocations(mappop.data.g,geocodeCallback);
	}
}
function geocodeCallback(g)
{
	if(!mappop.data) return;
	if(!g || g.Status.code!=200) {	// not found
		gmap.setCenter(new GLatLng(37.150677,-97.690431),2);
		document.getElementById("mapbalnotfound").style.top=90;
	} else {
		var b=0,ba=0;
		if(typeof(zipcodes)!="undefined") {
			for(i=0;i<g.Placemark.length;i++) {
				var a=0,zip=null,p=g.Placemark[i];
				if(p.AddressDetails) p=p.AddressDetails;
				if(p.Accuracy) a=p.Accuracy;
				if(p.Country) p=p.Country;
				if(p.AdministrativeArea) p=p.AdministrativeArea;
				if(p.SubAdministrativeArea) p=p.SubAdministrativeArea;
				if(p.Locality) p=p.Locality;
				if(p.LocalityName && p.LocalityName.toLowerCase()==mappop.data.c.toLowerCase()) a++;
				if(p.PostalCode) zip=p.PostalCode.PostalCodeNumber;
				if(!zip) continue;
				if(zipcodes.indexOf(","+zip+",")>=0 && a>ba) { b=i; ba=a; }
			}
		}
		var p=g.Placemark[b];
		var lev=7+p.AddressDetails.Accuracy;
		placeMarker(lev,p.Point.coordinates[1],p.Point.coordinates[0]);
	}
}
function placeMarker(lev,lat,lon)
{
	var pt=new GLatLng(lat,lon);
	gmap.setCenter(pt,lev); 
	var marker=new GMarker(pt,baseIcon);
	var cap="<div style='font-family:arial;font-size:11px;text-align:left'><b>"+mappop.data.n+"</b><br>"+mappop.data.g+"</div>";             
	GEvent.addListener(marker, "click", function() { marker.openInfoWindowHtml(cap);  }); 
	gmap.clearOverlays();
	gmap.addOverlay(marker);
}

var mappop=new GlassPopup(
	{autoHide:true,overDelay:popDelay,outDelay:popDelay,showing:mapshow,hiding:maphide,pointClick:mapclick},
	{create:false,x:-500,y:-500,width:335,height:330,innerHTML:"<span id=mapname style='position:absolute;top:10;left:5;width:310;font-family:arial;font-size:12px;font-weight:bold;text-align:center;overflow:hidden'></span><span id=mapaddr style='position:absolute;top:27;left:5;width:310;font-family:arial;font-size:12px;text-align:center;overflow:hidden'></span><div style='width:302px;height:242px;background-color:#666;position:absolute;top:45px;left:14px;overflow:hidden'><div id='mapbalmap' style='width:300px;height:240px;position:absolute;top:1px;left:1px;z-index:1;overflow:hidden'></div><div id='mapbalnotfound' style='width:200px;height:50px;position:absolute;top:-90px;left:51px;z-index:2;font-family:arial;font-size:14px;font-weight:bold;text-align:center;border-width:1px;border-style:solid;border-color:#f22;background-color:#ddd;padding-top:6px' onclick='this.style.top=-90'>Sorry, this address could<br>not be found on the map.</div><div style='width:66px;height:13px;position:absolute;top:0px;left:118px;z-index:2;font-family:arial;font-size:11px;line-height:12px;text-align:center;border-width:1px;border-style:solid;border-color:#008;background-color:#fff;color:#00c;cursor:pointer;-moz-opacity:.75;opacity:.75;filter:alpha(opacity=75);' onclick='mapclick()'>Large Map</div></div><span style='position:absolute;top:295;left:15;font-family:arial;font-size:12px'>Directions&nbsp;from:</span><form name=mapbalform onsubmit='mapdir();return false'><input name=addr1 type=text style='position:absolute;top:292;left:105;font-family:arial;font-size:12px;width:172;height:21'><img src='images/all/go.png' title='get directions' onclick='mapdir()' style='position:absolute;top:292;left:281' width=35 height=22 border=0></form>"},
	{showPoint:true,inWindow:"force"});
setTimeout("mappop.pane.show()",100);

