/********
 ****** (C) Wipplinger EDV Dienstleistungen 2003 - 2008 / Schweiz am Hölloch
 **** All Rights Reserved
 ** Confidential
 *
 ******************************************************************************/

 /**
  * caveseekers.activitymonitor support class.
  * client side js stuff 
  */

/*
================================================================================
Namespace Defintion
================================================================================
*/

var caveseekers;

if (!caveseekers) 
{
  caveseekers = {};
}
else if (typeof caveseekers != "object")
{
  throw new Error("module caveseekers already exists");
}

if (caveseekers.activitymonitor)
{
  throw new Error("caveseeker.activitymonitor already exists");
}

caveseekers.activitymonitor = {};

/*
================================================================================
Constructor and Memberdefinition
================================================================================
*/

caveseekers.activitymonitor = function(strTargetTagName, fPassiveLevel, fActiveLevel, iEntries,  iInitialDelay, strReportType, strUpPath)
{
	
  // Step 001: Member Definition
  this.strUpPath = strUpPath;
  this.iCurrentAlpha = 0;
  this.iAlphaTarget = 0;
  this.fUpFadeSpeed = 1.5;
  this.fDownFadeSpeed = 0.2;
  this.fPassiveLevel = fPassiveLevel;
  this.iEntries = iEntries;
  this.strReportType = strReportType;
  this.clActivityMonitorElement = document.getElementById(strTargetTagName);
  var self = this; 
  this.clActivityMonitorElement.onmouseover=function(){ self.setfadetarget(fActiveLevel); };
  this.clActivityMonitorElement.onmouseout=function(){ self.setfadetarget(fPassiveLevel); };
  this.clActivityMonitorElement.style.width= "100%";
  this.clActivityMonitorElement.style.opacity= "0";
  this.clActivityMonitorElement.style.filter= "alpha(opacity=0)";

  this.fadeEngine();
  new caveseekers.executor(this, iInitialDelay, 'updateMonitoring');
};

/*
================================================================================
Class Methods:
================================================================================
*/

caveseekers.activitymonitor.prototype.setOpacity = function(value) 
{
  this.clActivityMonitorElement.style.opacity = value/10;
  this.clActivityMonitorElement.style.filter = 'alpha(opacity=' + value*10 + ')';
}

caveseekers.activitymonitor.prototype.setfadetarget = function(iTarget)
{
  this.iAlphaTarget = iTarget;
}
 
caveseekers.activitymonitor.prototype.fadeEngine = function()
{

  if ((this.iCurrentAlpha <= this.iAlphaTarget-0.5) || (this.iCurrentAlpha >= this.iAlphaTarget+0.5))
  {
    if (this.iCurrentAlpha < this.iAlphaTarget)
    {
      this.iCurrentAlpha += this.fUpFadeSpeed ;
    }
    else if (this.iCurrentAlpha > this.iAlphaTarget)
    {
      this.iCurrentAlpha -= this.fDownFadeSpeed ;
    }
    this.setOpacity(this.iCurrentAlpha);
    
    if (this.iCurrentAlpha >= 10) // Burn in 
    {
      this.setfadetarget(this.fPassiveLevel);
      this.fUpFadeSpeed = 0.5;
    } 
  }
  new caveseekers.executor(this, 50, 'fadeEngine');
}   

caveseekers.activitymonitor.prototype.removeAllChildren = function(targetElement) 
{
  if (targetElement && targetElement.childNodes) 
  {
    for (var rloop = targetElement.childNodes.length -1; rloop >= 0 ; rloop--) 
    {
     targetElement.removeChild(targetElement.childNodes[rloop]);
    }
  }
}
  
caveseekers.activitymonitor.prototype.updateMonitoring = function()
{
  var myAjax = new Ajax();

  myAjax.url=this.strUpPath+"php/activitymonitoring/activitymonitor.php";
  myAjax.params="getReport="+this.iEntries+"&reportType="+this.strReportType;
  myAjax.bypassBrowserCache=true;
  
  var self = this; 
  myAjax.onSuccess=function(txt,xml){ self.reportRead(txt,xml); };

  myAjax.asynchronous=true;
  myAjax.doRequest();
}
  
caveseekers.activitymonitor.prototype.reportRead = function(txt,xml)
{
  var clTable;

  // Step 001: Doing JSON magic
  
  var clData = eval("("+txt+")");
  
  // Step 002: Removing previous content

  this.removeAllChildren(this.clActivityMonitorElement);

  var clTable = document.createElement("table");
  
  clTable.setAttribute("align","center");

  var thead = document.createElement("thead");
  var tr    = document.createElement("tr");
  var iRows = 3;
  
  // Step 003: Calculating Number of Rows
  
  var iRowCount = Math.floor(clData.length / iRows);
  
  // Step 003: Building Surrounding Table Header
  
  for (var i = 0; i < iRows; i++)
  {
    var th = document.createElement("th");
    tr.appendChild(th);
  }

  thead.appendChild(tr);
  clTable.appendChild(thead);
  
  var tbody = document.createElement("tbody");

  for (var iCurrentRow = 0; iCurrentRow < clData.length; iCurrentRow++)
  { 
   
   // Step 004: Building first row of table holding activity headline
   
   var tr = document.createElement("tr");
 
   // Step 005: Creating a new table for each plugin
   
   for (var i = iCurrentRow*iRows; i < iCurrentRow*iRows+iRows; i++)
   {
     var td = document.createElement("td");
     if (i < clData.length)
     {
      td.appendChild(this.createSubTable(clData[i]));
     }       
     tr.appendChild(td);
   }
   
   tbody.appendChild(tr);
  }
  clTable.appendChild(tbody);
  
  this.clActivityMonitorElement.appendChild(clTable);
  
  if (this.iAlphaTarget == 0) // initial Burn-In
  {
    this.iAlphaTarget = 10;
  }
  
  new caveseekers.executor(this, 120*1000, 'updateMonitoring');
}

caveseekers.activitymonitor.prototype.createSubTable = function(data)
{
  var iColumns = 4;
  
  var table = document.createElement("table");
  var thead = document.createElement("thead");
  var tr    = document.createElement("tr");

  table.className = "activitymonitor_text";
  table.setAttribute("width","100%");
  
  // Step 002: Building Table Header
  
  for (var i = 0; i < iColumns; i++)
  {
    var th = document.createElement("th");
    tr.appendChild(th);
  }

  thead.appendChild(tr);
  table.appendChild(thead);

  var tbody = document.createElement("tbody");

  // Step 003: Building first row of table holding activity headline
  
  var tr = document.createElement("tr");
  var td = document.createElement("td");
  td.colSpan = 4;
  with(td.style)
  {
    backgroundColor="#550000";
    fontSize="10px";
    textAlign="center";
  }
  
  td.appendChild(document.createTextNode(data.strHeadName.toUpperCase()));       
  tr.appendChild(td);
  tbody.appendChild(tr);

  for (var i = 0; i < data.claDataSetArray.length; i++)
  {
    var tr = document.createElement("tr");
    tr.height = "15";     
    for (var j=0; j < iColumns; j++)
    {
      var td = document.createElement("td");

      switch(j)
      {
        case 0: // Spalte 0: DATUM
        { 
          var strText = data.claDataSetArray[i].strDate;
          var newNode = document.createElement("a");
          
          if (strText.length > 8)
          {
            newNode.title = strText;
            strText = strText.substring(0,8);
          }
          
          newNode.href = data.claDataSetArray[i].strLink;
          newNode.className = "link_mini";
          newNode.innerHTML = strText;
        }
        break;  
     
        case 1: // Spalte 1: USER-BILDNISSE
        {
          var newNode = document.createElement("img");
          var strDirectoryShortcut = data.claDataSetArray[i].strUserShortcut;
          var strPath;

          if (strDirectoryShortcut == "muslima")
          {
            strPath="images/commonfeedback/muslima_k.jpg";
          }
          else if (strDirectoryShortcut == "darth")
          {
            strPath="images/commonfeedback/pope_k.jpg";
          }
          else
          {
            strPath = "comrades/"+data.claDataSetArray[i].strUserShortcut+"/smallface.jpg";
          }

          newNode.src = this.strUpPath+strPath;
          // newNode.src = "http://"+location.host+"/"+strPath;
          newNode.width = "8";
          newNode.height = "10";
          newNode.border = "0";
       }
       break;
       
       case 2: // Spalte 2: USERNAME
       {
         var strText = data.claDataSetArray[i].strUserName;
         var newNode = document.createElement("a");

         if (strText.length > 10)
         {
           newNode.title = strText;
           strText = strText.substring(0,10); 
         }
         newNode.href = data.claDataSetArray[i].strLink;
         newNode.className = "link_mini";
         newNode.innerHTML = strText.toUpperCase();
       }
       break;

       case 3: // Spalte 3: TEXT
       {
         var strText = data.claDataSetArray[i].strText;
         var newNode = document.createElement("a");

         if (strText.length > 19)
         {
           newNode.title = strText;
           strText = strText.substring(0,19);
         }
         
         newNode.href = data.claDataSetArray[i].strLink;
         newNode.className = "link_mini";
         newNode.innerHTML = strText.toUpperCase();
       }  
       break;

       default:
         var newNode = document.createTextNode("");
      }
      td.appendChild(newNode);       
      tr.appendChild(td);
    }
    tbody.appendChild(tr);
  }
  table.appendChild(tbody);
  return table;
}

/*
 ** (C) Wipplinger EDV Dienstleistungen 2008
 **** All Rights Reserved
 ****** Confidential
 ********
 **************************************************/