
var AskmeGoogleMapInstances=new Array();var AskmeGoogleMap=function(){this.map;this.gdir;this.selfRef=AskmeGoogleMapInstances.length;AskmeGoogleMapInstances[this.selfRef]=this;this.currentInstanceIndex=GlobalInstanceRegister.length;this.updateLatLongCB="AskmeGoogleMapInstances["+this.currentInstanceIndex+"].updateLatLongFields";this.proposeNewLocationCB="AskmeGoogleMapInstances["+this.currentInstanceIndex+"].proposeNewLocation";};AskmeGoogleMap.prototype.setGMapParams=function(gMapParam){this.loadInElementId=typeof(gMapParam.loadInElementId)!='undefined'?gMapParam.loadInElementId:'';this.latitude=typeof(gMapParam.latitude)!='undefined'?gMapParam.latitude:'';this.longitude=typeof(gMapParam.longitude)!='undefined'?gMapParam.longitude:'';this.listingId=typeof(gMapParam.listingId)!='undefined'?unescape(gMapParam.listingId):0;this.listingName=typeof(gMapParam.listingName)!='undefined'?unescape(gMapParam.listingName):'';this.street1=typeof(gMapParam.street1)!='undefined'?gMapParam.street1:'';this.street2=typeof(gMapParam.street2)!='undefined'?gMapParam.street2:'';this.street3=typeof(gMapParam.street3)!='undefined'?gMapParam.street3:'';this.crossStreet=typeof(gMapParam.crossStreet)!='undefined'?gMapParam.crossStreet:'';this.neighbourhood=typeof(gMapParam.neighbourhood)!='undefined'?gMapParam.neighbourhood:'';this.parentNeighbourhood=typeof(gMapParam.parentNeighbourhood)!='undefined'?gMapParam.parentNeighbourhood:'';this.city=typeof(gMapParam.city)!='undefined'?gMapParam.city:'';this.state=typeof(gMapParam.state)!='undefined'?gMapParam.state:'';this.metro=typeof(gMapParam.metro)!='undefined'?gMapParam.metro:'';this.pincode=typeof(gMapParam.pincode)!='undefined'?gMapParam.pincode:'';this.contacts=typeof(gMapParam.contacts)!='undefined'?gMapParam.contacts:'';this.zoom=typeof(gMapParam.zoom)!='undefined'?gMapParam.zoom:13;this.maxWidth=typeof(gMapParam.maxWidth)!='undefined'?gMapParam.maxWidth:'';this.showBubbleText=typeof(gMapParam.showBubbleText)!='undefined'?gMapParam.showBubbleText:false;this.userHandle=typeof(gMapParam.userHandle)!='undefined'&&gMapParam.userHandle?gMapParam.userHandle:'';this.isDraggable=typeof(gMapParam.isDraggable)!='undefined'?gMapParam.isDraggable:false;this.parentWinCallbackFunctionOnDragEnd=typeof(gMapParam.parentWinCallbackFunctionOnDragEnd)!='undefined'?gMapParam.parentWinCallbackFunctionOnDragEnd:null;};AskmeGoogleMap.prototype.setGMapDirectionParams=function(gMapDirParam){this.fromAddress=typeof(gMapDirParam.fromAddress)!='undefined'?gMapDirParam.fromAddress:'';this.toAddress=typeof(gMapDirParam.toAddress)!='undefined'?gMapDirParam.toAddress:'';this.loadInDirElemId=typeof(gMapDirParam.loadInDirectionsElementId)!='undefined'?gMapDirParam.loadInDirectionsElementId:'';};AskmeGoogleMap.prototype.renderGoogleMap=function(){if(GBrowserIsCompatible()){if(this.latitude&&this.latitude>0&&this.longitude&&this.longitude>0){this.renderMapByLatLong();}else{this.renderMapByAddress();}}};AskmeGoogleMap.prototype.renderMapByAddress=function(){this.address='';if(this.neighbourhood){this.address+=this.neighbourhood;}
if(this.city){this.address+=' , '+this.city;}
this.callGeoDecoder();};AskmeGoogleMap.prototype.callGeoDecoder=function(){var currentInstance=AskmeGoogleMapInstances[this.selfRef];var geocoder=new GClientGeocoder();geocoder.getLatLng(currentInstance.address,function(point){if(point){currentInstance.latitude=point.lat();currentInstance.longitude=point.lng();currentInstance.renderMapByLatLong();}else{var addressFromMap=currentInstance.neighbourhood+', '+currentInstance.city;if(currentInstance.address!=addressFromMap){currentInstance.address=addressFromMap;currentInstance.callGeoDecoder();}}});};AskmeGoogleMap.prototype.renderMapByLatLong=function(){var currentInstance=AskmeGoogleMapInstances[this.selfRef];this.loadInElement=bObject(this.loadInElementId);if(!this.loadInElement){return"";}
this.loadInElement.innerHTML='';if(this.showBubbleText){var bubbleText="";if(this.isDraggable){var areaStr=this.neighbourhood?this.neighbourhood:"";areaStr=areaStr?(this.city?areaStr+", "+this.city:areaStr):(this.city?this.city:"");bubbleText="<div style='font-size:13px;padding-top:5px'>Your current location is <b>"+
areaStr+"</b>";bubbleText+="<br/><br/>Drag the pin to relocate.</div>"}else{if(this.listingName||this.neighbourhood||this.city||this.contacts){if(this.listingName){bubbleText+="<span style='font-size:13px; font-weight:bold; padding-left:0px;'>"+this.listingName+"</span><br/>";}
if(this.street1){bubbleText+="<span style='font-size:12px; font-weigth:normal;padding-left:0px'>"+this.street1+"</span>";}
if(this.street2){bubbleText+=", <span style='font-size:12px; font-weigth:normal;padding-left:0px'>"+this.street2+"</span>";}
if(this.street3){bubbleText+=", <span style='font-size:12px; font-weigth:normal;padding-left:0px'>"+this.street3+"</span>";}
if(this.crossStreet){bubbleText+=", <span style='font-size:12px; font-weigth:normal;padding-left:0px'>"+this.crossStreet+"</span>";}
if(this.landmark){bubbleText+=", <span style='font-size:12px; font-weigth:normal;padding-left:0px'>"+this.landmark+"</span>";}
if(this.neighbourhood){bubbleText+=", <span style='font-size:12px; font-weigth:normal;padding-left:0px'>"+this.neighbourhood+"</span>";}
if(this.parentNeighbourhood){bubbleText+=", <span style='font-size:12px; font-weigth:normal;padding-left:0px'>"+this.parentNeighbourhood+"</span>";}
if(this.city){bubbleText+=", <span style='font-size:12px; font-weigth:normal;padding-left:0px'>"+this.city+"</span>";}
if(this.pincode){bubbleText+=" - <span style='font-size:12px; font-weigth:normal;padding-left:0px'>"+this.pincode+"</span>";}
if(this.contacts){bubbleText+="<br/><span style='font-size:12px; font-weigth:normal;padding-left:0px'>"+this.contacts+"</span>";}}else{this.showBubbleText=!this.showBubbleText;}}}
this.map=new GMap2(this.loadInElement);var position=new GLatLng(this.latitude,this.longitude);this.map.addControl(new GSmallMapControl());this.map.setCenter(new GLatLng(currentInstance.latitude,currentInstance.longitude),currentInstance.zoom);var currentPositionMarker=this.isDraggable?new GMarker(position,{draggable:true}):new GMarker(position);if(this.showBubbleText){currentPositionMarker.maxWidth=this.maxWidth;;GEvent.addListener(currentPositionMarker,"click",function(){this.openInfoWindow(bubbleText,{maxWidth:this.maxWidth});});}
if(this.isDraggable){GEvent.addListener(currentPositionMarker,"dragstart",function(){currentInstance.map.closeInfoWindow();});GEvent.addListener(currentPositionMarker,"dragend",function(){var latLang=this.getLatLng();var bubbleText="<div style='font-size:13px;padding-top:10px'>Click OK to relocate."+"&nbsp;&nbsp;<button id='jSubmitLatLng' lat='"+latLang.lat()+"' lng='"+latLang.lng()+"'>OK</button></div>";currentPositionMarker.openInfoWindowHtml(bubbleText,{maxWidth:this.maxWidth});$("#jSubmitLatLng").click(function(){if(currentInstance.parentWinCallbackFunctionOnDragEnd&&window.opener){window.opener[currentInstance.parentWinCallbackFunctionOnDragEnd]($(this).attr("lat"),$(this).attr("lng"));window.close();}
currentInstance.map.closeInfoWindow();});});}
this.map.addOverlay(currentPositionMarker);if(this.isDraggable){GEvent.trigger(currentPositionMarker,'click');}};AskmeGoogleMap.prototype.displayAccurateMarker=function(){var currentInstance=AskmeGoogleMapInstances[this.selfRef];var blueIcon=new GIcon(G_DEFAULT_ICON);blueIcon.image="http://www.google.com/intl/en_us/mapfiles/ms/micons/blue-dot.png";var options={draggable:true,bouncy:true,icon:blueIcon};var position=new GLatLng(this.latitude,this.longitude);this.futurePositionMarker=new GMarker(position,options);this.map.addOverlay(this.futurePositionMarker);var bubbleTextDrag="<div style=\"font-weight:700;padding:6px 15px; color:#323232\">";bubbleTextDrag+="Are you sure this is the correct location?<br/>";bubbleTextDrag+="<input id='sendLocation' type='submit' value='Yes' onclick='javascript:"+this.proposeNewLocationCB+"()'/>";bubbleTextDrag+="</div>"
var bubbleTextLoad="<div style=\"font-weight:700;padding:6px 15px; color:#323232\">Drag the blue marker to the correct location.</div>";this.futurePositionMarker.callback=this.updateLatLongCB;this.futurePositionMarker.openInfoWindowHtml(bubbleTextLoad,{maxWidth:this.futurePositionMarker.maxWidth});GEvent.addListener(this.futurePositionMarker,"dragstart",function(){currentInstance.map.closeInfoWindow();});GEvent.addListener(this.futurePositionMarker,"dragend",function(){var latLang=this.getLatLng();eval(this.callback+"("+latLang.lat()+","+latLang.lng()+","+true+")");this.openInfoWindowHtml(bubbleTextDrag,{maxWidth:this.maxWidth});});};AskmeGoogleMap.prototype.proposeNewLocation=function(){var uri="/flag/map/";var sendParams="cat=I";sendParams+="&recordId="+this.listingId;sendParams+="&description=Proposing new latitude and longitude for the listing.";sendParams+="&lat="+$("#latitudeField").val();sendParams+="&long="+$("#longitudeField").val();sendRequest(uri,sendParams,1);var bubbleTextSent="<div style=\"font-weight:700;padding:6px 15px; color:#323232\">Thanks! We'll get this fixed shortly.</div>";this.futurePositionMarker.openInfoWindowHtml(bubbleTextSent,{maxWidth:this.futurePositionMarker.maxWidth});};AskmeGoogleMap.prototype.updateLatLongFields=function(latitude,longitude,isDragged){if($('#latitudeField').length==0){var latInput=document.createElement('input');$(latInput).attr("type","hidden");$(latInput).attr("id","latitudeField");this.loadInElement.appendChild(latInput);}
$('#latitudeField').attr("value",latitude);if($('#longitudeField').length==0){var longInput=document.createElement('input');$(longInput).attr("type","hidden");$(longInput).attr("id","longitudeField");this.loadInElement.appendChild(longInput);}
$('#longitudeField').attr("value",longitude);};AskmeGoogleMap.prototype.renderDirections=function(){this.renderMapByLatLong();var currentInstance=AskmeGoogleMapInstances[this.selfRef];var loadInDirElem=null;if(this.loadInDirElemId){loadInDirElem=bObject(this.loadInDirElemId);loadInDirElem.style.display='block';loadInDirElem.innerHTML='';}
if(GBrowserIsCompatible()&&loadInDirElem){this.gdir=new GDirections(this.map,loadInDirElem);GEvent.addListener(this.gdir,"addoverlay",function(){var poly=currentInstance.gdir.getPolyline();});GEvent.addListener(this.gdir,"error",function(){if(currentInstance.gdir.getStatus().code==G_GEO_UNKNOWN_ADDRESS)
loadInDirElem.innerHTML="<b>No corresponding geographic location could be found for one of the specified addresses. This may be due to the fact that the address is relatively new, or it may be incorrect.</b>";else if(currentInstance.gdir.getStatus().code==G_GEO_SERVER_ERROR)
loadInDirElem.innerHTML="<b>A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.\n Error code: "+currentInstance.gdir.getStatus().code+"</b>";else if(currentInstance.gdir.getStatus().code==G_GEO_MISSING_QUERY)
loadInDirElem.innerHTML="<b>The HTTP q parameter was either missing or had no value. For geocoder requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input.\n Error code: "+currentInstance.gdir.getStatus().code+"</b>";else if(currentInstance.gdir.getStatus().code==G_GEO_BAD_KEY)
loadInDirElem.innerHTML="<b>The given key is either invalid or does not match the domain for which it was given. \n Error code: "+currentInstance.gdir.getStatus().code+"</b>";else if(currentInstance.gdir.getStatus().code==G_GEO_BAD_REQUEST)
loadInDirElem.innerHTML="<b>A directions request could not be successfully parsed.\n Error code: "+currentInstance.gdir.getStatus().code+"</b>";else loadInDirElem.innerHTML="<b>An unknown error occurred.</b>";});var from=this.fromAddress+(this.city?","+this.city:'');var to=this.toAddress+(this.city?","+this.city:'');this.gdir.load("from: "+from+" to: "+to,{"locale":"en_US"});}};AskmeGoogleMap.prototype.addImg=function(url,id){var img=document.createElement("img");img.src=url;document.getElementById(id).innerHTML="";document.getElementById(id).appendChild(img);};
