/*
JavaScript for taxa list generation
Author: James Nicol, Glossopteris Web Designs, www.glossopteris.com, December 2005.
*/


/*----- Register behaviours -----*/
var taxa_list_rules = {

	'.taxa_list a' : function(el) {
		if(el.parentNode.className!='species'){
			el.onclick = function() { get_sub_list(this, 'all'); return false; } 
		}
	}

	
}

var element_id = '';

/*----- Ajax command to generate and insert taxa sub list -----*/
function get_sub_list(element, extent) {

	//Initialise function variables
	var current_field = element.parentNode.className;
	var current_value = element.firstChild.nodeValue;
	element_id = current_value+'_submenu';
	var parent_value = element.parentNode.parentNode.parentNode.firstChild.nodeValue;
	if(parent_value==null){ //Test for case where submenus have different DOM trees
		parent_value = element.parentNode.parentNode.parentNode.firstChild.firstChild.nodeValue;
	}

	//Check to see if node has already been created, apply open or close functions instead.
	if($(element_id)) { 
		if($(element_id).style.display == 'none'){
			show_sub_list(element); return false;
		}else{
			close_sub_list(element); return false;
		}
	}

	//Create node for sub menu
	var sub_menu = document.createElement('ul');
	element.parentNode.appendChild(sub_menu);
	sub_menu.setAttribute('id',current_value+'_submenu');
	sub_menu.className = 'taxa_list';
	sub_menu.style.display = 'none';
	element.style.background = 'url('+domainroot+'images/minus_icon.gif) no-repeat left';

	//Use Prototype Ajax object to populate node with sub list
	var url = siteroot+'ajax/checklist';
	var pars = '/' + extent +'/'+region+'/' + parent_value.substring(0,parent_value.length-1) + '_' + current_value + '/yes/' + current_field +'/'+use;
	var myAjax = new Ajax.Updater(element_id,url+pars,{method:'get', onComplete: showResponse});
	
}


function showResponse(original_request){

	$(element_id).innerHTML = original_request.responseText;
	
	var links = $(element_id).getElementsByTagName("a");
	for (var i=0; i<links.length; i++){
		if(links[i].parentNode.className!='species'){
			links[i].onclick = function() { get_sub_list(this, 'all'); return false; } 
		}
	}
	new Effect.BlindDown(element_id);

}

function close_sub_list(element) {
	
	var current_value = element.firstChild.nodeValue;
	element.style.background = 'url('+domainroot+'images/plus_icon.gif) no-repeat left';
	new Effect.BlindUp(current_value+'_submenu');

}

function show_sub_list(element) {
	
	var current_value = element.firstChild.nodeValue;
	element.style.background = 'url('+domainroot+'images/minus_icon.gif) no-repeat left';
	new Effect.BlindDown(current_value+'_submenu');

}


Behaviour.register(taxa_list_rules);

