﻿var clicky_link_url_arg = "&link=";
var clicky_text_url_arg = "text=";
var clicky_stamp_url_arg = "&stamp=";
var clicky_image_load_pause_msecs = 500;
function clicky_getDomain(url) {
    var i = url.indexOf("://");
    if (i == -1) {
        return "";
    }

    url = url.substring(i + 3, url.length);
    i = url.indexOf("/");
    if (i != -1) {
        url = url.substring(0, i);
    }
    return url;
}

function clicky_handleError() {
    return true;
}

function clicky_report_onclick(ev) {
    var oldonerror = window.onerror;
    window.onerror = clicky_handleError;
    clicky_report_onclick_inner(ev);
    window.onerror = oldonerror;
    if (old_onclick) {
        old_onclick(e);
    }
}

function clicky_report_onclick_inner(ev) {
    var text;
    var url;
    var target;
    if (ev) {
        target = ev.target;
    }
    else {
        target = window.event.srcElement;
    }

    for (var i = 0; target && i <= 20; i++) {
        if (target.href) {
            break;
        }
        target = target.parentNode;
    }

    if (target && target.href) {
        url = target.href;
        if (target.innerHTML) {
            text = target.innerHTML;
        }
        else if (target.innerText) {
            text = target.innerText;
        }
        else if (target.text) {
            text = target.text;
        }
    }

    if (!url || typeof (url) != 'string' || url == "") {
        return;
    }
    if (!text || typeof (text) != 'string' || text == "") {
        text = "[no text found]";
    }

    var currentDomain = clicky_getDomain("" + window.location);
    var currentBaseDomain = currentDomain;

    if (currentBaseDomain.indexOf("www.") == 0) {
        currentBaseDomain = currentBaseDomain.substring(3, currentBaseDomain.length);
    }
    var expressionStr = "(\\w+[.])*(" + currentBaseDomain + ")$";
    var regex = new RegExp(expressionStr, "i");
    if (text.length > 200) {
        text = text.substring(0, 200) + "...";
    }
    if (!clicky_getDomain(url).match(regex)) {
        var timeStamp = new Date();
        //var reportUrl = "http://" + currentDomain + "/ClickLog/Pix.ashx?" + clicky_text_url_arg + escape(text) + clicky_link_url_arg + escape(url) + clicky_stamp_url_arg + timeStamp.valueOf();
        var reportUrl = "http://" + currentDomain + "/Prahari.ashx?" + clicky_text_url_arg + escape(text) + clicky_link_url_arg + escape(url) + clicky_stamp_url_arg + timeStamp.valueOf();
        //alert(reportUrl);
        var image = new Image();
        image.src = reportUrl;
        var now = new Date();
        var stopTime = now.getTime() + clicky_image_load_pause_msecs;
        while (now.getTime() < stopTime) {
            now = new Date();
        }
    }
}
try {
    window.captureEvents(Event.CLICK);
    window.onclick = clicky_report_onclick
}
catch (e) { }
var old_onclick = document.body.onclick;
document.body.onclick = clicky_report_onclick;