var cloudkeys = [];
var fadevalue = 255;
cloud_currentid = null;
var fading = false;

function cloudtag_click_handler()
{
  if (!cloud_currentid)
    alert("FAIL");
  if (cloud_currentid)
    window.location.href = '/tag/list/' + cloud_currentid;
//  fadeout();
}

function fbsort(arr)
{
  arr.sort();
  var newkeys = [];
  for (var i = 0; i <= arr.length >> 1; i++)
    newkeys[arr.length - 1 - (arr & 1) - i * 2] = arr[i];
  for (var i = arr.length >> 1; i < arr.length; i++)
    newkeys[i * 2 - arr.length] = arr[i];
  return newkeys;
}

function fadein()
{
  fading = true;
  if (fadevalue <= 128)
  {
    fading = false;
    return;
  }
  fadevalue -= 4;
  var c = "0123456789abcdef".charAt(fadevalue / 0x10) + "0123456789abcdef".charAt(fadevalue % 0x10);
  var i;
  for (var i = 0; i < cloudkeys.length; i++)
  {
    var cloudtag = document.getElementById('cloudtag_' + i);
    if (cloudtag.style.textDecoration == 'underline')
      cloudtag.style.color = '#00' + c + '00';
    else
      cloudtag.style.color = '#' + c + c + c;
  }
  setTimeout(fadein, 5);
}

function fadeout()
{
  fading = true;
  if (fadevalue >= 255)
  {
    fading = false;
    if (cloud_currentid)
      window.location.href = '/tag/list/' + cloud_currentid;
    return;
  }
  fadevalue += 4;
  var c = "0123456789abcdef".charAt(fadevalue >> 4) + "0123456789abcdef".charAt(fadevalue & 0xf);
  var i;
  for (var i = 0; i < cloudkeys.length; i++)
  {
    var cloudtag = document.getElementById('cloudtag_' + i);
    cloudtag.style.color = '#' + c + c + c;
  }
  setTimeout(fadeout, 5);
}

function init_cloud(pkey)
{
  var i,j,k;
  var MAX = 32.0;
  var MIN = 12.0;

  var cloud = $('tag_cloud');
  if (!cloud)
     return false;
  cloudkeys.length = 0;
  cloud.innerHTML = "";
  var max = 0;
  var min = 32767;
  for (var i in clouddata)
  {

    cloudkeys[cloudkeys.length] = i;
    if (clouddata[i] >= max)
      max = clouddata[i];
    if (clouddata[i] <= min)
      min = clouddata[i];
  }
  min = parseFloat(min);
  max = parseFloat(max);
  // for(var j, x, i = cloudkeys.length; i; j = parseInt(Math.random() * i), x = cloudkeys[--i], cloudkeys[i] = cloudkeys[j], cloudkeys[j] = x);
 
  cloudkeys = fbsort(cloudkeys);

  var cloudallkeys = [];
  for (var i in clouddata)
    cloudallkeys.push(i);
  cloudallkeys = fbsort (cloudallkeys);

  minindex = cloudkeys.length;
  if (pkey)
  {
    cloudallkeys.each(function(k,i) {
      var cloudfound = false;
      for (var j in cloudkeys)
	if (cloudkeys[j] == k)
	{
	  cloudfound = true;
	  break;
	}
      if (!cloudfound)
	cloudkeys.push (cloudallkeys[i]); 
    });
  }

  var cloudindices = [];
  cloudkeys.each(function(i,j) {
    if (j & 1 == 1)
      cloudindices.unshift (j);
    else
      cloudindices.push (j);
  });

  cloudindices.each(function(j,k) { 
    var j = cloudindices[k];
    var i = cloudkeys[j];

    xtraclass = '';

    if (j >= minindex)
      fs = '' +MIN + 'px';
    else if (i == pkey)
    {
      xtraclass = ' cloud_item_selected';
      fs = ''+MAX + 'px';
    }
    else
    {
      fs = ''+(Math.pow((parseFloat(clouddata[i]) - min) / (max - min), 2) * (MAX - MIN) + MIN) + 'px';
      if (cloudpairs.detect(function(e){return e[0]==i || e[1]==i;}) )
        xtraclass = ' cloud_item_bound';
    }


    var cloudtag = new Element('a', { 'id' : 'cloudtag_' + j, 'href' : '/tag/list/' + encodeURIComponent(i), 'style' : 'font-size:'+fs+';', 'class': ('cloud_item_link' + xtraclass)}).update(i);

    cloud.appendChild(new Element('li').update(cloudtag));
    cloud.innerHTML += "\n";
  });

  if (navigator.mimeTypes && navigator.mimeTypes["application/x-shockwave-flash"] && false){
    var tcloud = '<tags>' + cloud.innerHTML + '</tags>';
    var rnumber = Math.floor(Math.random()*9999999);
    var widget_so = new SWFObject("/tagcloud.swf?r="+rnumber, "flashcloud", "230", "400", "9", "#ffffff");
    widget_so.addParam("allowScriptAccess", "always");
    widget_so.addVariable("tcolor", "0x333333");
    widget_so.addVariable("tspeed", "115");
    widget_so.addVariable("distr", "true");
    widget_so.addVariable("mode", "tags");
    widget_so.addVariable("tagcloud", encodeURI(tcloud)); // tcloud.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;'));
    widget_so.write("cloud");
  }
 /* fadevalue = 255;
  fadein();*/
}
