/* Copyright (c) 2006 Yahoo! Inc. All rights reserved. */

/**
 * Emulates OmniOutliner's task view.  The check box marks a task complete.
 * It is a simulated form field with three states ...
 * 0=unchecked, 1=some children checked, 2=all children checked
 * When a task is clicked, the state of the nodes and parent and children
 * are updated, and this behavior cascades.
 *
 * @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 showlink {boolean} display a link or just the text
 * @param rowclass {string} row class (currently TD.xxx)
 */
 
YAHOO.widget.mcNode = function(oData, oParent, expanded, showlink, rowclass) {
    
	if (oParent) { 
		this.init(oData, oParent, expanded);
		this.setUpLabel(oData);
	}
	var showlink;
	this.showlink = showlink;
	var rowclass;
	this.rowclass = rowclass;	
};

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


/**
 * Refresh the state of this node's parent, and cascade up.
 */
YAHOO.widget.mcNode.prototype.updateParent = function() { 
	var p = this.parent;

	if (!p || !p.updateParent) {
		this.logger.debug("Abort udpate parent: " + this.index);
		return;
	}

	var somethingChecked = false;
	var somethingNotChecked = false;

	for (var i=0;i< p.children.length;++i) {
		if (p.children[i].checked) {
			somethingChecked = true;
			// checkState will be 1 if the child node has unchecked children
			if (p.children[i].checkState == 1) {
				somethingNotChecked = true;
			}
		} else {
			somethingNotChecked = true;
		}
	}

	if (somethingChecked) {
		p.setCheckState( (somethingNotChecked) ? 1 : 2 );
	} else {
		p.setCheckState(0);
	}

	p.updateCheckHtml();
	p.updateParent();
};


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

// Overrides YAHOO.widget.TextNode
YAHOO.widget.mcNode.prototype.getNodeHtml = function() { 
	var sb = new Array();


	sb[sb.length] = '<table cellpadding="0" cellspacing="0" width="100%">';
	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>';

	sb[sb.length] = '<td class="'+this.rowclass +'">';
	if (this.showlink==true) {
		sb[sb.length] = '<a';
		sb[sb.length] = ' id="' + this.labelElId + '"';
		sb[sb.length] = ' class="' + this.labelStyle + '"';
		sb[sb.length] = ' href="' + this.href + '"';
		sb[sb.length] = ' target="' + this.target + '"';
		if (this.hasChildren(true)) {
			sb[sb.length] = ' onmouseover="document.getElementById(\'';
			sb[sb.length] = this.getToggleElId() + '\').className=';
			sb[sb.length] = 'YAHOO.widget.TreeView.getNode(\'';
			sb[sb.length] = this.tree.id + '\',' + this.index +  ').getHoverStyle()"';
			sb[sb.length] = ' onmouseout="document.getElementById(\'';
			sb[sb.length] = this.getToggleElId() + '\').className=';
			sb[sb.length] = 'YAHOO.widget.TreeView.getNode(\'';
			sb[sb.length] = this.tree.id + '\',' + this.index +  ').getStyle()"';
		}
		sb[sb.length] = ' >';
		sb[sb.length] = this.label;
		sb[sb.length] = '</a>';
	} else {
		sb[sb.length] = this.label;
	}
	
	sb[sb.length] = '</td>';
	sb[sb.length] = '</tr>';
	sb[sb.length] = '</table>';

	return sb.join("");

};

