﻿//  tagcloud.js: code for building interactive tag cloud

var tags = new Array(); // will hold our actual links to highlight them
var tagCloudScope = 0;

function toggleTagCloudScope() 
{
    generateTagStream();
}

function GetTagOfVideo(vID)
{
        var paramArray = new Array();
        paramArray["videoID"] = vID;
        paramArray["SearchType"] = "Current";
        paramArray["FilterType"] = "All";
        AJAXFetch("tag", paramArray, processTagStream, true);
}

function generateTagStream() 
{
    var paramArray = new Array();
    paramArray["videoID"] = "";
    paramArray["SearchType"] = "All";
    paramArray["FilterType"] = "all";
    AJAXFetch("tag", paramArray, processTagStream, true);
}

function highlightTag(tagName) 
{
   if (tags[tagName] != null) 
   {
    tags[tagName].hilight();
   }
}


function unhighlightTag(tagName) 
{
   if (tags[tagName] != null) 
   {
    tags[tagName].unhilight();
   }
}

function unhighlightAllTags() 
{
    for (tagName in tags) 
    { 
        tags[tagName].unhilight();
    }
}

function processTagStream(xmldoc) 
{
    var r = xmldoc.getElementsByTagName('Tag');
    var myTagCloud = document.getElementById("TagCloudArray");
    
    // clear our current cloud and start new
    myTagCloud.innerText = "";
    tags = new Array();
    
    if(r.length != 0)
    {
        for (var i = 0; r != null && i < r.length; i++) 
        {
            var data = {
                tagKey : r[i].getAttribute("TagKey"),
                showTimes : parseFloat( r[i].getAttribute("ShowTimes"))
            };
            var tag = new Tag(data);
            tag.addToCloud();
        }
        CloudTags();
    }
    else
    {
        myTagCloud.innerText = "No Result";
    }
}

var tagFilter = new Array();
function tagClicked(name)
{

    if(VideoCurrentStatus()=="disconnected" || VideoCurrentStatus()=="stopped" || CurrentVideoID == 0)
    {
        SearchInput(name);
    }
    if(!tagFilter[name])
    {
            tagFilter[name] = name;
            tagFilter.length ++;
            AddFilter(name);
            updateTickerPosition(VideoCurrentPosition());
            ticker.style.height = document.getElementById("FilterList").scrollHeight;
            UpdataBookMark();
    }
}

function CloudTags()
{
    var tagarray = new Array();
    for(var tagname in tags)
    {
        var tagobj = tags[tagname];
        for(var itagname in tags)
        {
            if(tagobj.repeat >= tags[itagname].repeat && !Contains(tagarray,tagobj))
            {
                tagarray[tagarray.length] = tagobj;
            }
        }
    }
    for(var index = 0; index < tagarray.length ; index ++)
    {
        var tagA = tagarray[index].dom;
        tagA.style.fontSize = 18 - index/5;
        tagA.style.color = TagColor[index%7];
    }
}

function Contains(array,obj)
{
    for(var index = 0; index < array.length ; index ++)
    {
        if(array[index] == obj)
        {
            return true;
        }
    }
    return false;
}

var TagColor = new Array();
TagColor[0] = "#CC3333";
TagColor[1] = "#336600";
TagColor[2] = "#669966";
TagColor[3] = "#CC00CC";
TagColor[4] = "#9999CC";
TagColor[5] = "#996666";
TagColor[6] = "#999999";

function Tag(data)
{
    this.tagName = data.tagKey.toLowerCase();
    this.repeat = data.showTimes;
    this.dom = null;
}

TP = Tag.prototype;

TP.addToCloud = function()
{
    var tagA = document.createElement("a");
    tagA.className = "tag";
    if(this.tagName.length > 15)
    {
        var name = this.tagName.substring(0,12) + "...";
        tagA.appendChild(document.createTextNode(name));
    }
    else
    {
        tagA.appendChild(document.createTextNode(this.tagName));
    }
    this.AttachEvent(tagA);
    var tagCloud = document.getElementById("TagCloudArray");
    tagCloud.appendChild(tagA);
    this.dom = tagA;
    tagCloud.appendChild(document.createTextNode('\u00a0\n'));//space between tags.
    tags[this.tagName] = this;
}

TP.AttachEvent = function(obj)
{
    var re = /'/;
    var tagString = this.tagName.replace(re, "\\'");
    try
    {
        obj.onclick = new Function("tagClicked('"+tagString+"')");
    }
    catch(e)
    {
    
    }
}

TP.hilight = function()
{
    this.dom.className = "taghilight";
}

TP.unhilight = function()
{
    this.dom.className = "tag";
}
