/* Copyright (c) 2006 Yahoo! Inc. All rights reserved. */
/* *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
 *
 * Adds a custom event to 
 *
 * @extends YAHOO.widget.TextNode
 * @constructor
 * @param oData {object} a string or object containing the data that will
 * be used to render this node
 * @param oParent {YAHOO.widget.Node} this node's parent node
 * @param expanded {boolean} the initial expanded/collapsed state
 * @param checked {integer} the initial state.  whether the icon ... is
 *        shown when one child is shown, but more exist. either BLANK, HAS_MORE, IS_LEAF
 * *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** */
 
YAHOO.widget.MyNode = function(oData, oParent, expanded, checked, myuid) {

        this.checkState = checked;
        this.uid = myuid;
        this.node_clicked = false;
	if (oParent) { 
		this.init(oData, oParent, expanded);
		this.setUpLabel(oData);

	}
};

YAHOO.widget.MyNode.prototype = new YAHOO.widget.TextNode();

/**
 *  0 = more children , but not yet queried (has to be more than 1 because one is already showing
 *  1 = isLeaf == no children
 *  2 = node has queried for its children -- basically a blank icon
 *  3 = a temporary state indicating that this is the node that has requested the query
 * false if 0.
 *
 * @type boolean
 */
//YAHOO.widget.MyNode.prototype.checked = false;
YAHOO.widget.MyNode.prototype.labelStyle = "lbl";

/**
 * checkState
 * 10 = BLANK - no ... icon needed
 * 11 = HAS_MORE  show the ... icon
 * 12 = IS_LEAF is true -- no reason to query for children. 
 *
 * @type int
 */

/**
 * The id of the check element
 *
 * @type string
 */
YAHOO.widget.MyNode.prototype.getCheckElId = function() { 
	return "ygtvdot" + this.index; 
};

/**
 * Returns the check box element
 *
 * @return the check html element (img)
 */
YAHOO.widget.MyNode.prototype.getCheckEl = function() { 
	return document.getElementById(this.getCheckElId()); 
};

/* *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *
 * Either there is the ... icon indicating that some, but not all children
 * have been exposed.  or there is no icon, but I still need to connect
 * the leaves in tree format.  so this is a copy of the getStyle in the
 * node class.
 *
 * @return {string} the css style for the current check state
 * *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** */
YAHOO.widget.MyNode.prototype.getMyStyle = function() { 

        var loc ;

/******
        if (! this.isRoot()  && this.tree.root.hasChildren() ) {
            if (this.tree.root.children[0].data.id == this.data.id )
               loc = "r";
            else
               loc = (this.nextSibling) ? "t" : "l";
        }
        if (this.index == 1 ) {
doDebug (" 666-6 my style is root and expanded? " + this.expanded );
doDebug (" is root? " + this.isRoot() );
doDebug (" this.tree.root ? " + this.tree.root.children.length );
doDebug (" is this node the first child of the root?  " + this.tree.root.children[0].data.id + " " + this.data.id );
            loc = "r";
        } 
        else
         loc = (this.nextSibling) ? "t" : "l"; *****/

         loc = (this.nextSibling) ? "t" : "l"; 
        var type = "n"; 
        if (this.checkState == HAS_MORE )
            type = "m";
        else if (this.expanded )
            type = "e";
	return "ygtvdot" + loc + type ;
};

/* *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** */
YAHOO.widget.MyNode.prototype.needsQuery = function () {
   var flag = false;
   if (this.data && this.data.nchildren ) {
       if (this.data.nchildren > 0 && (this.children == 0 || this.children.length < this.data.nchildren  ))
           flag = true;
   }
   return flag;
}

/* *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
 * Returns the link that will invoke this node's check toggle
 *
 *  my behavour is different.  if checked or clicked the first time, query
 *  for children.
 * *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** */
YAHOO.widget.MyNode.prototype.getChildrenClick = function() { 
   
       //if (this.checkState == HAS_MORE  || this.checkState == NODE_CLICKED ) {
       //if (this.checkState == HAS_MORE  || this.data.node_clicked == true ) {
       if (this.checkState == HAS_MORE  ) {
          //var widget = document.getElementById ('buttonId'+this.uid).widget;

          //set this checkstate to identify which node has been clicked.  maybe this
          //this.data.node_clicked = true;

          //send a message to the OntologyTreeBrowser with this node.
          //widget.treeBrowser.queryForChildren (this, this.uid);

          OntologyTreeBrowser.prototype.queryForChildren (this, this.uid);
          this.updateCheckHtml();
          
       } 
}

/* *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** */
YAHOO.widget.MyNode.prototype.getCheckLink = function() { 

	return "YAHOO.widget.TreeView.getNode(\'" + this.tree.id + "\'," + 
		this.index + ").checkClick()"; 
};


/* *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *
 * Invoked when the user clicks the dot box
 * *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** */
YAHOO.widget.MyNode.prototype.checkClick = function() { 

        this.data.node_clicked = true;
	if (this.checkState == HAS_MORE || this.needsQuery()) {
             this.getChildrenClick();
        }
};



/* *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *
 * If the node has been rendered, update the html to reflect the current
 * state of the node.
 * *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** */
YAHOO.widget.MyNode.prototype.updateCheckHtml = function() { 
	if (this.parent && this.parent.childrenRendered) {
		this.getCheckEl().className = this.getMyStyle();
	}
};

/* *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *
 * 
 * @param the new check state
 * *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** */
YAHOO.widget.MyNode.prototype.setCheckState = function(state) { 
	this.checkState = state;
};


/* *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *
 *  when a term in the tree is clicked, put it in the text area, highlight it, locate it through the
 *  the tree, and unhighlight the previous one
 * *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** */

//YAHOO.widget.MyNode.prototype.labelClicked = function(me, myuid ) {
YAHOO.widget.MyNode.prototype.labelClicked = function() {

     var ta = document.getElementById ('TA'+this.uid);
     var oldId = ta.getAttribute ('termId'); 
     ta.value = this.data.label;
     ta.setAttribute ('termId',   this.data.id);  
     
};


/* *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *
 *   override getNodeHtml in Text node.  Add another icon and its behavior.  the label is no longer a
 *   href.
 * *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** */

// Overrides YAHOO.widget.TextNode
YAHOO.widget.MyNode.prototype.getNodeHtml = function() { 
	var sb = new Array();
        var getNode = 'YAHOO.widget.TreeView.getNode(\''+this.tree.id + '\',' + this.index + ')';

	sb[sb.length] = '<table border="0" cellpadding="0" cellspacing="0">';
	sb[sb.length] = '<tr>';
	
	for (i=0; i <this.depth;++i) {
		sb[sb.length] = '<td class="' + this.getDepthStyle(i) + '">&nbsp;</td>';
	}

	sb[sb.length] = '<td';
	sb[sb.length] = ' id="' + this.getToggleElId() + '"';
	sb[sb.length] = ' class="' + this.getStyle() + '"';
	if (this.hasChildren(true)) {
		sb[sb.length] = ' onmouseover="this.className=';
		sb[sb.length] = 'YAHOO.widget.TreeView.getNode(\'';
		sb[sb.length] = this.tree.id + '\',' + this.index +  ').getHoverStyle()"';
		sb[sb.length] = ' onmouseout="this.className=';
		sb[sb.length] = 'YAHOO.widget.TreeView.getNode(\'';
		sb[sb.length] = this.tree.id + '\',' + this.index +  ').getStyle()"';
	}
	sb[sb.length] = ' onclick="javascript:' + this.getToggleLink() + '">&nbsp;';
	sb[sb.length] = '</td>';

          
	// dot box
	sb[sb.length] = '<td';
	sb[sb.length] = ' id="' + this.getCheckElId() + '"';
	sb[sb.length] = ' class="' + this.getMyStyle() + '"';
	sb[sb.length] = ' onclick="javascript:' + this.getCheckLink() + '">';
	sb[sb.length] = '&nbsp;</td>';
	
     sb[sb.length] = '<td>';
        sb[sb.length] = '<a';
        sb[sb.length] = ' id="' + this.labelElId + '"';
        sb[sb.length] = ' class="' + this.labelStyle + '"';
        sb[sb.length] = ' onclick="return ' + getNode + '.onLabelClick(' + getNode +')"';
        sb[sb.length] = ' onmouseover="document.getElementById(\'' +this.labelElId + '\').className =\'';
        sb[sb.length] = this.labelStyle + 'h\'"';

        sb[sb.length] = ' onmouseout="document.getElementById(\'' +this.labelElId + '\').className= \'';
        sb[sb.length] = this.labelStyle + '\'"';

        if (this.hasChildren(true)) {
                sb[sb.length] = ' onmouseover="document.getElementById(\'';
                sb[sb.length] = this.getToggleElId() + '\').className=';
                sb[sb.length] = getNode + '.getHoverStyle()"';
                sb[sb.length] = ' onmouseout="document.getElementById(\'';
                sb[sb.length] = this.getToggleElId() + '\').className=';
                sb[sb.length] = getNode + '.getStyle()"';
        }
        sb[sb.length] = ' >';
        sb[sb.length] = this.label;
        sb[sb.length] = '</a>';
        sb[sb.length] = '</td>';
        sb[sb.length] = '</tr>';
        sb[sb.length] = '</table>';

	return sb.join("");

};
