// create map object
var map;
// create geocoder object
var geocoder;
// current region
// check the official documentation to change region
// http://code.google.com/apis/maps/documentation/v3/services.html#CountryCodes
// requires a ccTLD (county code top-level domain)
// for a list of ccTLD's visit: http://en.wikipedia.org/wiki/CcTLD
var region = 'nl';
// array of all markers on Map
var markers = new Array();

// on dom ready
$(document).ready(function(){
	// http://maps.google.nl/maps?f=q&source=s_q&hl=nl&geocode=&q=nederland&sll=53.055051,4.797088&sspn=0.002886,0.006539&ie=UTF8&hq=&hnear=Nederland&ll=52.224435,6.141357&spn=3.011744,6.696167&z=8
	// display map by default
	// if the map_canvas id exists
	if($('#map_canvas').length) {
		// set default lat/lng
		var lat = 53.055051;
		var lng = 4.797088;

		// create new geocoder object
		geocoder = new google.maps.Geocoder();
		// create new lat/long object
		var latlng = new google.maps.LatLng(lat,lng);

		// set map options
		var myOptions = {
			zoom: 6,
			center: latlng,
			mapTypeId: google.maps.MapTypeId.ROADMAP
		};
		// display map
		map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
		// show the map div
		$('#map_canvas').show();
	}


	// if the store locator form is on the page
	if($('#store_locator').length) {
		// take over submit action
		$('#store_locator').submit(function(){
			// get address
			var address = $('#address').val();
			var distance = $('#distance').val();

			// do lookup if address not empty
			if(address != '') {
				address_lookup(address, distance, region);
			} else {
				$('#ajax_msg').html("<ul class='flash_bad'><li>Het adres is niet bij ons bekend, probeer een goed adres in te vullen.</li></ul>");
			}
			
		return false;
		});
	}

	// display map on Add page
	if($('#add #map_canvas').length) {
		// set default lat/lng
		var lat = 53.055051;
		var lng = 4.797088;

		// get pre-populated value and focus map
		var display_marker = false;
		if($('#latitude').length) {
			val = $('#latitude').val()*1;
			if(val != '' && !isNaN(val)) {
				lat = val;
				display_marker = true;
			}
		}
		if($('#longitude').length) {
			val = $('#longitude').val()*1;
			if(val != '' && !isNaN(val)) {
				lng = val;
			}
		}

		// create new geocoder object
		geocoder = new google.maps.Geocoder();

		// create new lat/long object
		var latlng = new google.maps.LatLng(lat,lng);
		// set map options
		var myOptions = {
			zoom: 8,
			center: latlng,
			mapTypeId: google.maps.MapTypeId.ROADMAP
		};
		// display map
		map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

		if(display_marker) {
			// create a map marker
			var marker = new google.maps.Marker({
				map: map,
				position: latlng
			});
		}
	}

	// if store add form is on the page
	if($('#add #address').length) {
		// add an onBlur event
		$('#add #address').blur(function(){
			// get the entered address
			var address = $(this).val();
			// if address is not empty
			if(address != '') {
				// do the address lookup
				get_address(address,region);
			}
		});
	}
});


/**
 * Get a single address
 * @param string address
 * @param string region
 */
function get_address(address, region) {
	// set default region
	if(region==null || region == '') {
		region = 'uk';
	}

	// address not empty
	if(address != '') {
		$('#ajax_msg').html('<p>Loading...</p>');
		// lookup the address
		geocoder.geocode( {'address':address,'region':region}, function(results, status) {
			// if the address was found
			if(status == google.maps.GeocoderStatus.OK) {
				$('#ajax_msg').html('<p>Done</p>');
				// insert lat/long into form
				$('#latitude').val( results[0].geometry.location.lat() );
				$('#longitude').val( results[0].geometry.location.lng() );

				// set zoom option
				map.setZoom(10);
				// center the map on the new location
				map.setCenter(results[0].geometry.location);

				// create a map marker

			} else {
				// display error
				$('#ajax_msg').html('<p>Geocoder failed to retrieve address: '+status+'</p>');
			}
		});
	}
}


/**
 * Lookup an address
 * @param string address
 * @param int distance
 * @param string region
 */
function address_lookup(address,distance,region) {
	// set default region
	if(region==null || region == '') {
		region = 'uk';
	}

	// address not empty
	if(address != '') {
		// show ajax loading image
		$('#map_canvas').html("<img src='./imgs/ajax-loader.gif' alt='Ajax Loading Image' />").show();
		$('#ajax_msg').hide();
		// create new geocoder object
		geocoder = new google.maps.Geocoder();
		// lookup the address
		geocoder.geocode( {'address':address,'region':region}, function(results, status) {
			// if the address was found
			if(status == google.maps.GeocoderStatus.OK) {
				// get lat/lng
				var lat = results[0].geometry.location.lat();
				var lng = results[0].geometry.location.lng();
				var location = results[0].geometry.location;

				// set map options
				var myOptions = {
					zoom: 11,
					center: location,
					mapTypeId: google.maps.MapTypeId.ROADMAP
				};
				// display map
				map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

				// create a map marker
				//var marker = new google.maps.Marker({
				//	map: map,
				//	position: results[0].geometry.location,
				//	title:'Your entered address'
				//});

				// clear all markers
				jQuery.each(markers,function(k,v){
					v.setMap(null);
				});

				// do ajax request to find nearby stores
				$.ajax({
					type:"POST",
					url:$('#store_locator').attr('action'),
					data:"ajax=1&action=get_nearby_stores&distance="+distance+"&lat="+lat+"&lng="+lng,
					success:function(msg) {
						// parse the JSON result
						var results = JSON.parse(msg);

						// if request successful
						if( results.success ) {
							// loop through stores and display marker
							jQuery.each( results.stores,function(k,v){
								var marker = new google.maps.Marker({
									map: map,
									position: new google.maps.LatLng(v.lat,v.lng),
									title: v.name+' - '+v.description
								});

								// add to markers
								markers.push(marker);

								// build content string
								var content_string = build_content_string(v);

								// create new info window
								var infowindow = new google.maps.InfoWindow({
									maxWidth: "400",
									content: content_string
								});

								// attach popup to click event
								google.maps.event.addListener(marker, 'click', function() {
									infowindow.open(map,marker);
								});

							} );
							$('#ajax_msg').html("<p class='flash_good'>"+results.stores.length+" winkel(s) gevonden.</p>").fadeIn();
						} else {
							// display error message
							$('#ajax_msg').html("<p class='flash_bad'>"+results.msg+"</p>").fadeIn();
						}
					}
				});
			}
		});
	}
}


/**
 * Builds the HTML for the popup marker window
 * @param object v
 * @return string
 */
function build_content_string(v) {
	// build content string
	var content_string = "<div class='maps_popup'>";

	// include image
	if(v.img != '') {
		content_string += "<img class='img' src='"+v.img+"' alt='Store Image' />";
	}

	// include title & address
	content_string += "<h1>"+v.name+"</h1><h2>"+v.address+"</h2>";

	// include additional info
	if(v.telephone != '') {
		content_string += "<p class='tel'>Telefoon: <strong>"+v.telephone+"</strong></p>";
	}
	if(v.email != '') {
		content_string += "<p class='email'>E-mail: <a href='mailto:"+v.email+"'>"+v.email+"</a></p>";
	}
	if(v.website != '') {
		content_string += "<p class='web'>Website: <a target='_blanc' href='"+v.website+"'>"+v.website+"</a></p>";
	}
	if(v.description != '') {
		content_string += "<p class='desc'><br />"+v.description+"</p>";
	}
	content_string += "</div>";

return content_string;
}
