var UNTreeHandler = {
    MaxSize: 200,
    all: {},
    UserLanguage: 'is',
    
    toggle: function(oItem) {
        this.all[oItem.id.replace('link-', '')].toggle();
        return false;
    },
    expand: function(code) {
        this.all[code].expand();
    },
    selectCategory: function(code) {
        try {
            SelectCategory(code);
        } catch (e) { }
    },
    DisplayProducts: function(code) {
        try {
            ShowProducts(code);
        } catch (e) { }
    },
    expandchild: function(code) {
        try {
            this.all[code.id].toggle();
            stopUpdateWindow();
            return false;
        } catch (e) { }
    }
}

function UNTreeAbstractNode(code, title) {
    this.Type = 'UNTreeAbstractNode';
    this.Code = code;
    this.Title = title;
    this.ChildNodes = [];

    UNTreeHandler.all[this.Code] = this;
}

UNTreeAbstractNode.prototype.add = function(node) {
    node.parentNode = this;
    this.ChildNodes[this.ChildNodes.length] = node;

    var root = this;

    while (root.parentNode) { root = root.parentNode; }

    if (root.rendered) {
        $get("content-" + this.Code).innerHTML += node.toString();
    }

    return node;
}

UNTreeAbstractNode.prototype.toggle = function() {
    alert(this.Code);
}

UNTreeAbstractNode.prototype.expand = function() {
    alert('NOT IMPLEMENTED ERROR');
}

UNTreeAbstractNode.prototype.Reset = function() {
      for (var i = 0; i < this.ChildNodes.length; i++) {
        if (this.ChildNodes[i].loading != null)
            this.ChildNodes[i].loading = false;
        if (this.ChildNodes[i].ChildNodes.length > 0) {
            for (var j = 0; j < this.ChildNodes[i].ChildNodes.length; j++) {
                ResetTreeHandler(this.ChildNodes[i].ChildNodes[j]);
            }
            this.ChildNodes[i].ChildNodes = [];
        }
    }
}
function ResetTreeHandler(node) {
    UNTreeHandler.all[node.Code] = null;
    if (node.ChildNodes.length > 0) {
        for (var i = 0; i < node.ChildNodes.length; i++) {
            ResetTreeHandler(node.ChildNodes[i]);
        }
    }
}

function UNTree() {
    this.Type = 'UNTree';
    this.rendered = false;
    UNTreeHandler.UserLanguage = (readCookie('language') != 'en') ? 'is' : 'en';
}

UNTree.prototype = new UNTreeAbstractNode;



UNTree.prototype.toString = function() {
    var sb = [];

    for (var i = 0; i < this.ChildNodes.length; i++) {
        sb[sb.length] = this.ChildNodes[i].toString();
    }

    this.rendered = true;

    return '<ul>' + sb.join('') + '</ul>';
}

function UNTreeItem(Segment, Family, Class, Commodity, code, title, count) {
    this.base = UNTreeAbstractNode;
    this.base(code, title);
    this.Type = 'UNTreeItem';

    this.Segment = Segment;
    this.Family = Family;
    this.Class = Class;
    this.Commodity = Commodity;

    this.Count = count;
}

UNTreeItem.prototype = new UNTreeAbstractNode;

UNTreeItem.prototype.toggle = function() {
    //debugger;
    var code = this.Segment;
    if ((this.Family * 1) > 0) {
        code += '' + this.Family;
    }
    if ((this.Class * 1) > 0) {
        code += '' + this.Class;
    }
    if ((this.Commodity * 1) > 0) {
        code += '' + this.Commodity;
    }
    code += '*';

    var i = '';
    if (this.Segment == 0)
        i = i + '00';
    else
        i = i + this.Segment;

    if (this.Family == 0)
        i = i + '00';
    else
        i = i + this.Family;

    if (this.Class == 0)
        i = i + '00';
    else
        i = i + this.Class;

    if (this.Commodity == 0)
        i = i + '00';
    else
        i = i + this.Commodity;
    processTree('root-' + i);
    if (this.Family == 0 && this.Class == 0 && this.Commodity == 0)
        setAndColl('root-' + i);
    
    if (this.loading) {
        this.expand();
        UNTreeHandler.DisplayProducts(code);

        return;
    }

   this.loading = true;
    ShowUpdateWindow();
    ecSoftware.eMarketPlace.Web.SOAP.FrontEnd.GetUNSPSCList(this.Segment, this.Family, this.Class, this.Commodity, OnSuccessExpand, OnErrorExpand, this);

    UNTreeHandler.DisplayProducts(code);

}

var GID = '';

function setAndColl(val) {

   // alert('1qqq' + val + 'sss' + GID);
    
    
    if (GID == '') {
        GID = val;

    }
    if (GID != val) {
        

        var obj = document.getElementById(GID);
        
        collapseTree(obj);
        GID = val;
    }

}

UNTreeItem.prototype.hide = function() {
    $get("link-" + this.Code).className = 'hide';
    $get("content-" + this.Code).className = 'hide';
    $get("root-" + this.Code).className = 'hide';
}

UNTreeItem.prototype.show = function() {
    var objLink = $get("link-" + this.Code);
    var objContent = $get("content-" + this.Code);
    var objRoot = $get("root-" + this.Code);

    if (objLink)
        objLink.className = 'show_inline';

    if (objContent) {
        objContent.className = 'hide';
    }

    if (objRoot) {
        objRoot.className = '';
        (objRoot.parentNode).parentNode.className = 'selected';
	
    }

}

UNTreeItem.prototype.hideContent = function() {
    var objContent = $get("content-" + this.Code);

    if (objContent) {
        objContent.parentNode.className = '';
        objContent.className = 'hide';
    }
}


UNTreeItem.prototype.expand = function() {
    if (this.parentNode.Type != 'UNTree') {
        for (var i = 0; i < this.parentNode.ChildNodes.length; i++) {
            if (this.parentNode.ChildNodes[i] != this)
                this.parentNode.ChildNodes[i].hide();
        }
    } else {
        for (var i = 0; i < this.parentNode.ChildNodes.length; i++) {
            if (this.parentNode.ChildNodes[i] != this)
                this.parentNode.ChildNodes[i].hideContent();
        }
    }

    for (var i = 0; i < this.ChildNodes.length; i++) {
        this.ChildNodes[i].show();
    }

    $get("content-" + this.Code).className = 'show';
    $get("content-" + this.Code).parentNode.className = 'selected'
}

function OnSuccessExpand(results, context) {
    for (var i = 0; i < results.length; i++) {
        var title = '';
        if (UNTreeHandler.UserLanguage == 'en')
            title = (results[i].Title.length == 0) ? results[i].IS_Title : results[i].Title;
        else
            title = (results[i].IS_Title.length == 0) ? results[i].Title : results[i].IS_Title;

        UNTreeHandler.all[context.Code].add(new UNTreeItem(results[i].Segment, results[i].Family, results[i].Class, results[i].Commodity, results[i].Code, title, results[i].Count));
    }

    UNTreeHandler.expand(context.Code);
}

function OnErrorExpand(error) {
    alert(error.get_message());
}

UNTreeItem.prototype.toString = function() {
    //debugger;
    var str = '<li id="root-' + this.Code + '">';

    var showTitle = this.Title;
    var altTitle = this.Title;

    if (showTitle.length > UNTreeHandler.MaxSize) {
        showTitle = showTitle.substring(0, UNTreeHandler.MaxSize - 2) + '...';
    }
    var countStr = '';
    
    if (this.Count > 0) {
        countStr = ' (' + this.Count + ')';
    }

    if (this.Commodity > 0) {
        str += '<div><a title="' + altTitle + '" href="javascript:empty()" id="link-' + this.Code + '" onclick="return UNTreeHandler.selectCategory(\'' + this.Code + '\');">' + showTitle + countStr + '</a></div>';
    } else {
        str += '<div><a title="' + altTitle + '" href="javascript:emtpy()" id="link-' + this.Code + '" onclick="return UNTreeHandler.toggle(this);">' + showTitle + countStr + '</a></div>';
    }

    var sb = [''];

    for (var i = 0; i < this.ChildNodes.length; i++) {
        sb[sb.length] = this.ChildNodes[i].toString();
    }

    str += '<ul id="content-' + this.Code + '" class="hide">' + sb.join('') + '</ul></li>';

    return str;
}
