//
//  iWeb - MapWidget.js
//  Copyright (c) 2007 Apple Inc. All rights reserved.
//

function getArgs(){var args=new Object();var query=location.search.substring(1);var pairs=query.split("&");for(var i=0;i<pairs.length;++i){var pair=pairs[i];var pos=pair.indexOf('=');if(pos>0){var argname=decodeURIComponent(pair.substring(0,pos));var value=decodeURIComponent(pair.substring(pos+1));args[argname]=value;}}
return args;}
String.prototype.stringByEscapingXML=function()
{var str=this.replace(/&/g,'&amp;');str=str.replace(/</g,'&lt;');return str;};function MapWidget(){if(window.hostPrefsBridge!==undefined){this.preferences=window.hostPrefsBridge;if(this.preferences.registerExtraWidget)
this.preferences.registerExtraWidget(this);}else{this.preferences=getArgs();}}
MapWidget.prototype.onload=function(){if(GBrowserIsCompatible()){this.loadLocalizedStrings();var mapElt=document.getElementById("mapElt");if(mapElt){this.map=new GMap2(mapElt);if(this.map)
this.configMap();}}}
MapWidget.prototype.isIPhone=function(){var isIPhone=false;if(navigator.userAgent){var ua=navigator.userAgent.toLowerCase();isIPhone=ua.match(/applewebkit\/(\d+)/)&&(ua.indexOf("iphone")!=-1);}
return isIPhone;}
MapWidget.prototype.configMap=function(){var point=this.centerPointFromPreference();var zoom=this.zoomLevelFromPreference();var mapType=this.mapTypeFromPreference();this.map.setCenter(point,zoom,mapType);GEvent.bind(this.map,"dblclick",this,this.mapEvent_doubleClick);GEvent.bind(this.map,"movestart",this,this.mapEvent_movestart);GEvent.bind(this.map,"moveend",this,this.mapEvent_moveend);GEvent.bind(this.map,"dragstart",this,this.mapEvent_dragstart);GEvent.bind(this.map,"dragend",this,this.mapEvent_dragend);GEvent.bind(this.map,"zoomend",this,this.mapEvent_zoomend);GEvent.bind(this.map,"maptypechanged",this,this.mapEvent_maptypechanged);GEvent.bind(this.map,"infowindowopen",this,this.mapEvent_infowindowopen);GEvent.bind(this.map.getInfoWindow(),"closeclick",this,this.mapEvent_clickCloseBoxOnInfoWindow);if(this.isIPhone())
GEvent.bind(this.map,"click",this,this.mapEvent_clickForIPhone);this.map.addControl(new GMapTypeControl());this.map.addControl(new GScaleControl());this.updateZoomControl();this.updateMarker();this.updateInfoWindow();this.updateGoogleBar();var linkNodes=document.getElementById("mapElt").getElementsByTagName("A");for(var i=0;i<linkNodes.length;i++){linkNodes[i].target="_blank";};}
MapWidget.prototype.mapEvent_clickForIPhone=function(){var newURL="http://maps.google.com"+"?z="+this.map.getZoom()+"&t="+this.map.getCurrentMapType().getUrlArg()+"&ll="+this.map.getCenter().toUrlValue();var addressText=this.preferenceForKey("locatedAddress");if(addressText)
newURL=newURL+"&q="+encodeURIComponent(addressText);window.top.location=newURL;}
MapWidget.prototype.mapEvent_doubleClick=function(){if(this.booleanFromPreference("showInfo")){this.infoWindowIsOpeningOrClosing=true;this.setPreferenceForKey("0","showInfo");this.infoWindowIsOpeningOrClosing=false;}}
MapWidget.prototype.mapEvent_movestart=function(){this.isMoving=true;this.didOpenInfoWindowDuringMove=false;}
MapWidget.prototype.mapEvent_moveend=function(){if(!this.handlingPreferenceChange&&!this.isDragging){var disableUndo=this.didOpenInfoWindowDuringMove;if(disableUndo)
this.disableUndoRegistration();this.updateCenterPreferenceFromMap(false);if(disableUndo)
this.enableUndoRegistration();}
this.isMoving=false;}
MapWidget.prototype.mapEvent_dragstart=function(){this.isDragging=true;}
MapWidget.prototype.mapEvent_dragend=function(){this.updateCenterPreferenceFromMap(false);this.isDragging=false;}
MapWidget.prototype.mapEvent_zoomend=function(){if(!this.handlingPreferenceChange)
this.setPreferenceForKey(this.map.getZoom().toString(),"zoomLevel");}
MapWidget.prototype.mapEvent_maptypechanged=function(){if(!this.handlingPreferenceChange)
this.updateMapTypePreferenceFromMap();}
MapWidget.prototype.mapEvent_infowindowopen=function(){if(this.isMoving)
this.didOpenInfoWindowDuringMove=true;}
MapWidget.prototype.mapEvent_clickCloseBoxOnInfoWindow=function(){this.infoWindowIsOpeningOrClosing=true;this.setPreferenceForKey("0","showInfo");this.map.closeInfoWindow();this.infoWindowIsOpeningOrClosing=false;}
MapWidget.prototype.onunload=function(){GUnload();}
MapWidget.prototype.preferenceForKey=function(key){var value;if(this.preferences)
value=this.preferences[key];return value;}
MapWidget.prototype.setPreferenceForKey=function(preference,key){if(this.preferences)
this.preferences[key]=preference;}
MapWidget.prototype.disableUndoRegistration=function(){if(this.preferences.disableUndoRegistration)
this.preferences.disableUndoRegistration();}
MapWidget.prototype.enableUndoRegistration=function(){if(this.preferences.enableUndoRegistration)
this.preferences.enableUndoRegistration();}
MapWidget.prototype.changedPreferenceForKey=function(key){if(this.infoWindowIsOpeningOrClosing)
return;this.handlingPreferenceChange=true;if(key=="unlocatedAddress"){var addr=this.preferenceForKey(key);if(addr&&addr.length>0){var geocoder=new GClientGeocoder();var self=this;var callback=function(response){var unlocatedAddress=self.preferenceForKey("unlocatedAddress");self.setPreferenceForKey("","unlocatedAddress");if(response&&response.Status.code==200){var place=response.Placemark[0];var point=new GLatLng(place.Point.coordinates[1],place.Point.coordinates[0]);var pointString=point.toUrlValue();var locatedAddress=place.address;var locatedAddressIsFromGeocoder="1";var zoomLevel="16";if(place.AddressDetails!==undefined&&place.AddressDetails.Accuracy!==undefined&&place.AddressDetails.Accuracy<=1){locatedAddress=unlocatedAddress;locatedAddressIsFromGeocoder="0";zoomLevel="10";}
self.disableUndoRegistration();self.setPreferenceForKey(pointString,"center");self.setPreferenceForKey(zoomLevel,"zoomLevel");self.setPreferenceForKey(pointString,"locatedAddressPoint");self.setPreferenceForKey(locatedAddressIsFromGeocoder,"locatedAddressIsFromGeocoder");self.setPreferenceForKey(locatedAddress,"locatedAddress");self.enableUndoRegistration();}};geocoder.getLocations(addr,callback);}}else if(key=="center"){if(this.map!==undefined&&this.map.getCenter().toUrlValue()!=this.preferenceForKey("center")){this.map.setCenter(this.centerPointFromPreference());}}else if(key=="zoomLevel"){if(this.map)
this.map.setZoom(this.zoomLevelFromPreference());}else if(key=="showZoom"){this.updateZoomControl();}else if(key=="mapType"){if(this.map)
this.map.setMapType(this.mapTypeFromPreference());}else if(key=="showInfo"||key=="locatedAddress"){if(this.booleanFromPreference("showInfo"))
this.updateCenterPreferenceFromMap(true);this.updateInfoWindow();}else if(key=="locatedAddressPoint"){this.updateMarker();}else if(key=="showGoogleBar"){this.updateGoogleBar();}
this.handlingPreferenceChange=false;}
MapWidget.prototype.mapTypeFromPreference=function(){var mapType=G_NORMAL_MAP;var mapTypeUrlArg=this.preferenceForKey("mapType");if(mapTypeUrlArg){var mapTypes=G_DEFAULT_MAP_TYPES;for(var i=0;i<mapTypes.length;i++){if(mapTypes[i].getUrlArg()==mapTypeUrlArg){mapType=mapTypes[i];break;}}}
return mapType;}
MapWidget.prototype.updateMapTypePreferenceFromMap=function(){if(this.map)
this.setPreferenceForKey(this.map.getCurrentMapType().getUrlArg(),"mapType");}
MapWidget.prototype.centerPointFromPreference=function(){var point=this.pointFromString(this.preferenceForKey("center"));if(!point)
point=new GLatLng(37.330550,-122.029700);return point;}
MapWidget.prototype.updateCenterPreferenceFromMap=function(force){if(this.map){var newCenterStr=this.map.getCenter().toUrlValue();if(force||newCenterStr!=this.preferenceForKey("center"))
this.setPreferenceForKey(newCenterStr,"center");}}
MapWidget.prototype.zoomLevelFromPreference=function(){var zoomLevel=16;var zoomLevelString=this.preferenceForKey("zoomLevel");if(zoomLevelString!==undefined&&zoomLevelString.length>0)
zoomLevel=parseInt(zoomLevelString);return zoomLevel;}
MapWidget.prototype.resizeMap=function(){if(this.map){this.map.checkResize();this.updateZoomControl();this.disableUndoRegistration();this.updateCenterPreferenceFromMap(false);this.enableUndoRegistration();}}
MapWidget.prototype.updateZoomControl=function(){if(this.map){var currentState=0;if(this.zoomControl instanceof GSmallMapControl)currentState=1;else if(this.zoomControl instanceof GLargeMapControl)currentState=2;var newState=0;if(this.booleanFromPreference("showZoom")){if(document.body.clientHeight<=290)newState=1;else newState=2;}
if(newState!=currentState){if(this.zoomControl){this.map.removeControl(this.zoomControl);this.zoomControl=undefined;}
if(newState>0){this.zoomControl=((newState==1)?new GSmallMapControl():new GLargeMapControl());this.map.addControl(this.zoomControl);}}}}
MapWidget.prototype.updateGoogleBar=function(){if(this.map){if(this.booleanFromPreference("showGoogleBar",false)){this.map.enableGoogleBar();}else{this.map.disableGoogleBar();}}}
MapWidget.prototype.updateMarker=function(){if(this.map){var point=this.pointFromString(this.preferenceForKey("locatedAddressPoint"));if(point!==undefined){if(this.marker){this.marker.setPoint(point);}else{this.marker=new GMarker(point);this.map.addOverlay(this.marker);GEvent.bind(this.marker,"click",this,this.openMarkerInfoWindow);}}else{if(this.marker){GEvent.clearInstanceListeners(this.marker);this.map.removeOverlay(this.marker);this.marker=undefined;}}}}
MapWidget.prototype.updateInfoWindow=function(){if(this.booleanFromPreference("showInfo")&&this.marker){this.openMarkerInfoWindow();}else{this.map.closeInfoWindow();}}
MapWidget.prototype.openMarkerInfoWindow=function(){if(this.marker){var addressText=this.preferenceForKey("locatedAddress");if(addressText!==undefined&&addressText.length>0){if(localizedStrings===undefined)
{var self=this;setTimeout(function(){self.openMarkerInfoWindow()},250);}
else
{var formattedAddressText=addressText.stringByEscapingXML();if(this.booleanFromPreference("locatedAddressIsFromGeocoder",true)){formattedAddressText=formattedAddressText.replace(/,/,"<br/>");formattedAddressText=formattedAddressText.replace(/,[^,]*$/,"");}
var htmlText='<p class="addressHeaderPara">'+localizedStrings['address']+'</p>'+'<p class="addressBodyPara">'+formattedAddressText+'</p>'+'<p class="addressLinkPara">'+localizedStrings['directions']+'<a class="addressToLink" href="http://maps.google.com?saddr=&daddr='+encodeURIComponent(addressText)+'" target="_blank">'+localizedStrings['to']+'</a>'+' - '+'<a class="addressFromLink" href="http://maps.google.com?daddr=&saddr='+encodeURIComponent(addressText)+'" target="_blank">'+localizedStrings['from']+'</a>'+'</p>';this.infoWindowIsOpeningOrClosing=true;if(!this.booleanFromPreference("showInfo")){this.setPreferenceForKey("1","showInfo");this.updateCenterPreferenceFromMap(true);}
this.marker.openInfoWindowHtml(htmlText);this.infoWindowIsOpeningOrClosing=false;}}}}
MapWidget.prototype.booleanFromPreference=function(key,defaultValue){var result;if(defaultValue!==undefined)
result=defaultValue;var prefString=this.preferenceForKey(key);if(prefString!==undefined&&prefString.length>0)
result=Boolean(parseInt(prefString));return result;}
MapWidget.prototype.pointFromString=function(str){var point;if(str!==undefined&&str.length>0){var splitValues=str.split(",");if(splitValues.length==2)
point=new GLatLng(parseFloat(splitValues[0]),parseFloat(splitValues[1]));}
return point;}
var localizedStrings;MapWidget.prototype.loadLocalizedStrings=function(){var validLanguage="en";var language=this.preferenceForKey("language");if(language){var languages=new Array("da","de","en","es","fi","fr","it","ja","ko","nb","nl","pl","pt-PT","ru","sv","zh-Hans","zh-Hant");for(var i=0;i<languages.length;i++){if(languages[i]==language){validLanguage=language;break;}}}
var url='../'+validLanguage+'.lproj/localizedStrings.js';var head=document.getElementsByTagName("head")[0];var scriptElement=document.createElement('script');scriptElement.type='text/javascript';scriptElement.src=url;scriptElement.charset='utf-8';head.appendChild(scriptElement);}
var mapWidget=new MapWidget();

