var clixpy_domain   = "%s";
var clixpy_sess_id  = "%s";
var clixpy_recv_subdmn  = "";

var Clixpy=function()
    {
        this.w=window;
        this.d=document; 
        this.nav=null;
        this.error=false;
        this.protocol=null;
        this.impression=null;
        this.start_time=0;
        this.loaded_time=0;
        this.encoding=null;
        this.logdata=[];
        this.flash=null;
        this.body_loaded=false;
        this.idle_timeout=false;
        this.last_event=null;
        this.intervals={};
        this.domain=clixpy_domain;
        this.sess_id=clixpy_sess_id;
        this.recv_subdmn=clixpy_recv_subdmn
    };
Clixpy.prototype.INIT=function()
    {
        if(this.w.clixpy_disabled)return;
        this.start_time=this.get_time();
        this.nav=navigator.userAgent.toLowerCase();
        this.protocol=this.d.location.protocol=="https:"?"https://":"http://";
        var e=this;
        var g=function(){if(!e.body_loaded){e.init_step2()}};
        var h=this.w.onload;if(typeof h!='function'){this.w.onload=function(){g()}}else{this.w.onload=function(){h();g()}}
        this.attach_event(this.w,'load',function(){g()});setTimeout(function(){g()},3000);
        this.w.onerror=function(a,b,c,d){return true}
    };
Clixpy.prototype.init_step2=function()
    {
        this.body_loaded=true;
        this.loaded_time=this.get_time();
        if(this.error){return false}
        if(!this.d.body||!this.get_screen_size()||!this.get_screen_scroll()){
            this.error=true;
            return false
        }
        this.encoding=this.d.charset?this.d.charset:(this.d.characterSet?this.d.characterSet:(this.d.inputEncoding?this.d.inputEncoding:""));
        this.impression=this.get_time()+"i"+Math.round(Math.random()*10000000);
        var a=this;
        var f=this.d.createElement("IFRAME");//iframe
        var screen_i = this.d.createElement('INPUT');
        var s_i = this.d.createElement('INPUT');//input for session_id
        this.s = this.d.createElement('FORM');//form
        this.a_i = this.d.createElement('INPUT');

        f.name="form_ifrm";
        f.id=f.name;
        f.setAttribute('width', '0');
        f.setAttribute('height', '0');
        f.setAttribute('border', '0');
        f.setAttribute('frameborder', '1');
        f.style.position="absolute";
        f.style.left="-10000px";
        f.style.top="-10000px";

        this.s.style.display = 'none';
        this.s.setAttribute('method', 'post');
        this.s.setAttribute('target', f.name);
        this.s.setAttribute('action', 'http://'+ clixpy_domain+'/json.php');

        this.a_i .setAttribute('type', 'hidden');
        this.a_i.setAttribute('name', 'actions');

        s_i.setAttribute('type', 'hidden');
        s_i.setAttribute('name', 'session_id');
        s_i.value = clixpy_sess_id;

        screen_i.setAttribute('type', 'hidden');
        screen_i.setAttribute('name', 'screen');
        screen_i.value = this.get_screen_size();

        this.s.appendChild(s_i);
        this.s.appendChild(screen_i);
        this.s.appendChild(this.a_i);
        this.d.body.appendChild(this.s);
        this.d.body.appendChild(f);
        f.contentWindow.clixpy_obj=this;

        
        a.init_step3();
    };
Clixpy.prototype.init_step3=function()
    {
        var c,i,obj=this;
        this.attach_event(this.d,'click',function(a)
        {
            a=obj.get_event(a);
            if(a&&a.clientX){obj.write_log("CL",[a.clientX,a.clientY])}});
            this.attach_event(this.d,'mousemove',function(a)
            {
                if(Math.round(Math.random()*1000)<667){a=obj.get_event(a);if(a&&a.clientX){obj.write_log("MV",[a.clientX,a.clientY])}}
            });
        this.attach_event(this.w,'scroll',function(a){obj.write_log("SC",obj.get_screen_scroll())});

        var j=0,el,n;
        this.elms = this.d.body.getElementsByTagName("*");

        for(i=0; i<this.elms.length; i++) {
            el=this.elms[i];
            n=el.nodeName;
            if(!n||n=="SCRIPT"||n=="STYLE"||n=="TBODY"||n.charAt(0)=="#"||n.charAt(0)=="!")continue;
            j++;
        //    if(!el.id||el.id===""){el.id="clixpy_id_"+j}
            this.elem_attach_events(el, i);
        }

        setInterval(function(){obj.send_report()},3000);
    };

Clixpy.prototype.elem_attach_events=function(e, index){
    if(e.getAttribute("noclixpy")){return}
    var f,obj=this, tag=e.nodeName;
    if(this.get_func_code(e.onmouseover)!==""){
        this.attach_event(e,'mouseover',function(a){
            obj.write_log("OMO",index)}
                )}
    if(this.get_func_code(e.onmouseout)!==""){
        this.attach_event(e,'mouseout',function(a){  obj.write_log("OMOU",[index])}
                )}
    this.attach_event(e,'mousedown',function(a){ obj.write_log("OMDN",[index])  });
    this.attach_event(e,'mouseup',function(a){ obj.write_log("OMUP",[index]) });
    var g=false;
    if(tag=="A"){
        if(!e.href||e.href===""||e.href.charAt(0)=="#"||e.target=="_blank"||e.href.match(/^javascript/)){
            this.attach_event(e,'click',function(a){obj.write_log("OCL",[index])});
            g=true
        }else{
            this.attach_event(e,'click',function(a){
                a=obj.get_event(a);
                if(a&&a.clientX){
                    obj.write_log("CL",[a.clientX,a.clientY])}
                    obj.write_log("OCL",[index]);
                    obj.force_send_data()});
                    g=true
        }
    }else
        if(tag=="INPUT"&&(e.type=="checkbox"||e.type=="radio")){
            this.attach_event(e,'click',function(a){
                obj.write_log("CHBX",[index,obj.get_event_target(this,a).checked?1:0])
            });
            g=true
        }
    if(tag=="SELECT"){
        this.attach_event(e,'change',function(a){obj.write_log("SCH",[index,obj.get_event_target(this,a).selectedIndex])})
    }
    if(tag=="TEXTAREA"||(tag=="INPUT"&&e.type=="text")){
        this.intervals["txt---"+index]=false;
        var h=this.intervals["txt---"+index];
        this.attach_event(e,'focus',function(a){
            var b=obj.get_event_target(this,a);
            clearInterval(h);
            h=setInterval(function(){
                if(b.value!==undefined){
                    obj.write_log("TXT",[index,escape(b.value)])}},2123);
            setTimeout(function(){
                if(b.value!==undefined){
                    obj.write_log("TXT",[index,escape(b.value)])}},500)});
        this.attach_event(e,'blur',function(a){
            var b=obj.get_event_target(this,a);
            clearInterval(h);
            if(b.value!==undefined){
                obj.write_log("TXT",[index,escape(b.value)])}});
        this.attach_event(e,'mouseup',function(a){
            var b=obj.get_event_target(this,a);
            clearInterval(h);
            h=setInterval(function(){
                if(b.value!==undefined){
                    obj.write_log("TXT",[index,escape(b.value)])}},2123);
            if(b.value!==undefined){
                obj.write_log("TXT",[index,escape(b.value)])}});
        this.attach_event(e,'keypress',function(a){
            a=obj.get_event(a);
            if(a&&(a.keyCode||a.charCode)&&!a.ctrlKey&&!a.altKey){
                var b=obj.get_event_target(this,a);
                if(b.selectionStart!==undefined&&b.selectionStart==b.value.length){
                    var c=a.charCode?a.charCode:a.keyCode;
                    if((c>=32&&c<=126)||(c>=1040&&c<=1103)||(c==13&&b.nodeName=="textarea")){
                        var d=b.value.charAt(b.value.length-1);
                        if(d){obj.write_log("TXTK",[index,escape(d)])}}
                    else if(c==8||c==13||c==127){
                        if(b.value!==undefined){
                            obj.write_log("TXT",[index,escape(b.value)])}}}
                else if(Math.round(Math.random()*1000)<334){
                    if(b.value!==undefined){
                        obj.write_log("TXT",[index,escape(b.value)])}}
                clearInterval(h);
                h=setInterval(function(){if(b.value!==undefined){
                    obj.write_log("TXT",[index,escape(b.value)])}},2123)}})}
    else if(tag=="INPUT"){
        this.attach_event(e,'focus',function(){obj.write_log("FC",[index])});
        this.attach_event(e,'blur',function(){obj.write_log("BL",[index])})}
    if(tag=="FORM"){
        this.attach_event(e,'submit',function(){obj.force_send_data()})}
    if(!g){this.attach_event(e,'click',function(a){obj.write_log("OCL",[index])})}
    return e
};
Clixpy.prototype.send_report=function()
    {
        if(this.error){return false}
        if(this.logdata.length===0){return true}
        var a=this.logdata.slice();

        // ------------ PARS FOR PHP -----------
        this.logdata=[];
        var i2,data2='',cnt=[];
        for(i=0;i<a.length;i++){
            cnt[cnt.length]= '"'+cnt.length+'":' + '{"a":"'+a[i][0]+'","b":"'+a[i][1]+'","c":"'+a[i][2]+'"}';
        }
       
        // ------------- Php pars end
        this.a_i.value = '{' + cnt.join() + '}';
        this.s.submit();

        if(this.idle_timeout)
        {
            clearTimeout(this.idle_timeout);
            this.idle_timeout=false
        }
        var b=this;
        this.idle_timeout=setTimeout(function(){b.write_log("IDL",[6])},6000)
    };
Clixpy.prototype.do_send_data=function(a)
    {
        if(this.error){return false}
        if(this.inprogress){return false}
        this.inprogress=true;
        this.flash.jsfunc(a);
        var b=this
    };
Clixpy.prototype.receive_response=function(){this.inprogress=false};
Clixpy.prototype.write_log=function(a,b){
    if(this.error)return false;
    if(this.logdata.length>1){
        var c=this.logdata[this.logdata.length-1];
        if(c[1]==a&&c[2].toString()==b.toString()){return true}}
        this.logdata[this.logdata.length]=[this.get_time()-this.start_time,a,b];
};
Clixpy.prototype.force_send_data=function()
    {
        if(this.error){return false}
        if(this.logdata.length>0)
            {
                var a="";
                var b="";
                var c=this.logdata.reverse();
                for(var i=0;i<c.length;i++)
                    {
                        if(c[i][1]!="CL"&&c[i][1]!="OCL"&&c[i][1]!="MV"&&c[i][1]!="SC")continue;
                        b=c[i][0]+"-"+c[i][1]+"-"+c[i][2].join(",")+"|";
                        if(a.length+b.length>=3900){break}a+=b
                    }
             }
        this.inprogress=false;this.send_report()
    };
Clixpy.prototype.attach_event=function(b,c,d){
    if(!b){return false}
    if(b.addEventListener){
        b.addEventListener(c,d,false)}
    else
        if(b.attachEvent){
            b.attachEvent('on'+c,d)}
        else{
            var e=this.get_func_code(b['on'+c]);
            var f=this;
            b['on'+c]=function(a){
                a=f.get_event(a);
                if(e!==""){
                    if(a){
                        f.last_event=a}
                    f.exec_function(e,this)}
            }
        }
};
Clixpy.prototype.get_event_target=function(a,b){
    if(a!=this.w)return a;
    var c=this.get_event( b );
    var d=c.target?c.target:c.srcElement;
    return d;
};
Clixpy.prototype.get_func_code=function(a){var b="";if(a===undefined||a===null){return""}b=this.trim(a.toString());var c=b.indexOf("{");if(c!=-1){b=this.trim(b.substr(c))}if(b===""){return""}return b+";"};
Clixpy.prototype.exec_function=function(a,b){var c=this.last_event;if(b&&a.indexOf("this")!=-1){a=a.replace(/([^a-z0-9]{1})this([\.\,\;\s\)])/igm,"$1inwhichobj$2")};if(a.indexOf("return ")!=-1){var d=true;var f=function(){};var g=c;eval("tmpfunc = function(event, inwhichobj) { "+a+" };");d=f(c,b);if(d===undefined){return true}return d}else{try{eval(a)}catch(e){}}};
Clixpy.prototype.get_time=function(){return new Date().getTime()};
Clixpy.prototype.get_event=function(e){
    if(!e&&this.w.event)e=this.w.event;
    return e;
};
Clixpy.prototype.trim=function(a){if(typeof(a)!="string"){return a}a=a.replace(/^\s+/,"");a=a.replace(/\s+$/,"");return a};
Clixpy.prototype.get_screen_size=function(){var w=0,h=0;if(typeof(window.innerWidth)=='number'){w=window.innerWidth;h=window.innerHeight}else if(document.documentElement&&(document.documentElement.clientWidth||document.documentElement.clientHeight)){w=document.documentElement.clientWidth;h=document.documentElement.clientHeight}else if(document.body&&(document.body.clientWidth||document.body.clientHeight)){w=document.body.clientWidth;h=document.body.clientHeight}else{return false}return[w,h]};
Clixpy.prototype.get_screen_scroll=function(){var x=0,y=0;if(typeof(this.w.pageYOffset)=='number'){x=this.w.pageXOffset;y=this.w.pageYOffset}else if(this.d.documentElement&&(this.d.documentElement.scrollLeft||this.d.documentElement.scrollTop)){y=this.d.documentElement.scrollTop;x=this.d.documentElement.scrollLeft}else if(this.d.body&&(this.d.body.scrollTop!==undefined)){x=this.d.body.scrollLeft;y=this.d.body.scrollTop}else{return false}return[x,y]};
//Clixpy.prototype.get_current_domain=function(){var a=this.w.location.href;a=a.replace(/^http(s)?\:\/\/?/i,"");a=a.replace(/^([^\/]+)\/.*/i,"$1");a=a.replace(/^([^\.]+\.){1,}([^\.]+\.[^\.]+)$/i,"$2");return a};

var clixpy_obj=new Clixpy();
clixpy_obj.INIT();
