/* tag cloud */
function TagCloud(prefix, oriTags, viewAs, sortBy,type) {
  this.relTag;
  this.username;
  this.groupName;
  this.tags;
  this.step;
  // alpha sort by:
  this.tags1 = oriTags;
  // other sort by:
  this.tags2 = new Array();
  for(var i=0; i<oriTags.length; i++) {
    this.tags2.push(oriTags[i]);
  }
  this.sortByType = sortBy?sortBy:"0";
  this.viewAsType = viewAs?viewAs:"0";
  
  //0: group bookmarks 1:group forum topics (add by vincent)
  this.type = type?type:"0";

  this.prefix = prefix;
  this.cloudBox = prefix+"_cloundBox";
  this.viewerBox = prefix+"_viewerBox";
  this.sorterBox = prefix+"_sorterBox";
  
  //to surpport group forum :modiby by vincent 07-03-23 
  if(this.type=="0"){
    this.viewAs = TagCloud_viewAs;
  }
  else if(this.type=="1"){
    this.viewAs = TagCloud_Forum_viewAs;
  }
  
  this.sortBy = TagCloud_sortBy;
  this.renderSorter = TagCloud_renderSorter;
  this.renderViewer = TagCloud_renderViewer;
  this.calcStep = TagCloud_calcStep;
  this.init_u = TagCloud_init_u;
  this.init_r = TagCloud_init_r;
  this.init_fr = TagCloud_init_fr;
  this.init_g = TagCloud_init_g;
  // type: u, r, ur
  this.cloudType;
}


// calc cloud step
function TagCloud_calcStep(tags, minCount, maxCount) {
  var step = 1;
  var minCount = minCount?minCount:1000000;
  var maxCount = maxCount?maxCount:0;
  for(var i=0; i< tags.length; i++) {
    var tag = tags[i];
    if(tag.count > maxCount) {
      maxCount = tag.count;
    }
    if(tag.count < minCount) {
      minCount = tag.count;
    }
    step = (maxCount-minCount)/5;
  }
  step = parseInt(step) == 0 ? 1 : parseInt(step);
  return step;
}
// type: 0-alpha, 1-freq
function TagCloud_sortBy(type) {
  if(type=="0") {
    this.tags = this.tags1;
  }else {
    this.tags = this.tags2;
    this.tags.sort(function(a, b){return b.count-a.count});
  }
  this.sortByType = type;
  this.viewAs(this.viewAsType);
}

// type: 0-cloud, 1-list
function TagCloud_viewAs(type) {
  var type = type?type:this.viewAsType;
  var box = $(this.cloudBox);
  var content = ""; 
  switch(this.cloudType) {
  case "u":
    if(type=="0") {
      for(var i=0; i<this.tags.length; i++) {
        var tag = this.tags[i];
        var count = parseInt(tag.count/this.step);
        count = count > 5 ? 5 : count;
        if(tag.tName.indexOf(" ")!=-1) {
          content += "<a class='LinkCA LinkCTag_"+count+"' style='line-height:110%;' href='/"+encodeURIComponent(this.username)+"/bookmark/tag/\""+encodeURIComponent(tag.tName)+"\"'>"+tag.tName.substring(0, 12).replace("<", "&lt;").replace(">", "&gt;")+"</a>\n";
        }else {
          content += "<a class='LinkCA LinkCTag_"+count+"' style='line-height:110%;' href='/"+encodeURIComponent(this.username)+"/bookmark/tag/"+encodeURIComponent(tag.tName)+"'>"+tag.tName.substring(0, 12).replace("<", "&lt;").replace(">", "&gt;")+"</a>\n";
        }
      }
    }else {
       content += "<ul id='userTagsUL' class='tagList'>";
       for(var i=0; i<this.tags.length; i++) {
           var tag = this.tags[i];
           if(tag.tName.indexOf(" ")!=-1) {
               content += "<li><span class='count'>"+tag.count+"</span><a href='/"+encodeURIComponent(this.username)+"/bookmark/tag/\""+encodeURIComponent(tag.tName)+"\"'>"+tag.tName.substring(0, 20).replace("<", "&lt;").replace(">", "&gt;")+"</a></li>";
           }else {
               content += "<li><span class='count'>"+tag.count+"</span><a href='/"+encodeURIComponent(this.username)+"/bookmark/tag/"+encodeURIComponent(tag.tName)+"'>"+tag.tName.substring(0, 20).replace("<", "&lt;").replace(">", "&gt;")+"</a></li>";
           }
       }
       content += "</ul>";
//      content += "<table class='TabTagList'>";
//      for(var i=0; i<this.tags.length; i++) {
//        var tag = this.tags[i];
//        if(tag.tName.indexOf(" ")!=-1) {
//          content += "<tr><td class='TdLeft'>"+tag.count+"</td><td><a href='/"+encodeURIComponent(this.username)+"/bookmark/tag/"+encodeURIComponent(tag.tName)+"\"'>"+tag.tName.substring(0, 20).replace("<", "&lt;").replace(">", "&gt;")+"</a></td></tr>";
//        }else {
//          content += "<tr><td class='TdLeft'>"+tag.count+"</td><td><a href='/"+encodeURIComponent(this.username)+"/bookmark/tag/"+encodeURIComponent(tag.tName)+"'>"+tag.tName.substring(0, 20).replace("<", "&lt;").replace(">", "&gt;")+"</a></td></tr>";
//        }
//      }
//      content += "</table>";
    }
    box.innerHTML = content;
    this.viewAsType = type;
    this.renderSorter();
    this.renderViewer(); 
    break;
  case "r":
    if(this.relTag.indexOf(" ")!=-1) {
      this.relTag = '"'+this.relTag+'"';
    }
    if(this.username!="") {
      for(var i=0; i<this.tags.length; i++) {
        var tag = this.tags[i];
        if(tag.tName.indexOf(" ")!=-1) {
          content += "<div class='ListItem'><a href='/"+encodeURIComponent(this.username)+"/bookmark/tag/"+this.relTag+"+\""+encodeURIComponent(tag.tName)+"\"'>+</a> <a href='/user/"+this.username+"/\""+encodeURIComponent(tag.tName)+"\"'>"+tag.tName.substring(0, 20).replace("<", "&lt;").replace(">", "&gt;")+"</a></div>";
        }else {
          content += "<div class='ListItem'><a href='/"+encodeURIComponent(this.username)+"/bookmark/tag/"+this.relTag+"+"+encodeURIComponent(tag.tName)+"'>+</a> <a href='/user/"+this.username+"/"+encodeURIComponent(tag.tName)+"'>"+tag.tName.substring(0, 20).replace("<", "&lt;").replace(">", "&gt;")+"</a></div>";
        }
      }
    }else {
      for(var i=0; i<this.tags.length; i++) {
        var tag = this.tags[i];
        if(tag.tName.indexOf(" ")!=-1) {
          content += "<div class='ListItem'><a href='/tag/"+this.relTag+"+\""+encodeURIComponent(tag.tName)+"\"'>+</a> <a href='/tag/\""+encodeURIComponent(tag.tName)+"\"'>"+tag.tName.substring(0, 20).replace("<", "&lt;").replace(">", "&gt;")+"</a></div>";
        }else {
          content += "<div class='ListItem'><a href='/tag/"+this.relTag+"+"+encodeURIComponent(tag.tName)+"'>+</a> <a href='/tag/"+encodeURIComponent(tag.tName)+"'>"+tag.tName.substring(0, 20).replace("<", "&lt;").replace(">", "&gt;")+"</a></div>";
        }
      }
    }
    box.innerHTML = content;
    this.renderSorter();
    break;
  case "fr" :
    var url = self.location;
    alert(url);
    var url_2 = url.substring(0, url.indexOf("&what="));
    alert(url_2);
    for(var i=0; i<this.tags.length; i++) {
      var tag = this.tags[i];
      if(tag.tName.indexOf(" ")!=-1) {
        content += "<div class='ListItem'><a href='/"+encodeURIComponent(this.username)+"/bookmark/tag/"+this.relTag+"+\""+encodeURIComponent(tag.tName)+"\"'>+</a> <a href='/user/"+this.username+"/\""+encodeURIComponent(tag.tName)+"\"'>"+tag.tName.substring(0, 20).replace("<", "&lt;").replace(">", "&gt;")+"</a></div>";
      }else {
        content += "<div class='ListItem'><a href='/"+encodeURIComponent(this.username)+"/bookmark/tag/"+this.relTag+"+"+encodeURIComponent(tag.tName)+"'>+</a> <a href='/user/"+this.username+"/"+encodeURIComponent(tag.tName)+"'>"+tag.tName.substring(0, 20).replace("<", "&lt;").replace(">", "&gt;")+"</a></div>";
      }
    }
    box.innerHTML = content;
    this.renderSorter();
    break;
  case "g":
    if(type=="0") {
      for(var i=0; i<this.tags.length; i++) {
        var tag = this.tags[i];
        if(tag.tName.indexOf(" ")!=-1) {
          content += "<a class='LinkCA LinkCTag_"+parseInt(tag.count/this.step)+"' href='/"+encodeURIComponent(this.username)+"/bookmark/tag/\""+encodeURIComponent(tag.tName)+"\"'>"+tag.tName.substring(0, 12).replace("<", "&lt;").replace(">", "&gt;")+"</a>\n";
        }else {
          content += "<a class='LinkCA LinkCTag_"+parseInt(tag.count/this.step)+"' href='/"+encodeURIComponent(this.username)+"/bookmark/tag/"+encodeURIComponent(tag.tName)+"'>"+tag.tName.substring(0, 12).replace("<", "&lt;").replace(">", "&gt;")+"</a>\n";
        }
      }
    }else {
      content += "<table class='TabTagList'>";
      for(var i=0; i<this.tags.length; i++) {
        var tag = this.tags[i];
        if(tag.tName.indexOf(" ")!=-1) {
          content += "<tr><td class='TdLeft'>"+tag.count+"</td><td><a href='/"+encodeURIComponent(this.username)+"/bookmark/tag/\""+encodeURIComponent(tag.tName)+"\"'>"+tag.tName.substring(0, 20).replace("<", "&lt;").replace(">", "&gt;")+"</a></td></tr>";
        }else {
          content += "<tr><td class='TdLeft'>"+tag.count+"</td><td><a href='/"+encodeURIComponent(this.username)+"/bookmark/tag/"+encodeURIComponent(tag.tName)+"'>"+tag.tName.substring(0, 20).replace("<", "&lt;").replace(">", "&gt;")+"</a></td></tr>";
        }
      }
      content += "</table>";
    }
    box.innerHTML = content;
    this.viewAsType = type;
    this.renderSorter();
    this.renderViewer(); 
    break;
  }
}

// type: 0-cloud, 1-list
function TagCloud_Forum_viewAs(type) {
  var type = type?type:this.viewAsType;
  var box = $(this.cloudBox);
  var content = ""; 
  switch(this.cloudType) {
  case "u":
    if(type=="0") {
      for(var i=0; i<this.tags.length; i++) {
        var tag = this.tags[i];
        var count = parseInt(tag.count/this.step);
        count = count > 5 ? 5 : count;
        if(tag.tName.indexOf(" ")!=-1) {
          content += "<a class='LinkCA LinkCTag_"+count+"' style='line-height:110%;' href='/"+encodeURIComponent(this.username)+"/forum/tag/"+encodeURIComponent(tag.tName)+"'>"+tag.tName.substring(0, 12).replace("<", "&lt;").replace(">", "&gt;")+"</a>\n";
        }else {
          content += "<a class='LinkCA LinkCTag_"+count+"' style='line-height:110%;' href='/"+encodeURIComponent(this.username)+"/forum/tag/"+encodeURIComponent(tag.tName)+"'>"+tag.tName.substring(0, 12).replace("<", "&lt;").replace(">", "&gt;")+"</a>\n";
        }
      }
    }else {
       content += "<ul id='userTagsUL' class='tagList'>";
       for(var i=0; i<this.tags.length; i++) {
           var tag = this.tags[i];
           if(tag.tName.indexOf(" ")!=-1) {
               content += "<li><span class='count'>"+tag.count+"</span><a href='/"+encodeURIComponent(this.username)+"/forum/tag/\""+encodeURIComponent(tag.tName)+"\"'>"+tag.tName.substring(0, 20).replace("<", "&lt;").replace(">", "&gt;")+"</a></li>";
           }else {
               content += "<li><span class='count'>"+tag.count+"</span><a href='/"+encodeURIComponent(this.username)+"/forum/tag/"+encodeURIComponent(tag.tName)+"'>"+tag.tName.substring(0, 20).replace("<", "&lt;").replace(">", "&gt;")+"</a></li>";
           }
       }
       content += "</ul>";
//      content += "<table class='TabTagList'>";
//      for(var i=0; i<this.tags.length; i++) {
//        var tag = this.tags[i];
//        if(tag.tName.indexOf(" ")!=-1) {
//          content += "<tr><td class='TdLeft'>"+tag.count+"</td><td><a href='/"+encodeURIComponent(this.username)+"/forum/tag/"+encodeURIComponent(tag.tName)+"'>"+tag.tName.substring(0, 20).replace('<', '&lt;').replace('>', '&gt;')+"</a></td></tr>";
//        }else {
//          content += "<tr><td class='TdLeft'>"+tag.count+"</td><td><a href='/"+encodeURIComponent(this.username)+"/forum/tag/"+encodeURIComponent(tag.tName)+"'>"+tag.tName.substring(0, 20).replace('<', '&lt;').replace('>', '&gt;')+"</a></td></tr>";
//        }
//      }
//      content += "</table>";
    }
    box.innerHTML = content;
    this.viewAsType = type;
    this.renderSorter();
    this.renderViewer(); 
    break;
  case "r":
    if(this.relTag.indexOf(" ")!=-1) {
      this.relTag = '"'+this.relTag+'"';
    }
    if(this.username!="") {
      for(var i=0; i<this.tags.length; i++) {
        var tag = this.tags[i];
        if(tag.tName.indexOf(" ")!=-1) {
          content += "<div class='ListItem'><a href='/"+encodeURIComponent(this.username)+"/forum/tag/"+this.relTag+"+\""+encodeURIComponent(tag.tName)+"'>+</a> <a href='/user/"+this.username+"/\""+encodeURIComponent(tag.tName)+"\"'>"+tag.tName.substring(0, 20).replace("<", "&lt;").replace(">", "&gt;")+"</a></div>";
        }else {
          content += "<div class='ListItem'><a href='/"+encodeURIComponent(this.username)+"/forum/tag/"+this.relTag+"+"+encodeURIComponent(tag.tName)+"'>+</a> <a href='/user/"+this.username+"/"+encodeURIComponent(tag.tName)+"'>"+tag.tName.substring(0, 20).replace("<", "&lt;").replace(">", "&gt;")+"</a></div>";
        }
      }
    }else {
      for(var i=0; i<this.tags.length; i++) {
        var tag = this.tags[i];
        if(tag.tName.indexOf(" ")!=-1) {
          content += "<div class='ListItem'><a href='/tag/"+this.relTag+"+\""+encodeURIComponent(tag.tName)+"\"'>+</a> <a href='/tag/\""+encodeURIComponent(tag.tName)+"'>"+tag.tName.substring(0, 20).replace("<", "&lt;").replace(">", "&gt;")+"</a></div>";
        }else {
          content += "<div class='ListItem'><a href='/tag/"+this.relTag+"+"+encodeURIComponent(tag.tName)+"'>+</a> <a href='/tag/"+encodeURIComponent(tag.tName)+"'>"+tag.tName.substring(0, 20).replace("<", "&lt;").replace(">", "&gt;")+"</a></div>";
        }
      }
    }
    box.innerHTML = content;
    this.renderSorter();
    break;
  case "fr" :
    var url = self.location;
    alert(url);
    var url_2 = url.substring(0, url.indexOf("&what="));
    alert(url_2);
    for(var i=0; i<this.tags.length; i++) {
      var tag = this.tags[i];
      if(tag.tName.indexOf(" ")!=-1) {
        content += "<div class='ListItem'><a href='/"+encodeURIComponent(this.username)+"/forum/tag/"+this.relTag+"+\""+encodeURIComponent(tag.tName)+"'>+</a> <a href='/user/"+this.username+"/\""+encodeURIComponent(tag.tName)+"\"'>"+tag.tName.substring(0, 20).replace("<", "&lt;").replace(">", "&gt;")+"</a></div>";
      }else {
        content += "<div class='ListItem'><a href='/"+encodeURIComponent(this.username)+"/forum/tag/"+this.relTag+"+"+encodeURIComponent(tag.tName)+"'>+</a> <a href='/user/"+this.username+"/"+encodeURIComponent(tag.tName)+"'>"+tag.tName.substring(0, 20).replace("<", "&lt;").replace(">", "&gt;")+"</a></div>";
      }
    }
    box.innerHTML = content;
    this.renderSorter();
    break;
  case "g":
    if(type=="0") {
      for(var i=0; i<this.tags.length; i++) {
        var tag = this.tags[i];
        if(tag.tName.indexOf(" ")!=-1) {
          content += "<a class='LinkCA LinkCTag_"+parseInt(tag.count/this.step)+"' href='/"+encodeURIComponent(this.username)+"/forum/tag/"+encodeURIComponent(tag.tName)+"'>"+tag.tName.substring(0, 12).replace("<", "&lt;").replace(">", "&gt;")+"</a>\n";
        }else {
          content += "<a class='LinkCA LinkCTag_"+parseInt(tag.count/this.step)+"' href='/"+encodeURIComponent(this.username)+"/forum/tag/"+encodeURIComponent(tag.tName)+"'>"+tag.tName.substring(0, 12).replace("<", "&lt;").replace(">", "&gt;")+"</a>\n";
        }
      }
    }else {
      content += "<table class='TabTagList'>";
      for(var i=0; i<this.tags.length; i++) {
        var tag = this.tags[i];
        if(tag.tName.indexOf(" ")!=-1) {
          content += "<tr><td class='TdLeft'>"+tag.count+"</td><td><a href='/"+encodeURIComponent(this.username)+"/forum/tag/"+encodeURIComponent(tag.tName)+"'>"+tag.tName.substring(0, 20).replace("<", "&lt;").replace(">", "&gt;")+"</a></td></tr>";
        }else {
          content += "<tr><td class='TdLeft'>"+tag.count+"</td><td><a href='/"+encodeURIComponent(this.username)+"/forum/tag/"+encodeURIComponent(tag.tName)+"'>"+tag.tName.substring(0, 20).replace("<", "&lt;").replace(">", "&gt;")+"</a></td></tr>";
        }
      }
      content += "</table>";
    }
    box.innerHTML = content;
    this.viewAsType = type;
    this.renderSorter();
    this.renderViewer(); 
    break;
  }
}

// type: 0-cloud, 1-list
function TagCloud_renderViewer() {
  var box = $(this.viewerBox);
  if(this.viewAsType=="0") {
    box.innerHTML = "View as: <strong>cloud</strong> | <a href=\"javascript:eval('"+this.prefix+"'+'_tagCloud').viewAs('1')\">list</a>\n";
  }else {
    box.innerHTML = "View as: <a href=\"javascript:eval('"+this.prefix+"'+'_tagCloud').viewAs('0')\">cloud</a>\n | <strong>list</strong>";
  }
}
// type: 0-alpha, 1-freq
function TagCloud_renderSorter() {
  var box = $(this.sorterBox);
  if(this.sortByType=="0") {
    box.innerHTML = "Sort by: <strong>alpha</strong> | <a href=\"javascript:eval('"+this.prefix+"'+'_tagCloud').sortBy('1')\">freq</a>\n";
  }else {
    box.innerHTML = "Sort by: <a href=\"javascript:eval('"+this.prefix+"'+'_tagCloud').sortBy('0')\">alpha</a> | <strong>freq</strong>\n";
  }
}

function TagCloud_init_u(username) {  
  this.tags = this.tags1;
  this.step = this.calcStep(this.tags);
  this.username = username?username:"";
  this.cloudType = 'u';
  this.viewAs();
}

function TagCloud_init_r(relTag, username) {
  this.tags = this.tags1;
  this.cloudType = 'r';
  this.relTag = relTag?relTag:"";
  this.username = username?username:"";
  this.viewAs();
}

function TagCloud_init_fr(relTag) {
  this.tags = this.tags1;
  this.cloudType = 'fr';
  this.relTag = relTag?relTag:"";
  this.viewAs();
}

function TagCloud_init_g(groupName) {
  this.tags = this.tags1;
  this.step = this.calcStep(this.tags);
  this.groupName = groupName?groupName:"";
  this.cloudType = 'g';
  this.viewAs();
}


/* tag cloud */