/* 
	By David Eads, 2005
	Using Caio Chassot's library (http://www.v2studio.com/k/code/lib/), the whole thing for testing.
	Right now, we're using: 
		-getElementsByClass
		-getElem
		-addClass
		-addEvent
		-addLoadEvent
*/

// Move flag
var move_flag = 0;
var comment_buttons;
var collapse_buttons;

/*	-------------------------------------------------
	Interaction logic functions:  these are the
	functions that actually do stuff to the DOM
	tree.
	------------------------------------------------- */

/* Adds and removes a class called 'collapsed' on collapse buttons */
function collapse_toggle(caller) {
	var node = caller.currentTarget.parentNode.parentNode; // Go back up to the outer li
	if (node.className && hasClass(node,'collapsed')) {
		remClass(node,'collapsed');
	} else {
		addClass(node,'collapsed');
	}
	
	caller.stopPropagation();
	caller.preventDefault();
}

/* Moves the whole damn comment form to the user specified location on the page */
function move_reply(caller) {
	var display_button = caller.currentTarget; // The button that was pressed to hide or show comments
	var display_node = caller.currentTarget.parentNode; // The outer div it lives in
	var reply = getElem('add_comment'); // The add comment part of the comment form
	var reply_form = getElem('ii_commentform'); // The outer form
	
	var new_reply = reply.cloneNode(true); // Copy the add comment piece
	reply.parentNode.removeChild(reply); // Remove it from its old location
	
	if (!move_flag) {
		var new_reply_id = display_node.id.substring(10,display_node.id.length);  // Get the reply id
		display_button.innerHTML = 'Close reply (changes will be lost)'; // Change the button text
		display_node.appendChild(new_reply); // Add the node where we clicked
		getElem('reply_id').value = new_reply_id; // Add the correct hidden field
		getElem('comment').value = '';
		active_toggle(new_reply_id); // De-activate the other buttons
		move_flag = 1; // Let the app know we've moved something
	} else {
		display_button.innerHTML = 'Reply to this'; // Change text of button back
		reply_form.appendChild(new_reply); // Add the add comment form back to the end of the outer form
		getElem('reply_id').value = 0; // Reset the hidden field
		active_toggle(); // Reset the button values
		move_flag = 0; // Reset the move flag so that the app knows things are back to normal
	}
	
	caller.stopPropagation();
	caller.preventDefault();
	
}

/* 
	Toggle activation of buttons - takes an exception id as an argument so that the caller
	doesn't get	disabled when someone clicks the form.  It also adds a class called 'inactive'
	that you can style as you wish.  I like to just make it disappear.
*/
function active_toggle(exception_id) {
	if (exception_id) {
		for (var i=0; i<comment_buttons.length;i++) {
			if (comment_buttons[i].id != 'reply_' + exception_id) {
				addClass(comment_buttons[i],'inactive');
			}
		}
	} else {
		for (var i=0; i<comment_buttons.length;i++) {
			remClass(comment_buttons[i],'inactive');
		}		
	}
}

// Shows collapse buttons if Javascript is enabled
function show_collapse() {
	collapse_buttons = fastGetElementsByClass('collapse','a');
	for (var i=0; i<collapse_buttons.length; i++) {
		collapse_buttons[i].style.display = 'inline';
	}
}

// Gets elements by class... quickly
function fastGetElementsByClass(theclass,thetag) {
	thearray = new Array();
	var nodes = document.getElementsByTagName(thetag);
	for (i=0;i<nodes.length;i++) {
		if (nodes[i].className && hasClass(nodes[i],theclass)) {
			thearray.push(getElem(nodes[i]));
		}
	}
	return thearray;
}

/*	--------------------------------------------------
	Register event handlers
	-------------------------------------------------- */

function register() {
	/* Populate global arrays for use later */
	comment_buttons = fastGetElementsByClass('toggle_reply','a');
	collapse_buttons = fastGetElementsByClass('collapse','a');
	
	addEvent(comment_buttons,'click', move_reply); // Toggle reply
	addEvent(collapse_buttons,'click',collapse_toggle); // Toggle collapse
	show_collapse();
}

addLoadEvent(register);