function TreeRenderer() {
}

function TreeRenderer_setValues(controller, className, minusImage,plusImage,blankImage,searchUrl,catIDQS) {
	this.controller = controller;
	this.controller.setRenderer(this);
	this.searchUrl = searchUrl;
	this.controllerName = className;
	this.minusImage = minusImage;
	this.plusImage = plusImage;
	this.blankImage = blankImage;
	this.catIDQS = catIDQS;
	this.lastSelectedNode = null;
}

function TreeRenderer_defineLabelStylesByDepth() {
	// go through all the arguments
	args = this.defineLabelStylesByDepth.arguments;
	for( var i=0; i < args.length; i++ ) {
		this.depthStyle[i] = args[i];	
	}
}
function TreeRenderer_setSelectedRowStyle( selectedRowClass ) {
	this.selectedRowClass = selectedRowClass;	
}
function TreeRenderer_setRolloverRowStyle( rolloverStyle ) {
	this.rolloverStyle = rolloverStyle;
}
//-----------------------------------------------------------------------------------------------------------
function TreeRenderer_build(rootNode,locationID) {
	return(this.buildHTML(rootNode));
}
//-----------------------------------------------------------------------------------------------------------
function TreeRenderer_buildHTML(node) {
	var icon = this.buildIconHTML(node);
	var html = '';
	
	html += icon;
	html += '<a href="';
	html += this.createSearchUrl(node.id);
	html += '" class="topicLink';
	if (node.selected)
	{
	    html += ' selected';
	}
	html += '" title="';
	if (node.description != null)
	{
	    html += node.description;
	}
	else 
	{
	    html += node.label;
	}
	html += '">';
	html += node.label;
	html += '</a>';

    if(node.expanded && node.hasChildren)//||node.depth == 0
    {
        html += '<ul';
        if (node.expanded)
        {
            html += ' class="expanded" ';
        }
        html += '>';
        for( var iChild = 0; iChild < node.children.length; iChild++ )
        {
            html += '<li id="' + this.controllerName + node.children[iChild].id;
            if (node.children[iChild].selected)
            {
                html += '" class="selected';
            }
            html += '">';
		    html += this.buildHTML(node.children[iChild]);
		    html += '</li>';
	    }
	    html += '</ul>';
	
    }
    return html;
}

//-----------------------------------------------------------------------------------------------------------
function TreeRenderer_buildIconHTML(node){
    var str = '';
    var image;
    var alt;
    if (node.hasChildren())
    {
        if(node.expanded)
        {
            image = this.minusImage;
            alt = 'Hide subtopics for ' + node.label;
        }
        else
        {
            image = this.plusImage;
            alt = 'Show subtopics for ' + node.label;
        }
    }
    else 
    {
        image = this.blankImage;
        alt = node.label + ' has no subtopics';
        
    }
    if (node.hasChildren())
    {
        var base = '';
        var query = '';
        var url = location.href;
        
        if (url.indexOf("?") != -1)
        {
            base = url.substring(0,url.indexOf("?"))
            query = url.substring(url.indexOf("?"))
        }
        else
        {
            base = url;
            query = "?";
        }
        
        str += '<a href=\"' + base + this.updateQueryString(query,node.id);
        str += '" onclick="' + this.controllerName + '.junctionClick(' + node.id + ');return false;" class="';
        if (node.expanded)
        {
            str += 'contract';
        }
        else
        {
            str += 'expand';
        }
        str += '">';
     }
     str += '<img border=\"0\" src=\"' + image + '" alt="' + alt + '">'
     
     if (node.hasChildren())
     {
        str += '</a>';
     }
     return str;
}

//-----------------------------------------------------------------------------------------------------------
function TreeRenderer_collapse(node){
    if (this.lastSelectedNode == node.id)
    {
        this.lastSelectedNode = null;
    }
	var innerHTML;
	var nodeContent = document.getElementById(this.controllerName + node.id );
	if (node.selected)
	{
	    nodeContent.className = 'selected';
	}
	else
	{
	    nodeContent.className = '';
	}
	if( nodeContent ){
		nodeContent.innerHTML = this.buildHTML(node);
	}
}
//-----------------------------------------------------------------------------------------------------------
function TreeRenderer_expand(node){
    //first unselect the last selected node
    if (this.lastSelectedNode != null)
    {
        //this.lastSelectedNode.selected = false // deselect the old node
        //var lastNodeContent = document.getElementById(this.controllerName + this.lastSelectedNode.id)
        //lastNodeContent.className = 'expanded';
        //lastNodeContent.innerHTML = this.buildHTML(this.lastSelectedNode);
    }
    
    //now expand the current node
	var innerHTML;
	var nodeContent = document.getElementById(this.controllerName + node.id );
	if (node.selected)
	{
	    nodeContent.className = 'selected'
	}
	else
	{
	    nodeContent.className = '';
	}
	if( nodeContent ){
	    //node.selected = true; //select the current node
		nodeContent.innerHTML = this.buildHTML(node);
	}
	//set this node to be the last one selected
    this.lastSelectedNode = node;
}

//updates the given querystring to use the categoryID of the current node
function TreeRenderer_updateQueryString(query,nodeID)
{	
	//remove the old categoryID (if any)
	if (query.indexOf(this.catIDQS) != -1) {
		var spos = query.indexOf(this.catIDQS);
		var len = query.indexOf('&',spos);
		if (len == -1) {
			query = query.substr(0,spos);		
		} else {
			query = query.substr(0,spos) + query.substr(len + spos)
		}
	}
    //add the new id back in
	 if (query.substr(query.length -1) != "?") {
			query += "&";
		}
	
		query += this.catIDQS + '=' + nodeID;
		return query;
}

//Gets the current url and updates the querystring to point at the search page
function TreeRenderer_createSearchUrl(nodeID)
{
    var searchUrl = this.searchUrl;
    if (searchUrl.indexOf('?') != searchUrl.length - 1)
    {
        searchUrl += "&";
    }
    searchUrl += this.catIDQS;
    searchUrl += "=";
    searchUrl += nodeID;
    return searchUrl;
}

//-----------------------------------------------------------------------------------------------------------
TreeRenderer.prototype = new Object();
TreeRenderer.prototype.buildHTML = TreeRenderer_buildHTML;
//TreeRenderer.prototype.getTemplate = TreeRenderer_getTemplate;
//TreeRenderer.prototype.buildInnerHTML = TreeRenderer_buildInnerHTML;
TreeRenderer.prototype.buildIconHTML = TreeRenderer_buildIconHTML;
//TreeRenderer.prototype.buildLabelHTML = TreeRenderer_buildLabelHTML;
TreeRenderer.prototype.expand = TreeRenderer_expand;
TreeRenderer.prototype.collapse = TreeRenderer_collapse;
TreeRenderer.prototype.build = TreeRenderer_build;
TreeRenderer.prototype.defineLabelStylesByDepth = TreeRenderer_defineLabelStylesByDepth;
TreeRenderer.prototype.setSelectedRowStyle = TreeRenderer_setSelectedRowStyle;
TreeRenderer.prototype.setRolloverRowStyle = TreeRenderer_setRolloverRowStyle;
//TreeRenderer.prototype.doMouseOut = TreeRenderer_doMouseOut;
//TreeRenderer.prototype.doMouseOver = TreeRenderer_doMouseOver;
TreeRenderer.prototype.setValues = TreeRenderer_setValues;
TreeRenderer.prototype.createSearchUrl = TreeRenderer_createSearchUrl;
TreeRenderer.prototype.updateQueryString = TreeRenderer_updateQueryString;

