function explain(topic)
{
	$.get('http://register.notabletrees.org.nz/ajax/explain/'+topic, function(response) {
		alert(response);
	});
	return false;
}

function genusChanged(sourceElement, speciesControlId)
{
	var sourceElement = $(sourceElement);
	var select = $('#' + speciesControlId);
	var val = select.val();
	select.find('*').remove();
	$('<option value="">( not known or specified )</option>').appendTo(select);
	
	if (sourceElement.val() != '') {
		var ajaxURL = 'http://register.notabletrees.org.nz/ajax/genus/FetchSpecies/'+sourceElement.val();
		$.getJSON(ajaxURL, function(data)
		{
			for (var i=0; i<data.length; i++)
			{
				$('<option value="' + data[i] + '">'+data[i]+'</option>').appendTo(select);
			}
			select.val(val);
		});
	}
}

function geocodeAddress(address, latControl, lngControl)
{
	var geocoder = new GClientGeocoder();

	address = address.replace('[not specified]','');
	address = address.replace('Auckland - Coromandel','Auckland');
	address = address.replace('Wellington - Wairarapa','Wellington');
	address = address.replace(', ,',',');

	geocoder.getLatLng(address, function(point)
	{
		if (!point)
		{
			alert('Could not find a location for "'+address+'".  Please check the address or place the marker manually.')
		}
		else
		{
			latControl.val(point.lat());
			lngControl.val(point.lng());

			// Center the map on the point
			initialize(point.lat(),point.lng());
			alert('A location has been found for "'+address+'" - the map and marker will now be centred on the location.  Please check the accuracy of this location.  You may click and drag the marker to relocate it.');
		}
	});
}

// Script to make ENTER behave as TAB in forms
var textboxes;
function checkForEnter(event)
{
	if (this.tagName == 'TEXTAREA' || this.getAttribute('allowenter') == 'true')
	{
		// Allow textareas to receive enter keypresses
		return true;
	}

	if (event.keyCode == 13)
	{
		currentBoxNumber = textboxes.index(this);
		if (textboxes[currentBoxNumber + 1] != null)
		{
			nextBox = textboxes[currentBoxNumber + 1]
			nextBox.focus();
			// nextBox.select();
			event.preventDefault();
			return false;
		}
	}
}

jQuery(function() {

	jQuery.getJSON('http://register.notabletrees.org.nz/ajax/getTopicList', function(validTopics){

		jQuery('*[whatsThisTopic]').each(function(){
			var topic = this.getAttribute('whatsThisTopic');
			var elem = jQuery(this);
			if ( validTopics.indexOf(topic) != -1 )
			{
				elem.html( elem.html() + '<span class="whatsthis"><a onclick="return explain(\''+topic+'\');" href="#">what\'s this?</a></span>' );
			}
		});

	}, 'json' );
	
	// Bind any alert icons
	jQuery( '.mini-notification' ).click(function(elem){

		jQuery( '.notification' ).fadeOut( 'fast' );	
		var notification = jQuery( '#' + this.id.replace( 'link-', '' ) );
		notification
			.css( 'top', jQuery( this ).position().top )
			.css( 'left', jQuery( this ).position().left )
			.css( 'position', 'absolute' )
			.fadeIn( 'fast' );
			
		jQuery( 'body, a.thickbox' ).click(function(){
			notification.fadeOut();
		});
			
		return false;
	
	});
	
});

function showWhatsThisTags()
{
	var elems = jQuery('*[whatsThisTopic]');
	elems.each(function(){
		var topic = this.getAttribute('whatsThisTopic');
		var elem = jQuery(this);
		elem.html( elem.html() + '<button>'+topic+'</buton>' );
	});
	alert( elems.length + ' buttons (that do nothing when you click them) showing the names to use for each "What\'s This?" tag have been added to the page.');	
	return false;
}


jQuery(function($){
	textboxes = $('input, select, textarea');
	if ($.browser.mozilla) {
		$(textboxes).keypress (checkForEnter);
	} else {
		$(textboxes).keydown (checkForEnter);
	}
});

/*
 * For form change indication - can use this to highlight any changes to form fields
 *
	jQuery('input, textarea, select').bind('blur',function()
	{

		if (this.value != this.defaultValue)
			jQuery(this).addClass('input-changed');
		else
			jQuery(this).removeClass('input-changed');
	});
*/

//Display appropriate fields given the Group Species currently selected
jQuery(function($) {

	//Initialize events on the Group of Tree Species select
	var selectTreeGroup = $('select[name="tree_group_species"]');
	if (selectTreeGroup.size()>0)
	{
		updateSpeciesDisplay();
		selectTreeGroup.change(updateSpeciesDisplay);
	}
	
	//Load the Species drop down content according to genus selection
	loadSpeciesContent('feature_tree_genus', 'feature_tree_species');
	loadSpeciesContent('additional_feature_tree_genus', 'additional_feature_tree_species');
	loadSpeciesContent('genus', 'species');
	
	// Hide or Show the additional fearture tree content on the tree group view
	function updateSpeciesDisplay()
	{
		if (selectTreeGroup.val() == 1) //Single Species
		{
			$('.mixed_species_group').hide();
			$('select[name="additional_feature_tree_genus"]').val('');
			genusChanged( $('select[name="additional_feature_tree_genus"]'), 'additional_feature_tree_species');
		}
		else //Mixed Species
		{
			$('.mixed_species_group').show();
		}
	}
	
	function loadSpeciesContent(genus, species) {
		genusSel = $('select[name="' + genus + '"]');
		speciesSel = $('select[name="' + species + '"]');
		if (genusSel.size() > 0)
		{
			genusChanged(genusSel, species);
		}
	}
	
});

// Control the apperance of the reCaptcha input field
// Acceptable value: red (default), clean, white, blackglass
var RecaptchaOptions = {theme: 'white'};

