var geocoder;
var map;
var clusterManager;

$('document').ready(function(){
	
	//create map
	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById('map'));
		map.setCenter(new GLatLng(54.5, -4.2), 6);
		map.addControl(new GLargeMapControl());
		geocoder = new GClientGeocoder();
		geocoder.setBaseCountryCode('uk');
		clusterManager = new Clusterer(map);
	}
	
	//add functionality to map search form
	$('.mapsearch .submit').click(function(){
		var address = $('.mapsearch input').val();
		processAddress(address);
		return false;
	});
	$('.mapsearch input').keypress(function(e){
		if(e.which == 13) {
		var address = $('.mapsearch input').val();
		processAddress(address);	
		return false;
		}
	});
	
	//change text on search form
	//$('.mapsearch label').empty().append('Type your post code, address, or town in the box below to search for your nearest youth council.');
	$('.mapsearch').append('<p>Alternatively, explore the map by dragging it around and using the zoom tools.</p>');
	
	//bring in xml and add markers to the map
	parsexml('mapmarkers.php'); //mapmarkers.xml

	//add rounded corners
	$("#map").append('<img src="images/backgrounds/corners_grey.png" class="corner_tl" /><img src="images/backgrounds/corners_grey.png" class="corner_tr" /><img src="images/backgrounds/corners_grey.png" class="corner_bl" /><img src="images/backgrounds/corners_grey.png" class="corner_br" />');


});

//Uses Google Geocoding to get lat and long from the entered address - then center the on these coordinates
function processAddress(address) {
	geocoder.getLatLng(address,function(point) {
		if (!point) {
			alert(address + " not found");
		} else {
			map.setCenter(point, 11);
		} 
	});
}

//adds a cluster marker to the map
function addClusteredMarker(lat,lon,title,html){
	var point = new GLatLng(lat,lon);
	var marker = new GMarker(point);
	var clusteredMarker = clusterManager.AddMarker(marker, title);
	GEvent.addListener(marker,"click",function(){
		marker.openInfoWindowHtml(html)
	});
}

//loads in the xml, retrives the info and puts markers on the map.
function parsexml(url) {
	$.ajax({
		type: "GET",
		url: url,
		dataType: "xml",
		success: function(xml) {
			//iterate through each market
			$(xml).find('bycmarker').each(function(){
				//retrieve the info
				var lat = $(this).attr('lat');
				var lng = $(this).attr('lng');
				var title = $(this).attr('name');
				var addr1 = $(this).attr('addr1');
				var addr2 = $(this).attr('addr2');
				var addr3 = $(this).attr('addr3');
				var city = $(this).attr('city');
				var state = $(this).attr('state');
				var postalcode = $(this).attr('postalcode');
				var phone = $(this).attr('phone');
				var website = $(this).attr('website');
				var email = $(this).attr('email');
				
				//write html for the info window
				var html = '<strong>' + title + '</strong><address>';
				if(addr1) {html += addr1 + '<br />';}
				if(addr2) {html += addr2 + '<br />';}
				if(addr3) {html += addr3 + '<br />';}
				if(city) {html += city  + '<br />';}
				if(state) {html += state  + '<br />';}
				if(postalcode) {html += postalcode;}
				html += '</address>';
				if(phone) {html += '<p>' + phone + '</p>';}
				if(website) {html += '<p><a href="'+website+'" target="_blank">' + website + '</a></p>';}
				if(email) {html += '<p><a href="mailto:'+email+'">' + email + '</p>';}
				
				//add marker to the map
				addClusteredMarker(lat,lng,title,html);
			}); 
		}
	});	
}