var map,markerIcon,markerOptions;var stockists=new Array;var Ajax=new Request.JSON;var requestStarted=function(){Logger.log("Request started");overlay=new Element("div",{id:"map-loader"});overlay.setStyles($("google-map").getCoordinates());overlay.setStyle("opacity",0.5);$(document.body).adopt(overlay);};var requestSuccess=function(){Logger.log("Request succeeded");if($("map-loader")){$("map-loader").destroy();}};Ajax.addEvents({onRequest:requestStarted,onSuccess:requestSuccess});var stockistsLoad=function(){if(!map||!map.map.isLoaded()){stockistsLoad.delay(50);return ;}map.reset();if($("collection").value.length){Logger.log("We have a preset collection");$("collection").fireEvent("change");return ;}Ajax.addEvent("success",hydrateStockists);Ajax.send({url:GET_STOCKISTS,method:"get"});};var stockistsReady=function(){map=new GoogleMap;if(!map){return ;}markerIcon=new GIcon(G_DEFAULT_ICON);markerIcon.image=HOST_NAME+"/images/marker.png";markerIcon.iconSize=new GSize(16,32);markerIcon.iconAnchor=new GPoint(8,31);markerOptions={icon:markerIcon};$("city").addEvent("change",showCity);$("collection").addEvent("change",filterByCollection);};var filterByCollection=function(){$("city").set("value",null);map.reset();Ajax.addEvent("success",hydrateStockists);Ajax.send({url:GET_STOCKISTS,method:"get",data:"collection="+this.get("value")});};var showCity=function(){$("collection").set("value",null);if(this.get("value")==""){stockistsLoad();}$("available-collections").dissolve();Ajax.addEvent("success",map.updateViewport.bind(map));Ajax.send({url:GET_CITY,method:"get",data:"city="+this.get("value")});};var hydrateStockists=function(B){Ajax.removeEvent("success",hydrateStockists);stockists=new Array;if(!B.length){return ;}bounds={neLat:B[0].lat,swLat:B[0].lat,cLat:B[0].lat,neLng:B[0].lng,swLng:B[0].lng,cLng:B[0].lng};for(var A=0;A<B.length;A++){n=B[A];z=stockists.length;stockists.push(new Stockist(n));stockists[z].createMarker();if(n.lat<bounds.swLat){bounds.swLat=n.lat;}if(n.lng<bounds.swLng){bounds.swLng=n.lng;}if(n.lat>bounds.neLat){bounds.neLat=n.lat;}if(n.lng>bounds.neLng){bounds.neLng=n.lng;}}if(z===0){stockists[0].showInfoWindow();}bounds.cLat=(bounds.neLat+bounds.swLat)/2;bounds.cLng=(bounds.neLng+bounds.swLng)/2;map.updateViewport(bounds);};var Stockist=new Class({Implements:Log,latitude:false,longitude:false,point:false,marker:false,slug:false,sName:false,elabel:false,collections:false,collectionsUl:false,initialize:function(A){if(ENABLE_LOGGING){this.enableLog();}this.sName=A.name;this.slug=A.slug;this.latitude=A.lat;this.longitude=A.lng;this.point=new GLatLng(this.latitude,this.longitude);},createMarker:function(){this.marker=new GMarker(this.point,markerOptions);GEvent.addListener(this.marker,"click",this.showInfoWindow.bind(this));map.addMarker(this.marker);},showInfoWindow:function(){if(!this.elabel){Ajax.addEvent("success",this.createInfoWindow.bind(this));Ajax.send({url:GET_STOCKIST,method:"get",data:"stockist="+this.slug,onSuccess:this.createInfoWindow.bind(this)});}else{map.panTo(this.point);stockists.each(function(A){A.hideInfoWindow();});this.elabel.show();this.showCollections();}},createInfoWindow:function(A){Ajax.removeEvents("success");this.log(this);this.log("Creating a new infoWindow");this.log(A);dummy=new Element("div");addr=new Element("span",{html:A.address});dummy.adopt(addr);if(A.phone){phone=new Element("span",{html:A.phone});dummy.adopt(new Element("br"),phone);}if(A.web){web=new Element("a",{html:"website",href:A.web});dummy.adopt(new Element("br"),web);}if(A.collections){this.collections=A.collections;this.showCollections();}this.elabel=new ELabel(this.point,dummy.get("html"),"info-window",new GSize(-8,0));map.addInfoWindow(this.elabel);GEvent.addDomListener(this.elabel.div_,"click",function(B){this.hideInfoWindow();}.bind(this));this.showInfoWindow();Ajax.addEvents({onRequest:requestStarted,onSuccess:requestSuccess});},hideInfoWindow:function(){if(this.elabel){this.elabel.hide();$("available-collections").dissolve();}},showCollections:function(){this.log("Showing collections");$("available-collections").dissolve();if(!this.collections){return ;}this.log("We have "+this.collections.length+" to show");this.collectionsUl=new Element("ul");this.collections.each(function(A){li=new Element("li");a=new Element("a",{href:A.url,html:A.name});li.adopt(a);this.collectionsUl.adopt(li);}.bind(this));if($("available-collections").getElement("ul")){$("available-collections").getElement("ul").destroy();}$("available-collections").adopt(this.collectionsUl);$("available-collections").reveal();}});var GoogleMap=new Class({Implements:Log,map:false,markers:new Array,initialize:function(){G_NORMAL_MAP.getMinimumResolution=function(){return 2;};if(ENABLE_LOGGING){this.enableLog();}this.log("Initialising Google Map");if(!window.GBrowserIsCompatible||!GBrowserIsCompatible()){this.log("Browser incompatible");return false;}this.log("Browser is compatible");this.map=new GMap2($("google-map"));this.log("Adding controls and setting starting position");this.map.setCenter(new GLatLng(25,0),2);this.map.addControl(new GSmallMapControl(),new GControlPosition(G_ANCHOR_TOP_LEFT));this.map.addControl(new GScaleControl());},tearDown:function(){GUnload();},addMarker:function(A){i=this.markers.length;this.markers.push(A);this.map.addOverlay(this.markers[i]);return ;},panTo:function(A){this.map.panTo(A);},addInfoWindow:function(A){this.log("Adding infoWindow");this.map.addOverlay(A);},updateViewport:function(A){this.log("Updating viewport with",A);Ajax.removeEvent("success",this.updateViewport);if(!A.neLat){return ;}this.log(A);bounds=new GLatLngBounds(new GLatLng(A.swLat,A.swLng),new GLatLng(A.neLat,A.neLng));zoom=this.map.getBoundsZoomLevel(bounds);this.log("Setting zoom to ",zoom);this.map.setCenter(new GLatLng(A.cLat,A.cLng),zoom);},reset:function(){stockists=new Array;this.map.setCenter(new GLatLng(25,0),2);this.map.clearOverlays();$("available-collections").dissolve();}});window.addEvents({load:stockistsLoad,domready:stockistsReady,unload:GoogleMap.tearDown});
