var map;var geocoder;var rootMarkers;var waitingForRootMarkers=false;var initializingMap=true;var theJsonUrl;var theDetailUrl;var theSelectedTabIndex;var theSelectedPropertyId;var theProperties;var theSelectedPropertyMarker;var theSelectedPropertyZoom;var theGSmallMapControl;var theGMapTypeControl;var theAddress;var myZoomIncrease=2;var myMinZoom=1;var myMaxZoom=17;var myOpenInfoWindowZoom=14;var myIconCount=100;var myOpenInfoWindowZoomSingle=11;var theMode;function initMapWithJson(e,c,d,b,a,f){theMode="json";theProperties=null;theJsonUrl=e;theSelectedPropertyId=a;theSelectedPropertyZoom=f;initMapsApiLoader(c,d,b)}function initMapWithoutAddress(a){theMode="address";theAddress=null;if(map==null){initMapsApiLoader(null,a,null)}else{openAddressWindow(theAddress)}}function initMapWithAddress(a,b){theMode="address";theAddress=a;if(map==null){initMapsApiLoader(null,b,null)}else{openAddressWindow(theAddress)}}function initMapWithProperties(d,c,e,b,a,f){theMode="properties";theProperties=d;theJsonUrl=null;theSelectedPropertyId=a;theSelectedPropertyZoom=f;initMapsApiLoader(c,e,b)}function initMapsApiLoader(b,c,a){theDetailUrl=b;theSelectedTabIndex=a;$("head").append('<script src="http://www.google.com/jsapi?key='+c+'&callback=loadMapsApi" type="text/javascript"/>')}function loadMapsApi(){google.load("maps","2",{callback:mapsApiLoaded})}function mapsApiLoaded(){var a=contextPath+"/js/extinfowindow.js";$("head").append('<script src="'+a+'" type="text/javascript"/>');downloadProperties()}function downloadProperties(){if(theMode=="json"){GDownloadUrl(theJsonUrl,readProperties)}else{if(theMode=="address"){showMap()}else{if(theMode=="properties"){processProperties()}else{alert("Invalid mode: "+theMode)}}}}function showMap(){if(theMode=="address"){if(GBrowserIsCompatible()){setMap();configureMapAddress()}}else{if(!initializingMap){if(GBrowserIsCompatible()){if($("#subTabContainer ul").data("selected.tabs")==theSelectedTabIndex){setMap();waitingForRootMarkers=true;if(rootMarkers!=null){configureMap()}else{$("#mapContainer").css("backgroundImage","url('../images/pleaseWait/rotatingPleaseWait.gif')")}}}}}}function setMap(){map=new GMap2(document.getElementById("mapContainer"));geocoder=new GClientGeocoder();$("#mapContainer").css("backgroundColor","white");$("#mapContainer").css("backgroundRepeat","no-repeat");$("#mapContainer").css("backgroundPosition","center");map.removeMapType(G_SATELLITE_MAP);theGSmallMapControl=new GSmallMapControl();theGMapTypeControl=new GMapTypeControl();if(("address"==theMode)){map.disableDragging()}else{map.addControl(theGSmallMapControl);map.addControl(theGMapTypeControl)}}function configureMap(){var d;var a;if(theSelectedPropertyMarker==null){if(rootMarkers.length>1){a=new GLatLng(0,0);d=myMinZoom}else{var c=calculateLatLngBounds(theProperties);d=Math.min(map.getBoundsZoomLevel(c),myOpenInfoWindowZoom-myZoomIncrease);a=c.getCenter()}}else{a=theSelectedPropertyMarker.getLatLng();if(theSelectedPropertyZoom){d=theSelectedPropertyZoom}else{d=Math.min(theSelectedPropertyMarker.zoom,myOpenInfoWindowZoom)}}map.setCenter(a);map.setZoom(d);$("#mapContainer").css("backgroundImage","");var b=createMarkerManager(rootMarkers);GEvent.addListener(map,"click",handleClick);if(theSelectedPropertyMarker!=null){openInfoWindow(theSelectedPropertyMarker)}}function calculateLatLngBounds(g){var b;var d;var c;var f;for(var e=g.length-1;e>=0;e--){var j=g[e];b=!b?j.geoLatitude:Math.min(j.geoLatitude,b);d=!d?j.geoLatitude:Math.max(j.geoLatitude,d);c=!c?j.geoLongitude:Math.min(j.geoLongitude,c);f=!f?j.geoLongitude:Math.max(j.geoLongitude,f)}var h=new GLatLng(b,c);var a=new GLatLng(d,f);return new GLatLngBounds(h,a)}function configureMapAddress(){openAddressWindow(theAddress)}function handleClick(f,i){if(f&&f.propertyCount){var d=f.propertyCount;var h=map.getZoom();var c;if(d>1){var e=f.markers;var g;if(e!=null){var b=createBoundsFromMarkers(e);c=b.getCenter();g=map.getBoundsZoomLevel(b)}else{c=f.getLatLng();g=myMaxZoom}if(h>=myOpenInfoWindowZoom){openInfoWindow(f)}else{if(map.getExtInfoWindow()!=null){map.closeExtInfoWindow()}var a=h+myZoomIncrease;a=Math.min(a,g);if(a!=h){map.setCenter(c,a)}else{map.setCenter(c)}}}else{if(h>=myOpenInfoWindowZoomSingle){openInfoWindow(f)}else{c=f.getLatLng();var a=h+myZoomIncrease;map.setCenter(c,a)}}}}function readProperties(doc){var jsonData=eval("("+doc+")");theProperties=jsonData.properties;processProperties()}function processProperties(){rootMarkers=createRootMarkers(theProperties);initializingMap=false;if(waitingForRootMarkers){configureMap()}else{showMap()}}function openInfoWindow(h){var l=h.properties;var m=theDetailUrl+(theDetailUrl.indexOf("?")<0?"?":"&")+["mapZoom",map.getZoom()].join("=");var n="infoContents";var b=0;var o=h.propertyIdArrays;for(var g=0;g<o.length;g++){var p=o[g];for(var f=0;f<p.length;f++){var k=p[f];m+="&propertyId="+k;n+="_"+k;b++}}var c;var d;var a;if(b>1){c="map_details_multiple_window";d=276;a=117}else{c="map_details_single_window";d=260;a=77}var e='<div id="'+n+'" style="width:'+d+"px;height:"+a+'px;" src="../images/pleaseWait/rotatingPleaseWait.gif"/></div>';h.openExtInfoWindow(map,c,e,{ajaxUrl:m,beakOffset:3,paddingX:25,paddingY:25})}function openAddressWindow(c){if(c!=null){var a=new GLatLng(c.geoLatitude,c.geoLongitude);map.setCenter(a,16);var b=new GMarker(a,{icon:createMapsPointer(1),clickable:false});map.clearOverlays();map.addOverlay(b)}else{map.setCenter(new GLatLng(52.119999,5.537109),6)}}function createIcons(){var b=[];for(var a=1;a<=myIconCount;a++){b.push(createMapsPointer(""+a))}return b}function createRootMarkers(e){var g=createIcons();var h=myMaxZoom;var c=theSelectedPropertyZoom;var d=createGroupLatsFromProperties(e,h,g,c==h,theSelectedPropertyId);while(h>myMinZoom){d=processGroupLats(d,--h,g,c==h)}var i;for(var a in d){var f=d[a];for(var b in f){i=f[b]}}return i}function createMarkerManager(b){var a=new GMarkerManager(map,{borderPadding:1});addMarkers(a,b,myMinZoom,addMarkers);a.refresh();return a}function addMarkers(d,h,g,a){for(var c=h.length-1;c>=0;c--){var b=h[c];var f=b.zoom;d.addMarkers([b],g,f);var e=b.markers;if(e!=null){a(d,e,f+1,a)}}}function createMapsPointer(b){var a=new GIcon();a.image=contextPath+"/images/icons/mapspointer/"+b+".png";a.iconSize=new GSize(30,30);a.infoWindowAnchor=new GPoint(15,5);a.iconAnchor=new GPoint(15,15);a.imageMap=[23,43,7,31,7,27,3,23,15,6,21,8,33,4,45,23,41,26,39,31];return a}function createGroupLatsFromProperties(m,r,q,f,b){var k=new Object();var g=createMarker;var l=GLatLng;var e=GMarker;for(var j=m.length-1;j>=0;j--){var p=m[j];var c=(p.geoLatitude*1200)|0;var d=(p.geoLongitude*600)|0;var n=k[c];var h;if(n==null){var a=new l(p.geoLatitude,p.geoLongitude);h=g(a,null,r,1,[[p.propertyId]],q,e);h.properties=[p];var o=[h];n=new Object();n[d]=o;k[c]=n}else{var o=n[d];if(o==null){var a=new l(p.geoLatitude,p.geoLongitude);h=g(a,null,r,1,[[p.propertyId]],q,e);h.properties=[p];o=[h];n[d]=o}else{o[0]=updateMarkerFromProperty(o[0],p,q,e);h=o[0]}}if(b){if(b==p.propertyId){h.selected=true;if(f){theSelectedPropertyMarker=h}}}}return k}function reportGroupLats(f,e){var b=0;for(var a in f){var d=f[a];for(var g in d){var c=d[g];alert("groupLats "+f.length+", groupLngs "+d.length+", lat "+a+", lng "+g+" : "+c);b++;if(b==e){alert("return");return}}}alert("done")}function updateMarkerFromProperty(c,h,f,d){var e=c.properties;e.push(h);var b=createCenterFromProperties(e);var g=c.propertyIdArrays;g.push([h.propertyId]);var a=c.propertyCount+1;var c=createMarker(b,null,myMaxZoom,a,g,f,d);c.properties=e;return c}function processGroupLats(h,p,o,d){var q=new Object();var j=createMarkerFromMarkers;var f=createMarker;var e=GMarker;for(var a in h){var k=a>>>1;var c=q[k];if(c==null){c=new Object();q[k]=c}var n=h[a];for(var b in n){var m=b>>>1;var l=c[m];if(l==null){l=[];c[m]=l}var i=n[b];var g;if(i.length==1){g=i[0]}else{g=j(i,p,o,e,f)}if(d){if(g.selected==true){theSelectedPropertyMarker=g}}l.push(g)}}return q}function createMarkerFromMarkers(q,a,v,p,h){var c=q[0];var j=c.propertyCount;var t;var g=c.getLatLng();var r=a>=myOpenInfoWindowZoom;if(r){t=c.propertyIdArrays}var x=g.lat();var n=g.lng();var b=x;var m=x;var s=n;var f=n;var k=false;for(var o=q.length-1;o>0;o--){var e=q[o];j+=e.propertyCount;if(r){t=t.concat(e.propertyIdArrays)}var l=e.getLatLng();var d=l.lat();var w=l.lng();if(b>d){b=d}else{if(m<d){m=d}}if(s>w){s=w}else{if(f<w){f=w}}k|=e.selected}var u=new GLatLng((b+m)/2,(s+f)/2);return h(u,q,a,j,t,v,p,k)}function createMarker(a,d,j,b,h,i,f,e){var c=b;if(c>myIconCount){c=myIconCount}var g=new f(a,i[c-1]);g.markers=d;g.zoom=j;g.propertyCount=b;if(h!=null){g.propertyIdArrays=h}g.selected=e;return g}function createCenterFromProperties(d){var h=d[0].geoLatitude;var g=d[0].geoLatitude;var e=d[0].geoLongitude;var c=d[0].geoLongitude;for(var b=1;b<d.length;b++){var f=d[b].geoLatitude;var a=d[b].geoLongitude;if(h>f){h=f}else{if(g<f){g=f}}if(e>a){e=a}else{if(c<a){c=a}}}center=new GLatLng((h+g)/2,(e+c)/2);return center}function createBoundsFromProperties(c){var e=new GLatLngBounds();var b=c.length;for(var a=0;a<b;a++){var f=c[a];var d=new GLatLng(f.geoLatitude,f.geoLongitude);e.extend(d)}return e}function createBoundsFromMarkers(f){var e=new GLatLngBounds();var b=f.length;for(var c=0;c<b;c++){var a=f[c];var d=a.getLatLng();e.extend(d)}return e}function openDetails(a){alert("url "+a);window.open(a+"&mapZoom="+map.getZoom(),"_self")};