﻿var TrackingEvent = function (item) {
    var self = this;

    this.item = jQuery(item);
    this.events = 0;

    this.followLink = function () {
        this.events--;
        if (this.events <= 0) {
            if (this.item.attr('target') != '_blank') {
                top.location.href = this.item.attr('href');
            }
        }
    };

    this.unica = function (name) {
        this.events++;
        var self = this;

        this.item.click(function (e) {
            if (self.item.attr('target') != '_blank')
                e.preventDefault();

            ntptEventTag("ev=" + name);
            self.followLink();
        });
        return this;
    };

    this.floodlight = function (name) {
        this.events++;
        var self = this;

        this.item.click(function (e) {
            if (self.item.attr('target') != '_blank')
                e.preventDefault();

            var axel = Math.random() + "";
            var num = axel * 1000000000000000000;
            var img = new Image();
            img.src = 'http://ad.doubleclick.net/activity;src=1869704;type=biggc473;cat=' + name + ';ord=' + num;
            jQuery(img).load(function () {
                self.followLink();
            });
        });
        return this;
    };

    this.sem = function (name) {
        this.events++;
        var self = this;
        this.item.click(function (e) {
            if (self.item.attr('target') != '_blank')
                e.preventDefault();

            self.followLink();
        });
        return this;
    };

    return {
        unica: function (name) {
            return self.unica(name);
        },
        floodlight: function (name) {
            return self.floodlight(name);
        },
        sem: function (name) {
            return self.sem(name);
        }
    };
};

