//google.load("maps", "2.x");

var map;function initialize(){var loc=$('#location').val();if(loc==''||loc=="Address or Zip Code"){if(navigator.geolocation){navigator.geolocation.getCurrentPosition(function(position){$('#map_canvas').addClass('map');$('#locatorDiv').height("403px");var myLatLng=new google.maps.LatLng(position.coords.latitude,position.coords.longitude);var options={zoom:15,center:myLatLng,mapTypeId:google.maps.MapTypeId.ROADMAP};map=new google.maps.Map(document.getElementById("map_canvas"),options);var geocoder=new google.maps.Geocoder();geocoder.geocode({address:loc,latLng:myLatLng},showLocation);return false;});return false;}}else
showLocations(loc,1);}
function showLocation(response){if(response){for(var i=0;i<response.length;i++){result=response[i];if(result.types[0]=="postal_code")
$('#location').val(result.formatted_address);if(result.types[0]=="street_address")
marker=new google.maps.Marker({position:result.geometry.location,map:map,title:result.formatted_address});}}}
function showPrevNextLocations(pagenumber){var loc=$('#location').val();var backupLoc=$('#backupLocation').val();if(loc==undefined||loc==''||(loc!=''&&loc!=backupLoc)){showLocations(backupLoc,pagenumber);}else{showLocations(loc,pagenumber);}}
function showLocations(location,pagenumber){if(location!=undefined&&location.length>0&&location!="Address or Zip Code"){$('#location').val(location);$('#backupLocation').val(location);$('#locations').empty();$('#map_canvas').addClass('map');$('#pagenumber').val(pagenumber);$('#resultPages').show();$('#locations').show();$('#locatorDiv').height("403px");if(pagenumber>1)
$('#prev').show();else
$('#prev').hide();var geocoder=new google.maps.Geocoder();geocoder.geocode({address:location},function(response){var country="US";var city="";var state="";if(!response[0]){$('#retailers-near').text("");$('#locations').append("<div class=\"result\">Cannot locate "+$('#location').val()+".</div>");$('#next').hide();$('#prev').hide();return;}
for(var i=0;i<response[0].address_components.length;i++){if(response[0].address_components[i].types[0]=="locality")
city=response[0].address_components[i].short_name;if(response[0].address_components[i].types[0]=="administrative_area_level_1")
state=response[0].address_components[i].short_name;if(response[0].address_components[i].types[0]=="country")
country=response[0].address_components[i].short_name;}
$('#retailers-near').text("Retailers near "+city+" "+state+" :");var point=response[0].geometry.location;var options={center:point,mapTypeId:google.maps.MapTypeId.ROADMAP};map=new google.maps.Map(document.getElementById("map_canvas"),options);marker=new google.maps.Marker({position:point,map:map});var url="getlocations.php?latitude="+point.lat()+"&longitude="+point.lng()+"&pagenumber="+pagenumber+"&country="+country;$.getJSON(url,function(data){var latlngbounds=new google.maps.LatLngBounds();results=data["results"];for(var i=0;i<results.length;i++){result=results[i];var point=new google.maps.LatLng(result.geocode_latitude,result.geocode_longitude);latlngbounds.extend(point);addResult(result,point,i,location);}
if(pagenumber*3<parseInt(data["results_count"]))
$('#next').show();else
$('#next').hide();if(results.length>0)
map.fitBounds(latlngbounds);else{map.setZoom(15);$('#locations').append("<div class=\"result\">No results found in your area.</div>");}
if(country=="US"){$('#usSearchOnly').show();}else{$('#usSearchOnly').hide();}});});}}
var infowindow=new google.maps.InfoWindow();function addResult(location,point,index,startAddr){var image=new google.maps.MarkerImage("/images/sub/locator/blue-dot-attends2.png",new google.maps.Size(32,32));var marker=new google.maps.Marker({position:point,map:map,icon:image});google.maps.event.addListener(marker,'click',function(){getInfoWindowEvent(location,marker,index,startAddr);});var locationHTML=getLocationHTML(location);$('#locations').append("<div class=\"result\" id=\"result"+index+"\">"+locationHTML+"</div>");$("#result"+index).click(function(){getInfoWindowEvent(location,marker,index,startAddr);$("#locations div").removeClass("selected");$("#result"+index).addClass("selected");});}
function getInfoWindowEvent(location,marker,index,startAddr){infowindow.close();var daddr=location.geocode_fulladdress;infowindow.setContent("<div class=\"location-overlay\">"+getLocationHTML(location)+"<div class=\"r_directions_link\"><a href=\"http://maps.google.com/maps?saddr="+startAddr+"&daddr="+daddr+"\" target=\"_blank\">Get Directions</a></div></div>");infowindow.open(map,marker);}
function getLocationHTML(location){var locationHTML="<div class=\"r_name\">"+location.name+"</div><div class=\"r_address\">"+location.address1
if(location.address2!="")
locationHTML+=" "+location.address2;locationHTML+="</div><div class=\"r_city\">";if(location.city!="")
locationHTML+=location.city+", ";locationHTML+=location.state+" "+formatPostalCode(location.postal_code)+"</div><div class=\"r_phone\">"+formatPhone(location.phone)+"</div>";return locationHTML}
function getCapitalLetter(index){return String.fromCharCode("A".charCodeAt(0)+index);}
function formatPhone(phone){phone=phone.replace(/\s/gi,'');phone=phone.replace(/-/gi,'');var areacode=phone.substr(0,3);var exchange=phone.substr(3,3);var last4=phone.substr(6,4);return"("+areacode+") "+exchange+"-"+last4;}
function formatPostalCode(postalCode){var regex=new RegExp(/[A-z]/);if(regex.test(postalCode))
return postalCode.substr(0,3)+" "+postalCode.substr(3,3);if(postalCode.length>6)
return postalCode.substr(0,5)+"-"+postalCode.substr(postalCode.length-4,4);return postalCode;}
function clearTooltip(){var loc=$('#location').val();if(loc=="Address or Zip Code"){$('#location').val('');}}
