var _tags;
var _dbs;
var _count;

var FONT_SIZE_COUNT = 7
var _percent = new Array(0.05, 0.12, 0.35, 0.65, 0.88, 0.95, 1);

/*****************************************************************************

 init functions 

*****************************************************************************/
function InitCloudBase(param)
{
   var i;
   var alltag = param['tagIds'].split("\n");
   var dbCnt;
   var tg;

   /* gen dbs */
   _dbs = param['dbNames'].split(" ");
   
   /* gen tag */
   _tags = new Array(param['tagCnt']);
   for(i = 0; i < param['tagCnt']; i++)
   {
      _tags[i] = new Array(2);
      tg = alltag[i].split("\t");
      _tags[i][0] = tg[0];
      _tags[i][1] = tg[1];
   }

   /* gen count */
   _count = new Array(param['dbsCnt']);
   for(i = 0; i < param['dbsCnt']; i++)
      _count[i] = new Array(param['tagCnt']);

   for(i = 0; i < param['dbsCnt']; i++)
   {
      dbCnt = param['db'+i].split(" ");
      for(j = 0; j < param['tagCnt']; j++)
         _count[i][j] = dbCnt[j];
   }
}

function InitShow(param)
{
   /* set count value */
   document.getElementById("cloud_num").value = param['cloudNum'];
   
   /* init basic cloud */
   InitCloudBase(param);

   /* show cloud */
   ShowCloud(param['cloudNum'], false);
}

function InitQPageShow(isTagOn)
{
   var obj;
   var idx;
   var i;

   if(isTagOn == false)
      return;

   /* init basic cloud */
   InitCloudBase(param);

   /* mark checked index */
   i = 0;
   while((obj = document.getElementById("indexname_"+i)) != null)
   {
      if(obj.checked == true)
      {
         idx = GetDbsIndexInCloud(obj.value);
         if(idx != -1)
            _markCloudDbs[idx] = true;
      }
      i++;
   }
   
   /* show cloud */
   ShowCloud(param['cloudNum'], true);
}
/*****************************************************************************

 act functions 

*****************************************************************************/
function UpdateCloud()
{
   ShowCloud(document.getElementById("cloud_num").value);
}

function CalculateTagCount(total, isQpage)
{
   /* calculate tag total count */
   if(isQpage == true)
   {
      for(i = 0; i < param['tagCnt']; i++)
      {
         total[i] = new Array(2);
         total[i][0] = i;
         total[i][1] = 0;
         for(j = 0; j < param['dbsCnt']; j++)
         {
            if(_markCloudDbs[j] == true)
               total[i][1] += parseInt(_count[j][i]);
         }
      }
   }
   else
   {
      for(i = 0; i < param['tagCnt']; i++)
      {
         total[i] = new Array(2);
         total[i][0] = i;
         total[i][1] = 0;
         for(j = 0; j < param['dbsCnt']; j++)
            total[i][1] += parseInt(_count[j][i]);
      }
   }
}

function ShowCloud(cloudNum, isQpage)
{
   var i, j;
   var total = new Array(param['tagCnt']);
   var idxArr = new Array(FONT_SIZE_COUNT);
   
   var showCloud = (param['tagCnt'] > parseInt(cloudNum)) ? cloudNum : param['tagCnt'];
   var showCloudMaxIdx = showCloud - 1;
   var cloudStr;
   var fontSzMark = new Array(param['tagCnt']);

   /* calculate tag total count */
   CalculateTagCount(total, isQpage);
   
   /* sort tag count */
   total.sort(SortCount);
   
   /* kick out tags that count=0 */
   for(i = 0; i < param['tagCnt']; i++)
   {
      if(total[i][1] <= 0)
      {
         if(showCloud > i)
         {
            showCloud = i;
            showCloudMaxIdx = i - 1;
            break;
         }
      }
   }
   
   /* if no tag, not show cloud field */
   if(showCloud == 0)
      document.getElementById("tagCloud").style.display = "none";
   else
      document.getElementById("tagCloud").style.display = "";
   
   /* calculate size section index */
   for(i = 0; i < FONT_SIZE_COUNT; i++)
      idxArr[i] = Math.floor(showCloudMaxIdx * _percent[i]);
   
   /* init font size (-1 means not appear) */
   for(i = 0; i < param['tagCnt']; i++)
      fontSzMark[i] = -1;
   
   /* assign font size */
   j = 0;
   for(i = 0; i < FONT_SIZE_COUNT; i++)
   {
      for(; j <= idxArr[i]; j++)
      {
         if(j > 0 && total[j][1] == total[j-1][1])    // when tag count equals the last one, use the same font size
            fontSzMark[total[j][0]] = fontSzMark[total[j-1][0]];
         else
            fontSzMark[total[j][0]] = i;
      }
   }
   
   /* generate cloud */
   cloudStr = "";
   for(i = 0; i < param['tagCnt']; i++)
   {
      if(fontSzMark[i] != -1)
         cloudStr += '<span class="cloud_base_link cloud_size' + fontSzMark[i] + '" onclick="ActQueryTag(' + _tags[i][0] + ')"><a href="#' + _tags[i][0] + '">' + _tags[i][1] + '</a></span> ';
   }
   document.getElementById("cloud_content").innerHTML = cloudStr;
}

function RenderCloud(chkboxId, dbName)
{
   var obj = document.getElementById(chkboxId);
   var idx;
   
   idx = GetDbsIndexInCloud(dbName);
   if(idx == -1)
      return;
   
   /* is check or uncheck */
   if(obj.checked == true)
      _markCloudDbs[idx] = true;
   else
      _markCloudDbs[idx] = false;

   ShowCloud(param['cloudNum'], true);
}

function ActSetCloudNum()
{
   document.forms["set_cloud_form"].submit();
}

function ActQueryTag(tagId)
{
   var dbs;

   if(document.forms["query_form"] != null)
   {
      dbs = GenDbList("");    //use scomm function
      location.href = param['CGIPrefix'] + "search/query.cgi?dbs=" + dbs + "&tag=" + tagId;
   }
}
/*****************************************************************************

 tool functions 

*****************************************************************************/
function SortCount(a, b)
{
   return b[1] - a[1];
}

function GetDbsIndexInCloud(dbName)
{
   var i;
   
   for(i = 0; i < _dbs.length; i++)
   {
      if(_dbs[i] == dbName)
         return i;
   }
   return -1;
}

