// Copyright 2008 Zilpy LLC. All Rights Reserved.

var zMap;
var zEw;
var zTicons=[];
var zCicons=[];
var zOicons=[];

function addEvent(elm,evType,fn,useCapture) { 
  if(elm.addEventListener){ 
    elm.addEventListener(evType,fn,useCapture); 
    return true;
  }else if(elm.attachEvent){ 
    return elm.attachEvent("on"+evType,fn); 
  }else{ 
   elm["on"+evType]=fn; 
  } 
} 
addEvent(window,"load",load,false); 
addEvent(window,"unload",GUnload,false);

function pretty(t,b){
  var text='<table border="0" cellpadding="0" cellspacing="0"><tr><td class="etitle">';
  var tmp=t;
  if(tmp.length>30){
    tmp=tmp.substring(0,29);
    tmp+="...";
  }
  text+=tmp;
  text+='<a href="javascript:zEw.hide()"><img width="12" height="11" src="/img/eclose.png" border=0 style="position:absolute;right:4px;top:4px"/></a>'; 
  text+='</td></tr><tr><td>';
  text+=b;
  text+='</td></tr></table>';
  return text;
}

function loadIcons(color){
  var icons=[];
  for(var i=0;i<6;i++){
    icon=new GIcon();
    icon.image="/img/icon"+color+(i==0?"":i)+".png";
    icon.shadow="http://www.google.com/mapfiles/shadow50.png";
    icon.iconSize=new GSize(20,34);
    icon.shadowSize=new GSize(37,34);
    icon.iconAnchor=new GPoint(9,34);
    icon.infoWindowAnchor=new GPoint(9,2);
    icon.infoShadowAnchor=new GPoint(18,25);
    icon.transparent="http://www.google.com/intl/en_ALL/mapfiles/markerTransparent.png";
    // icon.printImage = "coldmarkerie.gif";
    // icon.mozPrintImage = "coldmarkerff.gif";
    icons.push(icon);
  }
  return icons;
}

function loadAllIcons(){
  zTicons=loadIcons('r');
  zCicons=loadIcons('g');
  zOicons=loadIcons('b');
}

function info(prop){
  var li='<li style="font-family:tahoma,sans-serif; font-size:11px;color:#818181;text-align:left;margin-bottom:3px;">';
  var str='<div style="width:180px;"><div style="float:left;margin-top:3px;"><ul>'
  str+=li;
  str+='<b style="color: #00598c;">';
  str+=prop.rent;
  str+='</b>';
  if (prop.date) {
    str+='&nbsp;<span style="font-size:10px;">(';
    str+=prop.date;
    str+=')</span>';
  }
  li='<li style="font-family:tahoma,sans-serif; font-size:11px; color:#00598c; text-align:left; margin-bottom: 3px;">';
  str+='</li>';
  str+=li;
  if(prop.br==0) str+='Studio';
  else{
    str+=prop.br;
    str+=' Bdr';
  }
  if(prop.ba&&prop.ba>0){
    str+=', ';
    str+=prop.ba;
    str+=' Ba';
  }
  if(prop.sqft&&prop.sqft>0){
    str+=', ';
    str+=prop.sqft;
    str+=' Sqft';
  }
  str+='</li>';
  if(prop.type){
    str+=li;
    if(prop.type=='s') str+='Single Family Home';
    else if (prop.type=='t') str+='Town House/Duplex';
    else str+='Apartment/Condo';
    str+='</li>';
  }
  str+='</ul></div>';
  if(prop.img){
    str+='<div style="float:right;border:1px solid #c2c7cc;"><a href="javascript:{}" title="Click to view listing" onclick=\"window.open(\'';
    str+=prop.url;
    str+='\', \'_blank\');"><img width="70" height="50" src="';
    str+=prop.img;
    str+='"/></a></div>';
  }
  str+='</div>';
 if(prop.amnt&&prop.amnt>0){
    li='<li style="font-family:tahoma,sans-serif; font-size:10px; color:#818181; text-align:left; margin-bottom: 2px;">';
    str+='<div style="float:left;clear:both;"><ul>';
    str+=li;
    var amenities=[];
    if (prop.amnt&1) amenities.push('Washer');
    if (prop.amnt&2) amenities.push('Dryer');
    if (prop.amnt&4) amenities.push('Stove');
    if (prop.amnt&8) amenities.push('Fridge');
    if (prop.amnt&8192) amenities.push('Dishwasher');
    if (prop.amnt&64) amenities.push('Central Air');
    if (prop.amnt&128) amenities.push('Air Conditioner');
    if (prop.amnt&256) amenities.push('Gas Included');
    if (prop.amnt&512) amenities.push('Electricity Included');
    if (prop.amnt&1024) amenities.push('Water Included');
    if (prop.amnt&2048) amenities.push('New Carpet');
    if (prop.amnt&4096) amenities.push('Fresh Paint');
    if (prop.amnt&32) amenities.push('Basement');
    if (prop.amnt&16) amenities.push('Garage');
    if (prop.amnt&32768) amenities.push('Backyard');
    if (prop.amnt&16384) amenities.push('Swimming Pool');
    if (prop.amnt&131072) amenities.push('Fitness Center');
    if (prop.amnt&65536) amenities.push('Playground');
    
    for(var i=0;i<amenities.length;i++){
      if(i>0) str+=', ';
      if((i+1)%4==0){
        str+='</li>';
        str+=li;
      }
      str+=amenities[i];
    }
    str+='</li></ul></div>';
  }

  if(prop.name){
    str+='<div style="float:left;clear:both;font-family:tahoma,sans-serif;font-size:11px;color:#818181;text-align:left;margin-bottom:2px;">';
    str+='Source:&nbsp;';
    if(prop.url){
      str+='<a style="font-family:tahoma,sans-serif;font-size:11px;color:#00598c;text-align:left;" href="javascript:{}" onclick=\"window.open(\'';
      str+=prop.url;
      str+='\', \'_blank\');">';
      str+=prop.name;
      str+='</a>';
    }else{
      str+=prop.name;
    }
    str+='</div>';
  }
  return str;  
}

function impOrder(marker,b){
  return 1;
}

function getIcon(data){
  var icon;
  if(data.prop=="t") icon=zTicons[data.br];
  else if(data.prop=="c") icon=zCicons[data.br];
  else icon=zOicons[data.br];
  return icon;
}
/*
function getIcon(data) {
    icon = new GIcon();
    name = data.type == 's' ? 'sfh' : (data.type == 't' ? 'th' : 'apt');
    color = data.prop == 't' ? 'r' : (data.prop == 'c' ? 'g' : 'b');
    icon.image = '/img/' + name + '_' + color + '.gif';
    icon.shadow = 'http://www.google.com/mapfiles/shadow50.png';
    icon.iconSize = new GSize(22, 37);
    icon.shadowSize = new GSize(37, 34);
    icon.iconAnchor = new GPoint(9, 34);
    icon.infoWindowAnchor = new GPoint(9, 2);
    icon.infoShadowAnchor = new GPoint(18, 25);
    icon.transparent = 'http://www.google.com/intl/en_ALL/mapfiles/markerTransparent.png';
    // icon.printImage = 'coldmarkerie.gif';
    // icon.mozPrintImage = 'coldmarkerff.gif';
    return icon;
}
*/

var isEditing=false;
function load(){
	if(GBrowserIsCompatible()){
    loadAllIcons();
	  function createMarker(point,prop){
      var html=pretty(prop.addr,info(prop)); 
      var marker=new GMarker(point,{icon:getIcon(prop),zIndexProcess:impOrder});
      GEvent.addListener(marker,"mouseover",function(){
        if(zEw.isHidden() || !isEditing){
          zEw.openOnMarker(marker,html);
        }
      });
      GEvent.addListener(marker,"mousedown",function(){
        isEditing = true;
        zEw.openOnMarker(marker,html);
      });
      return marker;
    }
		zMap=new GMap2(document.getElementById("map"));
		zMap.disableDoubleClickZoom();
		zMap.disableScrollWheelZoom();
    zMap.addControl(new GSmallZoomControl(),new GControlPosition(G_ANCHOR_TOP_LEFT,new GSize(2,2)));
		var mapControl=new GHierarchicalMapTypeControl();
    mapControl.addRelationship(G_SATELLITE_MAP,G_HYBRID_MAP,null,true);
    zMap.addControl(mapControl,new GControlPosition(G_ANCHOR_TOP_LEFT,new GSize(19,0)));
    zMap.addMapType(G_PHYSICAL_MAP);
    zMap.setCenter(new GLatLng(0,0),0);
    zMap.clearOverlays();
    zEw=new EWindow(zMap,E_STYLE);
    GEvent.addListener(zEw,"hidden",function(){
      isEditing=false;
    });
    GEvent.addListener(zMap,"click",function(o,p){
      if(p){
        isEditing=false;
      }
    });
    zMap.addOverlay(zEw);
    if(zData){
      zBounds=new GLatLngBounds();
      for (var i=0;i<zData.length;i++){
        var prop=zData[i];
	      var point=new GLatLng(prop.lat,prop.lng);
        var marker=createMarker(point,prop);
        zMap.addOverlay(marker);
        zBounds.extend(point);
      }
    }
    zMap.setZoom(zMap.getBoundsZoomLevel(zBounds));
    zMap.setCenter(zBounds.getCenter());
    document.getElementById("loading").innerHTML = "";
  }
	else {
	  alert("Sorry, the map is not compatible with your browser");
	}
}

// Version 0.0  Initial version 
// Version 0.1  10/10/2006 Added E_STYLE_7 
// Version 0.2  17/05/2007 Added .isHidden() and .supportsHide()
// Version 0.3  14/09/2007 added .zindex()
      function EStyle(stemImage, stemSize, boxClass, boxOffset) {
        this.stemImage = stemImage;
        this.stemSize = stemSize;
        this.boxClass = boxClass;
        this.boxOffset = boxOffset;
        //this.border = border;
        
        // Known fudge factors are:
        // Firefox (1.0.6 and 1.5)    5, -1
        // IE 6.0                     0, -1
        // Opera 8.54                 3, -1
        // Opera 9 prev               4, -1
        // Netscape (7.2, 8.0)        5, -1
        // Safari                     5, -1        
        
        var agent = navigator.userAgent.toLowerCase();
        
        var fudge = 3;  // assume Netscape if no match found
       
        if (agent.indexOf("opera") > -1) {
          fudge = 3;
        }   
        if (agent.indexOf("firefox") > -1) {
          fudge = 3;
        }   
        if (agent.indexOf("safari") > -1) {
          fudge = 3;
        }   
        if ((agent.indexOf("msie") > -1) && (agent.indexOf("opera") < 1)){
          fudge = 0;
        }
        this.fudge = fudge;
      }
      
      var E_STYLE = new EStyle("/img/stem.png", new GSize(24,24),  "estyle", new GPoint(-10,23));

      function EWindow(map,estyle) {
        // parameters
        this.map=map;
        this.estyle=estyle;
        // internal variables
        this.visible = false;
        // browser - specific variables
        this.ie = false;
        var agent = navigator.userAgent.toLowerCase();
        if ((agent.indexOf("msie") > -1) && (agent.indexOf("opera") < 1)){ this.ie = true} else {this.ie = false}
      } 
      
      EWindow.prototype = new GOverlay();

      EWindow.prototype.initialize = function(map) {
        var div1 = document.createElement("div");
        div1.style.position = "absolute";
        map.getPane(G_MAP_FLOAT_SHADOW_PANE).appendChild(div1);
        var div2 = document.createElement("div");
        div2.style.position = "absolute";
        div2.style.width = this.estyle.stemSize.width+"px";
        map.getPane(G_MAP_FLOAT_SHADOW_PANE).appendChild(div2);
        this.div1 = div1;
        this.div2 = div2;
      }

      EWindow.prototype.openOnMap = function(point, html, offset) {
        this.offset = offset||new GPoint(0,0);
        this.point = point;
        this.div1.innerHTML = '<div class="' + this.estyle.boxClass + '"><nobr>' + html + '</nobr></div>';
        if (this.ie && this.estyle.stemImage.toLowerCase().indexOf(".png")>-1) {
          var loader = "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+this.estyle.stemImage+"', sizingMethod='scale');";
          this.div2.innerHTML = '<div style="height:' +this.estyle.stemSize.height+ 'px; width:'+this.estyle.stemSize.width+'px; ' +loader+ '" ></div>';
        } else {
          this.div2.innerHTML = '<img src="' + this.estyle.stemImage + '" width="' + this.estyle.stemSize.width +'" height="' + this.estyle.stemSize.height +'">';
        }
        var z = GOverlay.getZIndex(this.point.lat());
        this.div1.style.zIndex = z;
        this.div2.style.zIndex = z+1;        
        this.visible = true;
        this.show();
        this.redraw(true);
      }
      
      EWindow.prototype.openOnMarker = function(marker,html) {
        var vx = marker.getIcon().iconAnchor.x - marker.getIcon().infoWindowAnchor.x;
        var vy = marker.getIcon().iconAnchor.y - marker.getIcon().infoWindowAnchor.y;
        this.openOnMap(marker.getPoint(), html, new GPoint(vx,vy));
      }

      EWindow.prototype.redraw = function(force) {
        if (!this.visible) {return;}
        var p = this.map.fromLatLngToDivPixel(this.point);
        this.div2.style.left   = (p.x + this.offset.x) + "px";
        this.div2.style.bottom = (-p.y + this.offset.y -this.estyle.fudge) + "px";
        this.div1.style.left   = (p.x + this.offset.x + this.estyle.boxOffset.x) + "px";
        this.div1.style.bottom = (-p.y + this.offset.y + this.estyle.boxOffset.y) + "px";
        this.div1.style.cursor = "default";
        this.div2.style.cursor = "default";
   var eover = GEvent.callback(this,this.onOver);
   var eout = GEvent.callback(this,this.onOut);
   GEvent.clearInstanceListeners(this.div1);//safety 
   GEvent.addDomListener(this.div1,"mouseover",function(){eover();});
   GEvent.addDomListener(this.div1,"mouseout",function(){eout();});
      }

      EWindow.prototype.remove = function() {
        this.div1.parentNode.removeChild(this.div1);
        this.div2.parentNode.removeChild(this.div2);
        this.visible = false;
      }

      EWindow.prototype.copy = function() {
        return new EWindow(this.map, this.estyle);
      }

      EWindow.prototype.show = function() {
        this.div1.style.display="";
        this.div2.style.display="";
        this.visible = true;
      }
      
      EWindow.prototype.hide = function() {
        this.div1.style.display="none";
        this.div2.style.display="none";
        this.visible = false;
        GEvent.trigger(this,"hidden");
      }
      
      EWindow.prototype.isHidden = function() {
        return !this.visible;
      }
      
      EWindow.prototype.supportsHide = function() {
        return true;
      }

      EWindow.prototype.zindex = function(zin) {
        var z = GOverlay.getZIndex(this.point.lat());
        this.div1.style.zIndex = z+zin;
        this.div2.style.zIndex = z+1+zin;
      }
      
EWindow.prototype.onOver = function(){
    GEvent.trigger(this,"mouseover");
}
EWindow.prototype.onOut = function(){
    GEvent.trigger(this,"mouseout");
}