
//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.text_exitmsg    = {};
    this.text_exitmsg.GR = '\
******************************************\n\n\
Π Ε Ρ Ι Μ Ε Ν Ε   Π Ρ Ι Ν   Φ Υ Γ Ε Ι Σ !\n\n\
ΚΑΝΕ ΚΛΙΚ ΣΤΟ ΚΟΥΜΠΙ [ΑΚΥΡΩΣΗ] ΓΙΑ ΝΑ ΔΕΙΣ ΤΙΣ ΕΙΔΙΚΕΣ ΠΡΟΣΦΟΡΕΣ ΑΠΟ ΤΟΥΣ ΣΥΝΕΡΓΑΤΕΣ ΜΑΣ!\n\n\
******************************************';
    this.text_exitmsg.PT = '\
******************************************\n\n\
A G U A R D E   A N T E S   D E   S A I R !\n\n\
CLIQUE NO BOTÃO [CANCELAR] PARA VER AS OFERTAS ESPECIAIS DOS NOSSOS PARCEIROS!\n\n\
******************************************';
    this.text_exitmsg.ES = '\
******************************************\n\n\
¡ E S P E R A   A N T E S   D E   I R T E !\n\n\
¡ PULSA EL BOTÓN [CANCELAR] PARA VER OFERTAS ESPECIALES DE NUESTROS SOCIOS!\n\n\
******************************************';

    var country = /\/(\w{2,4})\/ADS\//.exec(location.href)[1].toUpperCase();

    if (typeof this.text_exitmsg[country] != 'undefined') {
        defaultoptions.text_exitmsg = this.text_exitmsg[country];
    }

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

    //fetch initial url to a hidden image
    $(document).ready(function() {
        $('body').append("<img src='" + options.url_init + "' height='0' width='0'/>");
    });
    

    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() {

        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;
        });
    });
};