$(document).ready(function(){$("#searchbox").keypress(function(a){if(a.which==13){lookupAjax()}})});var thesexmap=function(){var doccument=document,imagepath="http://images.lovehoney.co.uk/lib/",map,geocoder,heatmap,timer,delay1=2000,delay2=10000,delay3=10000,groupname,shops,tileserver,currentpins=[],stopclick=false,stopclicktimer,placeIcon,pingIcon;function initmap(canvas,mapinfo){placeIcon=new GIcon();placeIcon.image=imagepath+"image.png";placeIcon.shadow=imagepath+"shadow.png";placeIcon.iconSize=new GSize(35,35);placeIcon.shadowSize=new GSize(53,35);placeIcon.iconAnchor=new GPoint(18,35);placeIcon.infoWindowAnchor=new GPoint(18,0);placeIcon.printImage=imagepath+"printImage.gif";placeIcon.mozPrintImage=imagepath+"mozPrintImage.gif";placeIcon.printShadow=imagepath+"printShadow.gif";placeIcon.transparent=imagepath+"transparent.png";placeIcon.imageMap=[29,2,30,3,31,4,31,5,31,6,31,7,31,8,31,9,31,10,31,11,31,12,31,13,31,14,30,15,29,16,29,17,28,18,27,19,26,20,26,21,25,22,24,23,23,24,23,25,22,26,21,27,21,28,20,29,19,30,19,31,19,32,18,33,18,34,17,34,17,33,16,32,16,31,16,30,15,29,15,28,14,27,13,26,12,25,12,24,11,23,10,22,9,21,9,20,8,19,7,18,6,17,6,16,5,15,4,14,4,13,4,12,4,11,4,10,4,9,4,8,4,7,4,6,4,5,4,4,5,3,6,2];pingIcon=new GIcon();pingIcon.image=imagepath+"ping_image.png";pingIcon.shadow=imagepath+"ping_shadow.png";pingIcon.iconSize=new GSize(20,17);pingIcon.shadowSize=new GSize(29,17);pingIcon.iconAnchor=new GPoint(10,0);pingIcon.infoWindowAnchor=new GPoint(10,0);pingIcon.printImage=imagepath+"ping_printImage.gif";pingIcon.mozPrintImage=imagepath+"ping_mozPrintImage.gif";pingIcon.printShadow=imagepath+"ping_printShadow.gif";pingIcon.transparent=imagepath+"ping_transparent.png";pingIcon.imageMap=[10,0,11,1,11,2,12,3,12,4,13,5,14,6,14,7,15,8,15,9,16,10,17,11,17,12,18,13,18,14,19,15,19,16,0,16,0,15,1,14,1,13,2,12,2,11,3,10,4,9,4,8,5,7,5,6,6,5,7,4,7,3,8,2,8,1,9,0];var mapTypes=G_DEFAULT_MAP_TYPES;for(var i=0;i<mapTypes.length;i++){mapTypes[i].getMaximumResolution=function(latlng){return 9};mapTypes[i].getMinimumResolution=function(latlng){return 6}}map=new GMap2(canvas,{draggableCursor:"pointer",mapTypes:mapTypes});map.enableContinuousZoom();map.enableScrollWheelZoom();map.addControl(new GLargeMapControl());map.addControl(new GMapTypeControl());geocoder=new GClientGeocoder();geocoder.setBaseCountryCode("UK");geocoder.setViewport(new GLatLngBounds(new GLatLng(-11.25,48.92),new GLatLng(2.82,61.61)));tileserver=mapinfo.SRV;centremap(mapinfo);if(mapinfo.LEV_NAME=="Group"){groupname=mapinfo.PLACE_NAME}if(mapinfo.MARKERS){shops=mapinfo.MARKERS;placemarkers(mapinfo.MARKERS)}if(mapinfo.PINS){if(mapinfo.LEV===0){showBubble(mapinfo.PINS,0,true)}else{placePins(mapinfo.PINS)}if(mapinfo.LEV===0||mapinfo.LEV===1){if(mapinfo.TTT){$("#showtoptowns").click(function(){clearTimeout(timer);placePins(mapinfo.TTT)})}if(mapinfo.TTC){$("#showtopcounties").click(function(){clearTimeout(timer);placePins(mapinfo.TTC)})}}}GEvent.addListener(map,"click",function(overlay,point,opoint){if(!stopclick&&overlay===null){stopclick=true;getOutcode(point);stopclicktimer=setTimeout(function(){stopclick=false},1000)}});GEvent.addListener(map,"dragstart",function(){clearTimeout(timer)});GEvent.addListener(map,"zoomend",function(){clearTimeout(timer)})}function findplacesCallback(response){if(response&&response.Status.code===200){clearTimeout(timer);var addr=new GLatLng(response.Placemark[0].Point.coordinates[1],response.Placemark[0].Point.coordinates[0]);getOutcode(addr)}}function createPin(pins,pin,index){var j=index+1;var numberedIcon=new GIcon(placeIcon);if(pin.TP){numberedIcon.image=imagepath+"sm_icon_p"+j+".png"}else{numberedIcon.image=imagepath+"sm_icon_"+j+".png"}var markerOptions={icon:numberedIcon};var marker=new GMarker(pin.C,markerOptions);GEvent.addListener(marker,"click",function(){showBubble(pins,index)});return marker}function placePins(pins){var newpin;clearPins();map.closeInfoWindow();for(var i=0;i<pins.length;i++){newpin=createPin(pins,pins[i],i);map.addOverlay(newpin);currentpins.push(newpin);if(pins[i].TP){showBubble(pins,i,false)}}}function clearPins(){var oldpin;while(currentpins.length>0){oldpin=currentpins.pop();map.removeOverlay(oldpin)}}function placemarkers(markers){for(var i=0;i<markers.length;i++){map.addOverlay(createmarker(markers[i]))}}function createmarker(marker){var markericon=new GIcon(pingIcon);var markerOptions={icon:markericon};var themarker=new GMarker(marker.C,markerOptions);GEvent.addListener(themarker,"click",function(){showmarkerbubble(marker)});return themarker}function showmarkerbubble(m,n){var div=doccument.createElement("ul");div.id="bubble";div.className="shops";for(var i=0;i<m.LOCS.length;i++){var li=doccument.createElement("li");li.style.backgroundImage="url("+imagepath+"sexmap-"+m.LOCS[i].TU+".png)";if(m.LOCS[i].N){addContent(li,"h3",m.LOCS[i].N)}addContent(li,"p",m.LOCS[i].A+", "+m.LOCS[i].T+", "+m.LOCS[i].P);if(m.LOCS[i].B){addContent(li,"p",m.LOCS[i].B)}div.appendChild(li)}if(n!=null&&n<shops.length-1){var li=doccument.createElement("li");li.className="last";var link=doccument.createElement("a");link.appendChild(doccument.createTextNode("More... "));link.onclick=function(){showmarkerbubble(shops[n+1],n+1)};li.appendChild(link);div.appendChild(li)}var addimg=doccument.createElement("img");addimg.src=imagepath+"add_your_place.png";addimg.style.cursor="pointer";addimg.onclick=function(){sform()};div.appendChild(addimg);map.openInfoWindow(m.C,div)}function getBubbles(){timer=setTimeout(function(){getOutcode()},delay1)}function getOutcode(point){geocoder.getLocations(point,getOutcodeCallback)}function getOutcodeCallback(response){if(response&&response.Status.code===200){var pcode=recurse(response,"PostalCodeNumber");if(pcode!==null){pcode=pcode.split(" ")[0];GDownloadUrl("/sexmap/"+pcode.toLowerCase()+".json",function(data,responseCode){showBubbles(data)})}}}function recurse(obj,name){var ret=null;for(var i in obj){if(obj.hasOwnProperty(i)){if(typeof(obj[i])==="object"){ret=recurse(obj[i],name)}else{if(i===name){ret=obj[i]}}}if(ret!==null){break}}return ret}function showBubbles(json){var bubbles=eval("("+json+")");if(bubbles.length>0){showBubble(bubbles,0)}}function addContent(parent,type,text){var el=doccument.createElement(type);el.appendChild(doccument.createTextNode(text));parent.appendChild(el)}function showBubble(bubbles,index,move){clearTimeout(timer);var b=bubbles[index];var div=doccument.createElement("ul");div.id="bubble";var li=doccument.createElement("li");li.className="first";addContent(li,"h3",b.T);addContent(li,"p","People in "+b.T+" spend "+(b.R==1?"the same as":b.R+" times")+" the national average on "+(groupname?groupname+".":" their sex lives overall."));div.appendChild(li);if(b.GS){for(var j=0;j<b.GS.length;j++){var li=doccument.createElement("li");li.className="on";addContent(li,"p",(b.GS[j].GR==1?"The same as":b.GS[j].GR+" times")+" the national average on "+b.GS[j].G);li.style.backgroundImage="url("+imagepath+"sexmap-"+b.GS[j].GU+".png)";div.appendChild(li)}}var li=doccument.createElement("li");li.className="last";var link=doccument.createElement("a");if(!b.TP){link.appendChild(doccument.createTextNode("Click to see more details about Sex in "+b.T));link.href=b.TU+".html"}else{if(shops&&shops.length>0){link.appendChild(doccument.createTextNode("Show Sex Shops near "+b.T));link.onclick=function(){showmarkerbubble(shops[0],0)}}}li.appendChild(link);div.appendChild(li);map.openInfoWindow(b.C,div);if(move==true){var nextindex=index+1;if(nextindex>=bubbles.length){nextindex=0}timer=setTimeout(function(){showBubble(bubbles,nextindex,true)},nextindex==1?delay2:delay3)}}function centremap(mapinfo){if(map.getBoundsZoomLevel(mapinfo.BOUNDS)<6){map.setCenter(new GLatLng(54.54,-3.57),6)}else{map.setCenter(mapinfo.CENTRE,Math.min(map.getBoundsZoomLevel(mapinfo.BOUNDS),9))}}function addtileoverlay(mapinfo){var screenOverlay=new GScreenOverlay(imagepath+"map-scale.png",new GScreenPoint(0,0),new GScreenPoint(0,0),new GScreenSize(0,0));map.addOverlay(screenOverlay);var bounds=new GLatLngBounds(new GLatLng(-90,-180),new GLatLng(90,180));var copyright=new GCopyright("LoveHoney",bounds,0,"(c) 2009 Lovehoney <http://www.lovehoney.co.uk/>");var copyrights=new GCopyrightCollection();copyrights.addCopyright(copyright);var heatmap=new GTileLayer(copyrights,10,0);heatmap.getTileUrl=function(tile,zoom){if(zoom<=9){return tileserver+zoom+"/"+tile.x+"/"+tile.y+".jpg"}else{return null}};heatmap.isPng=function(){return false};heatmap.getOpacity=function(){return 0.5};map.addOverlay(new GTileLayerOverlay(heatmap))}function findnr(placename,callback){var town="";geocoder.getLocations(placename,function(response){if(response&&response.Status.code===200){clearTimeout(timer);town=recurse(response.Placemark[0],"LocalityName");if(!town){town=recurse(response.Placemark[0],"DependentLocalityName")}if(!town){town=""}var addr=new GLatLng(response.Placemark[0].Point.coordinates[1],response.Placemark[0].Point.coordinates[0]);geocoder.getLocations(addr,function(response){if(response&&response.Status.code===200){var pcode=recurse(response,"PostalCodeNumber");var country=recurse(response,"CountryName");if(pcode!==null&&country=="United Kingdom"){pcode=pcode.split(" ")[0];GDownloadUrl("/sexmap/"+pcode.toLowerCase()+".json",function(data,responseCode){var towndat=eval("("+data+")");if(towndat.length>0){if(town!=towndat[0].T){callback([[town+" (nr "+towndat[0].T+")",towndat[0].TU]])}else{callback([[towndat[0].T,towndat[0].TU]])}}else{callback(null)}})}}})}})}function sform(){$("#addyourplace").attr("src","addyourplace.cfm");$("#navtab1").removeClass("active");$("#navtab2").removeClass("active");$("#navtab3").removeClass("active");$("#navtab4").addClass("active");$("#tab1").hide();$("#tab2").hide();$("#tab3").hide();$("#tab4").show()}return{initialise:function(canvas,mapinfo){if(GBrowserIsCompatible()){initmap(canvas,mapinfo);addtileoverlay(mapinfo)}},findnearest:function(placename,callback){findnr(placename,callback)},showfeedbackform:function(){sform()}}}();function findValue(a){if(a!=null){if(!!a.extra){var b=a.extra[0]}else{var b=a.selectValue}if(b!=undefined){document.location.href="/sexmap/"+b+".html"}}}function selectItem(a){findValue(a)}function formatItem(a){return a[0]}function lookupAjax(){var a=$("#searchbox")[0].autocompleter;a.findValue();return false}function setupTabs(){$("#searchbox").autocomplete("listener.cfm",{delay:10,minChars:3,matchSubset:true,matchContains:0,cacheLength:10,onItemSelect:selectItem,onFindValue:findValue,formatItem:formatItem,autoFill:true,namefinder:thesexmap.findnearest})};