/*
 * ==========================================
 * 	html code of a tree
 * ==========================================
 * 
 * <div class="widget_tree">
 * 	[root-node]
 * </div>
 * 
 * ==========================================
 * 	html code of a tree-node
 * ==========================================
 * 
 * <!-- widget_tree__node -->
 * <table class="widget_tree__node">
 * 	<tbody>
 * 		<!-- widget_tree__node_head -->
 * 		<tr>
 * 			<td><div class="widget_tree__branch"></div></td>
 * 			<td><div class="widget_tree__icon"></div<</td>
 * 			<td class="widget_tree__title">[title]</td>
 * 		</tr>
 * 		<tr>
 *			<!-- widget_tree-node__trunc -->
 * 			<td></td>
 *			
 * 			<!-- widget_tree-node__body -->
 * 			<td colspan=2">
 * 				<!-- [tree-node]* -->
 * 			</td>
 * 		</tr>
 * 	</tbody>
 * </table>
 */

// booleans

function widget_tree__is_leaf(vTreeNode)
{
	var vTABLE	= vTreeNode ;
	var vTBODY	= get_first_child(vTABLE) ;
	var vTR		= get_last_child(vTBODY) ;
	var vTD		= get_last_child(vTR) ;
	var vNodes	= get_child_nodes(vTD) ;
	return vNodes.length == 0 ;
}

function widget_tree__has_next(pTreeNode)
{
	return has_next_sibling(pTreeNode) ;
}

function widget_tree__has_prev(pTreeNode)
{
	return has_prev_sibling(pTreeNode) ;
}

function widget_tree__has_parent(pTreeNode)
{
	var vParent = get_parent_node(pTreeNode) ;
	return vParent.tagName == "TD" ;
}

// gets

function widget_tree__get_node(pSelf)
{
	var vSPAN	= pSelf ;
	var vTD		= get_parent_node(vSPAN) ;
	var vTR		= get_parent_node(vTD) ;
	var vTBODY	= get_parent_node(vTR) ;
	var vTABLE	= get_parent_node(vTBODY) ;
	return vTABLE ;
}

function widget_tree__get_tree(pTreeName)
{
	var vTree = document.getElementById("i_widget_tree__"+pTreeName) ;
	return vTree ;
}

function widget_tree__get_root(pTreeNode)
{
	while( widget_tree__has_parent(pTreeNode) )
	{
		pTreeNode = widget_tree__get_parent(pTreeNode) ;
	}
	return pTreeNode ;
}

function widget_tree__get_next(pTreeNode)
{
	return get_next_sibling(pTreeNode) ;
}

function widget_tree__get_prev(pTreeNode)
{
	return get_prev_sibling(pTreeNode) ;
}

function widget_tree__get_first(pTreeNode)
{
	var tbody	= get_first_child(pTreeNode) ;
	var tr		= get_last_child(tbody) ;
	var td		= get_last_child(tr) ;
	var first	= get_first_child(td) ;
	return first ;
}

function widget_tree__get_last(pTreeNode)
{
	var tbody	= get_first_child(pTreeNode) ;
	var tr		= get_last_child(tbody) ;
	var td		= get_last_child(tr) ;
	var last	= get_last_child(td) ;
	return last ;
}

function widget_tree__get_parent(pTreeNode)
{
	var vNode	= pTreeNode ;
	var vTD		= get_parent_node(vNode) ;
	var vTR		= get_parent_node(vTD) ;
	var vTBODY	= get_parent_node(vTR) ;
	var vTABLE	= get_parent_node(vTBODY) ;
	return vTABLE ;
}

// focus

function widget_tree__focus_node(pSelf)
{
	var vRen = pSelf.getAttribute("_renaming") ;
	if( vRen == "yes" ) return ;

	pSelf._class_name = pSelf.className ;
	pSelf.className += " widget_tree__focused" ;
}

function widget_tree__leave_node(pSelf)
{
	pSelf.className = pSelf._class_name ;
}

// select

function widget_tree__get_title(vNode)
{
	vTABLE	= vNode ;
	vTBODY 	= get_first_child(vTABLE) ;
	vTR	= get_first_child(vTBODY) ;
	vTD	= get_last_child(vTR) ;
	vSPAN	= get_first_child(vTD) ;
	return vSPAN ;
}

function widget_tree__select_node(pSelf)
{
	var vRen = pSelf.getAttribute("_renaming") ;
	if( vRen == "yes" ) return ;

	var vNode = widget_tree__get_node(pSelf) ;
	widget_tree__select(vNode) ;
}

function widget_tree__unselect(pNodeTitle)
{
	if( !pNodeTitle ) return ;
	pNodeTitle.className = "" ;
}

function widget_tree__select(vNode)
{
	var vRoot = widget_tree__get_root(vNode) ;
	var vTree = vRoot.parentNode ;
	var vLast = vTree._last_selected ;
	var vSelf = widget_tree__get_title(vNode) ;

	widget_tree__unselect(vLast) ;
	vSelf.className = "widget_tree__selected" ;
	vSelf._class_name = vSelf.className ;
	vTree._last_selected = vSelf ;
}



// expand collapse

function widget_tree__get_branch_icon(vNode)
{
	vTABLE	= vNode ;
	vTBODY 	= get_first_child(vTABLE) ;
	vTR	= get_first_child(vTBODY) ;
	vTD	= get_first_child(vTR) ;
	vDIV	= get_first_child(vTD) ;
	return vDIV ;
}

function widget_tree__get_body(vNode)
{
	vTABLE	= vNode ;
	vTBODY	= get_first_child(vTABLE) ;
	vTR	= get_last_child(vTBODY) ;
	return vTR ;
}

function widget_tree__swap_branch_state(pSelf)
{
	var vNode = widget_tree__get_node(pSelf) ;
	var vState = vNode.getAttribute("_state") ? vNode.getAttribute("_state") : "opened" ;
	switch( vState )
	{
		case "opened" :
			widget_tree__collapse(vNode) ;
			break ;
		case "closed" :
			widget_tree__expand(vNode) ;
			break ;
	}
}

function widget_tree__collapse(vNode)
{
	if( widget_tree__is_leaf(vNode) ) return ;
	var vIcon = widget_tree__get_branch_icon(vNode) ;
	var vSrc = vIcon.getAttribute("_src") ;
	vIcon.style.backgroundImage = "url("+vSrc+"-closed.gif)" ;
	vNode.setAttribute("_state","closed") ;
	var vBody = widget_tree__get_body(vNode) ;
	vBody.className = "hidden" ;
}

function widget_tree__expand(vNode)
{
	if( widget_tree__is_leaf(vNode) ) return ;
	var vIcon = widget_tree__get_branch_icon(vNode)
	var vSrc = vIcon.getAttribute("_src") ;
	vIcon.style.backgroundImage = "url("+vSrc+"-opened.gif)" ;
	vNode.setAttribute("_state","opened") ;
	var vBody = widget_tree__get_body(vNode) ;
	vBody.className = "" ;
}


// rename

function widget_tree__rename(pNodeTitle)
{/*
	var vRen = pNodeTitle.getAttribute("_renaming") ;
	if( vRen == "yes" ) return ;
	var vValue = pNodeTitle.firstChild.data ;

	pNodeTitle.innerHTML = "<input onkeydown=\"widget_tree__rename_enter(event,this)\" value=\""+vValue+"\"/>" ;
	pNodeTitle.setAttribute("_renaming","yes") ;
*/
}

function widget_tree__rename_enter(pEvent,pSelf)
{/*
	var vKey = get_key_code(pEvent) ;
	if( vKey != 13 ) return ;
	pSelf.parentNode.setAttribute("_renaming","no") ;
	pSelf.parentNode.innerHTML = pSelf.value ;
*/
}


// events

function widget_tree__key_pressed(pEvent,pTreeName)
{
	var vTree = widget_tree__get_tree(pTreeName) ;
	var vRoot = get_first_child(vTree) ;
	var vKey = get_key_code(pEvent) ;
	var vLast = vTree._last_selected ;
	var vNode = widget_tree__get_node(vLast) ;

	if( vKey == ASCII_SPACE ) widget_tree__select(vRoot) ;
	if( !vLast ) return ;

	switch( vKey )
	{
		case ASCII_F2	: widget_tree__rename(vLast) ;		break ;
		case ASCII_UP	:
			if( !widget_tree__has_prev(vNode) )
			{
				if( !widget_tree__has_parent(vNode) ) return ;
				else
				{
					vParent = widget_tree__get_parent(vNode) ;
					widget_tree__select(vParent) ;
				}
			}
			else // has prev
			{
				var vPrev = widget_tree__get_prev(vNode) ;
				vPrev = widget_tree__get_prev_in_list(vPrev) ;
				widget_tree__select(vPrev) ;
			}
			break ;
		case ASCII_DOWN	:
			if( widget_tree__is_leaf(vNode) )
			{
				var vNext = widget_tree__get_next_in_list(vNode) ;
				if( !vNext ) return ;
				widget_tree__select(vNext) ;
			}
			else // has first
			{
				var vState = vNode.getAttribute("_state") ? vNode.getAttribute("_state") : "opened" ;
				if( vState == "closed" )
				{
					var vNext = widget_tree__get_next(vNode) ;
					widget_tree__select(vNext) ;
				}
				else
				{
					var vFirst = widget_tree__get_first(vNode) ;
					widget_tree__select(vFirst) ;
				}
			}
			break ;
		case ASCII_LEFT	: widget_tree__collapse(vNode) ;	break ;
		case ASCII_RIGHT: widget_tree__expand(vNode) ;		break ;
		case ASCII_ESC	: widget_tree__unselect(vLast) ;	break ;
	}
}

function widget_tree__get_prev_in_list(vPrev)
{
	if( widget_tree__is_leaf(vPrev) )
	{
		return vPrev ;
	}
	else
	{
		var vState = vPrev.getAttribute("_state") ? vPrev.getAttribute("_state") : "opened" ;
		if( vState == "closed" ) return vPrev ;
		else
		{
			vLast = widget_tree__get_last(vPrev) ;
			return widget_tree__get_prev_in_list(vLast) ;
		}
	}
}

function widget_tree__get_next_in_list(vNode)
{
	if( widget_tree__has_next(vNode) )
	{
		var vNext = widget_tree__get_next(vNode) ;
		widget_tree__select(vNext) ;
	}
	else
	{
		if( !widget_tree__has_parent(vNode) ) return null ;
		else
		{
			vParent = widget_tree__get_parent(vNode) ;
			return widget_tree__get_next_in_list(vParent) ;
		}
	}
}

function widget_tree__insert_node(vParent,vIcon,vTitle)
{

}