_=function(){
    var f="http://www.triggerreplay.com/secure/server.php";  //http://www.triggerreplay.com/trigger_replay/secure/server.php
    var l=true;
    var b=new PHPRPC_Client("http://www.triggerreplay.com/secure/index.php",["statistics"]);  //http://www.triggerreplay.com:81
    var i=new PHPRPC_Client("http://www.triggerreplay.com/secure/server.php",["getConfigXml"]);  //http://www.triggerreplay.com:82
    b.setKeyLength(256);
    b.setEncryptMode(2);
    i.setKeyLength(256);
    i.setEncryptMode(2);
    var m="";
    Array.toArray=Array.to_a=function(p){
                                              var o=new Array();
                                              for(var q=0;q<p.length;q++){
                                                  o.push(p[q])}
                                              return o};
    function n(p){
        var r="0123456789ABCDEF",q=[],o=(p&255);
        while(p){
            p=(p>>8)&16777215;
            q.push(r[o&15]);
            q.push(r[o>>4]);
            o=(p&255)}
        return q.reverse().join("")}
    
    function k(o,p){while(o.length<p){o="0"+o}return o}
    function j(){return k(n(Math.floor(Math.random()*4294967295)),8)}
    function e(){return j()+j()}

    var h="DEBUG"+e();
    if(l){document.write('<div id="'+h+'"></div>')}

    function d(o){}
    function c(){var o;i.getConfigXml("",function(p,r,q,s){m=p},true)}
    function a(o,p){b.statistics(o,function(q,s,r,t){},true)}
    
    var g=function(){
        this.initialized=false;
        this.events=[];
        this.specialEvents=[];
        this.config={baseurl:"http://www.triggerreplay.com/secure/server.php",delay:20,init:null,shutdown:null};
        this.state={session:null,active:null};
        this.current_event=null;
        this.level_begin=null;
        this.level_end=null;
        this.level_life_time=null;
        this.interval=null;
        this.get_event=function(p){for(var o=0;o<events.length;o++){if(events[o].name==p){return events[o]}}return null};
        this.get_special_event=function(p){for(var o=0;o<specialEvents.length;o++){if(specialEvents[o].name==p){return specialEvents[o]}}return null};

        this.write=function(o,t,q,p,r){
            try{
                if(!o){
                    d("signal: no event specified, assuming event termination");
                    return}
                if(p!=""){
                    a([p.id,t,state.session,q,r].join("-"))
                }else{
                    a([o.id,t,state.session,q,""].join("-"))
                }
                current_event=(o.close?o:null)
            }catch(s){
                d("signal: write exception ignored ")}
        };

        this.init=function(){
            if(!config){d("init: missing config");return}
            if(state.session){d("init: forcing shutdown (previous session detected)")}
            if(state.session){shutdown()}
            d("init: new session");
            current_event=get_event("init");
            state.session=e();
            write({name:"$system$init",id:config.init},0,"","");
            date=new Date();
            level_begin=date.getTime()};

        this.signal=function(){
            var t=arguments[0];
            if(arguments[1]){
                var r=arguments[1];
            }else{
                var r="";
            }
            if(arguments[2]){
                var u=arguments[2];
            }else{
                var u="";
            }
            if(arguments[3]){
                var s=arguments[3];
            }else{
                var s="";
            }
            
            if(!config){
                d("signal: missing config");
                return;
            }

            if(!state.session){
                d("signal: forcing init (no session)");
            }
            if(!state.session){
                init();
            }

            var o=get_special_event(t);
            var q=get_event(t);
            if(u!=""){
                var p=get_event(u);
            }
            if(o==null){
                if(q){
                    d('signal: "'+t+'" (ID='+q.id+")");
                }else{
                    d('signal: "'+t+'" unknown, ignored');
                }
                if(q){
                    if(q.name=="end"){
                        date2=new Date();
                        level_end=date2.getTime();
                        level_life_time=(parseInt(level_end)-parseInt(level_begin))/1000;
                        d("level end  post the satistics data: time = "+level_life_time+"get points:"+s);
                        write(q,level_life_time,r,p,s);
                    }else{
                        date=new Date();
                        level_begin=date.getTime();
                        d("level just begin (event):"+q.name);
                    }
                }
            }else{
                if(o!=null){
                    d("special event just triggered: id:"+o.name);
                    write(o,0,r,"","");
                }
            }
        };

        this.shutdown=function(){
            if(!config){
                d("shutdown: missing config");
                return;
            }
            if(!state.session){
                d("shutdown: ignored (no current session)");
            }
            if(!state.session){
                return;
            }
            d("shutdown: ending current session");
            write({name:"$system$shutdown",id:config.shutdown},0,"","");
            state.session=null;
            current_event=null;
            if(interval){
                clearInterval(interval)
            }
        };
        
        this.parse_config=function(q){
            var p;
            i.getConfigXml(q,function(o,s,r,u){
                setTimeout(function(){},200);
                p=JSON.parse(o);
                if(p.init){
                    d("config: init="+(config.init=p.init));
                }
                if(p.shutdown){
                    d("config: shutdown="+(config.shutdown=p.shutdown));
                }
                
                var v=p.events==null ? "" : p.events;
                if(!p.init){
                    d("config: missing baseurl or delay, aborting (no events will be delivered)");
                    config=null;
                    return;
                }
                d("config: num_evts="+v.length);
                for(var t=0;t<v.length;t++){
                    d('config: event_name="'+v[t].name+'", evt_id='+v[t].id);
                    this.events.push({name:v[t].name,id:v[t].id,close:v[t].close})
                }
                
                var w=p.special_events==null ? "" : p.special_events;
                for(var t=0;t<w.length;t++){
                    d('config: special_event_name="'+w[t].name+'", special_evt_id='+w[t].id);
                    this.specialEvents.push({name:w[t].name,id:w[t].id,close:w[t].close})
                }
            },true)
        };
        return this
    };
    g=g();

    replay_init=function(o){
        g.parse_config(o);
    };

    replay_signal=function(o,r,p,q){
        g.signal(o,r,p,q);
    };

    replay_shutdown=function(){
        g.shutdown();
    };
    d("getting config");

    return null;
}();
