var _st_ = {
    init: function() {
        this.version = "1.2b";
        this.uuid = this._uuid();
        this.img = new Image();
        this.pauseTimer = 500;
        this.siteId = "";
        this.base = "";
        this.title = document.title;
        this.goal = null;
        this.custom = null;
        this.href = window.location.pathname;
        this.subid = "SUBID";
        this.liveReplace = false;
        this._pingInterval = -1;
    },
    trackPage: function(id) {
        if (id != null) {
            this.siteId = id;
        }
        var c = "";
        if (this.custom) {
            for (var i in this.custom) {
                c += "&_custom[" + this.enc(i) + "]=" + this.enc(this.custom[i]);
            }
        }
        var g = "";
        if (this.goal) {
            for (var i in this.goal) {
                g += "&goal[" + this.enc(i) + "]=" + this.enc(this.goal[i]);
            }
        }
        var r = RegExp("^https?://[^/]*" + location.host.replace(/^www\./i, "") + "/", "i").test(document.referrer) ? "": document.referrer;
        var query = window.location.search.substring(1);
        if (query.length > 0) {
            query = "&_" + query.replace(/&/g, "&_");
        }
        this.img.src = this.logBase() + '&title=' + this.enc(this.title) + '&href=' + this.enc(this.href) + '&ref=' + this.enc(r) + '&res=' + screen.width + 'x' + screen.height + '&lang=' + (navigator.language || navigator.browserLanguage).substr(0, 2) + '&cid=' + Math.random() + g + c + query;
    },
    log: function(href, title, type) {
        type = type || "click";
        if (type != "outbound") {
            href = href.replace(/^https?:\/\/([^\/]+)/i, "");
        }
        this.img.src = this.logBase() + "&action=" + type + "&title=" + this.enc(title) + "&href=" + this.enc(href) + "&cid=" + Math.random();
        this.pause();
    },
    download: function(e) {
        this.logSrc(e, "download");
    },
    outbound: function(e) {
        this.logSrc(e, "outbound");
    },
    click: function(e) {
        this.logSrc(e, "click");
    },
    ping: function() {
        _st_.log("", "", "ping");
    },
    startPing: function(interval) {
        if (this._pingInterval == -1) {
            this._pingInterval = setInterval(this.ping, interval * 1000);
        }
    },
    stopPing: function() {
        if (this._pingInterval != -1) {
            clearInterval(this._pingInterval);
        }
    },
    logSrc: function(e, type) {
        obj = this.get_target(e);
        this.log(this.get_href(obj), this.get_text(obj).substr(0, 60), type);
    },
    pause: function() {
        var now = new Date();
        var stop = now.getTime() + this.pauseTimer;
        while (now.getTime() < stop) {
            now = new Date();
        }
    },
    logBase: function() {
        return this.base + "/log.php?site=" + this.siteId + '&uuid=' + this.uuid + '&ver=' + this.version;
    },
    get_text: function(e) {
        do {
            var txt = e.text ? e.text: e.innerText;
            if (txt) {
                return txt;
            }
            if (e.alt) {
                return e.alt;
            }
            if (e.title) {
                return e.title;
            }
            if (e.src) {
                return e.src;
            }
            e = this.get_parent(e);
        }
        while (e);
        return "";
    },
    get_href: function(e) {
        do {
            if (e.href && !e.src) {
                return e.href;
            }
            e = this.get_parent(e);
        }
        while (e);
        return "";
    },
    get_parent: function(e) {
        return e.parentElement || e.parentNode;
    },
    get_target: function(e) {
        if (!e) {
            e = window.event;
        }
        var t = e.target ? e.target: e.srcElement;
        if (t.nodeType && (t.nodeType == 3)) {
            t = t.parentNode;
        }
        return t;
    },
    trackUrl: function(target) {
        if (this.isEmpty(this.uuid)) {
            return target;
        }
        return target.replace(new RegExp(this.subid, "g"), this.uuid);
    },
    trackSrc: function(e, src) {
        $j(function() {
            $j("#" + e).attr("src", _st_.trackUrl(src));
        });
    },
    isEmpty: function(value) {
        if (value == undefined) {
            return true;
        }
        if (value === null) {
            return true;
        }
        if (value.length === 0) {
            return true;
        }
        return false;
    },
    _uuidlet: function() {
        return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
    },
    enc: function(e) {
        return encodeURIComponent ? encodeURIComponent(e) : escape(e);
    },
    _uuid: function() {
        return (this._uuidlet() + this._uuidlet() + this._uuidlet());
    }
};
_st_.init();
$j(function() {
    $j('#redirect').each(function(idx, item) {
        alert(item.href);
        window.location.replace(_st_.trackUrl(item.href));
    });
    if (_st_.liveReplace) {
        $j("a").each(function(idx, item) {
            item.href = _st_.trackUrl(item.href);
        });
    }
    $j("a").click(function(e) {
        this.href = _st_.trackUrl(this.href);
        var href = this.href;
        if (href.match(/\.(doc|pdf|xls|ppt|zip|txt|vsd|vxd|js|css|rar|exe|wma|mov|avi|wmv|mp3|flv|mp4)$/i)) {
            _st_.download();
        } else {
            if ((href.match(/^http/i)) && (!href.match(document.domain))) {
                _st_.outbound();
            }
        }
        return true;
    });
});