var map=null, loadedDefaults=0;

var spProgWaiting = 0, spProgTotal = 0, spProgTmr = null, spLastPointAdded = 0, spPointsLoaded = 0;
var mapIsBusy = 0, mapElementID = null;
var loadedPoints = new Array();

var USE_ACTIONS,SHOW_PAST_LISTINGS,SHOW_PRESENT_LISTINGS,SHOW_PASTPRESENT_SPEC,USE_MINI_MAP,USE_MAPTYPE_SELECTOR,USE_MAP_ZOOM,USE_STATUS_ICONS,USE_BOUND_BL,USE_BOUND_TR,SHOW_ACTIVE,SHOW_CONTRACT,SHOW_SOLD,DROPZOOM,SUPRESS_POINTS,EXTRA_DATA;
var MAP_NOT_LOADED_WAIT = 1500;
var circBound = new Object();
circBound.maxlat=-100;circBound.minlat=100;circBound.maxlng=-180;circBound.minlng=180;

////////////////
var tmrLoadMap = null;
function loadMapInto(eid) {
 if(document.getElementById(eid)!=null) {
  var newbody = "<div style=\"display:none;\">";
  newbody += "<img src=\""+gethost()+"img/gmap/icon-normal.png\" />";
  newbody += "<img src=\""+gethost()+"img/gmap/icon-normal_old.png\" />";
  newbody += "<img src=\""+gethost()+"img/gmap/icon-contract.png\" />";
  newbody += "<img src=\""+gethost()+"img/gmap/icon-contract_old.png\" />";
  newbody += "<img src=\""+gethost()+"img/gmap/icon-sold.png\" />";
  newbody += "<img src=\""+gethost()+"img/gmap/icon-sold_old.png\" />";
  newbody += "<img src=\""+gethost()+"img/gmap/icon-shadow.png\" />";
  newbody += "</div><iframe id=\"gmap_update\" style=\"display:none;\"></iframe><div id=\""+eid+"_innerMap\" style=\"position:relative;\">"+document.getElementById(eid).innerHTML+"</div>"
  document.getElementById(eid).innerHTML = newbody;
 }
 mapElementID = eid+"_innerMap";
 iconCurr = new GIcon();
 makeIcon(iconCurr, "img/gmap/icon-normal.png");
 if((SHOW_PAST_LISTINGS)||(SHOW_PASTPRESENT_SPEC)) {
  iconOld = new GIcon();
  makeIcon(iconOld, "img/gmap/icon-normal_old.png");
 }
 if(USE_STATUS_ICONS) {
  iconContractCurr = new GIcon();
  makeIcon(iconContractCurr, "img/gmap/icon-contract.png");
  iconContractOld = new GIcon();
  makeIcon(iconContractOld, "img/gmap/icon-contract_old.png");
  iconSoldCurr = new GIcon();
  makeIcon(iconSoldCurr, "img/gmap/icon-sold.png");
  iconSoldOld = new GIcon();
  makeIcon(iconSoldOld, "img/gmap/icon-sold_old.png");
 }
 tmrLoadMap = setTimeout("loadMapInto_Main('" + eid + "_innerMap')", MAP_NOT_LOADED_WAIT);
}

////////////////
function loadMapInto_Main(eid) {
 if(tmrLoadMap!=null) clearTimeout(tmrLoadMap);
 var mapElm = document.getElementById(eid);
 if(mapElm==null) { return; }
 if(eid.indexOf("_innerMap")!=-1) {
  var mapElmPar = document.getElementById(eid.substring(0, eid.indexOf("_innerMap")));
  if(mapElmPar!=null) {
   if(isNaN(parseInt(mapElmPar.style.width))) {
    var objTmp = mapElmPar;
    if(objTmp.parentNode) { objTmp = objTmp.parentNode; }
    while((objTmp.offsetWidth==0)&&(objTmp.parentNode)) {
     objTmp = objTmp.parentNode;
    }
    mapElm.style.width = objTmp.offsetWidth+"px";
   } else {
    mapElm.style.width = mapElmPar.style.width;
   }
   if(isNaN(parseInt(mapElmPar.style.height))) {
    var objTmp = mapElmPar;
    if(objTmp.parentNode) { objTmp = objTmp.parentNode; }
    while((objTmp.offsetHeight==0)&&(objTmp.parentNode)) {
     objTmp = objTmp.parentNode;
    }
    mapElm.style.height = objTmp.offsetHeight+"px";
   } else {
    mapElm.style.height = mapElmPar.style.height;
   }
  }
 }
 map = new GMap2(mapElm);
 map.enableDoubleClickZoom();
 if(parseInt(USE_MAP_ZOOM)==1) { map.addControl(new GLargeMapControl()); }
 if(parseInt(USE_MAP_ZOOM)==2) { map.addControl(new GSmallMapControl()); }
 if(USE_MAPTYPE_SELECTOR) { map.addControl(new GMapTypeControl()); }
 map.setCenter(new GLatLng(28.033198, -82.716064), 7);
 if(USE_MINI_MAP) { map.addControl(new GOverviewMapControl(new GSize(((objHeight(mapElm)>0)?objHeight(mapElm)/4:150), ((objHeight(mapElm)>0)?objHeight(mapElm)/4:150)))); }
 setTimeout("loadMapPoints()", 5000);
 GEvent.addListener(map, 'load', loadMapPoints);

 GEvent.addListener(map, 'moveend', loadMapPoints);
 GEvent.addListener(map, 'zoomend', loadMapPoints);
}



////////////////
function reCenterMap() {
 if(map==null) { setTimeout("reCenterMap()", MAP_NOT_LOADED_WAIT / 2); return; }
 var sw, ne, centLat, centLng
 if((USE_BOUND_BL!=null)&&(USE_BOUND_TR!=null)) {
  if((USE_BOUND_BL.indexOf(',')>=0)&&(USE_BOUND_TR.indexOf(',')>=0)) {
   ne = new GLatLng(parseFloat(USE_BOUND_TR.substring(0,USE_BOUND_TR.indexOf(','))), parseFloat(USE_BOUND_TR.substring(USE_BOUND_TR.indexOf(',')+1,USE_BOUND_TR.length)));
   sw = new GLatLng(parseFloat(USE_BOUND_BL.substring(0,USE_BOUND_BL.indexOf(','))), parseFloat(USE_BOUND_BL.substring(USE_BOUND_BL.indexOf(',')+1,USE_BOUND_BL.length)));
  }
 }
 if(sw==null) { return; }
 var zoomLevel = map.getBoundsZoomLevel(new GLatLngBounds(sw, ne));

//var L1=parseFloat(USE_BOUND_TR.substring(0,USE_BOUND_TR.indexOf(',')));
//var L2=parseFloat(USE_BOUND_BL.substring(0,USE_BOUND_BL.indexOf(',')));
//var G1=parseFloat(USE_BOUND_TR.substring(USE_BOUND_TR.indexOf(',')+1,USE_BOUND_TR.length));
//var G2=parseFloat(USE_BOUND_BL.substring(USE_BOUND_BL.indexOf(',')+1,USE_BOUND_BL.length));
//setPoint(L1+","+G1,L1,G1,"CONTRACT-N");
//setPoint(L1+","+G2,L1,G2,"CONTRACT-N");
//setPoint(L2+","+G1,L2,G1,"CONTRACT-N");
//setPoint(L2+","+G2,L2,G2,"CONTRACT-N");

 if((DROPZOOM!=null)&&(DROPZOOM!=0)) { zoomLevel -= DROPZOOM; }
 map.setCenter(new GLatLng((ne.lat()+sw.lat())/2, (ne.lng()+sw.lng())/2), zoomLevel);
 if(document.getElementById("debug")!=null) { document.getElementById("debug").value=map.getBounds(); }
}

///////////////
function makeIcon(el, img) {
 el.image = gethost() + img;
 el.shadow = gethost() + "img/gmap/icon-shadow.png";
 el.iconSize = new GSize(12, 20);
 el.shadowSize = new GSize(22, 20);
 el.iconAnchor = new GPoint(6, 20);
 el.infoWindowAnchor = new GPoint(5, 1);
}
//////////
function showPointInfo(pID) {
 window.open("viewproperty.asp?prop="+pID);
// for(var i=0;i<loadedPoints.length;i++) {
//  if((loadedPoints[i]!=null)&&(pID==loadedPoints[i].pID)) { /* do something */; }
// }
}
//////////
function setPoint(pID,lat,lng,stat,reserved) {
 for(var i=0;i<loadedPoints.length;i++) {
  if((loadedPoints[i]!=null)&&(pID==loadedPoints[i].pID)) { return; }
 }

 var whichIcon = null;
 if(SHOW_PASTPRESENT_SPEC) {
  if((stat=="ACTIVE-O")) return;
 } else {
  if((!SHOW_PAST_LISTINGS)&&((stat.indexOf("-N")==-1))) return;
  if((!SHOW_PRESENT_LISTINGS)&&(stat.indexOf("-N")!=-1)) return;
 }

 if((!SHOW_ACTIVE)&&((stat=="ACTIVE-O")||(stat=="ACTIVE-N"))) return;
 if((!SHOW_CONTRACT)&&((stat=="CONTRACT-O")||(stat=="CONTRACT-N"))) return;
 if((!SHOW_SOLD)&&((stat=="SOLD-O")||stat=="SOLD-N")) return;

 if(map==null) { setTimeout("setPoint("+sto_str(pID)+","+lat+","+lng+","+sto_str(stat)+")", MAP_NOT_LOADED_WAIT); return; }

var needToWait = (ticks() - spLastPointAdded < 100);

if(reserved==null) {
 spProgTotal++;
 if(needToWait) { spProgWaiting++; }
} else {
 if(!needToWait) { spProgWaiting--; }
}

if(spProgTotal>0) {
 if(spProgTmr!=null) { clearTimeout(spProgTmr); }
 var tmpObj = document.getElementById(mapElementID+"_progress");
 if(spProgWaiting > 0) {
  if(tmpObj==null) {
   tmpObj = document.createElement("div");
   tmpObj.id = mapElementID+"_progress";
   tmpObj.style.height = "15px";
   tmpObj.style.width = (document.getElementById(mapElementID).offsetWidth / 4) +"px";
   tmpObj.style.border = "2px solid #203";
   tmpObj.style.backgroundColor = "#fff";
   tmpObj.style.position = "absolute";
   tmpObj.style.top = "5px";
   tmpObj.style.left = "5px";
   tmpObj.style.zIndex = 10000;
   tmpObj.style.overflow = "hidden";
   document.getElementById(mapElementID).appendChild(tmpObj);
   tmpObj = document.createElement("div");
   tmpObj.id = mapElementID+"_progress_inner";
   tmpObj.style.height = "20px";
   tmpObj.style.backgroundColor = "#50f";
   document.getElementById(mapElementID+"_progress").appendChild(tmpObj);
   tmpObj = document.getElementById(mapElementID+"_progress");
  }
  tmpObj.style.display = "block";
  spProgTmr = setTimeout("document.getElementById('"+tmpObj.id+"').style.display='none';spProgWaiting = 0;spProgTotal = 0;", 5000);
  document.getElementById(mapElementID+"_progress_inner").style.width = ((spProgTotal - spProgWaiting) / spProgTotal) * (document.getElementById(mapElementID+"_progress").offsetWidth) + "px";
 } else if(tmpObj!=null) {
  tmpObj.style.display = "none";
  spProgWaiting = 0;
  spProgTotal = 0;
 }
}


if(needToWait) {
 setTimeout("setPoint("+sto_str(pID)+","+lat+","+lng+","+sto_str(stat)+",1);", 30);
 return;
}
spLastPointAdded = ticks();


 mapIsBusy++;
// tryReBounds(lat, lng);
 if(USE_STATUS_ICONS) {
  if(stat=="SOLD-O") { whichIcon = iconSoldOld;
  } else if(stat=="SOLD-N") { whichIcon = iconSoldCurr;
  } else if(stat=="CONTRACT-O") { whichIcon = iconContractOld;
  } else if(stat=="CONTRACT-N") { whichIcon = iconContractCurr;
  } else if(stat=="ACTIVE-O") { whichIcon = iconOld;
  } else { whichIcon = iconCurr;
  }
 } else {
  whichIcon = ((stat.indexOf("-N")!=-1)?iconCurr:iconOld);
 }
 loadedPoints[loadedPoints.length] = new DataPoint(pID,lat,lng,stat);
  var opts = new Object();
  opts.icon = whichIcon;
  opts.title = "MLS# "+pID; 
 loadedPoints[loadedPoints.length-1].gObj = new GMarker(new GLatLng(lat, lng), opts);
 //if(USE_ACTIONS) { GEvent.addListener(loadedPoints[loadedPoints.length-1].gObj, "click", function() { marker.openInfoWindowHtml("<b>"+label+"</b><br />"+desc); }); }
 map.addOverlay(loadedPoints[loadedPoints.length-1].gObj);
 spPointsLoaded++;
 GEvent.addListener(loadedPoints[loadedPoints.length-1].gObj, 'dblclick', function() { showPointInfo(pID); } );

 mapIsBusy--;
}
function ticks() {
 var dt = new Date();
 return ((dt.getHours() * 60 + dt.getMinutes()) * 60 + dt.getSeconds()) * 1000 + dt.getMilliseconds()
}
////////////////
function resetMap() {
 resetPoints();
}
function resetPoints() {
 if(map==null) { return; }
 while(loadedPoints.length>0) {
  var pObj = loadedPoints.pop();
  pObj = null;
 }
 map.clearOverlays();
 spPointsLoaded = 0;
 spProgWaiting = 0
 spProgTotal = 0;
 circBound = null;
 circBound = new Object();
 circBound.maxlat=-100;circBound.minlat=100;circBound.maxlng=-180;circBound.minlng=180;
}
//////////


function DataPoint(pID,lat,lng,stat) {
 this.pID = pID;
 this.latitude = lat;
 this.longitude = lng;
 this.status = stat;

 this.gObj;
}




//////////////////////
function loadMapPoints() {
 if(SUPRESS_POINTS) { return; }
 if(map!=null) {
  if(USE_BOUND_TR==null) {
   loadUrl("gmap-dyn.asp?seek_bounds=1"+(((EXTRA_DATA!=null)&&(EXTRA_DATA.length>0))?"&extra="+EXTRA_DATA:""));
  } else {
   var mpBnd = map.getBounds();
   loadUrl("gmap-dyn.asp?load_bound="+mpBnd.getNorthEast().lat()+","+mpBnd.getNorthEast().lng()+","+mpBnd.getSouthWest().lat()+","+mpBnd.getSouthWest().lng()+(((EXTRA_DATA!=null)&&(EXTRA_DATA.length>0))?"&extra="+EXTRA_DATA:""));
  }
 } else if((USE_BOUND_TR!=null)&&(USE_BOUND_BL!=null)) {
  loadUrl("gmap-dyn.asp?load_bound="+USE_BOUND_TR+","+USE_BOUND_BL+(((EXTRA_DATA!=null)&&(EXTRA_DATA.length>0))?"&extra="+EXTRA_DATA:""));
 }
}

function loadUrl(url, reserved) {
 var xFrame = document.getElementById("gmap_update");
 if(xFrame==null) { if((reserved==null)||(reserved<10)) { setTimeout("loadUrl("+sto_str(url)+","+((reserved==null)?1:reserved+1)+")", 300); }; return; }
 if(url==null) { return; }
 if((xFrame.contentWindow)&&(xFrame.contentWindow.location)&&(xFrame.contentWindow.location.replace)) {
  xFrame.contentWindow.location.replace(url);
 } else {
  xFrame.src=url;
 }
}

////////////////////

function gethost() {
 var ret = window.location.toString();
 if(ret.lastIndexOf('/')!=-1) ret = ret.substring(0, ret.lastIndexOf('/') + 1);
 return ret;
}
/////////
function mapSetOptions(opt, vl) {
 if(!loadedDefaults) { loadMapDefaults(); }
 if(opt=="ACTIONS") {
  USE_ACTIONS = vl;
 } else if(opt=="SHOW_PAST") {
  SHOW_PAST_LISTINGS = vl;
 } else if(opt=="SHOW_PRESENT") {
  SHOW_PRESENT_LISTINGS = vl;
 } else if(opt=="SHOW_PASTPRESENT_SPECIAL") {
  SHOW_PASTPRESENT_SPEC = vl;
 } else if(opt=="MINIMAP") {
  USE_MINI_MAP = vl;
 } else if(opt=="MAPTYPE") {
  USE_MAPTYPE_SELECTOR = vl;
 } else if(opt=="ZOOM") {
  USE_MAP_ZOOM = vl;
 } else if(opt=="ZOOM_SMALL") {
  USE_MAP_ZOOM = ((vl)?2:0);
 } else if(opt=="STATUS_ICONS") {
  USE_STATUS_ICONS = vl;
 } else if(opt=="RESET") {
  resetMap();
 } else if(opt=="RESET_POINTS") {
  resetPoints();
 } else if(opt=="RELOAD_POINTS") {
  loadMapPoints();
 } else if(opt=="BOUND_BL") {
  USE_BOUND_BL = vl;
 } else if(opt=="BOUND_TR") {
  USE_BOUND_TR = vl;
 } else if(opt=="BOUNDS") {
  if(vl==null) { return; }
  var tmp = vl.split(",");
  if(tmp.length==4) {
   USE_BOUND_TR = ((parseFloat(tmp[0])>parseFloat(tmp[2]))?tmp[0]:tmp[2]) + "," + ((parseFloat(tmp[1])<parseFloat(tmp[3]))?tmp[3]:tmp[1]);
   USE_BOUND_BL = ((parseFloat(tmp[0])>parseFloat(tmp[2]))?tmp[2]:tmp[0]) + "," + ((parseFloat(tmp[1])<parseFloat(tmp[3]))?tmp[1]:tmp[3]);

//resetPoints();
//xSetMarker(((parseFloat(tmp[0])>parseFloat(tmp[2]))?tmp[0]:tmp[2]) + "," + ((parseFloat(tmp[1])<parseFloat(tmp[3]))?tmp[3]:tmp[1]));
//xSetMarker(((parseFloat(tmp[0])>parseFloat(tmp[2]))?tmp[0]:tmp[2]) + "," + ((parseFloat(tmp[1])>parseFloat(tmp[3]))?tmp[3]:tmp[1]));
//xSetMarker(((parseFloat(tmp[0])>parseFloat(tmp[2]))?tmp[2]:tmp[0]) + "," + ((parseFloat(tmp[1])<parseFloat(tmp[3]))?tmp[1]:tmp[3]));
//xSetMarker(((parseFloat(tmp[0])>parseFloat(tmp[2]))?tmp[2]:tmp[0]) + "," + ((parseFloat(tmp[1])>parseFloat(tmp[3]))?tmp[1]:tmp[3]));

  }
  reCenterMap();
 } else if(opt=="SHOW_ACTIVE") {
  SHOW_ACTIVE = vl;
 } else if(opt=="SHOW_CONTRACT") {
  SHOW_CONTRACT = vl;
 } else if(opt=="SHOW_SOLD") {
  SHOW_SOLD = vl;
 } else if(opt=="CIRCLE") {
  drawCircleStr(vl);
 } else if(opt=="SKIP_POINTLOAD") {
  SUPRESS_POINTS = vl;
 } else if(opt=="POINT") {
  resetPoints();
  SUPRESS_POINTS = 1;
  xSetMarker(vl);
  mapSetOptions("BOUNDS", vl+","+vl);
 } else if(opt=="DROPZOOM") {
  DROPZOOM = vl;
  reCenterMap();
 } else if(opt=="MAP_DEFAULTS") {
  loadMapDefaults();
 } else if(opt=="EXT_SS_ONLY") {
  setExtras("SS_ONLY", vl);
 } else if(opt=="EXT_ALL_NETWORKS") {
  setExtras("ALL_NETWORKS", vl);
 } else if(opt=="SHOW_STATE") {
  loadUrl("gmap-dyn.asp?seek_state="+vl);
 } else if(opt=="SHOW_CITY") {
  loadUrl("gmap-dyn.asp?seek_city="+vl);
 } else if(opt=="SHOW_COUNTY") {
  loadUrl("gmap-dyn.asp?seek_county="+vl);
 } else if(opt=="SUPRESS_POINTS") {
  SUPRESS_POINTS = vl;
 }
}
////////////////
function sto_str(str) {
 if(str==null) return "";
 return "\""+str.replace(/"/g,'\\\"')+"\"";
}
////////////////
function loadMapDefaults() {
 DROPZOOM = 0;
 SUPRESS_POINTS = 0;
 USE_ACTIONS = 1;
 SHOW_PAST_LISTINGS = 0;
 SHOW_PRESENT_LISTINGS = 1;
 SHOW_PASTPRESENT_SPEC = 0;
 USE_MINI_MAP = 1;
 USE_MAPTYPE_SELECTOR = 1;
 USE_MAP_ZOOM = 1;
 USE_STATUS_ICONS = 0;
 USE_BOUND_BL = null;
 USE_BOUND_TR = null;
 USE_BOUNDS = null;
 SHOW_ACTIVE = 1;
 SHOW_CONTRACT = 1;
 SHOW_SOLD = 1;
 EXTRA_DATA = "";
 loadedDefaults = 1;
}
///////////////////////
function objHeight(sObj) {
 if(sObj==null) { return -1; }
 if(sObj.offsetHeight) { return sObj.offsetHeight; }
 if(sObj.height) { return parseInt(sObj.height); }
 if((sObj.style)&&(sObj.style.height)) { return parseInt(sObj.style.height); }
 return 0;
}
///////////////////
function xSetMarker(str) {
 if(map==null) { setTimeout("xSetMarker(\""+str+"\")", MAP_NOT_LOADED_WAIT); return; }
 var dat = str.split(",");
 if(dat.length==2) {
  var marker = new GMarker(new GLatLng(parseFloat(dat[0]), parseFloat(dat[1])), iconCurr);
  map.addOverlay(marker);
  spPointsLoaded++;
 }
}
/////////////////////
function drawCircleStr(str) {
 var dat=str.split(',');
 if(dat.length==3) {
  drawCircle(parseFloat(dat[0]), parseFloat(dat[1]), parseFloat(dat[2]));
 }
}
////////////////
function drawCircle(lat,lng,radius) {
 if(map==null) { setTimeout("drawCircle("+lat+","+lng+","+radius+")", MAP_NOT_LOADED_WAIT); return; }
 radius = radius/69.172;
 var points = [],bndChng=0;
 if(circBound.maxlat<lat+radius) { circBound.maxlat=lat+radius;bndChng=1; }
 if(circBound.minlat>lat-radius) { circBound.minlat=lat-radius;bndChng=1; }
 if(circBound.maxlng<lng+radius) { circBound.maxlng=lng+radius;bndChng=1; }
 if(circBound.minlng>lng-radius) { circBound.minlng=lng-radius;bndChng=1; }
 if(bndChng) { mapSetOptions("BOUNDS", circBound.maxlat+","+circBound.maxlng+","+circBound.minlat+","+circBound.minlng); }
 for(var i=0;i<37;i++) {
  var x1=lat+radius*Math.cos(2*Math.PI*i/36);
  var y1=lng+radius*Math.sin(2*Math.PI*i/36);
  points.push(new GLatLng(x1, y1));
 }
 map.addOverlay(new GPolyline(points));
}
//////////////////
function setExtras(newVal, setOrRemove) {
 if(EXTRA_DATA==null) { EXTRA_DATA = ""; }
 if(setOrRemove) {
  if((","+EXTRA_DATA+",").indexOf(","+newVal+",")==-1) {
   if((EXTRA_DATA==null)||(EXTRA_DATA.length==0)) {
    EXTRA_DATA = newVal;
   } else {
    EXTRA_DATA += "," + newVal;
   }
  }
 } else {
  if(EXTRA_DATA==newVal) {
   EXTRA_DATA = "";
  } else if(EXTRA_DATA.substring(0,newVal.length+1)==newVal+",") {
   EXTRA_DATA = EXTRA_DATA.substring(newVal.length+1, EXTRA_DATA.length);
  } else if(EXTRA_DATA.substring(EXTRA_DATA.length-newVal.length-1,EXTRA_DATA.length)==","+newVal) {
   EXTRA_DATA = EXTRA_DATA.substring(0, EXTRA_DATA.length-newVal.length-1);
  } else if(EXTRA_DATA.indexOf(","+newVal+",")!=-1) {
   EXTRA_DATA = EXTRA_DATA.substring(0, EXTRA_DATA.indexOf(","+newVal+",")) + EXTRA_DATA.substring(newVal.length + EXTRA_DATA.indexOf(","+newVal+",") + 1, EXTRA_DATA.length);
  }
 }
}
////////////////////////