// get taxonomic info for current species as identified from filename 
// and insert into top of DIV#utilities

/* --------------------------------------------------

!!! - THIS IS VERSION 3 OF THIS SCRIPT - (despite the name...)

inserts content into top of DIV#utilities rather 
than replacing default content in a known DIV ID.

version 1 is still (Jan 2010) in use but can be 
discontinued if those pages already using it are 
ammended to use this one (including removing the 
specids-of-day utility fragment).

-------------------------------------------------- */


$(document).ready(function() {

	// initialise
	var genusRankLevel = 350001 + 0;
	var speciesRankLevel = 250001 + 0;
	var currRankLevel = 0;
	var currURL = window.location.href;


	// identify taxon name string
	
	// ----- OPTION 1 ----- from filename
	/* --------------------
		var taxonNameString = currURL.substring(currURL.lastIndexOf('/') + 1,currURL.lastIndexOf('.'));	
	----------------------- */	
	
	// ----- OPTION 2 ----- from immediate parent directory name
	/* --------------------
	var taxonNameString = currURL.substring(0,currURL.lastIndexOf('/'));
	taxonNameString = taxonNameString.substring(taxonNameString.lastIndexOf('/') + 1);
	----------------------- */	

	// ----- OPTION 3 ----- from immediate child directory of /species-of-the-day/ directory name
	/* --------------------
	var taxonNameString = currURL.substring(currURL.lastIndexOf('/species-of-the-day/') + 20);
	taxonNameString = taxonNameString.substring(0, taxonNameString.indexOf('/'));	
	----------------------- */	
	
	// ----- OPTION 4 ----- from name of directory 2 levels down from /species-of-the-day/ directory 

	var taxonNameString = currURL.substring(currURL.lastIndexOf('/species-of-the-day/') + 20);	// get string after /species-of-the-day/
	
	// !!! TEMP FIX for /evolution which currently has no subdirs (when subdirs added, remove this IF block but leave content)
	if(taxonNameString.substring(0, taxonNameString.indexOf('/')) != 'evolution') {
	
	taxonNameString = taxonNameString.substring(taxonNameString.indexOf('/') + 1);				// go down a level
	
	}
	
	taxonNameString = taxonNameString.substring(taxonNameString.indexOf('/') + 1);				// go down a level
	taxonNameString = taxonNameString.substring(0, taxonNameString.indexOf('/'))				// get directory name
	//alert(taxonNameString);
	//return;


	// create request URL
	var taxonURL = '/ScienceFramework/species-of-the-day/species.xml?genus=GENUS&species=SPECIES';
	taxonURL = taxonURL.replace('GENUS', taxonNameString.substring(0,taxonNameString.indexOf('-')));
	// following line would identify species if folder name is gen-sp-ssp but this logic fails if species epithet is hyphenated
	//taxonURL = taxonURL.replace('SPECIES', taxonNameString.substring(taxonNameString.lastIndexOf('-') + 1));
	taxonURL = taxonURL.replace('SPECIES', taxonNameString.substring(taxonNameString.indexOf('-') + 1));	
	//alert(taxonURL);



	jQuery.ajax({
		url: taxonURL,
		success: function(response) {

				//alert('success');

				var taxonList = '<dl class="taxonomy">';	// start DL

				var binomial = '';							// start binomial string
				var authority = '';							// start authority string

				jQuery(response).find('taxon').each(function() {

					taxonList += '<dt>' + jQuery(this).children('rank').children('name').text() + '</dt>';	// add DT
					taxonList += '<dd>' + jQuery(this).children('name').text() + '</dd>';						// add DD
					//alert(taxonList);

					currRankLevel = jQuery(this).children('rank').children('level').text();

					// if genus level or lower, add name to binomial (subgen should have brackets)
					if (currRankLevel < genusRankLevel + 1) {
						binomial += ' ' + jQuery(this).children('name').text();
					}


					// following code prob ok for sotd, but maybe not fully robust (eg - if incomplete data or unusual infraspecific ranks?)


					// if species level or lower, create authorship string if present
					if (currRankLevel <= speciesRankLevel) {

						if (jQuery(this).children('nameAuthorship').children('authorship').text().length > 0) {		// if author present
							authority += jQuery(this).children('nameAuthorship').children('authorship').text();		// author
							if (jQuery(this).children('nameAuthorship').children('year').text().length > 0) {
								authority += ', ' + jQuery(this).children('nameAuthorship').children('year').text();	// year
							}
						}

						// iczb - add brackets
						if ((authority.length > 0) && (jQuery(this).children('isOriginalCombination').text() == 'false')) {
							authority = '(' + authority + ')';
						}

						// icbn - add conbination author
						if (jQuery(this).children('combinationAuthorship').children('authorship').text().length > 0) {
							authority = '(' + authority + ')';
							authority += ' ' + jQuery(this).children('combinationAuthorship').children('authorship').text();	// author
							if (jQuery(this).children('combinationAuthorship').children('year').text().length > 0) {
								authority += ', ' + jQuery(this).children('nameAuthorship').children('year').text();		// year
							}
						}
					}
				});

				// end DL
				taxonList += '</dl>';

				var output 	= 	'<div id="utility-taxonomy"><h5>Taxonomy</h5>';
				output 		+= 	'<h6><i>' + binomial + '</i> ' + authority + '</h6>';
				output 		+= 	taxonList;
				output 		+= 	'</div>';

				// append completed list to correct element - FIX
				jQuery('#utilities').prepend(output);
				
				//document.getElementById('utilities').appendChild(output);

		},
		error: function(xhr) {
			 
			// alert('Error!  status = ' + xhr.status);
		}
	});


});
