//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\
******************************************',
        text_alertmsg : ''
    };

    this.text_exitmsg    = {};
    this.text_alertmsg    = {};
    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.BR = '\
******************************************\n\n\
ESPERE UM POUCO!\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\
******************************************';

    this.text_alertmsg.ES = '\
******************************************\n\n\
¡Espera!\n\n\
¡Felicidades!\n\n\
¡ PULSA EL BOTÓN ***CANCELAR*** PARA VER OFERTAS ESPECIALES DE NUESTROS SOCIOS!\n\n\
Haga clic en ACEPTAR para continuar\n\n\
******************************************';

    this.text_exitmsg.PL = '\
******************************************\n\n\
¡ Z A C Z E K A J   Z A N I M   W Y J D Z I E S Z!\n\n\
¡ KLIKNIJ PRZYNISK [ANULUJ] BY ZOBACZYĆ SPECJALNE OFERTY OD NASZYCH PARTNERÓW!\n\n\
******************************************';


    this.text_exitmsg.CA = '\
> > > W A I T < < <\n\n\n\
CLICK THE ***CANCEL*** BUTTON\n\n\
on the NEXT Window for Something\n\n\
VERY Special!';

    this.text_alertmsg.CA = '\
W A I T   B E F O R E   Y O U   G O !\n\n\n\
CLICK THE *CANCEL* BUTTON RIGHT NOW\n\n\
TO STAY ON THE CURRENT PAGE.\n\n \n\
I HAVE SOMETHING VERY SPECIAL FOR YOU!';

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

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

    if (typeof this.text_alertmsg[country] != 'undefined') {
        defaultoptions.text_alertmsg = this.text_alertmsg[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);
            } else {
                if (_this.options.text_alertmsg != '') {
                    // show the first alert, if the message is defined.
                    window.alert(_this.options.text_alertmsg);
                }
                skipunload = true;
                _this.fire();
            }

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