﻿var maze = {
    timeout: null,
    autoTimeout: null,
    isScrolling: true,
    fadeInOutSpeed: 0,
    scrollSpeed: 2000,
    easing: "easeInOutSine",
    slide: 1,
    slide2: {
        isLoaded: false,
        loadCalled: false,
        loaded: 0,
        onload: function () {
            maze.slide2.loaded++;
            if (maze.slide2.loaded == 4) {
                maze.slide2.isLoaded = true;
            }
        },
        load: function () {
            if (!maze.slide2.loadCalled) {
                maze.slide2.loadCalled = true;
                var fore2 = $('<img src="images/homeHero/maze/foreground_2.png" />');
                fore2.load(maze.slide2.onload);
                var fore3 = $('<img src="images/homeHero/maze/foreground_3.png" />');
                fore3.load(maze.slide2.onload);
                var top2 = $('<img src="images/homeHero/maze/maze_top_2.png" />');
                top2.load(maze.slide2.onload);
                var bot2 = $('<img src="images/homeHero/maze/maze_bot_2.jpg" />');
                bot2.load(maze.slide2.onload);
                if (evokeUtility.browser.isIE()) {
                    fore2.load(function () {
                        evokeUtility.fixPng(this);
                    });
                    fore3.load(function () {
                        evokeUtility.fixPng(this);
                    });
                    top2.load(function () {
                        evokeUtility.fixPng(this);
                    });
                }
                $("#foreground").append(fore2);
                $("#foreground").append(fore3);
                $("#mazetop").append(top2);
                $("#mazebot").append(bot2);
            }
        }
    },
    slide3: {
        isLoaded: false,
        loadCalled: false,
        loaded: 0,
        onload: function () {
            maze.slide3.loaded++;
            if (maze.slide3.loaded == 4) {
                maze.slide3.isLoaded = true;
            }
        },
        load: function () {
            if (!maze.slide3.loadCalled) {
                maze.slide3.loadCalled = true;
                var fore4 = $('<img src="images/homeHero/maze/foreground_4.png" />');
                fore4.load(maze.slide3.onload);
                var fore5 = $('<img src="images/homeHero/maze/foreground_5.png" />');
                fore5.load(maze.slide3.onload);
                var top3 = $('<img src="images/homeHero/maze/maze_top_3.png" />');
                top3.load(maze.slide3.onload);
                var bot3 = $('<img src="images/homeHero/maze/maze_bot_3.jpg" />');
                bot3.load(maze.slide3.onload);
                if (evokeUtility.browser.isIE()) {
                    fore4.load(function () {
                        evokeUtility.fixPng(this);
                    });
                    fore5.load(function () {
                        evokeUtility.fixPng(this);
                    });
                    top3.load(function () {
                        evokeUtility.fixPng(this);
                    });
                }
                $("#foreground").append(fore4);
                $("#foreground").append(fore5);
                $("#mazetop").append(top3);
                $("#mazebot").append(bot3);
            }
        }
    },
    slide4: {
        isLoaded: false,
        loadCalled: false,
        loaded: 0,
        onload: function () {
            maze.slide4.loaded++;
            if (maze.slide4.loaded == 4) {
                maze.slide4.isLoaded = true;
            }
        },
        load: function () {
            if (!maze.slide4.loadCalled) {
                maze.slide4.loadCalled = true;
                var fore6 = $('<img src="images/homeHero/maze/foreground_6.png" />');
                fore6.load(maze.slide4.onload);
                var fore7 = $('<img src="images/homeHero/maze/foreground_7.jpg" />');
                fore7.load(maze.slide4.onload);
                var top4 = $('<img src="images/homeHero/maze/maze_top_4.png" />');
                top4.load(maze.slide4.onload);
                var bot4 = $('<img src="images/homeHero/maze/maze_bot_4.jpg" />');
                bot4.load(maze.slide4.onload);
                if (evokeUtility.browser.isIE()) {
                    fore6.load(function () {
                        evokeUtility.fixPng(this);
                    });
                    top4.load(function () {
                        evokeUtility.fixPng(this);
                    });
                }
                $("#foreground").append(fore6);
                $("#foreground").append(fore7);
                $("#mazetop").append(top4);
                $("#mazebot").append(bot4);
            }
        }
    },
    scroll: function () {
        $("#sky").animate({
            left: "-=206"
        }, maze.scrollSpeed, maze.easing);
        $("#maze").animate({
            left: "-=822"
        }, maze.scrollSpeed, maze.easing);
        $("#foreground").animate({
            left: "-=1644"
        }, maze.scrollSpeed, maze.easing);
    },
    scrollLast: function () {
        $("#sky").animate({
            left: "-=308"
        }, maze.scrollSpeed * 1.5, maze.easing);
        $("#maze").animate({
            left: "-=1233"
        }, maze.scrollSpeed * 1.5, maze.easing);
        $("#foreground").animate({
            left: "-=2586"
        }, maze.scrollSpeed * 1.5, maze.easing);
    },
    prevSlide: function () {
        if (!this.isScrolling) {
            this.isScrolling = true;
            switch (this.slide) {
                case 4:
                    $("#mazeNavLeft").css("display", "block");
                    $("#mazeNavRight").css("display", "block");
                    $("#pagination > .on").css("left", $("#pagination > .page3").css("left"));
                    $("#copy > .slide4").hide();
                    $("#buttons > .slide4").hide();
                    $("#sky").css("left", "-412px");
                    $("#maze").css("left", "-1644px");
                    $("#foreground").css("left", "-3288px");
                    $("#copy > .slide3").fadeIn(maze.fadeInOutSpeed);
                    $("#buttons > .slide3").fadeIn(maze.fadeInOutSpeed);
                    this.slide--;
                    break;
                case 3:
                    $("#mazeNavLeft").css("display", "block");
                    $("#mazeNavRight").css("display", "block");
                    $("#pagination > .on").css("left", $("#pagination > .page2").css("left"));
                    $("#copy > .slide3").hide();
                    $("#buttons > .slide3").hide();
                    $("#sky").css("left", "-206px");
                    $("#maze").css("left", "-822px");
                    $("#foreground").css("left", "-1644px");
                    $("#copy > .slide2").fadeIn(maze.fadeInOutSpeed);
                    $("#buttons > .slide2").fadeIn(maze.fadeInOutSpeed);
                    this.slide--;
                    break;
                case 2:
                    $("#mazeNavLeft").css("display", "none");
                    $("#mazeNavRight").css("display", "block");
                    $("#pagination > .on").css("left", $("#pagination > .page1").css("left"));
                    $("#copy > .slide2").hide();
                    $("#buttons > .slide2").hide();
                    $("#sky").css("left", "0px");
                    $("#maze").css("left", "0px");
                    $("#foreground").css("left", "0px");
                    $("#copy > .slide1").fadeIn(maze.fadeInOutSpeed);
                    this.slide--;
                    break;
            }
            this.isScrolling = false;
        }
    },
    nextSlide: function () {
        if (!this.isScrolling) {
            this.isScrolling = true;
            switch (this.slide) {
                case 1:
                    if (maze.slide2.isLoaded) {
                        maze.slide3.load();
                        $("#copy > .slide1").fadeOut(maze.fadeInOutSpeed, function () {
                            window.setTimeout(function () {
                                $("#copy > .slide2").fadeIn(maze.fadeInOutSpeed);
                                $("#buttons > .slide2").fadeIn(maze.fadeInOutSpeed);
                                $("#pagination > .on").css("left", $("#pagination > .page2").css("left"));
                                maze.isScrolling = false;
                            }, maze.scrollSpeed);
                            maze.scroll();
                        });
                        $("#mazeNavLeft").css("display", "block");
                        $("#mazeNavRight").css("display", "block");
                    } else {
                        window.clearTimeout(maze.timeout);
                        maze.timeout = window.setTimeout(maze.nextSlide, 250);
                    }
                    this.slide++;
                    break;
                case 2:
                    if (maze.slide3.isLoaded) {
                        maze.slide4.load();
                        $("#copy > .slide2").fadeOut(maze.fadeInOutSpeed);
                        $("#buttons > .slide2").fadeOut(maze.fadeInOutSpeed, function () {
                            window.setTimeout(function () {
                                $("#copy > .slide3").fadeIn(maze.fadeInOutSpeed);
                                $("#buttons > .slide3").fadeIn(maze.fadeInOutSpeed);
                                $("#pagination > .on").css("left", $("#pagination > .page3").css("left"));
                                maze.isScrolling = false;
                            }, maze.scrollSpeed);
                            maze.scroll();
                        });
                        $("#mazeNavLeft").css("display", "block");
                        $("#mazeNavRight").css("display", "block");
                    } else {
                        window.clearTimeout(maze.timeout);
                        maze.timeout = window.setTimeout(maze.nextSlide, 250);
                    }
                    this.slide++;
                    break;
                case 3:
                    if (maze.slide4.isLoaded) {
                        $("#copy > .slide3").fadeOut(maze.fadeInOutSpeed);
                        $("#buttons > .slide3").fadeOut(maze.fadeInOutSpeed, function () {
                            window.setTimeout(function () {
                                $("#copy > .slide4").fadeIn(maze.fadeInOutSpeed);
                                $("#buttons > .slide4").fadeIn(maze.fadeInOutSpeed);
                                $("#pagination > .on").css("left", $("#pagination > .page4").css("left"));
                                maze.isScrolling = false;
                            }, maze.scrollSpeed * 1.5);
                            maze.scrollLast();
                        });
                        $("#mazeNavLeft").css("display", "block");
                        $("#mazeNavRight").css("display", "none");
                    } else {
                        window.clearTimeout(maze.timeout);
                        maze.timeout = window.setTimeout(maze.nextSlide, 250);
                    }
                    this.slide++;
                    break;
                case 4:
                    $("#mazeNavLeft").css("display", "none");
                    $("#mazeNavRight").css("display", "block");
                    $("#pagination > .on").css("left", $("#pagination > .page1").css("left"));
                    $("#copy > .slide4").hide();
                    $("#buttons > .slide4").hide();
                    $("#sky").css("left", "0px");
                    $("#maze").css("left", "0px");
                    $("#foreground").css("left", "0px");
                    $("#copy > .slide1").fadeIn(maze.fadeInOutSpeed);
                    this.isScrolling = false;
                    this.slide = 1;
                    break;
            }
        }
    },
    autoRotate: function () {
        var delay = 10000;
        switch (maze.slide) {
            case 2:
            case 3:
                delay += maze.scrollSpeed;
                break;
            case 4:
                delay += maze.scrollSpeed * 1.5;
                break;
        }
        this.autoTimeout = window.setTimeout(function () {
            maze.next();
        }, delay);
    },
    prev: function () {
        window.clearTimeout(this.autoTimeout);
        this.prevSlide();
        this.autoRotate();
    },
    next: function () {
        window.clearTimeout(this.autoTimeout);
        this.nextSlide();
        this.autoRotate();
    }
};

$(window).load(function () {
    maze.isScrolling = false;
    maze.slide2.load();
    maze.autoRotate();
    $("#buttons").find("a").each(function () {
        $(this).mouseover(function () {
            window.clearTimeout(maze.autoTimeout);
        });
        $(this).mouseout(maze.autoRotate);
    });
});
