﻿$(document).ready(function () {
    $(window).load(
    function () {

        //check to see if intro should play

        var playMMBIntro = readCookie('hasVisitedMMB')

        if (playMMBIntro == null) {
            introAnimation();
        }

        createCookie('hasVisitedMMB', true, 0)

        function createCookie(name, value, days) {
            if (days) {
                var date = new Date();
                date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
                var expires = "; expires=" + date.toGMTString();
            }
            else var expires = "";
            document.cookie = name + "=" + value + expires + "; path=/";
        }

        function readCookie(name) {
            var nameEQ = name + "=";
            var ca = document.cookie.split(';');
            for (var i = 0; i < ca.length; i++) {
                var c = ca[i];
                while (c.charAt(0) == ' ') c = c.substring(1, c.length);
                if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
            }
            return null;
        }

        function eraseCookie(name) {
            createCookie(name, "", -1);
        }


        //Intro animation
        function introAnimation() {

            var logo = $("#logo");
            var logoTop = logo.position().top;
            var logoLeft = logo.position().left;
            var flashLogo = $("#flashLogo");
            var fsWrap = $("#fsWrap");
            var fsLight = $("#fsLight");
            var aniDiv = $(".aniDiv");

            $(flashLogo).css("opacity", "1");
            $(flashLogo).css("z-index", "9999");
            $(aniDiv).css("display", "block");
            $(fsWrap).css("opacity", "0.8");
            $(fsLight).css("opacity", "0");
            $(fsLight).css("display", "block");

            //use either "fade animation" or "move animation" - not both!

            //fade animation 
            $(flashLogo).delay(1500).animate({
                opacity: '0.1'
            }, 3000, function () {
                // Animation complete.
                $(flashLogo).css("z-index", "-555");
            });

//            //move animation 
//            $(flashLogo).delay(1500).animate({
//                top: logoTop,
//                left: logoLeft,
//                height: "107",
//                width: "293",
//                opacity: "0"
//            }, 2500, 'linear', function () {
//                // Animation complete.
//                $(flashLogo).css("z-index", "-555");
//            });

            //black background fade
            $(fsWrap).delay(1500).animate({
                opacity: '0.0'
            }, 2500, function () {
                // Animation complete.
                $(fsWrap).css("display", "none");
            });

            //white flash
            $(fsLight).delay(1100).animate({
                opacity: '1.0'
            }, 150, function () {
                // Animation complete.
                $(fsLight).animate({
                    opacity: '0.0'
                }, 300, function () {
                    // animation complete
                    $(fsLight).css("display", "none");
                });
            });

            $(flashLogo).click(function () {
                $(this).stop(true, true).fadeOut();
                $(fsWrap).stop(true, true).fadeOut();
                $(fsLight).stop(true, true).fadeOut();
            });

        }
    });
});
