
/**
 * Show a tag in the history element on the page.
 * The link allows the user to return to that constellation.
 *
 * @param tag  The tag to put into the history
 */
function showHistory(tag)
{
   if(tagHistory[tagHistory.length-1]!=tag) tagHistory.push(tag);
   if(tagHistory.length>5) tagHistory.shift();
   var html = "History: ";
   for(i=0;i<tagHistory.length;i++)
   {
      if(i>0) html+= "&nbsp;:&nbsp;";
      html += constellationLink(tagHistory[i],false);
   }
   document.getElementById("tagHistory").innerHTML = html;
}

/**
 * Show the menu for a given tag in the constellation.
 * @param div     The tag div
 * @para div      The tag name
 */
function showMenu(div, tag)
{
   var menu = getMenu(tag);
   menu.style.display = "inline";
   menu.style.position = "absolute";
   menu.style.top = (div.offsetTop -15) + "px";
   menu.style.left = (div.offsetLeft + 45) + "px";
   menu.style.zIndex = div.style.zIndex+1000;
   
   showStatus(getTagStatus(tag));
}

function getTagStatus(tag)
{
   if(centralTag==null) return "";
   if(tag==centralTag)
   {
      html = "";
   }
   else
   {
      var score = getScore(centralTag,tag);
      if(score==1)
      {
         html = "'" + centralTag + "' and '" + tag + "' only appear together in one post";
      }
      else
      {
         var plural = (score==1)?" post talks ":" posts talk ";
         html = score + plural + "about '" + centralTag + "' and '" + tag + "'";
      }
   }
   return html;
}

/** 
 * Show something in the status area.
 */
function showStatus(message)
{
   var tagStatus = document.getElementById("tagStatus");
   tagStatus.innerHTML=unescape(message);
}

/**
 * Locate or create the menu div.
 */
function getMenu(tag)
{
   var workspace = document.getElementById("workspace");
   var menu = document.getElementById("constellation_menu");
   if(!menu)
   {
      menu = document.createElement("div");
      menu.id = "constellation_menu";
      menu.style.display = "none";
      menu.style.position = "absolute";
      workspace.appendChild(menu);
   }
   menu.innerHTML = constellationLink(tag,true);
   return menu;
}

/**
 * Build a link that can recreate the constellation.
 */
function constellationLink(tag, showImage)
{
   var status = "Center constellation on: " + tag;
   var html = '<a href="#" class="bullseye" title="' + status + '" '
            + 'onclick="buildConstellation(\'' + escape(tag) + '\');return false;" '
            + 'onmouseover="showStatus(\'' + escape(status) + '\')">';
   if(showImage)
   {
      html +='<img title="' + status + '"  alt="(+)"  border="0"  src="images/bullseye.gif">';
   }
   else
   {
      html +=tag;
   }
   html += '</a></div>';
   return html;
}


function showTag(tag, layer, divvy, name)
{
   var div = document.createElement("div");
   div.className = name;

   var link = document.createElement("a");
   link.className="ctag";
   link.href="tagprofile.php?tag=" + escape(tag) + "&date=" + constellationDate;
   link.innerHTML=tag;
   link.title=getTagStatus(tag);
   link.onmouseover = function(){showMenu(div,tag);return false;}
   div.appendChild(link);

   divvy.addItem(div,layer);
}

/**
 * Gets the strength of association between
 * the primary tag and another tag.
 */
function getScore(tag, other)
{
   var raw = association[tag][other];
   if(raw!=null)
   {
      return raw+1;
   }
   else
   {
      return 0;
   } 
}


/**
 * Build a constellation around a tag.
 */
function buildConstellation(tag)
{
   if(tag=='') 
   {
      document.getElementById("workspace").innerHTML = "<br/><h2>Not enough information to build a constellation yet.</h2>";
      return;
   }
   tag = unescape(tag);
   centralTag = tag;
   
   // Empty workspace
   divvy = new Divvy("workspace", 600, 550);
   getMenu(tag).style.display = "none";
   document.getElementById("workspace").innerHTML = "";
   
   var centerX = 250;
   var centerY = 260;
   showTag(tag, new DivvyLayer(new PointLayout( centerX, centerY )), divvy, "layer0");
   var layer1 = new DivvyLayer(new CircleLayout( 80, centerX, centerY ));
   var layer2 = new DivvyLayer(new CircleLayout(150, centerX, centerY ));
   var layer3 = new DivvyLayer(new CircleLayout(240, centerX, centerY ));

   // Place each related tag at a diff point
   for(var other in association[tag])
   {
      if(other==tag) continue;
      var score = getScore(tag,other);

      if(score==1)
      {
         showTag(other,layer3,divvy,"layer4");
      }
      else if(score==2)
      {
         showTag(other,layer3,divvy,"layer3");
      }
      else if(score==3)
      {
         showTag(other,layer2,divvy,"layer2");
      }
      else if(score==4)
      {
         showTag(other,layer1,divvy,"layer1");
      }
   }

   // show it
   divvy.render();

   // put it in tagHistory
   showHistory(tag);
}


// Create Divvy
var centralTag;
var divvy;
var tagHistory = new Array();
