var systemtimer_interval=1000;
var systemtimer_buisy=0;
var systemtimer_data=new Array();

$(document).ready( function() {
 systemtimer_init();
});

function systemtimer_init()
{
 $(document).everyTime(systemtimer_interval, 'systemtimer', function(i) {
 systemtimer_execute();	
 });
 $(window).unload( function() {
  $(document).stopTime('systemtimer');
 });
}

function  systemtimer_add(interval,callfunc,label,callfirst)
{
 if(typeof label=='undefined') label=''; 
 var timerdata={interval:interval,callfunc:callfunc,label:label};
 timerdata.counter=0;
 timerdata.age=0;
 timerdata.lastcall=datestamp();
 if(callfirst==true) systemtimer_call(timerdata); 
 systemtimer_data.push(timerdata);
}

function  systemtimer_delete(label)
{
 for(var i=0;i<systemtimer_data.length;i++)
 if(systemtimer_data[i].label==label)
 {
  systemtimer_data.splice(i,1);
 }
}


function  systemtimer_execute()
{
 if(systemtimer_buisy) return;
 systemtimer_buisy=1;
 for(var i=0;i<systemtimer_data.length;i++)
 {
  var timerdata=systemtimer_data[i];
  if(timerdata.lastcall+timerdata.interval<=now()) 
   systemtimer_call(timerdata);
 }
 systemtimer_buisy=0;
}

function  systemtimer_call(timerdata)
{
 timerdata.counter+=1;
 timerdata.age=datestamp()-timerdata.lastcall;
 timerdata.callfunc(timerdata);
 timerdata.lastcall=datestamp();
}

function  systemtimer_ajax(ajaxparams,timerdata)
{
 ajaxparams.data.timerdata=timerdata;
 ajaxrequest0(ajaxparams);
}
function  systemtimer_addajax(interval,ajaxparams,data,label,callfirst)
{
 ajaxparams.url=systemdata.http_path+'/'+systemdata.index_path+'/index.php'+'?command=ajaxrequest&sitemode=rawdata&ajaxrequest=1';
 ajaxparams.data=data;
 ajaxparams.data['ajaxcontrol']=1;
 systemtimer_add(interval,(function(timerdata) {systemtimer_ajax(ajaxparams,timerdata);}),label,callfirst);
}




