// JavaScript Document
function StatisticAction()
{
}

StatisticAction.prototype.actionPerformed=function(anEvent)
{
  var theStats = window.callStatistic;
  var theResult = 'Statistics: ';
  if (theStats)
  {
    var theElement;
    for (theElement in theStats)
    {
      var theStatObj = theStats[theElement];
      
      var theValues = theStatObj.getValues();
      theResult+=theStatObj.name+'\n'; 
      var theName;
      for (theName in theValues)
      {
        var theCalls = theStatObj.getCalls(theName);
        var theTime = theStatObj.getTime(theName);
        var theAvTime = theStatObj.getAverageTime(theName);
      
        theResult+=" - "+theName;
        theResult+=" Calls: "+theCalls+"\t";
        theResult+="Sum: "+theTime+" ms\t";
        theResult+="Average: "+theAvTime.toFixed(2)+" ms\t";
        theResult+="Max: "+theStatObj.getMaxTime(theName)+" ms\n";
      }
      alert (theResult);
      theResult = 'Statistics:\n';
    }
  }
  
}

function Statistic(aName)
{
  this.name =aName;
  this.CALL = 'c';
  this.TIME='t';
  this.MAXTIME='mt';
  this.BEGIN='b';
  
  this.values = new Array();
}

Statistic.prototype.getValues=function(aFunction)
{ 
 return this.values;
}

Statistic.prototype.incCall=function(aFunction)
{
 if(!this.values[aFunction])
 {
  this.values[aFunction]=new Array();
  
 }
 if (!this.values[aFunction][this.CALL])
 {
  this.values[aFunction][this.CALL]=0;
 } 
 var theOld = this.values[aFunction][this.CALL];
 if(!theOld)
 {
  theOld = 1;
 }
 else
 {
  theOld++;
 }
 this.values[aFunction][this.CALL]=theOld;
 this.startCall(aFunction);
} 

Statistic.prototype.startCall=function(aFunction)
{
 if(!this.values[aFunction])
 {
  this.values[aFunction]=new Array();
 }
 this.values[aFunction][this.BEGIN]=new Date();
}

Statistic.prototype.endCall=function(aFunction)
{
 if(!this.values[aFunction])
 {
  this.values[aFunction]=new Array();
  this.values[aFunction][this.BEGIN]=new Date();
  this.values[aFunction][this.TIME]=0;
  this.values[aFunction][this.MAXTIME]=0;
 }
 else {
   if (!this.values[aFunction][this.BEGIN])
   {
    this.values[aFunction][this.BEGIN]=new Date();
   }
   if (!this.values[aFunction][this.TIME])
   {
    this.values[aFunction][this.TIME]=0;
   }
   if (this.values[aFunction][this.MAXTIME] == null)
   {
    this.values[aFunction][this.MAXTIME]=0;
   }
 }
 
 var theBegin=this.values[aFunction][this.BEGIN].getTime();
 var theEnd = new Date().getTime();
 var theOld = this.values[aFunction][this.TIME];
 var theNew = theEnd-theBegin;
 theOld= theOld+(theNew);
 
 if (theNew > this.values[aFunction][this.MAXTIME])
 {
  this.values[aFunction][this.MAXTIME]=theNew;
 }
 
 this.values[aFunction][this.TIME]=theOld;
}
 
Statistic.prototype.getAverageTime=function(aFunction)
{
  if (this.values[aFunction])
  {
    var theValue = this.values[aFunction][this.TIME];
    var theCall = this.values[aFunction][this.CALL];
    if (theValue && theCall)
    {
      return theValue/theCall;
    }
  }
  return 0;
}

Statistic.prototype.getMaxTime=function(aFunction)
{
  if (this.values[aFunction])
  {
    return this.values[aFunction][this.MAXTIME];
  }
  return 0;
}


Statistic.prototype.getTime=function(aFunction)
{
  if (this.values[aFunction])
  {
    var theValue = this.values[aFunction][this.TIME];
    
    if (theValue)
    {
      return theValue;
    }
  }
  return 0;
}


Statistic.prototype.getCalls=function(aFunction)
{
  if (this.values[aFunction])
  {
    var theCall = this.values[aFunction][this.CALL];
    if (theCall)
    {
      return theCall;
    }
  }
  return 0;
}


function StatisticFactory()
{

}

StatisticFactory.prototype.createStatistic=function(aName)
{
  if (window.callStatistic)
  {
    if (!window.callStatistic[aName])
    {
      window.callStatistic[aName]=new Statistic(aName);
    }
  }
  else
  {
    window.callStatistic = new Array();
    window.callStatistic[aName]=new Statistic(aName);
  }
  return window.callStatistic[aName];
}
