window.addEvent("domready", function () {
    addExternalLinkTracking();
    transitionPageIn();
    addTransitionActionToLinks();
    addParallaxScrolling()
});
window.addEvent("unload", function () {});
var loaded = true;

function transitionPageIn() {
    if (transition_status == -1 || transition_status == 2) {
        return
    }
    if (window.orientation != undefined || Browser.ie) {
        $$(".blocker").destroy();
        return
    }
    if (!loaded) {
        var src = document.id("bgImageTop");
        if (src) {
            src.getStyle("background-image");
            src = src.replace('url("', "").replace('")', "");
            document.id("bgImageTop").setStyle("background-image", "none");
            var image = new Image();
            image.addEvent("load", function (e) {
                loaded = true;
                transitionPageIn.delay(250);
                document.id("bgImageTop").setStyle("background-image", src)
            });
            image.src = src
        }
        return
    }
    if (transition_status != 2) {
        var blockers = $$(".blocker");
        for (var i = 0; i < blockers.length; i++) {
            var fx = new Fx.Tween(blockers[i], {
                property: "opacity",
                link: "cancel",
                duration: 1000
            });
            fx.start(0)
        }
        if ($("bgImageTop")) {
            $("bgImageTop").setStyle("opacity", "0");
            var bgfx = new Fx.Tween($("bgImageTop"), {
                property: "opacity",
                link: "cancel",
                duration: 2000,
                transition: Fx.Transitions.Cubic.easeOut
            });
            (function () {
                bgfx.start(0, 1)
            }).delay(500)
        }
    }
}
function addTransitionActionToLinks() {
    if (transition_status == -1 || window.orientation != undefined || Browser.ie) {
        return
    }
    var section = (window.location.href).split("/")[3];
    if (section == "") {
        section = "home"
    }
    $$("a[href^=/]").each(function (a) {
        if (a.get("href") == "/change_language/") {
            a.addEvent("click", function (e) {
                e.preventDefault();
                fadePage(changeLanguage)
            })
        } else {
            if (!a.get("href").contains(section)) {
                a.addEvent("click", function (e) {
                    if (e.event.metaKey) {
                        return
                    }
                    e.preventDefault();
                    var href = this.href;
                    fadePage(function () {
                        $("content").empty();
                        document.location.href = href
                    })
                })
            }
        }
    })
}
function addParallaxScrolling() {
    if (!$("bgImageTop")) {
        return
    }
    if (window.orientation == undefined && !Browser.firefox) {
        var body = document.getElement("body"),
            headerimage = $("bgImageTop"),
            headergrad = $("bgGradientTop");
        window.addEvent("scroll", function windowScrollEvent() {
            body.setStyle("background-position", "0px " + (this.getScroll().y / 4) + "px");
            headerimage.setStyle("top", this.getScroll().y / 4 + "px");
            headergrad.setStyle("top", headerimage.getStyle("top").toInt() + headerimage.getStyle("height").toInt() + "px")
        })
    }
}
function changeLanguage() {
    Cookie.dispose("selector_verified");
    Cookie.dispose("country");
    window.location.reload()
}
function fadePage(callback) {
    var blocker = $$(".blocker.out")[0];
    blocker.setStyle("opacity", 0);
    blocker.setStyle("display", "block");
    blocker.setStyle("height", $(window).document.getScrollSize().y);
    var fx = new Fx.Tween(blocker, {
        property: "opacity",
        link: "cancel",
        duration: 300
    });
    fx.start(1).chain(function () {
        callback()
    })
}
function addExternalLinkTracking() {
    $$("a").addEvent("click", function (e) {
        var white_list = [window.location.host, "zenimax", "bethsoft", "bsw", "elderscrolls.com", "bethblog"],
            grey_list = ["facebook"],
            reg = new RegExp(white_list.join("|"), "ig");
        grey_reg = new RegExp(grey_list.join("|"), "ig");
        var href = this.get("href") || "#";
        var passes = (href.test("http") && !href.test(reg));
        !href.test(reg);
        if (passes) {
            e.stop();
            recordOutboundLink("outbound links", href);
            if (!href.test(grey_reg) && Cookie.read('country') == 'en-us') {
                href = "http://www.zenimax.com/external.php?locale=" + Cookie.read('country') + "&url=" + href
            }
            window.open(href, "_blank")
        }
    })
};
