﻿var Lbjs = {

    IsMouse: false,
    IsClick: false,
    Unload: 0,
    AdType: 0,
    Token: "",
    AdUrl: "",
    TargetUrl: "",
    Countdown: 0,

    Init: function () {

        this.Buster();
        this.Content();

        switch (this.AdType) {
            case 1:
                this.Banner();
                break;
            case 2:
                this.Timer();
                break;
            case 6:
                this.Banner();
                break;
        }

        document.onmouseover = this.MouseOverHandler;
        window.onbeforeunload = this.UnloadHandler;
        document.onmousedown = this.MouseDownHandler;
    },

    Timer: function () {

        var timer = document.getElementById("timer");
        var skip = document.getElementById("skip");
        var skip_disabled = document.getElementById("skip_disabled");

        timer.innerHTML = this.Countdown;

        if (--this.Countdown >= 0) {
            setTimeout(function () { Lbjs.Timer() }, 1000);
        }
        else {
            this.Fader.FadeOut(timer, 500, null);
            this.Fader.FadeOut(skip_disabled, 750, function () { Lbjs.Skip(); });
        }
    },

    MouseOverHandler: function () {

        if (Lbjs.IsMouse)
            return;

        Lbjs.IsMouse = true;

        switch (Lbjs.AdType) {
            case 1:
                Lbjs.Track(1);
                break;
            case 6:
                Lbjs.Track(1);
                break;
        }

        Lbjs.Track(2);
    },

    MouseDownHandler: function () {
        Lbjs.IsClick = true;
    },

    UnloadHandler: function () {
        Lbjs.Unload++;
    },

    Content: function () {

        var content = document.getElementById("content");
        var parent = content.parentNode;

        parent.removeChild(content);
        parent.appendChild(content);

        content.style.display = "";

        switch (this.AdType) {
            case 1:
                content.src = this.TargetUrl;
                break;
            case 2:
                content.src = this.AdUrl;
                break;
            case 6:
                content.src = this.TargetUrl;
                break;
        }
    },

    Banner: function () {

        var banner = document.getElementById("banner");
        var parent = banner.parentNode;

        parent.removeChild(banner);
        parent.appendChild(banner);

        banner.style.display = "";
        banner.src = this.AdUrl;
    },

    Skip: function () {

        var skip = document.getElementById("skiplink");
        skip.style.display = "";
        skip.href = this.TargetUrl;

        Lbjs.Track(1);
    },

    RemoveFrame: function () {
        window.location = Lbjs.TargetUrl;
        return false;
    },

    Browser: function () {

        var div = document.createElement('div');
        div.innerHTML = '<!--[if IE]><![endif]-->';
        this.IsIE = (div.innerHTML.length == 0) ? true : false;
    },

    Buster: function () {

        var interval = setInterval(function () {

            if (Lbjs.IsClick) {
                clearInterval(interval);
            }
            else if (Lbjs.Unload > 0) {
                Lbjs.Unload -= 2;
                window.top.location.replace("/CancelNavigation.aspx");
                Lbjs.NavigationNotice();
            }
        }, 1);

        var clearDelay = (this.Countdown > 0) ? this.Countdown : 8;
        setTimeout(function () { clearInterval(interval); }, clearDelay * 1000);
    },

    NavigationNotice: function () {

        var navNotice = document.getElementById("navNotice");
        navNotice.innerHTML = "<span class=\"warning\" style=\"text-align:center;height:20px;width: 400px;padding: 10px;\"><b>Request Cancelled</b> - Navigation is disabled for 8 Seconds...</span>";

        if (navNotice.style.display == "none") {
            this.Fader.FadeIn(navNotice, 200, function () {
                setTimeout(function () {
                    Lbjs.Fader.FadeOut(navNotice, 200, null);
                }, 1500);
            });
        }
    },

    Track: function (type) {

        var img = new Image();
        var rnd = Math.random();
        var url = "/track/" + type + "/" + this.Token + "?rnd=" + rnd;

        img.src = url;
    },

    Fader: {

        FadeOut: function (element, duration, callback) {

            var params = {
                State: 0,
                Element: element,
                Duration: duration,
                Callback: callback,
                Remaining: 0,
                LastTick: 0
            };

            this.Init(params);
            this.Animate(params);
        },

        FadeIn: function (element, duration, callback) {

            var params = {
                State: 1,
                Element: element,
                Duration: duration,
                Callback: callback,
                Remaining: 0,
                LastTick: 0
            };

            this.Init(params);
            this.Animate(params);
        },

        Init: function (params) {

            var display = params.Element.style.display;

            if (display === "inline") {
                params.Element.style.display = "inline-block";
            }
            else {
                params.Element.style.display = "inline";
                params.Element.style.zoom = 1;
            }

            params.Remaining = params.Duration;
            params.LastTick = new Date().getTime();
        },

        Animate: function (params) {

            var newTick = new Date().getTime();
            var scope = this;
            var opacity = 0;

            params.Remaining -= newTick - params.LastTick;
            opacity = params.Remaining / params.Duration;
            opacity = (params.State == 0) ? opacity : (1 - opacity);

            var setOpacity = function (opacity) {
                params.Element.style.opacity = opacity;
                params.Element.style.filter = 'alpha(opacity = ' + (opacity * 100) + ')';
            }

            if (params.Remaining <= 0) {

                if (params.State == 0) {
                    opacity = 0;
                    params.Element.style.display = "none"
                }
                else {
                    opacity = 1;
                    params.Element.style.display = "";
                }

                setOpacity(opacity);

                if (typeof params.Callback == "function")
                    params.Callback();

                return;
            }
            else {
                setOpacity(opacity);
            }

            params.LastTick = new Date().getTime();
            setTimeout(function () { scope.Animate(params); }, 50);
        }
    }

}
