
//Xtraffic constructor
function Xtraffic(options) {

    var defaultoptions = {
        type    : 'exit',
        poptype : 'popup',
        delay   : 0,
        text_exitmsg : '\
    ******************************************\n\n\
    W A I T   B E F O R E   Y O U   G O !\n\n\
    CLICK THE [CANCEL] BUTTON TO SEE SPECIAL OFFERS FROM OUR PARTNERS!\n\n\
    ******************************************'
        //,url         : ''
        //,url_init    : ''
        //,url_report  : ''
    };

    this.options = options;
    for (var i in defaultoptions) {
        if (typeof this.options[i] == 'undefined') {
            this.options[i] = defaultoptions[i];
        }
    }

    switch (this.options.type) {
        case 'entry':
            this.setup_entry();
            break;
        case 'click':
            this.setup_click(this.options.elem);
            break;
        default:
            //exit is default
            this.setup_exit();
            break;

    }
}

Xtraffic.prototype.fire = function() {

        //fetch initial url to a not displayed image
        var i = new Image();
        i.src = this.options.url_init;

        if (this.options.delay > 0) {
            var _this = this;
            window.setTimeout(function() {
                    if (_this.options.poptype == 'popunder' || _this.options.poptype == 'popup') {
                        var pp = window.open(_this.options.url, 'pp');
                        //pp = window.open($('#bannerpixel').attr('rel'), 'pp', 'width=600,height=200');
                        if (_this.options.poptype == 'popunder' && pp) {
                            pp.blur();
                        }
                    }
                    else {
                        //redirect
                        window.location.href = _this.options.url;
                    }
            }, this.options.delay);
        }
        else {
            if (this.options.poptype == 'popunder' || this.options.poptype == 'popup') {
                var pp = window.open(this.options.url, 'pp');
                //pp = window.open($('#bannerpixel').attr('rel'), 'pp', 'width=600,height=200');
                if (this.options.poptype == 'popunder' && pp) {
                    pp.blur();
                }
            }
            else {
                //redirect
                window.location.href = this.options.url;
            }
        }
};

Xtraffic.prototype.setup_click = function(elem) {
    var _this = this;
    $(document).ready(function() {
        $(elem).click(function() {
            _this.fire();
        });
    });
};

Xtraffic.prototype.setup_entry = function() {
    var _this = this;
    $(document).ready(function() {
        _this.fire();
    });
};

Xtraffic.prototype.setup_exit = function() {
    skipunload = false;
    //save this reference
    var _this = this;
    $(document).ready(function() {
        //setup onbeforeunload handler
        window.onbeforeunload = function(e) {

            if (skipunload) return;

            window.setTimeout(function() {

                //reporting - exit_reporturl set as global var
                if (typeof exit_reporturl != 'undefined' && exit_reporturl) {
                    $('body').append("<img src='" + exit_reporturl + "' height='0' width='0'/>");
                }

                window.setTimeout(function(){
                    //delay the load in order to have time for the reporting pixel to fire
                    skipunload = true;
                    _this.fire();
                }, 1000);
            }, 1000);

          return _this.options.text_exitmsg;
        };

        //disable handler for links and formposts
        $('a').click(function(){
            var t = $(this).attr('target');
            if (!t || t == '_self') {
                window.onbeforeunload = null;
                return true;
            }
        });
        $('form').submit(function(){
            window.onbeforeunload = null;
            return true;
        });
    });
};