// Google map JS used to find a local shop on the map
// Gaetan Priour, 2009

var map;
var geocoder;

function load() {
  if (GBrowserIsCompatible()) {
    geocoder = new GClientGeocoder();
    map = new GMap2(document.getElementById('map'));
    map.addControl(new GSmallMapControl());
    map.addControl(new GMapTypeControl());
    map.setCenter(new GLatLng(53.915124,-4.08545), 5); // Center on England
  }
}

function searchShops() {
 var address = document.getElementById('location').value;
 geocoder.getLatLng(address + ', UK', function(latlng) {
   if (!latlng) {
     alert(address + ' not found');
   } else {
     searchLocationsNear(latlng);
   }
 });
}

function searchLocationsNear(center) {
 var radius = document.getElementById('radius').value;
 var searchUrl = '/static/search_xml.php?lat=' + center.lat() + '&lng=' + center.lng() + '&radius=' + radius;
 GDownloadUrl(searchUrl, function(data) {
   var xml = GXml.parse(data);
   var markers = xml.documentElement.getElementsByTagName('marker');
   map.clearOverlays();

	var listing = document.getElementById('shop-listings');
	listing.innerHTML = '';
	if (markers.length == 0) {
		listing.innerHTML = 'No results found.';
		map.setCenter(new GLatLng(53.915124,-4.08545), 5); // Center on England
		return;
	}

   var bounds = new GLatLngBounds();
   for (var i = 0; i < markers.length; i++) {
     var shopid = markers[i].getAttribute('shopid');
     var name = markers[i].getAttribute('name');
     var address = markers[i].getAttribute('address');
     var town = markers[i].getAttribute('town');
     var county = markers[i].getAttribute('county');
     var country = markers[i].getAttribute('country');
     var postcode = markers[i].getAttribute('postcode');
     var phone = markers[i].getAttribute('phone');
     var website = markers[i].getAttribute('website');
     var distance = parseFloat(markers[i].getAttribute('distance'));
     var point = new GLatLng(parseFloat(markers[i].getAttribute('lat')),
                             parseFloat(markers[i].getAttribute('lng')));
     
     var marker = createMarker(point, name, shopid, distance);
     map.addOverlay(marker);
     var listingEntry = createListingEntry(marker, name, shopid, distance, address, town, county, country, postcode, phone, website);
     listing.appendChild(listingEntry);
     bounds.extend(point);
   }
   map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds));
 });
}

function createMarker(point, name, shopid, distance) {
  var marker = new GMarker(point);
  var html = '<div style="line-height: 1.5em"><b>' + name + '</b><br />' + distance.toFixed(1) + ' miles away<br /><p><a href="http://www.thecyclingexperts.co.uk/find-a-cycle-shop/?shop=' + shopid + '" target="_blank">View shop details &raquo;</a></p><p><small>(link will open in a new window)</small></p></div>';
  GEvent.addListener(marker, 'click', function() {
    marker.openInfoWindowHtml(html);
  });
  return marker;
}

function createListingEntry(marker, name, shopid, distance, address, town, county, country, postcode, phone, website) {
	var div = document.createElement('div');
	
	var html = '<h3 class="header-link"><a href="/find-a-cycle-shop/?shop=' + shopid + '" target="_blank">' + name + '</a></h3>'
		+ '<div id="shop-address" class="clearfix">'
		+ '<h4 id="header-contact-details"><span>Contact Details</span></h4>'
		+ '<a class="view-shop small-link" href="/find-a-cycle-shop/?shop=' + shopid + '" target="_blank">View Full Listing for this shop</a>'
		+ '<dl id="contact-info">'
		+ '<dt>Distance:</dt><dd>' + distance.toFixed(1) + ' miles</dd>'
		+ '<dt>Address:</dt><dd>' + address + '</dd>'
		+ '<dt>Town / City:</dt><dd>' + town + '</dd>'
		+ '<dt>County:</dt><dd>' + county + '</dd>'
		+ '<dt>Postcode:</dt><dd>' + postcode + '</dd>'
		+ '<dt>Telephone:</dt><dd>' + phone + '</dd>'
		+ '<dt>Website:</dt><dd><a href="http://' + website + '">' + website + '</a></dd>'
		+ '</dl>'
		+ '</div>'
		+ '<span class="divider"></span>';

	div.innerHTML = html;
	div.style.cursor = 'pointer';
	div.style.marginBottom = '5px';
	GEvent.addDomListener(div, 'click', function() {
		GEvent.trigger(marker, 'click');
	});
	GEvent.addDomListener(div, 'mouseover', function() {
		div.style.backgroundColor = '#eee';
	});
	GEvent.addDomListener(div, 'mouseout', function() {
		div.style.backgroundColor = '#fff';
	});
	return div;
}

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

function addUnLoadEvent(func) {
	var oldonunload = window.onunload;
	if (typeof window.onunload != 'function') {
	  window.onunload = func;
	} else {
	  window.onunload = function() {
	    oldonunload();
	    func();
	  }
	}
}

addLoadEvent(load);
addUnLoadEvent(GUnload);

