// JavaScript Document
function TreeControl(oTextbox /*:HTMLInputElement*/, 
                            oProvider /*:SuggestionProvider*/,
                            id /*newspaper id*/) {
    /**
     * Suggestion provider for the autosuggest feature.
     * @scope private.
     */
    this.provider /*:SuggestionProvider*/ = oProvider;
    
    /**
     * The textbox to capture.
     * @scope private
     */
    this.textbox /*:HTMLInputElement*/ = oTextbox;
    this.newspaperId = id;

    /**
     * The dropdown list layer.
     * @scope private
     */
    this.layer = null;
    this.flayer = null;
    this.callwrapper = null;
    this.ptitle = null;
	
	this.tree = null;
	
	this.ids = null;
	
    //initialize the control
    this.init();
    
}

/**
 * Initializes the textbox with event handlers for
 * auto suggest functionality.
 * @scope private
 */
 TreeControl.prototype.init = function () {
    this.createTree();
    this.provider.requestRootNodeTree(this); //this.provider.requestNodeTree(this);
 };


/**
 * Handles keyup events.
 * @scope private
 * @param oEvent The event object for the keyup event.
 */
TreeControl.prototype.handleKeyUp = function (oEvent /*:Event*/) {

    var iKeyCode = oEvent.keyCode;

    //make sure not to interfere with non-character keys
    if (iKeyCode < 32 || (iKeyCode >= 33 && iKeyCode <= 46) || (iKeyCode >= 112 && iKeyCode <= 123)) {
        //ignore
    } else {
        //request suggestions from the suggestion provider
        //this.provider.requestNodeTree(this);
    }
};

TreeControl.prototype.autosuggest = function (aNames /*:Array*/,aIds /*:Array*/) {
    
    //re-initialize pointer to current suggestion
    this.cur = -1;
    //make sure there's at least one suggestion
    if (aNames.length > 0) {
        this.showTree(aNames,aIds);
    } else {
        //this.hideSuggestions();
    }
};

TreeControl.prototype.settitle = function (nptitle) {
   this.ptitle = nptitle;
};

TreeControl.prototype.showTree = function (aNames /*:Array*/,aIds /*:Array*/) {
   this.layer.innerHTML = "";	
   var myTree = new tree();
   	
   var header1 = new header('nph_001',this.ptitle);
   myTree.add(header1);
   
   for (var i=0; i < aNames.length; i++) {
       var branch1 = new branch(aIds[i],aNames[i]);
       myTree.add(branch1);
   }
   myTree.write(this.layer);  
   //this.layer.style.left = this.getLeft() + "px";
   //this.layer.style.background='#f6f6f6';
   //this.layer.style.top = (this.getTop()+this.textbox.offsetHeight) + "px";
   //this.layer.style.height = getWHeight();
   this.layer.style.visibility = "visible";
   this.tree = myTree;
   
   //this.flayer.innerHTML = "";	
   //this.flayer.style.left = (this.getLeft()+260) + "px";
   //this.flayer.style.top = (this.getTop()+this.textbox.offsetHeight) + "px";
   //this.flayer.style.height = getWHeight();
   //this.flayer.style.visibility = "visible";
};
   
/**
 * Gets the left coordinate of the textbox.
 * @scope private
 * @return The left coordinate of the textbox in pixels.
 */
TreeControl.prototype.getLeft = function () /*:int*/ {

    var oNode = this.textbox;
    var iLeft = 0;
    
    while(oNode.tagName != "BODY") {
        iLeft += oNode.offsetLeft;
        oNode = oNode.offsetParent;        
    }
    
    return iLeft;
};
function getElementLeft(){

    var oNode = document.getElementById('lefttop'); //txt1
    var iLeft = 0;
    
    while(oNode.tagName != "BODY") {
        iLeft += oNode.offsetLeft;
        oNode = oNode.offsetParent;        
    }
    
    return iLeft;
};

//TreeControl.prototype.getWHeight = 
function getWHeight() {
     var height = (window.innerHeight) ? window.innerHeight : document.body.offsetHeight;
     if(height>60) height=height-60;
     return height;
}
//TreeControl.prototype.
 function getWWidth() {
     var width = (window.innerWidth) ? window.innerWidth+19 : document.body.offsetWidth;
     return width;
}
/**
 * Gets the top coordinate of the textbox.
 * @scope private
 * @return The top coordinate of the textbox in pixels.
 */
TreeControl.prototype.getTop = function () /*:int*/ {

    var oNode = this.textbox;
    var iTop = 0;
    
    while(oNode.tagName != "BODY") {
        iTop += oNode.offsetTop;
        oNode = oNode.offsetParent;
    }
    
    return iTop;
};

TreeControl.prototype.addChildes = function (oBranches,id,name) {
    
    if(oBranches){
    for (var i=0; i < oBranches.length; i++) {
	  this.ids.push(oBranches[i].id);
	  if(oBranches[i].id == id){
		  if(oBranches[i].leaves.length == 0){
			  // add
		    if(oBranches[i].id && oBranches[i].id.indexOf('full_')==0){
		        this.provider.requestFullPageNodeChildes(this,oBranches[i],name);
		        return true;
		    }else{
		        // pu wait.. message
                        //var leaf1 = new leaf('wait..','wwait'); //name,id
                        //oBranches[i].add(leaf1);
                        //this.reWriteTree(name);
		        //
  	 	        this.provider.requestNodeChildes(this,oBranches[i],name);
			return true; 
		    }
		  }
	  }else{
	        if(this.addChildes(oBranches[i].leaves,id,name)){
			  return true;
		}
	  }
	  this.ids.length = this.ids.length-1;
  }
  }
  return;
}

TreeControl.prototype.reWriteTree = function (id) {
          
	//this.layer.innerHTML = "";
	//this.tree.write(this.layer);
	el(searchNodeId).innerHTML = "";
	this.tree.write(el(searchNodeId));
	//for(var i =0;i<this.ids.length;i++){
	//	showBranch(this.ids[i]);
	//}
			
			
}

TreeControl.prototype.createTree = function () {
    //create the layer and assign styles

    this.layer = el("myware-folders-body");
    //this.layer.className = "browse";
    //this.layer.style.visibility = "hidden";
    //this.layer.style.width = '200px'; //getWWidth()-290-this.getLeft(); //this.textbox.offsetWidth+300;
   
    var oThis = this;
    
    this.layer.onclick = function (oEvent) {
        oEvent = oEvent || window.event;
        oTarget = oEvent.target || oEvent.srcElement;
        if(oEvent.type == "click"){
            var nodeId = '',name='';
            if(oTarget.firstChild!=null){
               nodeId = oTarget.firstChild.id;
               name = oTarget.firstChild.name;
               }
            else{
               nodeId = oTarget.id;
               name = oTarget.name;
               }
  	      oThis.ids = new Array();
  	      //
  	       //oThis.layer = el(nodeId.substring(1));
  	       //alert(getCurrentBranch().leaves);
  	       //oThis.addChildes(getCurrentBranch().leaves,nodeId.substring(1),name);
  	       
  	      //
	      oThis.addChildes(oThis.tree.branches,nodeId.substring(1),name);
            }

    };	
}
