var map;
var geocoder;
var jwIcon;
var BASE_URL = 'http://jocelynwarner.com'; /* No leading forward slash */


window.onload = function() {
	if (GBrowserIsCompatible()) {
		geocoder = new GClientGeocoder();
		map = new GMap2(document.getElementById('map'));
		map.addControl(new GLargeMapControl3D());
		map.setCenter(new GLatLng(54.6, -4), 5);
	}
	jwIcon = new GIcon();
	jwIcon.image = BASE_URL + '/images/markers/image.png';
	jwIcon.printImage = BASE_URL + '/images/markers/printImage.gif';
	jwIcon.mozPrintImage = BASE_URL + '/images/markers/mozPrintImage.gif';
	jwIcon.iconSize = new GSize(38, 46);
	jwIcon.shadow = BASE_URL + '/images/markers/shadow.png';
	jwIcon.transparent = BASE_URL + '/images/markers/transparent.png';
	jwIcon.shadowSize = new GSize(61, 46);
	jwIcon.printShadow = BASE_URL + '/images/markers/printShadow.gif';
	jwIcon.iconAnchor = new GPoint(19, 46);
	jwIcon.infoWindowAnchor = new GPoint(19, 0);
	jwIcon.imageMap = [32, 0, 33, 1, 34, 2, 35, 3, 36, 4, 36, 5, 36, 6, 37, 7, 37, 8, 37, 9, 37, 10, 37, 11, 37, 12, 37, 13, 37, 14, 37, 15, 37, 16, 37, 17, 37, 18, 37, 19, 37, 20, 37, 21, 37, 22, 37, 23, 37, 24, 37, 25, 37, 26, 37, 27, 37, 28, 37, 29, 36, 30, 36, 31, 36, 32, 35, 33, 34, 34, 33, 35, 32, 36, 29, 37, 25, 38, 24, 39, 23, 40, 22, 41, 21, 42, 20, 43, 19, 44, 18, 45, 18, 45, 17, 44, 16, 43, 15, 42, 14, 41, 13, 40, 12, 39, 11, 38, 8, 37, 4, 36, 3, 35, 2, 34, 1, 33, 0, 32, 0, 31, 0, 30, 0, 29, 0, 28, 0, 27, 0, 26, 0, 25, 0, 24, 0, 23, 0, 22, 0, 21, 0, 20, 0, 19, 0, 18, 0, 17, 0, 16, 0, 15, 0, 14, 0, 13, 0, 12, 0, 11, 0, 10, 0, 9, 0, 8, 0, 7, 0, 6, 0, 5, 1, 4, 1, 3, 2, 2, 3, 1, 5, 0];
};

function createMarker(point, name, address, telephone, products) {
	var marker = new GMarker(point, {icon: jwIcon});
	var html = '<h2>' + name + '</h2><p class="mapproducts">' + products + '</p><p>' + telephone + '</p><h1>' + address + '</h1>';
	GEvent.addListener(marker, 'click', function () {
		marker.openInfoWindowHtml(html);
	});
	return marker;
}

function createresultsEntry(marker, name, address, telephone, website, products, distance) {
	
	var li = document.createElement('li');
	var className = document.createAttribute('class');
	className.nodeValue = 'stockist';
	li.setAttributeNode(className);

	var title = document.createElement('h2');
	title.innerHTML = name;
	li.appendChild(title);
	
	var smalldistance = document.createElement('small');
	smalldistance.innerHTML = 'Stocking our: ' + products;
	li.appendChild(smalldistance);		
	
	var paddress = document.createElement('p');
	paddress.innerHTML = address;
	li.appendChild(paddress);		
	
	var ptelephone = document.createElement('p');
	ptelephone.innerHTML = telephone;
	li.appendChild(ptelephone);		
	
	var pwebsite = document.createElement('p');
	var awebsite = document.createElement('a');
	awebsite.innerHTML = website;
	awebsite.href = website;
	awebsite.target = '_blank';
	pwebsite.appendChild(awebsite);
	li.appendChild(pwebsite);
	
	var smalldistance = document.createElement('small');
	smalldistance.innerHTML = 'Distance: ' + distance.toFixed(1) + ' miles';
	li.appendChild(smalldistance);		
	
	GEvent.addDomListener(li, 'click', function () {
		GEvent.trigger(marker, 'click');
	});
	
	return li;
}

function searchLocationsNear(center) {
	var radius = document.getElementById('radius').value;
	var product = document.getElementById('products').value;
	var searchUrl = 'search.php?lat=' + center.lat() + '&lng=' + center.lng() + '&radius=' + radius + '&product=' + product ;
	
	GDownloadUrl(searchUrl, function (json) {
		
		
		
		document.getElementById('resultstitle').innerHTML = 'Stockists near you...';
		var results = document.getElementById('results');
	
		var markers = eval(json);
		
		results.innerHTML = '';
		
		map.clearOverlays();

		if (json == 'null') {
			document.getElementById('resultstitle').innerHTML = 'No results found.';
			map.panTo(new GLatLng(55, -4), 5);
			return;
		}

		var bounds = new GLatLngBounds();
		for (var i = 0; i < markers.length; i++) {
			
			var name = markers[i].name;
			var address = markers[i].address;
			var telephone = markers[i].telephone;
			var website = markers[i].website;
			var products = markers[i].products;
			var distance = parseFloat(markers[i].distance);
			var point = new GLatLng(parseFloat(markers[i].lat), parseFloat(markers[i].lng));

			var marker = createMarker(point, name, address, telephone, products);
			map.addOverlay(marker);
					
			var result = createresultsEntry(marker, name, address, telephone, website, products, distance);
			
			if ((i % 4) == 3) {
				result.className = 'stockist end';
			}
			
			results.appendChild(result);
			
			bounds.extend(point);
		}
	
		map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds)-1);
	
		new Effect.BlindDown('results', {queue: 'end', duration: .5});
	
	});
}

function searchLocations() {
	new Effect.BlindUp('results', {queue: 'end', duration: .5});
	document.getElementById('resultstitle').innerHTML = 'Searching...';
	var address = document.getElementById('postcode').value;
	geocoder.setBaseCountryCode('GB');
	geocoder.getLatLng(address, function (latlng) {
		if (!latlng) {
			alert('Your address could not be found, please enter a town or postcode.');
		} else {
			searchLocationsNear(latlng);
		}
	});
	return false;
}
