/* Copyright (c) 2006 Yahoo! Inc. All rights reserved. */
YAHOO.widget.Node=function(oData,oParent,expanded){if(oParent){this.init(oData,oParent,expanded);}};YAHOO.widget.Node.prototype={index:0,children:null,tree:null,data:null,parent:null,depth:-1,href:null,target:"_self",expanded:false,multiExpand:true,renderHidden:false,childrenRendered:false,previousSibling:null,nextSibling:null,_dynLoad:false,dataLoader:null,isLoading:false,hasIcon:true,init:function(oData,oParent,expanded){this.data=oData;this.children=[];this.index=YAHOO.widget.TreeView.nodeCount;++YAHOO.widget.TreeView.nodeCount;this.logger=new ygLogger("Node");this.expanded=expanded;if(oParent){this.tree=oParent.tree;this.parent=oParent;this.href="javascript:"+this.getToggleLink();this.depth=oParent.depth+1;this.multiExpand=oParent.multiExpand;oParent.appendChild(this);}},appendChild:function(node){if(this.hasChildren()){var sib=this.children[this.children.length-1];sib.nextSibling=node;node.previousSibling=sib;}
this.tree.regNode(node);this.children[this.children.length]=node;return node;},getSiblings:function(){return this.parent.children;},showChildren:function(){if(!this.tree.animateExpand(this.getChildrenEl())){if(this.hasChildren()){this.getChildrenEl().style.display="";}}},hideChildren:function(){this.logger.debug("hiding "+this.index);if(!this.tree.animateCollapse(this.getChildrenEl())){this.getChildrenEl().style.display="none";}},getElId:function(){return"ygtv"+this.index;},getChildrenElId:function(){return"ygtvc"+this.index;},getToggleElId:function(){return"ygtvt"+this.index;},getEl:function(){return document.getElementById(this.getElId());},getChildrenEl:function(){return document.getElementById(this.getChildrenElId());},getToggleEl:function(){return document.getElementById(this.getToggleElId());},getToggleLink:function(){return"YAHOO.widget.TreeView.getNode(\'"+this.tree.id+"\',"+
this.index+").toggle()";},collapse:function(){if(!this.expanded){return;}
if(!this.getEl()){this.expanded=false;return;}
this.hideChildren();this.expanded=false;if(this.hasIcon){this.getToggleEl().className=this.getStyle();}
this.tree.onCollapse(this);},expand:function(){if(this.expanded){return;}
if(!this.getEl()){this.expanded=true;return;}
if(!this.childrenRendered){this.getChildrenEl().innerHTML=this.renderChildren();}
this.expanded=true;if(this.hasIcon){this.getToggleEl().className=this.getStyle();}
if(this.isLoading){this.expanded=false;return;}
if(!this.multiExpand){var sibs=this.getSiblings();for(var i=0;i<sibs.length;++i){if(sibs[i]!=this&&sibs[i].expanded){sibs[i].collapse();}}}
this.showChildren();this.tree.onExpand(this);},getStyle:function(){if(this.isLoading){this.logger.debug("returning the loading icon");return"ygtvloading";}else{var loc=(this.nextSibling)?"t":"l";var type="n";if(this.hasChildren(true)||this.isDynamic()){type=(this.expanded)?"m":"p";}
this.logger.debug("ygtv"+loc+type);return"ygtv"+loc+type;}},getHoverStyle:function(){var s=this.getStyle();if(this.hasChildren(true)&&!this.isLoading){s+="h";}
return s;},expandAll:function(){for(var i=0;i<this.children.length;++i){var c=this.children[i];if(c.isDynamic()){alert("Not supported (lazy load + expand all)");break;}else if(!c.multiExpand){alert("Not supported (no multi-expand + expand all)");break;}else{c.expand();c.expandAll();}}},collapseAll:function(){for(var i=0;i<this.children.length;++i){this.children[i].collapse();this.children[i].collapseAll();}},setDynamicLoad:function(fnDataLoader){this.dataLoader=fnDataLoader;this._dynLoad=true;},isRoot:function(){return(this==this.tree.root);},isDynamic:function(){var lazy=(!this.isRoot()&&(this._dynLoad||this.tree.root._dynLoad));return lazy;},hasChildren:function(checkForLazyLoad){return(this.children.length>0||(checkForLazyLoad&&this.isDynamic()&&!this.childrenRendered));},toggle:function(){if(!this.tree.locked&&(this.hasChildren(true)||this.isDynamic())){if(this.expanded){this.collapse();}else{this.expand();}}},getHtml:function(){var sb=[];sb[sb.length]='<div class="ygtvitem" id="'+this.getElId()+'">';sb[sb.length]=this.getNodeHtml();sb[sb.length]=this.getChildrenHtml();sb[sb.length]='</div>';return sb.join("");},getChildrenHtml:function(){var sb=[];sb[sb.length]='<div class="ygtvchildren"';sb[sb.length]=' id="'+this.getChildrenElId()+'"';if(!this.expanded){sb[sb.length]=' style="display:none;"';}
sb[sb.length]='>';if(this.hasChildren(true)&&this.expanded){sb[sb.length]=this.renderChildren();}
sb[sb.length]='</div>';return sb.join("");},renderChildren:function(){this.logger.debug("rendering children for "+this.index);var node=this;if(this.isDynamic()&&!this.childrenRendered){this.isLoading=true;this.tree.locked=true;if(this.dataLoader){this.logger.debug("Using dynamic loader defined for this node");setTimeout(function(){node.dataLoader(node,function(){node.loadComplete();});},10);}else if(this.tree.root.dataLoader){this.logger.debug("Using the tree-level dynamic loader");setTimeout(function(){node.tree.root.dataLoader(node,function(){node.loadComplete();});},10);}else{this.logger.debug("no loader found");return"Error: data loader not found or not specified.";}
return"";}else{return this.completeRender();}},completeRender:function(){this.logger.debug("renderComplete: "+this.index);var sb=[];for(var i=0;i<this.children.length;++i){sb[sb.length]=this.children[i].getHtml();}
this.childrenRendered=true;return sb.join("");},loadComplete:function(){this.logger.debug("loadComplete: "+this.index);this.getChildrenEl().innerHTML=this.completeRender();this.isLoading=false;this.expand();this.tree.locked=false;},getAncestor:function(depth){if(depth>=this.depth||depth<0){this.logger.debug("illegal getAncestor depth: "+depth);return null;}
var p=this.parent;while(p.depth>depth){p=p.parent;}
return p;},getDepthStyle:function(depth){return(this.getAncestor(depth).nextSibling)?"ygtvdepthcell":"ygtvblankdepthcell";},getNodeHtml:function(){return"";}};
