﻿// JScript File
function tagList(groupName, tags, tagareaID, model)
{
    this.tagareaID = tagareaID;
    this.model = model;
    this.groupName = groupName;
    this.tags = tags;
    var tagGroupArea = document.getElementById(tagareaID);    
    tagarea = document.createElement("div");
    tagarea.className = 'tagArea';
    tagarea.id = 'tagGroup'+groupName;
    
    this.tagarea = tagarea;
    tagGroupArea.appendChild(tagarea); 
    
    this.setupTagGroup(groupName, tags, tagarea.id, model, 'name');
    this.visible = true;    
    this.useHue = false;
}

function toggleHue(e)
{

    ts = getEventSrc(getEvent(e));        
    theTagList = ts.tagList;    
    if (theTagList.useHue) {
        theTagList.useHue = false;
        theTagList.removeAllChildren();   
        theTagList.setupTagGroup(theTagList.groupName, theTagList.tags, theTagList.tagarea.id, theTagList.model, theTagList.sortOrder, theTagList.useHue);
    } else {
        model = theTagList.model;
        for (gn in model.tagList) {
            tl = model.tagList[gn];
            if (tl.useHue) {
                tl.useHue = false;
                tl.removeAllChildren();   
                tl.setupTagGroup(tl.groupName, tl.tags, tl.tagarea.id, tl.model, tl.sortOrder, tl.useHue);
            }
        }
        theTagList.useHue = true;
        theTagList.removeAllChildren();   
        theTagList.setupTagGroup(theTagList.groupName, theTagList.tags, theTagList.tagarea.id, theTagList.model, theTagList.sortOrder, theTagList.useHue);
    }
    theTagList.model.colorNodesFromGroup(theTagList.groupName);
    stopPropagation(e);
}

tagList.prototype.setupTagGroup = function(groupName, tags, tagareaID, model, sortOrder, setHue)
{
    tagarea = document.getElementById(tagareaID);     
    this.sortOrder = sortOrder;
    tagTitle = document.createElement("div");    
    tagTitle.className = 'tagTitle';
    tagTitle.onclick = toggleGroupVisibility;
    tagTitle.tagList = this;

    tagTitleTable = document.createElement("table");
    tagTitleBody = document.createElement("tbody");
    tagTitleTable.appendChild(tagTitleBody);
 // new
    tagTitleRow = document.createElement("tr");
    tagTitleBody.appendChild(tagTitleRow);
    
    tagTitleText = document.createElement("td");
    tagTitleText.innerHTML = groupName;
    tagTitleText.tagList = this;
    tagTitleText.className = 'tagTitleText';
    tagTitleRow.appendChild(tagTitleText);
    tagTitleColors = document.createElement("td"); 
    if (setHue) {       
       tagTitleColors.className = 'tagTitleColorsOn';    
    } else {
       tagTitleColors.className = 'tagTitleColorsOff';    
    }
    tagTitleColors.tagList = this;
    tagTitleColors.onclick = toggleHue;
    tagTitleRow.appendChild(tagTitleColors);
    tagTitle.appendChild(tagTitleTable);
 // new
 /*   
    tagTitleText = document.createElement("span");
    tagTitleText.innerHTML = groupName;
    tagTitleText.tagList = this;
    tagTitleText.className = 'tagTitleText';
    tagTitle.appendChild(tagTitleText);
    tagTitleColors = document.createElement("div"); 
    if (setHue) {       
       tagTitleColors.className = 'tagTitleColorsOn';    
    } else {
       tagTitleColors.className = 'tagTitleColorsOff';    
    }
    tagTitleColors.tagList = this;
    tagTitleColors.onclick = toggleHue;
    tagTitle.appendChild(tagTitleColors);
*/
    tagarea.appendChild(tagTitle);    
    tagContentArea = document.createElement('div');
    tagarea.appendChild(tagContentArea);
    tagContentArea.className = 'tagContentArea';
    tagContentArea.id = 'content'+groupName;
    this.tagContentAreaId = tagContentArea.id;        
/*    numPart = document.createElement("span");
    numPart.className = 'titletagNumPart';
    numPart.innerHTML = '#';
    numPart.tagList = this;    
    numPart.onclick = sortTagList;
    textPart = document.createElement("span");
    textPart.innerHTML = 'name';
    textPart.className = 'titletagTextPart';
    textPart.tagList = this;
    textPart.onclick = sortTagList;
    someNode = document.createElement("div");
    someNode.className = 'tagTitleText';
    someNode.appendChild(textPart);
    someNode.appendChild(numPart);
    tagContentArea.appendChild(someNode);        
*/
    tagHeaderTable = document.createElement("table");
    tagHeaderTable.className = 'tagHeaderTable';
    tagHeaderArea = document.createElement("tbody");    
    tagHeaderRow = document.createElement("tr");
    tagHeaderRow.className = 'tagHeaderRow';
    tagHeaderArea.appendChild(tagHeaderRow);
    tagHeaderText = document.createElement("td");
    tagHeaderText.innerHTML = 'name';
    tagHeaderText.className = 'tagHeaderText';
    tagHeaderText.onclick = sortTagList;
    tagHeaderText.tagList = this;
    tagHeaderRow.appendChild(tagHeaderText);
    tagHeaderNum = document.createElement("td");
    tagHeaderNum.onclick = sortTagList;
    tagHeaderNum.tagList = this;
    tagHeaderNum.innerHTML = '#';
    tagHeaderNum.className = 'tagHeaderNum';
    tagHeaderRow.appendChild(tagHeaderNum);
    tagHeaderArea.appendChild(tagHeaderRow);
    
    tagHeaderTable.appendChild(tagHeaderArea);
    tagContentArea.appendChild(tagHeaderTable);
    this.tagMap = new Object();
    this.numMap = new Object();
    this.checkMap = new Object();
    this.hueMap = new Object();
    this.tagSelectCallback = '';
    this.filterOnSelect = false;
    if (sortOrder == 'name') {
        sortTags = $H(tags).sortBy(function(aTag) {return(aTag)});
    } else {
       sortTags = $H(tags).sortBy(function(aTag) {return(-aTag.value)});
    }
    
    var tagCount = 0;
    sortTags.each(function(aHash) {
        tagCount++;
    });
    
    aColor = new Color(255,255,255);
    aColor.setSaturation(0.5);
    aColor.setBrightness(0.7);
 
     
    sortTags.each(function(aHash,i) {        
        aTag = aHash.key;
        someNode = document.createElement("div");
        hue = i/tagCount;
        aColor.setHue(hue);       
        this.tagMap[aTag] = someNode;
        huePart = document.createElement("span");
        huePart.className = 'tagHuePart';
        huePart.innerHTML = "&nbsp";
        if (setHue) {
            huePart.style.background = aColor.toString();
        }
        this.hueMap[aTag] = huePart;
        numPart = document.createElement("span");
        numPart.className = 'tagNumPart';        
        numPart.innerHTML = "(" + aHash.value + ")";
        this.numMap[aTag] = numPart;
        textPart = document.createElement("span");
        textPart.innerHTML = aTag;
        textPart.className = 'tagTextPart';
        textPart.onmouseover = tagList.prototype.tagOver;
        textPart.onmousedown = tagList.prototype.tagSelect;
        checkPart = document.createElement("span");
        checkPart.className = 'tagCheckPart';
        checkPart.innerHTML = '&#10003';
        //checkPart.style.visibility = 'hidden';
        //checkPart.style.display = 'hidden';
        this.checkMap[aTag] = checkPart;
        someNode.appendChild(huePart);
        someNode.appendChild(checkPart);        
        someNode.appendChild(textPart);        
        someNode.appendChild(numPart);
        someNode.id = aTag;
        someNode.onmouseover = tagList.prototype.tagOver;
        someNode.onmouseout = tagList.prototype.clearHighlight;
        someNode.onmousedown = tagList.prototype.tagSelect;
        someNode.ondblclick = tagList.prototype.tagSelectAndFilter;
        someNode.className = 'tagText';
        someNode.model = model;
        someNode.tagList = this;
        tagContentArea.appendChild(someNode);
    }.bind(this));
    this.tagArray = sortTags;
}

tagList.prototype.removeAllChildren = function()
{
    children = this.tagarea.childNodes;
    for (x=children.length-1; x>=0; x--) {
        ele = children.item(x);
        this.tagarea.removeChild(ele);
    }    
}


tagList.prototype.UpdateListBySort = function(sortKey)
{
    
}

tagList.prototype.updateCounts = function(newCounts)
{
    hashTags = $H(newCounts);
    hashTags.each(function(aTag) {
        theTag = aTag.key;
        theValue = aTag.value;
        divFromMap = this.numMap[theTag];
        tagFromMap = this.tagMap[theTag];
        if (divFromMap) {
            divFromMap.innerHTML = "(" + theValue + ")";
//            tagFromMap.style.display = 'block';            
            if (theValue == 0) {
                tagFromMap.style.display = 'none';
            } else {
                tagFromMap.style.display = 'block';            
            }            

        }
    }.bind(this));
}

tagList.prototype.highLightByTag = function(tag)
{
    elem = this.tagMap[tag];
    if (elem) elem.className = 'tagText tagHighLight';
}

tagList.prototype.clear = function(e) 
{
    this.tagArray.each(function(tag) {
        var elem = document.getElementById(tag.key);
        elem.className = 'tagText';
    });
}

tagList.prototype.highLight = function(e) 
{
    ts = getEventSrc(getEvent(e));
//    var elem = document.getElementById(t);
    ts.className = 'tagText tagHighLight'; 
}

tagList.prototype.clearHighlight = function(e)
{
    ts = getEventSrc(getEvent(e));
    if (!(ts.id)) {
        ts = ts.parentNode;
    }
    ts.className = 'tagText tagOver';
    tagName = ts.id;
    ts.model.clearHighlights();
    ts.className = 'tagText'; 
}

tagList.prototype.tagOver = function(e)
{
    ts = getEventSrc(getEvent(e));
    if (!(ts.id)) {
        ts = ts.parentNode;
    }
    ts.className = 'tagText tagOver';
    tagName = ts.id;
    ts.model.clearHighlights();
    ts.model.highlightNodesByTag(ts.tagList.groupName, tagName);
    stopPropagation(e);
}


tagList.prototype.tagSelectAndFilter = function(e)
{
    ts = getEventSrc(getEvent(e));
    if (!(ts.id)) {
        ts = ts.parentNode;
    }
    ts.className = 'tagText tagHighLight';
    tagName = ts.id;
    ts.model.clearAllSelection();
    ts.model.clearHighlights();
    ts.model.selectNodesByTag(ts.tagList.groupName, tagName);
    ts.tagList.checkMap[tagName].style.visibility = 'visible';
    ts.model.layoutSelected(0.5);
    if (ts.tagList.tagSelectCallback) 
    {
        eval(ts.tagList.tagSelectCallback);
    }
    stopPropagation(e);
    
}

function addFilter(filterList, group, tag)
{
    if (!(filterList[group])) {
        filterList[group] = new Array();
    }
    filterList[group].push(tag);
}

function removeFilter(filterList, group, tag)
{
    if (!(filterList[group])) return;
    newFilter = new Object();
    for (fgroup in filterList) {
        if (fgroup != group) {
            newFilter[fgroup] = filterList[fgroup];
        } else {
            if (filterList[group].length > 1) {
                newFilter[group] = new Array();
                filterList[group].each(function(name) {
                    if (name != tag) {
                        newFilter[group].push(name);
                    }
                });
            } else {
                if (filterList[group][0] != tag) {
                  newFilter[group] = new Array();
                  newFilter[group].push(tag);                
                }
            }
        }
    }
    return (newFilter);
}

   

tagList.prototype.tagSelect = function(e)
{
    newfilt = new Object();
    ts = getEventSrc(getEvent(e));
    if (!(ts.id)) {
        ts = ts.parentNode;
    }
    ts.className = 'tagText tagHighLight';
    tagName = ts.id;
    ts.model.clearAllSelection();
    ts.model.clearHighlights();
  //  ts.model.selectNodesByTag(ts.tagList.groupName, tagName);    
    newfilt['group'] = ts.tagList.groupName;
    newfilt['tag'] = tagName;
    if (ts.tagList.checkMap[tagName].style.visibility == 'hidden' || !(ts.tagList.checkMap[tagName].style.visibility)) {
        ts.tagList.checkMap[tagName].style.visibility = 'visible';
        addFilter(ts.model.currentFilter, ts.tagList.groupName, tagName);
    } else {
        ts.model.currentFilter = removeFilter(ts.model.currentFilter, ts.tagList.groupName, tagName);
        ts.tagList.checkMap[tagName].style.visibility = 'hidden';
    }        
    ts.model.layoutSelected(0.5);
/*    if (ts.tagList.filterOnSelect) {
        ts.model.layoutSelected(0.5);
    }
    if (ts.tagList.tagSelectCallback) 
    {
        eval(ts.tagList.tagSelectCallback);
    }
    */
    stopPropagation(e);
}

tagList.prototype.clearChecks = function()
{
    for (tn in this.checkMap) {
        this.checkMap[tn].style.visibility = 'hidden';
    }    
}

tagList.prototype.checkFilteredTags = function(aFilter)
{
    for (group in aFilter) {
        if (this.groupName == group) {
            tagnames = aFilter[group];
            tagnames.each(function(tagn) {
                if (this.checkMap[tagn]) {
                    this.checkMap[tagn].style.visibility = 'visible';
                }
            }.bind(this));
        }
    }
}
function sortTagList(e)
{
    ts = getEventSrc(getEvent(e));    
    theTagList = ts.tagList;
    theTagList.removeAllChildren();    
    if (ts.innerHTML == 'name') {
       theTagList.setupTagGroup(theTagList.groupName, theTagList.tags, theTagList.tagarea.id, theTagList.model, 'name', theTagList.useHue);
    } else {
       theTagList.setupTagGroup(theTagList.groupName, theTagList.tags, theTagList.tagarea.id, theTagList.model, 'value', theTagList.useHue);
    }
    theTagList.checkFilteredTags(theTagList.model.currentFilter);
}


function toggleGroupVisibility(e)
{
    ts = getEventSrc(getEvent(e));
    theTagList = ts.tagList;
    if (theTagList.visible) {
        theTagList.visible = false;
        effect_1 = Effect.BlindUp(theTagList.tagContentAreaId,{duration:0.5}); 
    } else {
       theTagList.visible = true;
       effect_1 = Effect.BlindDown(theTagList.tagContentAreaId,{duration:0.5}); 
    }
}
