/*
    Copyright 2006 Charles Moad, Indianapolis Museum of Art
    This file is part of Steve.Museum.
    Steve.Museum is free software; you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation; either version 2.1 of the License, or
    (at your option) any later version.

    Steve.Museum is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with Steve.Museum; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

// global editing state
var editing = false;
var editingTermTextDiv;
var editingImagesetId;
var editingMimeId;
var editingTermId;

function submit() {
   document.forms.tagging_form.submit();
}

/*
 * Turn on the edit and delete icons when we mouse over a tag
 */
function onTermMouseOver(sender) {
	if (editing) return; // die out if editing

	sender.className = 'term-tag term-hover';

	// make the edit and delete icons visible
	var children = getElementsByClassName(sender, 'a', 'term-control');
	for (child in children) {
		if (children[child].style)
			children[child].style.visibility = 'visible';
	}
}

/*
 * Hide the edit and delete icons when we mouse over a tag
 */
function onTermMouseOut(sender) {
	if (editing) return; // die out if editing

	sender.className = 'term-tag';

	// make the edit and delete icons hidden
	var children = getElementsByClassName(sender, 'a', 'term-control');
	for (child in children) {
		if (children[child].style)
			children[child].style.visibility = 'hidden';
	}
}

/*
 * Mark a term for deletion
 */
function deleteTerm(sender, imagesetId, mimeId, termId, phpself, controller) {
	// marshal the url for deleting a term
	var target = phpself + "?taskOnError=viewImageset&task=" + controller + "_deleteTerm";
	target += "&imagesetId=" + imagesetId;
	target += "&mimeId=" + mimeId;
	target += "&termId=" + termId;
	window.location = target;
}

/*
 * Replace the term text with a textbox for editing
 */
function editTerm(sender, imagesetId, mimeId, termId) {
	if (editing) return; // die out if editing

	// manually hide the edit/delete icons
	onTermMouseOut(sender.parentNode);

	// make sure we are swapping the span.term_text element
	var termText;
	if (sender.className == 'term-text') {
		termText = sender;
	} else { // get reference to the span.term_text
		termText = sender.parentNode.getElementsByTagName('span')[0];
	}
	
	// save the global editing state
	var termTextDiv = termText.parentNode;
	editingTermTextDiv = termTextDiv;
	editingImagesetId = imagesetId;
	editingMimeId = mimeId;
	editingTermId = termId;
	
	// Clone the term_editor div in the dom
	var termEditor = document.getElementById('term-editor').cloneNode(true);
	termEditor.style.visibility = 'visible';
	
	// swap with termText
	termTextDiv.parentNode.replaceChild(termEditor, termTextDiv);
	
	var termInput = termEditor.getElementsByTagName('input')[0];
	termInput.value = termText.childNodes[0].nodeValue;
	termInput.select();
	termInput.focus();
	
	// set the editing state
	editing = true;
}

/*
 * Cancel out of the editing state
 */
function cancelEdit(sender) {
	sender.parentNode.parentNode.replaceChild(editingTermTextDiv, sender.parentNode);
	editingTermTextDiv = null;
	editing = false;
}

/*
 * Save the edited term
 */
function saveEdit(sender, phpself, controller) {
	var termInput = sender.parentNode.getElementsByTagName('input')[0];
	var termText = editingTermTextDiv.getElementsByTagName('span')[0].childNodes[0];
	
	// invoke a cancel if no change or blank input
	if (termText.nodeValue == termInput.value || termInput.value == '') {
		cancelEdit(sender);
		return;
	}

	// Submit the change
	var target = phpself + "?taskOnError=viewImageset&task="+ controller + "_correctTerm";
	target += "&imagesetId=" + editingImagesetId;
	target += "&mimeId=" + editingMimeId;
	target += "&termId=" + editingTermId;
	target += "&termText=" + termInput.value;
	window.location = target;
}

function getElementsByClassName(elm, tag, className){
	var testClass = new RegExp("(^|\\s)" + className + "(\\s|$)");
	var tag = tag || "*";
	var elm = elm || document;
	var elements = (tag == "*" && elm.all)? elm.all : elm.getElementsByTagName(tag);
	var returnElements = [];
	var current;
	var length = elements.length;
	for(var i=0; i<length; i++){
		current = elements[i];
		if(testClass.test(current.className)){
			returnElements.push(current);
		}
	}
	return returnElements;
}

