var map;
var geocoder;

//Start position: Belgium
var defaultLtt	= 50.502946;
var defaultLng	= 4.251709;
var defaultZoom	= 13;

function loadGMap() {
	if (GBrowserIsCompatible()) {
		geocoder = new GClientGeocoder();
		map = new GMap2(document.getElementById('map'));
		map.addControl(new GSmallMapControl());
		map.addControl(new GMapTypeControl());
		map.setCenter(new GLatLng(defaultLtt, defaultLng), defaultZoom);
	}
}

function searchLocations(address, countrycode) {
 //var address = document.getElementById('addressInput').value;
 geocoder.getLatLng(address, function(latlng) {
	 if (!latlng) {
		// alert(address + ' not found');
	 } else {
		 searchLocationsNear(latlng, countrycode);
	 }
 });
}

function updateLocation(id, address, shipping) {
 geocoder.getLatLng(address, function(latlng) {
	 if (!latlng) {
		// trace(ctr + ' ' + address + ' not found');
		// ctr++;
	 } else {
		if (shipping) {
			var searchUrl = 'ajax/dealerxml.asp?ltt=' + latlng.lat() + '&lng=' + latlng.lng() + '&id=' + id + '&shipping=1';
		} else {
			var searchUrl = 'ajax/dealerxml.asp?ltt=' + latlng.lat() + '&lng=' + latlng.lng() + '&id=' + id;
		}
		GDownloadUrl(searchUrl, function(data) {});
	 }
 });
}

function updateLocationUser(id, address, shipping) {
 geocoder.getLatLng(address, function(latlng) {
	 if (!latlng) {
		// trace(ctr + ' ' + address + ' not found');
		// ctr++;
	 } else {
		var searchUrl = 'ajax/dealerxml.asp?ltt=' + latlng.lat() + '&lng=' + latlng.lng() + '&uid=' + id;
		GDownloadUrl(searchUrl, function(data) {});
	 }
 });
}

function searchLocationsNear(center, countrycode) {
 //var radius = 20; //document.getElementById('radiusSelect').value;
 var searchUrl = 'ajax/dealerxml.asp?ltt=' + center.lat() + '&lng=' + center.lng() + '&cc='+countrycode; // + '&gKey=' + GMapKey + '&radius=' + radius;
 
 GDownloadUrl(searchUrl, function(data) {
	 var xml = GXml.parse(data);
	 var markers = xml.getElementsByTagName('marker'); //xml.selectNodes('/markers/marker'); //
	 map.clearOverlays();
	
	 var sidebar = document.getElementById('sidebar');
	 //sidebar.innerHTML = '';
	 if (markers.length == 0) {
		 //sidebar.innerHTML = 'No results found.';
		 map.setCenter(center, defaultZoom);
			var tr = document.createElement('tr');
			var td = document.createElement('th');
				td.setAttribute('colspan', '6');
				td.innerHTML = xml.getElementsByTagName('error')[0].firstChild.nodeValue;
			tr.appendChild(td);
			sidebar.appendChild(tr);
		 return;
	 }

	 var bounds = new GLatLngBounds();
	 for (var i = 0; i < markers.length; i++) {
		 var name = markers[i].childNodes[0].firstChild.nodeValue;
		 var address = markers[i].childNodes[1].firstChild.nodeValue;
		 var phone = markers[i].childNodes[2].firstChild.nodeValue;
		 var fax = markers[i].childNodes[3].firstChild.nodeValue;
		 var email = markers[i].childNodes[4].firstChild.nodeValue;
		 var website = markers[i].childNodes[5].firstChild.nodeValue;
		 var distance = parseFloat(markers[i].childNodes[8].firstChild.nodeValue);
		 var point = new GLatLng(parseFloat(markers[i].childNodes[6].firstChild.nodeValue), parseFloat(markers[i].childNodes[7].firstChild.nodeValue));
		 var part = markers[i].childNodes[9].firstChild.nodeValue;
		 var prof = markers[i].childNodes[10].firstChild.nodeValue;
		 var rent = markers[i].childNodes[11].firstChild.nodeValue;
		 
		 var marker = createMarker(point, name, address);
		 map.addOverlay(marker);
		 
		 var sidebarEntry = createSidebarEntry({ name: name, address: address, distance: distance, phone: phone, fax: fax, email: email, website: website, part: part, prof: prof, rent: rent }, marker, i%2);
		 sidebar.appendChild(sidebarEntry);
		 if (markers[i].childNodes[12].firstChild.nodeValue != '1' || (markers.length == 1 && markers[i].childNodes[12].firstChild.nodeValue == '1')) {
			bounds.extend(point);
		 }
	 }
	 map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds));
 });
}

function createMarker(point, name, address) {
	var marker = new GMarker(point);
	var html = '<b>' + name + '</b> <br/>' + address;
	GEvent.addListener(marker, 'click', function() {
		marker.openInfoWindowHtml(html);
	});
	return marker;
}

function createSidebarEntry(data, marker, odd) {
	var tr = document.createElement('tr');
		tr.className = 'tbody'+odd;
		
	var td1 = document.createElement('th');
		td1.innerHTML = data.name;
		td1.style.cursor = 'pointer';
		GEvent.addDomListener(td1, 'click', function() {
			GEvent.trigger(marker, 'click');
		});
	var td2 = document.createElement('td');
		td2.innerHTML = data.address;
	var td3 = document.createElement('td');
		td3.innerHTML = data.phone + ((data.fax) ? '<br />' + data.fax: '');
	var td4 = document.createElement('td');
		td4.innerHTML = (data.email ? '<a href=\'mailto:' + data.email + '\'>' + data.email + '</a>' : '');
	var td5 = document.createElement('td');
		var prefix = '';
		if (data.website.substring(0,7) != 'http://') { prefix = 'http://'; }
		td5.innerHTML = (data.website ? '<a href=\'' + prefix + data.website + '\' target=\'_blank\'>' + data.website + '</a>' : '');
	var td6 = document.createElement('td');
		td6.setAttribute('width','35px');
		var part = data.part;
		var prof = data.prof;
		var rent = data.rent;
		if (part == '1') {
			var divpart = document.createElement('img');
				divpart.setAttribute('title','Private');
				divpart.setAttribute('alt','Private');
				divpart.setAttribute('src','../images/layout/'+iStyle+'/part.gif');
				//divpart.className = 'dealerstyle_1';
			td6.appendChild(divpart);
		}
		if (prof == '1') {
			var divprof = document.createElement('img');
				divprof.setAttribute('title','Professional');
				divprof.setAttribute('alt','Professional');
				divprof.setAttribute('src','../images/layout/'+iStyle+'/prof.gif');
				//divprof.className = 'dealerstyle_3';
			td6.appendChild(divprof);
		}
		if (rent == '1') {
			var divrent = document.createElement('img');
				divrent.setAttribute('title','Rental');
				divrent.setAttribute('alt','Rental');
				divrent.setAttribute('src','../images/layout/'+iStyle+'/rent.gif');
				//divrent.className = 'dealerstyle_2';
			td6.appendChild(divrent);
		}
	
	tr.appendChild(td1);
	tr.appendChild(td2);
	tr.appendChild(td3);
	tr.appendChild(td4);
	tr.appendChild(td5);
	tr.appendChild(td6);
	
	return tr;
}

function createSidebarEntry_o(marker, name, address, distance) {
	var div = document.createElement('div');
	var html = '<b>' + name + '</b> (' + distance.toFixed(1) + ')<br/>' + address;
	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;
}

