﻿var searchGuid;

window.onload = function()
{
    var head = document.getElementsByTagName("head")[0];
    var script = document.createElement('script');
    script.type = 'text/javascript';
	script.src = 'jquery-1.3.2.min.js';
	head.appendChild(script);
	
	
	script.type = 'text/javascript';
	script.src = 'json.js';
	head.appendChild(script);
	
	
	script = document.createElement('script');
	script.type = 'text/javascript';
	script.src = 'http://log.ewoss.net/Logger.ashx?proxy';
	head.appendChild(script);

    setTimeout("doLog()", 400);
};

function doLog()
{
    searchGuid = getCookie('hitGuid');
    if(searchGuid == null || searchGuid == "")
    {
        searchGuid = guid();
        setCookie('hitGuid', searchGuid, 1);
    }
    
    var s = new Logger();
    s.channel = new JQueryChannel({ jsonp: true });
    s.kwargs = true;
    s.getuserinfo(dump);
    
    var anchors = document.getElementsByTagName("a");
    var size = anchors.length;
    
    for(var i = 0; i < size; i++)
    {
        anchors[i].onclick = doClick(anchors[i], i);
    }
}

function doClick(anchor, indx)
{
    return function() {
        var s = new Logger();
        s.channel =  new JQueryChannel({ jsonp: true });
        s.kwargs = true;
        s.insertclicklog(searchGuid, anchor.href, anchor.innerHTML, (indx + 1), function() {}); 
    }
}

function JQueryChannel(options) {
    options = options || {};
    this.rpc = function(call) {
        if (call.request.params.constructor === Array)
            throw new Error('Positional parameters are not supported.');
        var params = [];
        $.each(call.request.params, function(k, v) {
            if (v) params.push(k + '=' + encodeURIComponent(v));
        });
        var ajax_REQ = $.ajax({
            url: call.url + '/' + call.request.method,
            type: 'GET',
            jsonp: options.jsonp ? 'jsonp' : null,
            cache: options.cache,
            data: params.join('&'),
            dataType: options.jsonp ? 'jsonp' : 'json',
            timeout: 10000,
            // TODO error: ...
            success: function(data) {
                if (ajax_REQ) { ajax_REQ.abort(); }
                call.callback(data);
            }
        });
    }
}

function dump(response) {
    if(document.referrer != "")
    {
        var s = new Logger();
        s.channel = new JQueryChannel({ jsonp: true });
        s.kwargs = true;
        s.inserthitlog(searchGuid, document.URL, response.result.userIp, response.result.userAgent, document.referrer, userId, dump2);
    }
}

function dump2()
{

}

function S4() {
   return (((1+Math.random())*0x10000)|0).toString(16).substring(1);
}
function guid() {
   return (S4()+S4()+S4()+S4()+S4()+S4()+S4()+S4());
}

function setCookie(c_name,value,expiredays)
{
    var exdate=new Date();
    exdate.setDate(exdate.getDate()+expiredays);
    document.cookie=c_name+ "=" +escape(value) + ((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}

function getCookie(c_name)
{
    if (document.cookie.length>0)
      {
      c_start=document.cookie.indexOf(c_name + "=");
      if (c_start!=-1)
        {
        c_start=c_start + c_name.length+1;
        c_end=document.cookie.indexOf(";",c_start);
        if (c_end==-1) c_end=document.cookie.length;
        return unescape(document.cookie.substring(c_start,c_end));
        }
      }
    return "";
}