/**
 * FileStreamer
 * Copyright (c) 2006 MakProject.com. All Rights Reserved.
 * http://www.filestreamer.makproject.com
 *
 * Authors:
 *  Toni Rajkovski <rtoni@makproject.com>
 *  Vladimir Jakimov <vjakimov@makproject.com>
 */
 

/**
 *  SubCategoryItem Class
 */
function SubCategoryItem(itemNode, ownerTreeParam){
	this.loadItemFromNode(itemNode);
	this.ownerTree = ownerTreeParam;
	this.URL = this.ownerTree.URL;
};

SubCategoryItem.prototype.ownerTree;
SubCategoryItem.prototype.URL;

SubCategoryItem.prototype.subCateogoryId;
SubCategoryItem.prototype.parentId;
SubCategoryItem.prototype.name;
SubCategoryItem.prototype.hasChilds;
SubCategoryItem.prototype.level;
SubCategoryItem.prototype.allowUploads;
SubCategoryItem.prototype.expanded;

SubCategoryItem.prototype.getNodeNameElementValue = function(itemNode, elementName){
    var element = itemNode.getElementsByTagName(elementName).item(0);
    if(element.childNodes.length > 0)
        return element.childNodes.item(0).data;
    else return "";
}

SubCategoryItem.prototype.loadItemFromNode = function(itemNode){
    this.subCategoryId = this.getNodeNameElementValue(itemNode, "category_id");
    this.parentId = this.getNodeNameElementValue(itemNode, "parent_id");
    this.name = this.getNodeNameElementValue(itemNode, "name");
    this.hasChilds = (this.getNodeNameElementValue(itemNode, "has_childs") == '1') ? true : false;
    this.level = this.getNodeNameElementValue(itemNode, "level");
    this.allowUploads = (this.getNodeNameElementValue(itemNode, "allow_uploads") == '1') ? true : false;
    this.expanded = false;
}

SubCategoryItem.prototype.getDisplayString = function(){
	var html = "<tr><td valign='top'><table border='0' cellpadding='1' cellspacing='0' align='left'>";
	html +="<tr><td style='padding-left:" + (this.level * 20).toString() + "px; padding-top:2px;' valign='top'>";
	if(this.hasChilds){
		if(this.expanded) html += "<a href='javascript:" + this.ownerTree.instanceName.toString() + ".minusClick(" + this.subCategoryId + ")'><img src='" + this.URL + "images/icons/minus.jpg' border='0'/></a>";
		else html += "<a href='javascript:" + this.ownerTree.instanceName.toString() + ".plusClick(" + this.subCategoryId + ")'><img src='" + this.URL + "images/icons/plus.jpg' border='0' /></a>";
	} else {
		html += "<img src='" + this.URL + "images/icons/no_plus_minus.gif' border='0'>";
	}
	html += "</td>"

	html += "<td valign='top'  style='padding-top:1px;'>"
	if (this.allowUploads && this.expanded) html += "<img src='" + this.URL + "images/icons/folder_opened_uploads.jpg' border='0' hspace='2' />";
	else if (this.allowUploads && !this.expanded) html += "<img src='" + this.URL + "images/icons/folder_uploads.jpg' border='0' hspace='2' />";
	else if (!this.allowUploads && this.expanded) html += "<img src='" + this.URL + "images/icons/folder_opened.jpg' border='0' hspace='2' />";
	else if (!this.allowUploads && !this.expanded) html += "<img src='" + this.URL + "images/icons/folder.jpg' border='0' hspace='2' />";
	
	html += "</td><td valign='top' style='padding-bottom:0px;'>"
	
	html += "<a href='javascript:" + this.ownerTree.instanceName.toString() + ".linkClick(" + this.subCategoryId.toString() + ")'>";
	html += this.name;
	html += "</a>";
	
	html += "</td></tr></table></td></tr>";
	return html;
}




/**
 *  SubCategoryTree Class
 */
function SubCategoryTree(instanceNameParam, divNameParam, topParentIdParam, url){
	this.items = new Array();
	this.divName = divNameParam;
	this.instanceName = instanceNameParam;
	this.topParentId = topParentIdParam;
	this.URL = url;
}

SubCategoryTree.prototype.URL;
SubCategoryTree.prototype.items;
SubCategoryTree.prototype.divName;
SubCategoryTree.prototype.instanceName;
SubCategoryTree.prototype.topParentId;


SubCategoryTree.prototype.loadTreeFromXml = function(subCategoriesTreeXml){
	var rootElem = subCategoriesTreeXml.childNodes[0];
    for (var i=0; i < rootElem.childNodes.length; i++) {
        var elem = rootElem.childNodes[i];
        var item = new SubCategoryItem(elem, this);
        this.items.push(item);
    }
}

SubCategoryTree.prototype.display = function(html){
    var element = document.getElementById(this.divName);
    element.innerHTML = html;
};

SubCategoryTree.prototype.getSubCategoryItemById = function(subCategoryId){
    for(var i=0; i < this.items.length; i++){
    	var item = this.items[i];
    	if(item.subCategoryId == subCategoryId)
    		return item;
    };
    return false;
};


SubCategoryTree.prototype._getDisplayStringRec = function(parentId){
	var html = "";
    for(var i=0; i < this.items.length; i++){
    	var item = this.items[i];
    	if(item.parentId == parentId){
    		html += item.getDisplayString();
    		if(item.expanded)
    			html += this._getDisplayStringRec(item.subCategoryId);
    	}
    };
    return html;
};


SubCategoryTree.prototype.getDisplayString = function(){
	var html = "<table cellspacing='0' cellpadding='0' border='0'>";
	html += this._getDisplayStringRec(this.topParentId);
    html += "</table>";
    return html;
};

SubCategoryTree.prototype.refreshTree = function(){
	var html = this.getDisplayString();
	this.display(html);
};

SubCategoryTree.prototype.plusClick = function(subCategoryId){
	var item = this.getSubCategoryItemById(subCategoryId);
	item.expanded = true;
	this.refreshTree();
};

SubCategoryTree.prototype.minusClick = function(subCategoryId){
	var item = this.getSubCategoryItemById(subCategoryId);
	item.expanded = false;
	this.refreshTree();
};

SubCategoryTree.prototype.linkClick = function(subCategoryId){
	var item = this.getSubCategoryItemById(subCategoryId);
	if(!item.expanded) {
		item.expanded = true;
		this.refreshTree();
	}
	 parent.xftp.subCategoryClick(subCategoryId);
	// dispatch to xftp that subCategory is clicked
};

