var sLastClickedItem = -1;
var sLastClickedRoot = -1;

function check_event_target( ev )
{
	if ( ev.target )
	{
		return ev.target.type != "textarea" && ev.target.type != "input"
	}
	else if ( ev.srcElement )
	{
		return !ev.srcElement.isTextEdit;
	}
	else
	{
		return false;
	}
}

function get_items_for_root( root_id )
{
	var el = document.getElementById( "root_" + root_id );
	var tags = el.getElementsByTagName( "li" );
	var ret = new Array();
	
	var _in = 0;
	var _out = 0;
	for ( _in = 0; _in < tags.length; ++_in )
	{
		if ( tags[ _in ].id && tags[ _in ].id.indexOf( "item_" ) == 0 )
		{
			ret[ _out++ ] = tags[ _in ].id;
		}
	}
	
	return ret;
}

function get_prior_item_for_root( root_id, id )
{
	var items = get_items_for_root( root_id );
	var item_found = false;
	var prior_item = false;
	var i = 0;
	
	for ( i = 0; i < items.length; ++i )
	{
		if ( items[i] == "item_" + id )
		{
			item_found = true;
			break;
		}
		
		prior_item = items[i];
	}
	
	if ( item_found )
	{
		return prior_item;
	}
	else
	{
		return false;
	}
}

function get_next_item_for_root( root_id, id )
{
	var items = get_items_for_root( root_id );
	var item_found = false;
	var next_item = false;
	var i = 0;
	
	for ( i = 0; i < items.length; ++i )
	{
		if ( items[i] == "item_" + id )
		{
			item_found = true;
		}
		else if ( item_found )
		{
			next_item = items[i];
			break;
		}
	}
	
	if ( next_item )
	{
		return next_item;
	}
	else
	{
		return false;
	}
}

function get_item_number_from_item_string( item_id )
{
	if ( item_id != false && item_id.indexOf( "item_" ) == 0 )
	{
		return item_id.substr( 5 );
	}
	else
	{
		return false;
	}
}

function larynx_onkeypress( ev )
{
	if (!ev) ev = window.event;
	
	var keyPressed = String.fromCharCode(ev['keyCode']);
	
	if ( sLastClickedItem != -1 && sLastClickedRoot != -1 && 
	     check_event_target( ev ) )
	{
		if ( keyPressed == 'Z' )
		{
			id = get_item_number_from_item_string( get_next_item_for_root( sLastClickedRoot, sLastClickedItem ) );
			
			if ( id != false )
			{
				clickItem( sLastClickedRoot, id );
			}
		}
		
		if ( keyPressed == 'A' )
		{
			id = get_item_number_from_item_string( get_prior_item_for_root( sLastClickedRoot, sLastClickedItem ) );
			
			if ( id != false )
			{
				clickItem( sLastClickedRoot, id );
			}
		}
	}
	
	return true;
	
}

function toggle_larynx_settings()
{
	var el = document.getElementById( "commentssettings" );
	
	if ( does_element_have_class( el, "hidden" ) )
	{
		remove_class_from_element( el, "hidden" );
	}
	else
	{
		add_class_to_element( el, "hidden" );
	}
	
	return false;
}

function remove_old_fullpost( root_id, article_id, documentObject )
{
	var root = documentObject.getElementById( "root_" + root_id );
	var i = 0;
	var oldposts = find_elements_by_class( root, "DIV", "fullpost" );
	
	for ( i = 0; i < oldposts.length; ++i )
	{
		var oldFullpost = oldposts[i];
		
		if ( ( oldFullpost.parentNode.id != "item_" + root_id ) || ( article_id == root_id ) )
		{
			// If we had a preview, delete the old preview node, and tell the parent that
			// it's no longer selected.
			
			oldFullpost.parentNode.className = remove_from_className( oldFullpost.parentNode.className, "sel" );
			var oldOneline = find_element( oldFullpost.parentNode, "DIV", "oneline" );
			oldOneline.className = remove_from_className( oldOneline.className, "hidden" );
			oldFullpost.parentNode.removeChild( oldFullpost );
		}
	}
}

function show_item_fullpost( root_id, article_id, fullpost_element ) 
{
//   alert( "copy_fullpost( " + root_id + ", " + article_id + ")" );

	remove_old_fullpost( root_id, article_id, parent.document );
   
	var parentRoot = parent.document.getElementById( "root_" + root_id );
	// Now we make a new preview node
	var newPreviewParent = parent.document.getElementById( "item_" + article_id );
//	var newPreviewParent = find_element( parentRoot, "li", "item_" + article_id );
	
	// Let's grab me...
	push_front_element( newPreviewParent, fullpost_element );
if ( (window.navigator.userAgent.indexOf( "MSIE " ) < 0 ) && (window.navigator.userAgent.indexOf ( "FireFox/3.0" ) < 0 ) && (window.navigator.userAgent.indexOf( "WebKit" ) < 0 ) ) 
				{
					var eleY, eleY2 = 0;
					var win = window.top;
					if (document.getBoxObjectFor) {
						var eleBox = fullpost_element.ownerDocument.getBoxObjectFor(fullpost_element);
						eleY = eleBox.y;
						eleY2 = eleBox.y + eleBox.height;
					} else {
						// get element_top (eleY) and element_bottom (eleY2) for Safari and Opera
					}
					if (eleY < win.pageYOffset) {
						//alert("align top. topY: " + win.pageYOffset + " eleY: " + eleY);
						fullpost_element.scrollIntoView(true);	// align to top
					} else if (eleY2 > win.pageYOffset + win.innerHeight) {
						//alert("align bottom. bottomY: " + win.pageYOffset+win.innerHeight + " eleY2: " + eleY2);
						fullpost_element.scrollIntoView(false);	// align to bottom of screen
					}
 }

	newPreviewParent.className = add_to_className( newPreviewParent.className, "sel" );
	var newOneline = find_element( newPreviewParent, "DIV", "oneline" );
	newOneline.className = add_to_className( newOneline.className, "hidden" );
}

function on_reply_close()
{
	var postbox = document.getElementById( "postbox" );
	if ( postbox )
	{
		fixup_postbox_parent_for_remove( postbox.parentNode );
	}
}

function close_reply_if_inside( article_id )
{
	var postbox = document.getElementById( "postbox" );
	var article = document.getElementById( "item_" + article_id );
	if ( postbox && article )
	{
		if ( is_element_child_of( postbox, article ) )
		{
			fixup_postbox_parent_for_remove( postbox.parentNode );
		}
	}
}

function close_post( article_id )
{
	close_reply_if_inside( article_id );
	
	var root_node = document.getElementById( "root_" + article_id );
	
	if ( root_node )
	{
		var article_node = document.getElementById( "item_" + article_id );
		var fullpost_node = find_element( article_node, "DIV", "fullpost" );
		var postmeta_node = find_element( fullpost_node, "DIV", "postmeta" );
		var showpost_node = find_element( postmeta_node, "A", "showpost" );
		var closepost_node = find_element( postmeta_node, "A", "closepost" );
		
		// We're the root post.  This requires... special work.
		add_class_to_element( root_node, "collapsed" );
		remove_class_from_element( showpost_node, "hidden" );
		add_class_to_element( closepost_node, "hidden" );
	}
	else
	{
		var article_node = document.getElementById( "item_" + article_id );
		var fullpost_node = find_element( article_node, "DIV", "fullpost" );
		var oneline_node = find_element( article_node, "DIV", "oneline" );
		
		if ( article_node && fullpost_node && oneline_node )
		{
			remove_class_from_element( article_node, "sel" );
			remove_class_from_element( oneline_node, "hidden" );
			article_node.removeChild( fullpost_node );
		}
	}
}

function toggle_collapse( article_id )
{
	var article_node = document.getElementById( "item_" + article_id );
	
	if ( article_node )
	{
		var oneline_node = find_element( article_node, "DIV", "oneline" );
		var treecollapse_node = find_element( oneline_node, "DIV", "treecollapse" );
		
		close_reply_if_inside( article_id );
		
		if ( treecollapse_node )
		{
			var uls = oneline_node.parentNode.getElementsByTagName( "UL" );
			var ancs = treecollapse_node.getElementsByTagName( "A" );
			
			if ( ( uls.length > 0 ) && ( ancs.length > 0 ) )
			{
				// We have a UL.
				if ( does_element_have_class( uls[0], "hidden" ) )
				{
					// If it's hidden, unhide it
					remove_class_from_element( uls[0], "hidden" );
					remove_class_from_element( ancs[0], "closed" );
					add_class_to_element( ancs[0], "open" );
				}
				else
				{
					add_class_to_element( uls[0], "hidden" );
					remove_class_from_element( ancs[0], "open" );
					add_class_to_element( ancs[0], "closed" );
				}
			}
		}
	}
}

function show_post( article_id )
{
	var root_node = document.getElementById( "root_" + article_id );
	
	if ( root_node )
	{
		var article_node = document.getElementById( "item_" + article_id );
		var fullpost_node = find_element( article_node, "DIV", "fullpost" );
		var postmeta_node = find_element( fullpost_node, "DIV", "postmeta" );
		var showpost_node = find_element( postmeta_node, "A", "showpost" );
		var closepost_node = find_element( postmeta_node, "A", "closepost" );
		
		// We're the root post.  This requires... special work.
		remove_class_from_element( root_node, "collapsed" );
		remove_class_from_element( closepost_node, "hidden" );
		add_class_to_element( showpost_node, "hidden" );
	}
}

function fixup_postbox_parent_for_remove( postbox_parent )
{
	if ( does_element_have_class( postbox_parent, "inlinereply" ) )
	{
		var fullpost = find_element( postbox_parent.parentNode, "DIV", 	"fullpost" );
		if ( fullpost )
		{
			remove_class_from_element( fullpost, "replying" );
		}
		postbox_parent.parentNode.removeChild( postbox_parent );
	}
	else if ( does_element_have_class( postbox_parent, "newcommentform" ) )
	{
		if ( postbox_parent.hasChildNodes() )
		{
			var comment_form = postbox_parent.parentNode;
			
			add_class_to_element( comment_form, "formclosed" );
			remove_class_from_element( comment_form, "formopen" );
			postbox_parent.removeChild( postbox_parent.firstChild );
		}
	}
}

function init_new_postbox()
{
	var postbox = document.getElementById( "postbox" );
	var elements = document.getElementsByName( "body" );
	var i;
	
	for ( i = 0; i < elements.length; ++i )
	{
		if ( is_element_child_of( elements[i], postbox ) )
		{
			elements[i].focus();
			break;
		}
	}
}

function new_comment()
{
	var comment_button = document.getElementById( "newcommentbutton" );
	var comment_form = comment_button.parentNode;
	
	if ( does_element_have_class( comment_form, "formopen" ) )
	{
		fixup_postbox_parent_for_remove( document.getElementById( "postbox" ).parentNode );
	}
	else
	{
		var old_postbox = document.getElementById( "postbox" );
		
		if ( old_postbox )
		{
			fixup_postbox_parent_for_remove( old_postbox.parentNode );
		}
		
		var postbox = document.getElementById( "postbox_template" );
		var postbox_new  = postbox.cloneNode(true);
		postbox_new.id = "postbox";
		
		add_class_to_element( comment_form, "formopen" );
		remove_class_from_element( comment_form, "formclosed" );
		
		var actual_form = find_element( comment_form, "div", "newcommentform" );
		
		actual_form.appendChild( postbox_new );
		init_new_postbox();
	}
}

function move_postbox_here( article_id )
{
	var article_node = document.getElementById( "item_" + article_id );
	var fullpost_node = find_element( article_node, "DIV", "fullpost" );
	
	if ( fullpost_node )
	{
		
		var postbox_parent = find_element( fullpost_node.parentNode, "DIV", "inlinereply" );
		
		if ( !postbox_parent )
		{
			postbox_parent = document.createElement( "div" );
			postbox_parent.className = "inlinereply";
			append_element_after_element( postbox_parent, fullpost_node );
		}
		
		// If our parent has a child, it's a post box, so just remove it and 
		// treat reply like a toggle
		
		if ( postbox_parent.hasChildNodes() )
		{
			fixup_postbox_parent_for_remove( postbox_parent );
		}
		else
		{
			// Remove the last post box we were using.
			
			var real_postbox = document.getElementById( "postbox" );
			
			if ( real_postbox )
			{
				fixup_postbox_parent_for_remove( real_postbox.parentNode );
			}

			var postbox = document.getElementById( "postbox_template" );
			var postbox_new  = postbox.cloneNode(true);
			postbox_new.id = "postbox";
			
			var parent_element = find_element_by_name( postbox_new, "input", "parent" );
			parent_element.value = article_id;
			
			postbox_parent.appendChild( postbox_new );
			add_class_to_element( fullpost_node, "replying" );
			init_new_postbox();
		}
	}
}

function copy_iframe_fullpost_2( root_id, article_id, iframe_fullpost_element )
{
	var cloned = import_node( parent.document, iframe_fullpost_element );
	
	show_item_fullpost( root_id, article_id, cloned );
}

function copy_iframe_onepost( root_id, article_id )
{
	// This function copies the one full post from the iframe, then causes the entire 
	// root item to load

	copy_iframe_fullpost_2( root_id, article_id, find_element( document, "DIV", "fullpost" ) );
	
	navigate_page_no_history( window, "frame_laryn.x?root=" + root_id );
}

function clickItem( root, id )
{
	var iframe = window.frames[ 'dom_iframe' ];

	// First, check and see if we've cached the item in the iframe

  var subItem = iframe.document.getElementById( "item_" + id );

  if ( uncap_thread( root ) )
  {
//    document.getElementById( "item_" + id ).scrollIntoView( false );
//    window.location.hash = "itemanchor_" + id;
  }

	sLastClickedItem = id;
	sLastClickedRoot = root;
	
	if ( iframe.document.getElementById( 'items_complete' ) &&
		   subItem )
	{
		var fullpost = find_element( subItem, "DIV", "fullpost" );
		var cloned = import_node( document, fullpost )

		show_item_fullpost( root, id, cloned );
	  return false;
	}
	else
	{
// 	  iframe.src = "frame_laryn.x?root=" + root + "&id=" + id;
	  navigate_page_no_history( iframe, "frame_laryn.x?root=" + root + "&id=" + id );
	  return false;
	}
  
}

function navigate_in_iframe( dst )
{
	var iframe = window.frames[ 'dom_iframe' ];

  navigate_page_no_history( iframe, dst );
}

function do_iframe_logout()
{
	var elements = parent.document.getElementsByName( "loginform" );
	
	for ( i = 0; i < elements.length; ++i )
	{
		var element = elements[i];
		
		var logoutdiv = find_element( element, "DIV", "loggedin" );
		var logindiv = find_element( element, "DIV", "anon" );
		var loginboxesdiv = find_element( element, "DIV", "login" );
		
		// Only show login div
		remove_class_from_element( logindiv, "hidden" );

		add_class_to_element( logoutdiv, "hidden" );
		add_class_to_element( loginboxesdiv, "hidden" );
	}
}

function do_iframe_login( username )
{
	var elements = parent.document.getElementsByName( "loginform" );
	
	for ( i = 0; i < elements.length; ++i )
	{
		var element = elements[i];
		
		var logoutdiv = find_element( element, "DIV", "loggedin" );
		var logindiv = find_element( element, "DIV", "anon" );
		var loginboxesdiv = find_element( element, "DIV", "login" );
		var typeelement = find_element_by_name( element, "INPUT", "type" );
		
		// Only show logout div
		remove_class_from_element( logoutdiv, "hidden" );

		add_class_to_element( logindiv, "hidden" );
		add_class_to_element( loginboxesdiv, "hidden" );

		typeelement.value = "logout";

		// Find the first SPAN in the logoutdiv.  That's the name.
		var elements = logoutdiv.getElementsByTagName( "SPAN" );
		
		if ( elements && elements.length > 0 )
		{
			while ( elements[0].firstChild )
			{
				elements[0].removeChild( elements[0].firstChild );
			}
			
			elements[0].appendChild( parent.document.createTextNode( username ) );
		}
	}
}

function do_anon_to_login()
{
  var elements = document.getElementsByName( "loginform" );

	for ( i = 0; i < elements.length; ++i )
  {
		var element = elements[i];
		var logoutdiv = find_element( element, "DIV", "loggedin" );
		var logindiv = find_element( element, "DIV", "anon" );
		var loginboxesdiv = find_element( element, "DIV", "login" );
		var typeelement = find_element_by_name( element, "INPUT", "type" );
					
		// Only show login boxes div
		remove_class_from_element( loginboxesdiv, "hidden" );

		add_class_to_element( logindiv, "hidden" );
		add_class_to_element( logoutdiv, "hidden" );
		
		typeelement.value = "login";
	}
}

function remove_spoiler( element )
{
	var inspecting = element;
	
	while ( !does_element_have_class( inspecting, "root" ) )
	{
		if ( does_element_have_class( inspecting, "jt_spoiler" ) )
		{
			add_class_to_element( inspecting, "jt_spoiler_clicked" );
			remove_class_from_element( inspecting, "jt_spoiler" );
		}
		
		inspecting = inspecting.parentNode;
	}
}

function doSpoiler( evt )
{

	var isIE = document.all && navigator.appVersion.indexOf("MSIE") != -1;
	if (isIE)
	{

		var element = evt.srcElement

		while ( !does_element_have_class( element, "postbody" ) && !does_element_have_class( element, "oneline_body" ) )
		{

			if ( does_element_have_class( element, "jt_spoiler" ) )
			{
				remove_spoiler(evt.srcElement);
				evt.cancelBubble = true;
				return false;
			}
			element = element.parentNode;
		}
	}

	else
	{

		var element = evt.target

		while ( !does_element_have_class( element, "postbody" ) && !does_element_have_class( element, "oneline_body" ) )
		{

			if ( does_element_have_class( element, "jt_spoiler" ) )
			{
				remove_spoiler(evt.target);
				evt.stopPropagation();
				return false;
			}
			element = element.parentNode;
		}
	}
}

function close_nukebox()
{
	var element = document.getElementById( "nukebox" );

	if ( element )
	{	
		element.parentNode.removeChild( element );
	}
	
	return false;
}

function do_nuke( id, root_id, user_name )
{
	close_nukebox();
	
	// move the nuke box here
	var item_elem = document.getElementById( "item_" + id );
	var par = find_element( item_elem, "DIV", "nukeboxparent" );
	var placeholder = document.getElementById( "nukebox_parent_node" );
	var cloned = placeholder.cloneNode( true );
	cloned.id = "nukebox";
	cloned.className = "";

	var root_element = find_element_by_name( cloned, "input", "root_id" );
	var id_element = find_element_by_name( cloned, "input", "nuke_post" );
	var user_element = find_element_by_name( cloned, "input", "nuke_who" );
	
	root_element.value = root_id;
	id_element.value = id;
	user_element.value = user_name;
	
	par.appendChild( cloned );
}

function uncap_thread( root_id )
{
  var root_element = document.getElementById( "root_" + root_id );
  
  if ( root_element && does_element_have_class( root_element, "capped" ) )
  {
    remove_class_from_element( root_element, "capped" );
    return true;
  }
  else
  {
    return false;
  }
}