
var sLastClickedItem = -1;
var sLastClickedRoot = -1;
var scroll_y ;
chat_start_up();
function chat_start_up() {
	if (typeof(window['$']) == "undefined") {
		setTimeout("chat_start_up()",500); 	
	} else {
		$(document).ready(function(e) {
			$(document).keydown(function(e) {
				chat_onkeypress(e);
			});	
			// show_anchoritem();
		});
		
		$(document).mousemove(function(e) {
		    scroll_y = (e.pageY - $(window).scrollTop());
		});
		
		
	}
}


function scroll_and_new_comment()
{
  window.location.hash = "newcommentbutton";
  new_comment();
  return false;
}

function navigate_page_no_history( windowObject, newLocation )
{
	the_window = windowObject;
	windowObject.location.replace( "about:blank" );
	windowObject.location.replace( newLocation );
}

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 chat_onkeypress( ev )
{
	// alert(ev.keyCode);

	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_chat_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 ) 
{
	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, thread_content_id, thread_content_type_id )
{
	// // console.log ('move_postbox_here => ' + article_id +' '+ injected_thread_content_id +' '+ is_injected_thread);
	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_id" );
			parent_element.value = article_id;
			var content_id_element = find_element_by_name( postbox_new, "input", "content_id" );
			content_id_element.value = thread_content_id;
			var content_type_id_element = find_element_by_name( postbox_new, "input", "content_type_id" );
			content_type_id_element.value = thread_content_type_id;			
			postbox_parent.appendChild( postbox_new );
			add_class_to_element( fullpost_node, "replying" );
			init_new_postbox();
		}
	}
}

function copy_iframe_fullpost_3( root_id, article_id)
{
	// if (iframe_fullpost_element = document.getElementById('item_' + article_id))
	if (iframe_fullpost_element = find_element( document, "DIV", "fullpost")) 
	{
		alert('here ' + 'item_' + article_id);
		copy_iframe_fullpost_2(root_id, article_id, iframe_fullpost_element);
	}
	// copy_iframe_fullpost_2(root_id, article_id, iframe_fullpost_element);
}

function copy_iframe_fullpost_2( root_id, article_id, iframe_fullpost_element )
{
	// console.log('copy_iframe_fullpost_2' + ' ' + root_id + ' ' + article_id);
	// console.log(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_chatty.x?root=" + root_id);
}

function import_node( destination_document, node_to_import )
{	
	// console.log(node_to_import);
	// alert(node_to_import);
	// // console.log(destination_document);
	
	
	if ( destination_document.importNode )
	{
		return destination_document.importNode( node_to_import, true );
	}
	else
	{
		// IE Path, since IE doesn't support importNode
		
		var tmpNode = destination_document.createElement("div");
		tmpNode.innerHTML = node_to_import.outerHTML;
		return tmpNode.firstChild.cloneNode(true);
	}
}


function clickItem( root, id )
{
	var iframe = window.frames[ 'dom_iframe' ];
	// console.log('iframe: ' + iframe);
	
	// First, check and see if we've cached the item in the iframe

	var subItem = iframe.document.getElementById( "item_" + id );
	// console.log('subitem:item_' + id + ': ' + subItem);
	
	
	if ( uncap_thread( root ) )
	{
	    // document.getElementById( "item_" + id ).scrollIntoView( true );
	    elem_position = $("#item_" + id).position();
	    window.scrollTo(0, elem_position.top - scroll_y + 12);
	}

	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 );
		// console.log ('cloned ' + root );
		return false;
	}
	else
	{
		path_pieces = document.location.pathname.split('?');
		parent_url = path_pieces[0];
		navigate_page_no_history( iframe, "/frame_chatty.x?root=" + root + "&id=" + id + "&parent_url=" + parent_url);	
		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, user_id )
{
	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" );
	var user_id_element = find_element_by_name( cloned, "input", "nuke_user_id" );
	
	root_element.value = root_id;
	id_element.value = id;
	user_element.value = user_name;
	user_id_element.value = user_id;
	
	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;
  }
}

function add_to_className( theClassName, theValue )
{
	var classes = theClassName.split( " " );
	if ( index_in_array( classes, theValue ) == -1 )
	{
		classes[ classes.length ] = theValue;
	}
	
	return classes.join( " " );
}

function remove_class_from_element( element, theClass )
{
	element.className = remove_from_className( element.className, theClass );
}

function add_class_to_element( element, theClass )
{
	element.className = add_to_className( element.className, theClass );
}

function does_element_have_class( element, theClass )
{
	if ( index_in_array( element.className.split( " " ), theClass ) > (-1) )
	{
		return true;
	}
	else
	{
		return false;
	}
}

function find_element( parentRoot, element_type, element_class )
{
	var elements = parentRoot.getElementsByTagName( element_type );
	
  for ( var i = 0; i < elements.length; ++i )
  {
  	if ( index_in_array( elements[i].className.split( " " ), element_class ) > -1 )
    {
//    	 alert( "Found element: " + elements[i].className );
       return elements[i];
    }
  }
   
  return false;
}

function append_element_after_element( to_append, where )
{
	var sib = where.nextSibling;
		
	if ( sib )
	{
		sib.parentNode.insertBefore( to_append, sib );
	}
	else
	{
		where.parentNode.appendChild( to_append );
	}
}

function find_elements_by_class( parentRoot, element_type, element_class )
{
	var elements = parentRoot.getElementsByTagName( element_type );
	var dst = new Array();
	
	for ( var i = 0; i < elements.length; ++i )
	{
		if ( index_in_array( elements[i].className.split( " " ), element_class ) > -1 )
		{
			dst.push( elements[i] );
		}
	}
	return dst;
}

function find_element_by_name( parentRoot, element_type, element_name )
{
	var elements = parentRoot.getElementsByTagName( element_type );

  for ( var i = 0; i < elements.length; ++i )
  {
  	if ( elements[i].name == element_name )
  	{
  		return elements[i];
  	}
	}	
	
	return false;
}

function push_front_element( parent_element, child_element )
{
   var elements = parent_element.childNodes;
   
   if ( elements.length > 0 )
   {
      return parent_element.insertBefore( child_element, elements[0] );
   }
   else
   {
      return parent_element.appendChild( child_element );
   }
}

function remove_from_className( theClassName, theValue )
{
	var classes = remove_value_from_array( theClassName.split( " " ), theValue );
	return classes.join( " " );
}


function replace_whole_element_from_iframe( element_name )
{
	var cloned = import_node( parent.document, document.getElementById( element_name ) );
	var found = parent.document.getElementById( element_name );
	
	found.parentNode.replaceChild( cloned, found );
}

function remove_from_array( theArray, theIndex )
{
	if ( theIndex != -1 )
	{
		var _in = 0; 
		var _out = 0;
		var _ret = new Array();
		
		for ( _in = 0; _in < theArray.length; ++_in )
		{
			if ( _in != theIndex )
			{
				_ret[_out] = theArray[_in];
				++_out;
			}
		}
		
		return _ret;
	}
	else
	{
		return theArray;
	}
}

function index_in_array(theArray, theValue)
{
	var arLength = theArray.length;
	for(var i=0; i < arLength; ++i )
	{
		if (theArray[i] == theValue)
		{
			return i;
		}
	}
	return -1;
}

function remove_value_from_array( theArray, theValue )
{
	var index = index_in_array( theArray, theValue );
	return remove_from_array( theArray, index );
}



function is_element_child_of( child_element, parent_element )
{
	var checking = child_element.parentNode;
	
	while ( checking )
	{
		if ( checking == parent_element )
		{
			return true;
		}
		
		checking = checking.parentNode;
	}
	
	return false;
}

function jump_to_anchor( anchor_name )
{
	window.location.hash = anchor_name;
}

function jump_to_anchor_from_iframe( anchor_name )
{
	parent.window.location.hash = anchor_name;
}
/*
// @todo not needed
function show_anchoritem()
{
	if(window.location.hash) {
		anchoritem = window.location.hash;
		anchoritem = 'li#item_' + anchoritem.substr(12);
		// alert(anchoritem);
		oneline = anchoritem + ' div.oneline a';
		$(oneline).click();
	}
}

function show_item_by_id(id)
{
	if(li_item = document.getElementById('item_' + id)) {
		anchoritem = 'li#item_' + id;
		// alert(anchoritem);
		oneline = anchoritem + ' div.oneline a';
		$(oneline).click();
	} // else if (li_item = parent.document.getElementById('item_' + id)) {
}
*/



function QuickEntry(tagopen, tagclose, desc) {
	GetSelection(tagopen, tagclose);
}


function GetSelection(tagopen, tagclose) {

	// lifted from http://www.webmasterworld.com/forum91/5005.htm because god.. i'm lazy
	var isFF = false;
	var textSelected = false;

	if(navigator.userAgent.toLowerCase().indexOf("firefox") > 0)
		isFF = true;

	var myArea = document.getElementById("frm_body");
	var begin,selection,end;

	if (isFF == true) {
		if (myArea.selectionStart!= undefined) {
			begin = myArea.value.substr(0, myArea.selectionStart);
			selection = myArea.value.substr(myArea.selectionStart, myArea.selectionEnd - myArea.selectionStart);
			end = myArea.value.substr(myArea.selectionEnd);
				if (selection.length > 0){
					textSelected = true;
				}
			}
	}
	else {
		if (window.getselection) {
			selection = window.getselection();
		}
		else if (document.getselection){
			selection = document.getselection();
		}
		else if (document.selection){
			selection = document.selection.createRange().text;
		}
		var startPos = myArea.value.indexOf(selection);

		if (selection.length > 0) {
			var endPos = GetCaretPosition(myArea) + selection.length;
			begin = myArea.value.substr(0,GetCaretPosition(myArea));
			end = myArea.value.substr(endPos, myArea.value.length);
			textSelected = true;
		}
	}

	//alert(GetCaretPosition(myArea));
	if (textSelected == true)  {
		myArea.value = begin +  tagopen + selection + tagclose + end;
	}

}

//OMG CODE THEIF.. stolen from http://blog.vishalon.net/Post/57.aspx
function GetCaretPosition(control) {
	var CaretPos = 0;
	// IE Support
	if (document.selection)	{
		control.focus();
		var Sel = document.selection.createRange();
		var Sel2 = Sel.duplicate();
		Sel2.moveToElementText(control);
		var CaretPos = -1;
		while(Sel2.inRange(Sel)) {
			Sel2.moveStart('character');
			CaretPos++;
		}
	}
	// Firefox support
	else if (control.selectionStart || control.selectionStart == '0')
	CaretPos = control.selectionStart;
	return (CaretPos);
}

function toggle_shacktags_legend() {	
 	$('#shacktags_legend_table').toggle();
}



