/*
    Modified by: Jerritte Couture | U! Creative/Edge Webware
    Modification notes: This script is based upon the script written by Ian Lunn (see comments below),
                        but has been significantly modified to meet this site's needs. Thanks for the 
                        initial framework, Ian!

    JavaScript for the demo: Recreating the Nikebetterworld.com Parallax Demo
    Demo: Recreating the Nikebetterworld.com Parallax Demo
    Author: Ian Lunn
    Author URL: http://www.ianlunn.co.uk/
    Demo URL: http://www.ianlunn.co.uk/demos/recreate-nikebetterworld-parallax/
    Tutorial URL: http://www.ianlunn.co.uk/blog/code-tutorials/recreate-nikebetterworld-parallax/

    License: http://creativecommons.org/licenses/by-sa/3.0/ (Attribution Share Alike). Please attribute work to Ian Lunn simply by leaving these comments in the source code or if you'd prefer, place a link on your website to http://www.ianlunn.co.uk/.

    Dual licensed under the MIT and GPL licenses:
    http://www.opensource.org/licenses/mit-license.php
    http://www.gnu.org/licenses/gpl.html
*/

$(document).ready(function () { //when the document is ready...

    //save selectors as variables to increase performance
    var $window = $(window);
    var $body = $(document);
    var $firstBG = $('#home');

    //#history bgs
    var $secondBG = $('#history');
    var deathStar = $('#history .bg');
    var millenniumFalcon = $('#history .bg2');
    var uBuilding = $('#history .bg3');
    var lightStreaks = $('#history .bg4');

    //#bios bgs
    var $thirdBG = $('#bios');
    var bioNavHeader = $('#bios .slideshow-nav-header');
    var bioSlideshow = $('#bio-slider-section');

    //#friends bgs
    var $fourthBG = $('#friends');
    var cloudsTop = $('#friends .bg');
    var cloudsBottom = $('#friends .bg2');
    var friendsText = $('#friends .bg3');
    var tiger = $('#friends .bg4');
    var burtonMonsterBvo = $('#friends .bg5');
    var airstreamElh = $('#friends .bg6');
    var lyndaSeaworldBuschGardens = $('#friends .bg7');
    var dollywoodCassanosSeymourDuncan = $('#friends .bg8');
    var rolandHuffyStryper = $('#friends .bg9');

    //#friends bgs
    var $sixthBG = $('#social-farm');
    var socialLogo = $('#social-farm .bg');
    var socialCouple = $('#social-farm .bg2');

    //#overdrive bgs
    var $seventhBG = $('#overdrive');
    var odBackground = $('#overdrive .bg');
    var odAnimation = $('#overdrive .bg2');
    var odStripes = $('#overdrive .bg3');
    var odLogo = $('#overdrive .bg4');
    var odSmokeLeft = $('#overdrive .bg5');
    var odSmokeRight = $('#overdrive .bg6');
    var odText = $('#overdrive .bg7');

    //#d2 bgs
    var $eighthBG = $('#d2');
    var d2Water = $('#d2 .bg');
    var d2Animation = $('#d2 .bg2');
    var d2Bubbles1 = $('#d2 .bg3');
    var d2Bubbles2 = $('#d2 .bg4');
    var d2Bubbles3 = $('#d2 .bg5');

    //#tweets bgs
    var $ninthBG = $('#tweets');
    var tweetsBird = $('#tweets .bg');
    var tweetsContainer = $('#tweets section');

    //#gallery bgs
    var $tenthBG = $('#gallery');
    var galleryGrid = $('#gallery section');

    //#cause bgs
    var $eleventhBG = $('#cause');
    var causeRibbon1 = $('#cause .bg');
    var causeRibbon2 = $('#cause .bg2');
    var causeHand = $('#cause .bg3');

    //#contact bgs
    var $twelthBG = $('#contact');
    var contactLightning = $('#contact .bg');


    var windowHeight = $window.height(); //get the height of the window
    var windowWidth = $window.width(); //get the width of the window


    //apply the class "inview" to a section that is in the viewport
    $('#home, #history, #bios, #friends, #work, #social-farm, #overdrive, #d2, #tweets, #gallery, #cause, #contact').bind('inview', function (event, visible) {
        if (visible == true) {
            $(this).addClass("inview");
        } else {
            $(this).removeClass("inview");
        }
    });


    //function that places the navigation in the center of the window -- JCouture: Not needed
    //    function RepositionNav() {
    //        var windowHeight = $window.height(); //get the height of the window
    //        var navHeight = $('#nav').height() / 2;
    //        var windowCenter = (windowHeight / 2);
    //        var newtop = windowCenter - navHeight;
    //        $('#nav').css({ "top": newtop }); //set the new top position of the navigation list
    //    }


    /* Function that is called for every pixel the user scrolls. Determines the position of the BACKGROUND
    
    Arguments: 
        x               = horizontal position of background
        xType           = type of x-value for background position (i.e., "px", "%" -- added by JCouture)
        windowHeight    = height of the viewport
        pos             = position of the scrollbar
        adjuster        = adjust the position of the background
        inertia         = how fast the background moves in relation to scrolling
    */
    function moveBG(x, xType, windowHeight, pos, adjuster, inertia) {
        return x + xType + " " + (-((windowHeight + pos) - (windowHeight + adjuster)) * inertia) + "px";
    }


    /* JCouture -   Functions that are called for every pixel the user scrolls.
                    Determines the position of the ELEMENT (i.e., adjusts either the top or left css property).
    Arguments: 
        direction       = switch based on what direction to move target element
        windowHeight    = height of the viewport
        windowWidth     = width of the viewport
        pos             = position of the scrollbar
        adjuster        = adjust the position of the background
        inertia         = how fast the background moves in relation to scrolling
    */
    var setNewPosition = 0; // JCouture: declared outside of function, because it is used below

    function moveElement(direction, windowHeight, pos, adjuster, inertia) {
        switch (direction) {
            case 'moveUp':
                setNewPosition = (-((windowHeight + pos) - (windowHeight + adjuster)) * inertia);
                return (setNewPosition + "px");
                break;
            case 'moveDown':
                setNewPosition = (((windowHeight + pos) - (windowHeight + adjuster)) * inertia);
                return (setNewPosition + "px");
                break;
            case 'moveLtR':
                setNewPosition = (((windowWidth + pos) - adjuster) * inertia);
                return (setNewPosition + "px");
                break;
            case 'moveRtL':
                setNewPosition = ((-(windowWidth + pos) + adjuster) * inertia);
                return (setNewPosition + "px");
                break;
            default:
                document.write("I'm looking forward to this weekend!");
        }
    } // END moveElements()


    //function to be called whenever the window is scrolled or resized
    function Move() {
        var pos = $window.scrollTop(); //position of the scrollbar

        //if the first section is in view... -- JCouture: Nothing moving in first section
        //if ($firstBG.hasClass("inview")) {
        //call the moveBG function and change the background position
        //$firstBG.css({ 'backgroundPosition': moveBG(50, "%", windowHeight, pos, 900, 0.3) });
        //homeLogo.css({ 'backgroundPosition': moveBG(50, "%", windowHeight, pos, 1100, 0.3) });
        //}
        
        if ($secondBG.hasClass("inview")) {
            //call the moveBG function and change the background position
            $secondBG.css({ 'backgroundPosition': moveBG(100, "px", windowHeight, pos, 1, 0.2) });
            deathStar.css({ 'top': moveElement('moveUp', windowHeight, pos, 1300, 0.1) });
            millenniumFalcon.css({
                'top': moveElement('moveDown', windowHeight, pos, 585, 0.5),
                'left': moveElement('moveRtL', windowWidth, pos, (windowWidth + 2060), 0.5)
            });
            uBuilding.css({ 'top': moveElement('moveUp', windowHeight, pos, 1150, 0.2) });
            lightStreaks.css({ 'top': moveElement('moveUp', windowHeight, pos, 4500, 0.2) });
        }

        if ($thirdBG.hasClass("inview")) {
            //call the moveBG function and change the background position
            $thirdBG.css({ 'backgroundPosition': moveBG(0, "px", windowHeight, pos, 3500, -0.1) });
            bioSlideshow.css({ 'top': moveElement('moveUp', windowHeight, pos, 2700, -0.1) });
            bioNavHeader.css({ 'top': moveElement('moveUp', windowHeight, pos, 550, -0.1) });
        }

        if ($fourthBG.hasClass("inview")) {
            //call the moveBG function and change the "top" position
            $fourthBG.css({ 'backgroundPosition': moveBG(50, "%", windowHeight, pos, 2275, 0.1) });
            cloudsTop.css({
                'top': moveElement('moveDown', windowHeight, pos, 2050, 0.2),
                'left': moveElement('moveLtR', windowWidth, pos, (windowWidth + 9560), 0.03)
            });
            cloudsBottom.css({
                'top': moveElement('moveUp', windowHeight, pos, 4950, 0.1),
                'left': moveElement('moveRtL', windowWidth, pos, (windowWidth + 2400), 0.08)
            });
            friendsText.css({ 'top': moveElement('moveUp', windowHeight, pos, 12000, 0.02) });
            tiger.css({ 'top': moveElement('moveUp', windowHeight, pos, 2620, 0.8) });
            burtonMonsterBvo.css({ 'top': moveElement('moveUp', windowHeight, pos, 2350, 0.7) });
            airstreamElh.css({ 'top': moveElement('moveUp', windowHeight, pos, 2295, 1.0) });
            lyndaSeaworldBuschGardens.css({ 'top': moveElement('moveUp', windowHeight, pos, 2310, 1.3) });
            dollywoodCassanosSeymourDuncan.css({ 'top': moveElement('moveUp', windowHeight, pos, 2400, 0.4) });
            rolandHuffyStryper.css({ 'top': moveElement('moveUp', windowHeight, pos, 2480, 0.55) });
        }

        if ($sixthBG.hasClass("inview")) {
            //call the moveBG function and change the background position for CSS3 multiple backgrounds
            //$sixthBG.css({ 'backgroundPosition': moveBG(0, windowHeight, pos, 200, 0.9) + ", " + moveBG(50, windowHeight, pos, 0, 0.7) + ", " + moveBG(50, windowHeight, pos, 0, 0.5) + ", " + moveBG(50, windowHeight, pos, 700, 0.3) });
            socialLogo.css({ 'top': moveElement('moveDown', windowHeight, pos, 3650, 0.3) });
            socialCouple.css({ 'top': moveElement('moveUp', windowHeight, pos, 4630, 0.2) });
        }

        if ($seventhBG.hasClass("inview")) {
            //call the moveBG function and change the "top" position
            odBackground.css({ 'top': moveElement('moveDown', windowHeight, pos, 7000, 0.1) });
            odAnimation.css({ 'top': moveElement('moveDown', windowHeight, pos, 4570, 0.3) });
            odStripes.css({ 'top': moveElement('moveUp', windowHeight, pos, 2660, 0.2) });
            odLogo.css({ 'top': moveElement('moveUp', windowHeight, pos, 5300, 0.2) });
            odText.css({ 'top': moveElement('moveUp', windowHeight, pos, 4350, 0.2) });
            odSmokeLeft.css({ 'top': moveElement('moveUp', windowHeight, pos, 5200, 0.4) });
            odSmokeRight.css({ 'top': moveElement('moveUp', windowHeight, pos, 5050, 0.7) });
        }

        if ($eighthBG.hasClass("inview")) {
            //call the moveBG function and change the "top" position
            d2Animation.css({ 'top': moveElement('moveDown', windowHeight, pos, 4200, 0.3) });
            d2Water.css({ 'top': moveElement('moveUp', windowHeight, pos, 3950, 0.1) });
            d2Bubbles1.css({ 'top': moveElement('moveUp', windowHeight, pos, 5380, 0.7) });
            d2Bubbles2.css({ 'top': moveElement('moveUp', windowHeight, pos, 5440, 1.0) });
            d2Bubbles3.css({ 'top': moveElement('moveUp', windowHeight, pos, 5350, 0.4) });
        }

        if ($ninthBG.hasClass("inview")) {
            //call the moveBG function and change the "top" position
            tweetsBird.css({
                'top': moveElement('moveUp', windowHeight, pos, 5900, 0.3),
                'left': moveElement('moveLtR', windowWidth, pos, (windowWidth + 5600), 0.3)
            });
            tweetsContainer.css({ 'top': moveElement('moveUp', windowHeight, pos, 5950, 0.3) });
        }

        if ($tenthBG.hasClass("inview")) {
            //call the moveBG function and change the "top" position
            galleryGrid.css({ 'top': moveElement('moveUp', windowHeight, pos, 5275, 0.5) });
        }

        if ($eleventhBG.hasClass("inview")) {
            //call the moveBG function and change the "top" position
            $eleventhBG.css({ 'background-position': moveBG(50, "%", windowHeight, pos, 5500, 1.2) });
            causeRibbon1.css({ 'top': moveElement('moveDown', windowHeight, pos, 8225, 0.3) });
            causeRibbon2.css({ 'top': moveElement('moveUp', windowHeight, pos, 5475, 0.2) });
            causeHand.css({ 'top': moveElement('moveUp', windowHeight, pos, 7325, 0.5) });
        }

        if ($twelthBG.hasClass("inview")) {
            //call the moveBG function and change the "top" position

            // JCouture: Have to get new top first, so setNewPosition can have the necessary value
            var getNewTop = moveElement('moveDown', windowHeight, pos, 8460, 0.5);
            if (setNewPosition < -317) {
                contactLightning.css({
                    'top': getNewTop,
                    'left': moveElement('moveLtR', windowWidth, pos, (windowWidth + 6560), 0.3)
                });
            }
        }

        //$('#pixels').html(pos); //display the number of pixels scrolled at the bottom of the page -- JCouture: Not used
    } // END Move()


    //RepositionNav(); //Reposition the Navigation to center it in the window when the script loads -- JCouture: Not used


    $window.resize(function () { //if the user resizes the window...
        Move(); //move the background images in relation to the movement of the scrollbar
        //RepositionNav(); //reposition the navigation list so it remains vertically central -- JCouture: Not used
    });

    $window.bind('scroll', function () { //when the user is scrolling...
        Move(); //move the background images in relation to the movement of the scrollbar
    });

});
