// Globals
/* The current thread id. each time the mouse is over a thread,
this variable is updated. */
var currentId;


function toggleEditorForId(id) 
{	
	// Try to open the editor in place, or use simpler bottom of page editor

	var editor = document.getElementById('editor');
	var text = document.getElementById(id + "/text");
	if (editor.parentNode != text) {
		// Take the editor from another node
		if (text.appendChild) {
			// Take the editor from another node and open it under
			// the message text
			text.appendChild(editor);	
			editor.style.display = '';
			text.style.display = '';
		} else {
			// Append is not available, just toggle the editor
			// and scroll to it.
			editor.style.display = editor.style.display ? '' : 'none';
			window.location = "#editor";
		}		
	} else {
		// We alreay own the editor
		if (text.style.display == 'none') {
			// Show the editor and text
			editor.style.display = '';
			text.style.display = '';
		} else {
			// Just toogle the editor
			editor.style.display = editor.style.display ? '' : 'none';
		}
	}
	
	// Set the message id variable in the editor
	// Not implemented yet		
}

function sendReply()
{
	// Hide shared editor
	var editor = document.getElementById('editor');
	editor.style.display = 'none';

	// Should submit the form here
}

function toggleMessageId(id)
{
	// toggle single message
	
	var text = document.getElementById(id + "/text");
    var display = text.style.display ? '' : 'none';
    text.style.display = display;
    updateIconForDisplay(id, display);
}

function updateIconForDisplay(id, display)
{
	// Update icon
	
	var icon = document.getElementById(id + "/icon");
	if (display == 'none') {
		icon.src = icon.src.replace("colapse", "expand");
	} else {
		icon.src = icon.src.replace("expand", "colapse");
	}
}

function setDisplayDeep(id, display)
{
	// Set everything under id to display
	//   	id 		-- a message id
	//   	dispaly -- the display to set
	
	var message = document.getElementById(id);
	var contents = message.parentNode;
	var messages = contents.getElementsByTagName('table');
	for (i = 0; i < messages.length; i++) {
		// Find table elements at our path, then set their text display
		// e.g id = "/324/3" --> "324/3/1-n/1-n/..."
		if (messages[i].id.indexOf(id) == 0) {
			// Set display
			text = document.getElementById(messages[i].id + "/text");
			text.style.display = display;
			// Update icon
			updateIconForDisplay(messages[i].id, display);
		}
	}
	
}

function toggleMessageDeepId(id) 
{
	// Toggle everything under message id
	
	// Get our disply status and toggle it
	var message = document.getElementById(id);
	var text = document.getElementById(id + "/text");
	display = text.style.display ? '' : 'none';
	setDisplayDeep(id, display);
	
}

function toggleThreadDeepId(id)
{
	// Toggle the thread deeply
	// If the thread is open, collapse all messages
	
	var text = document.getElementById(id + "/text");
	var contents = document.getElementById(id + "/contents");
	if (text.style.display == 'none') {
		// Deep expand
	    text.style.display = '';
	    contents.style.display = '';
	    updateIconForDisplay(id, '');
	    setDisplayDeep(id, '');
	} else {
		// Deep collapse
	    text.style.display = 'none';
	    contents.style.display = 'none';
	    updateIconForDisplay(id, 'none');
	    setDisplayDeep(id, 'none');
	}
}

function toggleThreadId(id)
{
	// toggle the display of thread text and contents
	
	var text = document.getElementById(id + "/text");
	var contents = document.getElementById(id + "/contents");
	display = contents.style.display ? '' : 'none';
    text.style.display = display;
	contents.style.display = display;
    updateIconForDisplay(id, display);
}

function setRate(rate)
{
	var rateImage = document.getElementById(currentId + "/rate");
	rateImage.src = "images/rate-" + rate + ".gif";
}

function setCurrentId(id)
{
	currentId = id;
}
