// JavaScript Document

var Organism = {};

Organism.editLocation = function(pin) {
	GabeHttpRequest(	"../request/editLocation.php?word_id=" + pin.pinId + "&xspace=" + pin.left + "&yspace=" + pin.top, 
						function(text) {});
}

Organism.detectSelectedCategory = function(elementId) {
	var category = document.getElementById(elementId);
	category.onchange = function(e) {
		Organism.loadAddFields(category.value);
	}
	Organism.loadAddFields(category.value);
}

Organism.loadAddFields = function(category) {
	switch(category) {
		case 'animal':
			Organism.showField('tc_phylum');
			Organism.showField('tc_subphylum');
			Organism.showField('biology');
			Organism.showField('habitat');
			Organism.showField('remarks');
			
			Organism.hideField('tc_division');
			Organism.hideField('tc_subdivision');
			Organism.hideField('uses');
			break;
		case 'plant':
			Organism.showField('tc_division');
			Organism.showField('tc_subdivision');
			Organism.showField('uses');
			
			Organism.hideField('tc_phylum');
			Organism.hideField('tc_subphylum');
			Organism.hideField('biology');
			Organism.hideField('habitat');
			Organism.hideField('remarks');
			break;
	}
}

Organism.hideField = function(fieldId) {
	if(!document.getElementById(fieldId)) {
		return;	
	}
	var field = document.getElementById(fieldId);
	var parentNode = field.parentNode;
	if(parentNode.tagName == 'TD') {
		parentNode.parentNode.style.display = 'none';	
	}
}

Organism.showField = function(fieldId) {
	if(!document.getElementById(fieldId)) {
		return;	
	}
	var field = document.getElementById(fieldId);
	var parentNode = field.parentNode;
	if(parentNode.tagName == 'TD') {
		if(window.addEventListener) {
			parentNode.parentNode.style.display = 'table-row';
		} else {
			parentNode.parentNode.style.display = 'block';	
		}
	}
}

Organism.manageOrganismsList = function(listId, buttonId, buttonText, optionsHtml, /* array id|quantity */ organisms) {
	
	var list = document.getElementById(listId);
	
	var dropdownList = document.createElement("select");
	dropdownList.id = "organism_id[]";
	dropdownList.name = "organism_id[]";
	
	for(oId in optionsHtml) {
		var option = null;
		option = document.createElement("option");
		option.value = oId;
		option.appendChild(document.createTextNode(optionsHtml[oId]));
		dropdownList.appendChild(option);
	}
	
	var appendList = function(selectedValue) {
		
		var li = document.createElement("li");
		var dropdown = dropdownList.cloneNode(true);
		li.appendChild(dropdown);
		var quantity = appendQuantityField(li, "list_");
		attachDeleteButton(li);
		list.appendChild(li);
		
		if(typeof selectedValue != "undefined") {
			var value = selectedValue.split("|");
			var loptions = dropdown.getElementsByTagName("option");
			for(oId in loptions) {
				if(loptions[oId].value == value[0]) {
					loptions[oId].selected = true;	
				}
			}
			quantity.value = value[1];	
		}
		
	}
	
	var buttonList = document.getElementById(buttonId);
	buttonList.onclick = function() {
		appendList();
	}
	
	var buttonText = document.getElementById(buttonText);
	buttonText.onclick = function() {
		
		var textbox = document.createElement("input");
		textbox.type = "text";
		textbox.id = "common_name[]";
		textbox.name = "common_name[]";
		
		var textbox2 = document.createElement("input");
		textbox2.type = "text";
		textbox2.id = "scientific_name[]";
		textbox2.name = "scientific_name[]";
		
		var category = document.createElement("select");
		category.name = "category[]";
		category.add(new Option("Animal", "animal", false, false), null);
		category.add(new Option("Plant", "plant", false, false), null);
		
		var li = document.createElement("li");
		li.appendChild(document.createTextNode("Common Name: "));
		li.appendChild(textbox);
		li.appendChild(document.createTextNode(" Scientific Name (optional): "));
		li.appendChild(textbox2);
		li.appendChild(category);
		appendQuantityField(li, "text_");
		attachDeleteButton(li);
		
		list.appendChild(li);
	}
	
	var appendQuantityField = function(aLi, prefix) {
		var quantity = document.createElement("input");
		quantity.type = "text";
		quantity.name = prefix + "quantity[]";
		quantity.value = "1";
		quantity.size = "10";
		aLi.appendChild(document.createElement("br"));
		aLi.appendChild(document.createTextNode(" Quantity spotted: "));
		aLi.appendChild(quantity);
		return quantity;
	}
	
	var attachDeleteButton = function(aLi) {
		var delButton = document.createElement("input");	
		delButton.type = "button";
		delButton.value = "Remove from sightings";
		delButton.onclick = function() {
			if(confirm("Delete this organism from the list?")) {
				aLi.parentNode.removeChild(aLi);	
			}
		}
		aLi.appendChild(document.createTextNode(" "));
		aLi.appendChild(delButton);
	}
	
	if(typeof organisms != "undefined") {
		for(oId in organisms) {
			appendList(organisms[oId]);
		}		
	}
}

Organism.notifyDataCountDropdown = function(selElement) {
	var value = selElement.value;
	var qtytative = ['water_temperature', 'air_temperature', 'rainfall', 'vertical', 'horizontal', 'relative_humidity',
					 'location', 'sea_condition', 'weather', 'wind_direction'];
	var len = qtytative.length;
	for(var i = 0; i < len; i++) {
		if(value == qtytative[i]) {
			document.getElementById('data_count').style.display = 'inline';
			return;
		}
	}
	document.getElementById('data_count').style.display = 'none';
	document.getElementById('data_count').selectedIndex = 0;
}