
$(function(){
$(window).bind('unload',function(){
if(typeof(GUnload)!="undefined"){ //ie doesnt load the google stuff in interact and causes errors
GUnload();
}
});
});


(function(){ //closure
var map;
var geocoder;

function load(){
if (typeof(GBrowserIsCompatible)!="undefined" && GBrowserIsCompatible()){
geocoder = new GClientGeocoder();
map = new GMap2(document.getElementById('map1'));
map.addControl(new GSmallMapControl());
map.addControl(new GMapTypeControl());
map.setCenter(new GLatLng(40, -100), 4);
showAllLocations();
}
}

function showAllLocations(){
searchLocationsNear();
}

 function searchLocations(){
 var address = document.getElementById('addressInput1').value;
 if(!address){ //no address entered
 showAllLocations();
 return;
 }
 geocoder.getLatLng(address, function(latlng){
 if (!latlng){
 alert(address + ' not found');
 } else {
 searchLocationsNear(latlng);
 }
 });
 }

 function searchLocationsNear(center){
 var radius = document.getElementById('radiusSelect1').value;
 var searchUrl;
 if(center){
 searchUrl = '/cirkuit/cms/include/contentTypes/StoreLocator/lookup-locations.xml?StoreLocatorId=1&limit-results=100&latitude='+center.lat()+'&longitude='+center.lng()+'&distance='+radius;
 }
 else{
 searchUrl = '/cirkuit/cms/include/contentTypes/StoreLocator/lookup-locations.xml?StoreLocatorId=1&limit-results=100&list-all=1'; 
 }
 GDownloadUrl(searchUrl, function(data){
 var xml = GXml.parse(data);
 var markers = xml.documentElement.getElementsByTagName('marker');
 map.clearOverlays();

 var sidebar = document.getElementById('sidebar1');
 sidebar.innerHTML = '';
 if (markers.length == 0){
 sidebar.innerHTML = 'No results found.';
 map.setCenter(new GLatLng(40, -100), 4);
 return;
 }

 var bounds = new GLatLngBounds();
 for (var i = 0; i < markers.length; i++){
 var name = markers[i].getAttribute('name');
 var phone = markers[i].getAttribute('phone');
 var website = markers[i].getAttribute('website');
 if(website)website = '<a href="'+website+'" target="_blank">'+website+'</a>';
 var address = markers[i].getAttribute('address');
 var distance = parseFloat(markers[i].getAttribute('distance'));
 if(isNaN(distance))distance=null;
 var point = new GLatLng(parseFloat(markers[i].getAttribute('lat')),
 parseFloat(markers[i].getAttribute('lng')));
 
 var marker = createMarker(point, name, address, phone, website);
 map.addOverlay(marker);
 var sidebarEntry = createSidebarEntry(marker, name, address, distance, phone, website);
 sidebar.appendChild(sidebarEntry);
 bounds.extend(point);
 }
 var zoomLevel =map.getBoundsZoomLevel(bounds);
 if(zoomLevel > 13)zoomLevel = 13; //dont zoom in too far
 map.setCenter(bounds.getCenter(), zoomLevel);
 });
 }

function createMarker(point, name, address, phone, website){
var marker = new GMarker(point);
var html = '<div class="StoreLocator_name">' + name + '</div> <div class="StoreLocator_address">' + address + '</div>';
if(phone){
html += '<div class="StoreLocator_phone">' + phone + '</div>'
}
if(website){
html += '<div class="StoreLocator_website">' + website + '</div>'
}
GEvent.addListener(marker, 'click', function(){
marker.openInfoWindowHtml(html);
});
return marker;
}

function createSidebarEntry(marker, name, address, distance, phone, website){
var div = document.createElement('div');
var distanceTxt = "";
if(distance){ 
distanceTxt = '<span class="StoreLocator_distance">(' + distance.toFixed(1)+ ' miles)</span>';
}
var html = '<div class="StoreLocator_name">' + name + ' ' + distanceTxt + '</div> <div class="StoreLocator_address">' + address + '</div>';
if(phone){
html += '<div class="StoreLocator_phone">' + phone + '</div>'
}
if(website){
html += '<div class="StoreLocator_website">' + website + '</div>'
}
div.innerHTML = html;
div.className= 'StoreLocator_locationContainer';
div.title= 'Click to view on map...';
GEvent.addDomListener(div, 'click', function(){
GEvent.trigger(marker, 'click');
});
return div;
}

$(function(){
$(window).bind('load',load);

$('#StoreLocator1 .StoreLocator_showAllLocations').click(function(){
if(typeof(GUnload)!="undefined"){ //ie doesnt load the google stuff in interact and causes errors
showAllLocations();
}
return false;
});

$("#StoreLocator1 form.StoreLocator_inputForm").submit(function(){
if(typeof(GUnload)!="undefined"){ //ie doesnt load the google stuff in interact and causes errors
searchLocations();
}
return false;
});
});
})();


