var hideDelay=500,magMinSize=24,quadMaxDepth=3,scrollBarWidth=16,smartMaps=[],currentSmartMap=null,SM_MODE_NULL=0,SM_MODE_WINDOW_ZOOM=1,SM_MODE_IDENTIFY=2,modeCursor=["default","crosshair","help"];
function HoverPoint(cx,cy,x,y,w,h,p,u,t,i,d,n){this.cx=cx;this.cy=cy;this.x=x;this.y=y;this.w=w;this.h=h;this.pointCss=p;this.url=u;this.thumb=t;this.info=null;this.htn=n&&n.length?n.toLowerCase():"default";if(!IsNull(i)){var p=new DOMParser();var d=p.parseFromString("<info>"+i+"</info>","text/xml");this.info=d.firstChild;}this.div=d;}
function QuadNode(w,h){this.w=w;this.h=h;this.points=[];this.q1=null;this.q2=null;this.q3=null;this.q4=null;}
QuadNode.prototype.addPoint=function(e,x,y,ww,hh,d){var u=parseInt(this.w/2);var v=parseInt(this.h/2);if(d==null)d=quadMaxDepth;if(d==0||(x<u&&x+ww>u)||(y<v&&y+hh>v)){this.points[this.points.length]=e;}else{var q;if(x<u){if(y<v){if(!this.q1)this.q1=new QuadNode(u,v);this.q1.addPoint(e,x,y,ww,hh,--d);}else{if(!this.q3)this.q3=new QuadNode(this.w-u,this.h-v);this.q3.addPoint(e,x,y-v,ww,hh,--d);}}else{if(y<v){if(!this.q2)this.q2=new QuadNode(u,v);this.q2.addPoint(e,x-u,y,ww,hh,--d);}else{if(!this.q4)this.q4=new QuadNode(this.w-u,this.h-v);this.q4.addPoint(e,x-u,y-v,ww,hh,--d);}}}}
function InitializeSmartMaps(){for(var i=0;i<smartMaps.length;i++)smartMaps[i].init();}
function HideAllSmartMapHovers(){for(var i=0;i<smartMaps.length;i++)smartMaps[i].resetHoverLayer();}
AddEvent(window,"load",InitializeSmartMaps);
function SmartMap(v,id,url,w,h,m){this.vn=v;smartMaps[smartMaps.length]=this;this.mode=m?m:SM_MODE_NULL;this.pMode=this.mode;this.ph=GetElem(id);if(!this.ph){alert("Unable to find the SmartMap place holder.");return;}this.ph.style.position="relative";ResizeElem(this.ph,new Rect(0,0,w,h));this.createZoomBox();this.createMapContainer(w,h);this.createMapImage(url,w,h);this.createHoverLayer();this.setCursor(modeCursor[this.mode]);AddEvent(document,"mousedown",this.onMouseDown);AddEvent(document,"mouseup",this.onMouseUp,false);AddEvent(document,"mousemove",this.onMouseMove,false);this.hTimer=null;this.hpCSSs=[];this.hQT=new QuadNode(w,h);this.ht=[];this.hpPrev=0;this.hSepCss="";this.hpOver=[];this.hoverPoints=[];this.mouse=new Rect(0,0,0,0);this.image=new Rect(0,0,w,h);this.zoom=new Rect(0,0,0,0);this.zc=null;this.ic=null;this.hlCss='';}
SmartMap.prototype.createHoverLayer=function(){var d=CElem("div");d.id="SMHover";d.ref=this;d.style.position="absolute";d.style.display="none";d.style.zIndex=10;this.ph.appendChild(d);this.hoverLayer=d;var e=CElem("div");e.cRef=this;e.id="SMHoverContainer";d.appendChild(e);this.hoverContainer=e;}
SmartMap.prototype.onMouseDown=function(e){var t=IsIE?window.event.srcElement:e.target;if((IsIE?event.button:e.which)!=1||!t||t.id=="SMHover"||!t.ref)return true;var m=t.ref.mouse,i=t.ref.image;m.left=m.right=IsIE?event.clientX+document.body.scrollLeft:e.pageX;m.top=m.bottom=IsIE?event.clientY+document.body.scrollTop:e.pageY;t.ref.updateImageCoords();if(currentSmartMap!=null)currentSmartMap.stopDrag(false);currentSmartMap=t.ref;if(t.ref.mode==SM_MODE_WINDOW_ZOOM&&m.left>=i.left&&m.left<=i.right&&m.top>=i.top&&m.top<=i.bottom)t.ref.startDrag();return false;}
SmartMap.prototype.onMouseUp=function(e){var r=currentSmartMap;if((IsIE?event.button:e.which)!=1||!r)return true;if(r.zoom.right==0&&r.zoom.bottom==0&&r.hpOver.length){r.onHPClick();}else{if(r.mode==SM_MODE_IDENTIFY&&r.ic)r.ic(r.mouse.left-r.image.left,r.mouse.top-r.image.top);else if(r.mode==SM_MODE_WINDOW_ZOOM&&currentSmartMap)r.stopDrag(true);}currentSmartMap=null;DeselectText();return false;}
SmartMap.prototype.onMouseMove=function(e){var x=IsIE?event.clientX+document.body.scrollLeft:e.pageX,y=IsIE?event.clientY+document.body.scrollTop:e.pageY,r=currentSmartMap;if(r){r.mouse.right=x;r.mouse.bottom=y;r.updateZoomCoords();SetDisplay(r.magIcon,Math.abs(r.zoom.right)>magMinSize&&Math.abs(r.zoom.bottom)>magMinSize);ResizeElem(r.zoomBox,r.zoom);r.setCursor(modeCursor[r.mode]);return false;}else{var t=IsIE?window.event.srcElement:e.target;r=t.ref;if(t){var i="SmartMap";if (r){r.checkHoverLayer(x,y);i=r.ph.id;}while(t&&t.id!=i)t=t.parentNode;if(!t)HideAllSmartMapHovers();}}return true;}
SmartMap.prototype.checkHoverLayer=function(x,y){if(x>0&&y>0){this.mouse.right=x;this.mouse.bottom=y;}else{x=this.mouse.right;y=this.mouse.bottom;}this.updateImageCoords();x-=this.image.left;y-=this.image.top;this.hpOver=this.findHoverLayers(this.hQT,x,y,x,y);var c=this.hpOver.length;if(c){this.setCursor("pointer");if(IsDisplayed(this.hoverLayer)){if(c>this.hpPrev)this.drawHoverLayer();else if(c<this.hpPrev&&!this.hTimer)this.hideHoverDelay();}else this.drawHoverLayer();}else{this.setCursor(modeCursor[this.mode]);if(IsDisplayed(this.hoverLayer)&&!this.hTimer)this.hideHoverDelay();}this.hpPrev=this.hpOver.length;}
SmartMap.prototype.hideHoverDelay=function(){this.hTimer=eval("setTimeout('"+this.vn+".hideHoverLayer()',hideDelay)");}
SmartMap.prototype.hideHoverLayer=function(){this.resetHoverLayer();this.checkHoverLayer(this.mouse.right,this.mouse.bottom);}
SmartMap.prototype.drawHoverLayer=function(){var hps=this.hpOver;this.resetHoverLayer();if(hps.length){var k=0,m=-1,lp=[],hl=this.hoverLayer,hc=this.hoverContainer,hlw=hl.offsetWidth,hlh=IsIE?6:hl.offsetHeight,t=CElem("table"),tb=CElem("tbody"),x=0,hp=null;t.className=this.hlCss;hc.appendChild(t);t.appendChild(tb);SetVis(hl,false);SetDisplay(hl,true);for(var i=0;i<hps.length;i++){var idx=hps[i].index.split(',');for(var j=0;j<idx.length;j++){x=idx[j];if(x.length&&!lp[x]){if(m<0)m=x;lp[x]=1;var tr=CElem("tr"),td=CElem("td");tb.appendChild(tr);if(k++)td.className=this.hSepCss;tr.appendChild(td);hp=this.hoverPoints;td.innerHTML=this.serializeNode(this.ht[(hp[x]&&this.ht[hp[x].htn]?hp[x].htn:"default")],x," width='100%' onmouseover='"+this.vn+".onHoverOver(event);' onmousedown='"+this.vn+".onHoverClick("+x+");' ");}}}if(m!=-1){hp=this.hoverPoints[m],mi=this.mapImage,sb=0,oh=hlh+t.offsetHeight+3,f=oh+3>mi.height,ow=hlw+t.offsetWidth,nl=hp.x+hp.w-1,nt=hp.y-parseInt(hp.h/2);if(nt<1||f){nt=3;if(f){sb=scrollBarWidth;hc.style.width=(ow+sb+(IsIE?1:3))+"px";hc.style.overflow="auto";hc.style.height=(mi.height-(IsIE?0:hlh+2)-6)+"px";hc.scrollTop=0;}}else if(nt+oh>mi.height)nt=mi.height-oh-2;if((hp.x+hp.w+ow+3)>mi.width&&ow+2<hp.x)nl=hp.x-ow-2-sb;MoveElem(hl,nl,nt);}SetVis(hl,true);}}
SmartMap.prototype.onHoverOver=function(e){var t=IsIE?window.event.srcElement:e.target;while(t&&!t.id)t=t.parentNode;if(t)t.style.cursor='pointer';this.resetTimer();}
function ClearContainer(e){if(e&&e.childNodes)while(e.childNodes.length)e.removeChild(e.childNodes[0]);}
SmartMap.prototype.resetHoverLayer=function(){var hl=this.hoverLayer,hc=this.hoverContainer;SetDisplay(hl,false);MoveElem(hl,0,0);hc.style.overflow="visible";hc.style.width=hc.style.height="";ClearContainer(hc);this.resetTimer();}
SmartMap.prototype.createMapImage=function(u,w,h){var i=CElem("img");i.id="SMImg";i.src=u;i.width=w;i.height=h;i.ref=this;this.mapImage=i;this.ph.appendChild(i);}
SmartMap.prototype.createMapContainer=function(w,h){var d=CElem("div");d.id="SMContainer";d.style.position="absolute";d.ref=this;ResizeElem(d,new Rect(0,0,w,h));this.ph.appendChild(d);}
SmartMap.prototype.resetTimer=function(){if(this.hTimer)clearTimeout(this.hTimer);this.hTimer=null;}
SmartMap.prototype.addHDHP=function(cx,cy,pc,lid,mid,mpid,ln,sn,hdid,st,t,info,n){var s="/HomeDetail.aspx?"+(lid-0>0?"ListingID="+lid:"MLSID="+mid+"&MLSProviderID="+mpid)+(ln-0>0?"&ListingNum="+ln:"");if(sn-0>0)s+="&SearchNum="+sn;else if(hdid-0>0)s+="&HomeDeliveryID="+hdid;if(IsNumeric(st))s+="&SearchType="+st;this.addHP(cx,cy,pc,s,t,info,n);}
SmartMap.prototype.addHP=function(cx,cy,pc,url,t,info,htn){var w=10,h=10,x=cx,y=cy,ss=this.getCssInfo(pc);if(ss){w=parseInt(ss.width);h=parseInt(ss.height);x-=parseInt(w/2);y-=parseInt(h/2);}var n=((url&&url.length)||(t&&t.length)||(info&&info.length))?this.hoverPoints.length:-1,d=this.findHoverLayer(this.hQT,cx,cy,cx,cy);if(!d){var i=this.image,u=0,v=0;d=CElem("div");d.className=pc;d.index="";d.ref=this;d.style.position="absolute";d.style.zIndex=5;d.style.left=x+"px";if(x<0){u=w+x;d.style.left="0px";d.style.width=u+"px";d.style.backgroundPosition=x+"px 0px";}d.style.top=y+"px";if(y<0){v=h+y;d.style.top="0px";d.style.height=v+"px";d.style.backgroundPosition=(x<0?x:0)+"px "+y+"px";}if(x+w>i.right)d.style.width=(w-u-(x+w-i.right))+"px";if(y+h>i.bottom)d.style.height=(h-v-(y+h-i.bottom))+"px";this.ph.appendChild(d);}if(n!=-1){d.index+=(d.index.length?",":"")+n;d.id="hp"+n;d.hp=this.hoverPoints[n];this.hoverPoints[n]=new HoverPoint(cx,cy,x,y,w,h,pc,url,t,info,d,htn);this.hQT.addPoint(d,x,y,w,h);}}
SmartMap.prototype.createZoomBox=function(){var d=CElem("div"),t=CElem("table"),tb=CElem("tbody"),tr=CElem("tr"),td=CElem("td"),dm=CElem("div");d.id="ZoomBox";d.style.position="absolute";d.style.display="none";d.style.zIndex=15;t.style.width=t.style.height="100%";td.align="center";td.className="ZoomBox";td.style.verticalAlign="middle";dm.className="MagnifyingGlass";dm.id="MagIcon";dm.style.postion="relative";dm.style.display="none";d.appendChild(t);t.appendChild(tb);tb.appendChild(tr);tr.appendChild(td);td.appendChild(dm);this.zoomBox=d;this.magIcon=dm;this.ph.appendChild(d);}
SmartMap.prototype.onHoverClick=function(i){var hp=this.hoverPoints[i];if(hp&&!IsNull(hp.url))self.location.href=hp.url;}
SmartMap.prototype.onHPClick=function(){for(var i=0;i<this.hpOver.length;i++){var is=this.hpOver[i].index.split(',');for(var j=0;j<is.length;j++){var hp=this.hoverPoints[is[j]];if(hp&&!IsNull(hp.url)){self.location.href=hp.url;return;}}}}
SmartMap.prototype.setCursor=function(s){this.mapImage.style.cursor=this.ph.style.cursor=s;}
SmartMap.prototype.startDrag=function(){this.resetHoverLayer();this.updateZoomCoords();ResizeElem(this.zoomBox,this.zoom);SetDisplay(this.zoomBox,true);}
SmartMap.prototype.stopDrag=function(b){SetDisplay(this.zoomBox,false);SetDisplay(this.magIcon,false);var z=this.zoom;if(b&&this.zc){this.zc(z.left,z.top,z.right==0&&z.bottom>0?1:z.right,z.right>0&&z.bottom==0?1:z.bottom);}}
SmartMap.prototype.init=function(){for(var i=0;i<this.hoverPoints.length;i++){var t=this.hoverPoints[i];if(t&&!IsNull(t.thumb)){var img=new Image();img.src=t.thumb;}}}
SmartMap.prototype.setHoverTemplate=function(c,c2,h,s){this.addHoverTemplate("default",c,c2,h,s);}
SmartMap.prototype.addHoverTemplate=function(n,c,c2,h,s){var p=new DOMParser(),d=p.parseFromString("<template>"+s+"</template>","text/xml");this.hoverLayer.className=c;this.hlCss=c2;this.hSepCss=h;this.ht[n.toLowerCase()]=d.firstChild.firstChild;}
SmartMap.prototype.serializeNode=function(n,i,s){if(n.nodeType==Node.TEXT_NODE)return n.nodeValue;var s="<"+n.nodeName+s,a=n.attributes;if(a)for(var j=0;j<a.length;j++)if(a[j].nodeValue&&a[j].nodeName.toLowerCase()!="contenteditable")s+=" "+a[j].nodeName+"=\""+a[j].nodeValue+"\"";if(n.childNodes.length){s+=">";for(var j=0;j<n.childNodes.length;j++){var c=n.childNodes[j];if(c.nodeType==Node.ELEMENT_NODE){switch(c.nodeName.toLowerCase()){case "thumb":s+="<img src=\""+this.hoverPoints[i].thumb+"\"/>";break;case "info":var hp=this.hoverPoints[i].info;for(var k=0;k<hp.childNodes.length;k++)s+=SerializeXML(hp.childNodes[k]);break;default:s+=this.serializeNode(c,i,"");}}else s+=this.serializeNode(c,i,"");}return s+"</"+n.nodeName+">";}return s+"/>";}
SmartMap.prototype.getCssInfo=function(c){c=c.toLowerCase();if(this.hpCSSs[c])return this.hpCSSs[c];if(!document.styleSheets)return null;var n=-1;for(var i=0;i<document.styleSheets.length;i++){if(document.styleSheets[i].href.toLowerCase().indexOf("/core.css")!=-1){n=i;break;}}if(n==-1)return null;var rules=[];if(document.styleSheets[n].cssRules)rules = document.styleSheets[n].cssRules;else if(document.styleSheets[n].rules)rules=document.styleSheets[n].rules;else return null;var d=new Dim(0,0);var p=-1;for(var i=0;i<rules.length;i++){var tmp=rules[i].selectorText.toLowerCase().split(' ');for(var j=0;j<tmp.length;j++){p=tmp[j].indexOf('.');if(p!=-1)tmp[j]=tmp[j].substring(p+1);if(tmp[j]==c){var s=rules[i].style;d=new Dim(s.width?parseInt(s.width):0,s.height?parseInt(s.height):0);}}}if(d.width>0||d.height>0)return (this.hpCSSs[c]=d);return null;}
SmartMap.prototype.setMode=function(m){this.mode=m.toLowerCase()=="identify"?SM_MODE_IDENTIFY:SM_MODE_WINDOW_ZOOM;this.setCursor(modeCursor[this.mode]);}
SmartMap.prototype.setZoomCallback=function(c){this.zc=c;}
SmartMap.prototype.setIdentifyCallback=function(c){this.ic=c;}
SmartMap.prototype.findHoverLayer=function(n,x,y,ox,oy){if(n){var p,idx,hp;for(var i=0;i<n.points.length;i++){idx=n.points[i].index;p=idx.indexOf(',');if(p!=-1)idx=idx.substring(0,p);hp=this.hoverPoints[idx];if(ox==hp.cx&&oy==hp.cy)return n.points[i];}var u=parseInt(n.w/2);var v=parseInt(n.h/2);if(x<u){if(y<v&&n.q1)return this.findHoverLayer(n.q1,x,y,ox,oy);else if(y>=v&&n.q3)return this.findHoverLayer(n.q3,x,y-v,ox,oy);}else{if(y<v&&n.q2)return this.findHoverLayer(n.q2,x-u,y,ox,oy);else if(y>=v && n.q4)return this.findHoverLayer(n.q4,x-u,y-v,ox,oy);}}return null;}
SmartMap.prototype.findHoverLayers=function(n,x,y,ox,oy){var tmp=[];if(n){var p,idx,hp;for(var i=0;i<n.points.length;i++){idx=n.points[i].index;p=idx.indexOf(',');if(p!=-1)idx=idx.substring(0,p);hp=this.hoverPoints[idx];if(ox>=hp.x&&ox<=hp.x+hp.w&&oy>=hp.y&&oy<=hp.y+hp.h)tmp[tmp.length]=n.points[i];}var u=parseInt(n.w/2);var v=parseInt(n.h/2);if(x<u){if(y<v&&n.q1)tmp=tmp.concat(this.findHoverLayers(n.q1,x,y,ox,oy));else if(y>=v&&n.q3)tmp=tmp.concat(this.findHoverLayers(n.q3,x,y-v,ox,oy));}else{if(y<v&&n.q2)tmp=tmp.concat(this.findHoverLayers(n.q2,x-u,y,ox,oy));else if(y>=v&&n.q4)tmp=tmp.concat(this.findHoverLayers(n.q4,x-u,y-v,ox,oy));}}return tmp;}
SmartMap.prototype.updateZoomCoords=function(x,y){var m=this.mouse;var z=this.zoom;var i=this.image;z.left=m.left<m.right?m.left:m.right;z.right=m.left<m.right?m.right:m.left;z.top=m.top<m.bottom?m.top:m.bottom;z.bottom=m.top<m.bottom?m.bottom:m.top;if(z.left<i.left)z.left=i.left;if(z.right>i.right)z.right=i.right;if(z.top<i.top)z.top=i.top;if(z.bottom>i.bottom)z.bottom=i.bottom;z.right=z.right-z.left;z.bottom=z.bottom-z.top;z.left-=i.left;z.top-=i.top;}
SmartMap.prototype.updateImageCoords=function(){var i=this.image;i.left=0;i.top=0;var e=this.ph;do{i.left+=e.offsetLeft;i.top+=e.offsetTop;}while(e=e.offsetParent);i.right=this.ph.offsetLeft+this.mapImage.offsetWidth;i.bottom=this.ph.offsetTop+this.mapImage.offsetHeight;}