/* Author: 

*/

$(document).ready(function () {
    // Save page elements
    var $window = $(window);
    var $home = $("#home");
    var $whoWeAre = $("#whoWeAre");
    var $howWereDifferent = $("#howWereDifferent");
    var $whatWeMake = $("#whatWeMake");
    var $contact = $("#contact");

    var windowHeight = $window.height();


    $('header nav ul').localScroll();

    $('.scroll').click(function () {
        $("header nav ul a:[href=" + $(this).attr('href') + "]").trigger('click');
        return false;
    });

    $('header nav ul li').click(function () {
        //$('header nav ul li a').removeClass('current');
        //$("a", this).addClass('current');
    });

    $("header nav ul li").hoverIntent({
        over: navToolTipShow,
        out: navToolTipHide,
        sensitivity: 10,
        interval: 2
    });

    //$("#video .wrapper").fitVids();

    var zIndexNumber = 1000;
    $('.content').each(function () {
        $(this).css('zIndex', zIndexNumber);
        zIndexNumber -= 10;
    });

    $('#home, #whoWeAre, #howWereDifferent, #whatWeMake, #contact').bind('inview', function (event, visible) {
        if (visible == true) {
            $(this).addClass("inview");
        } else {
            $(this).removeClass("inview");
        }
    });

    $("#playVideo").click(function () {
        videoOpen();
        return false;
    });

    $("#closeVideo").click(function () {
        videoClose();
        return false;
    });

    $('#contact form').ajaxForm(function () {
        var ele = $('#contact form');
        $(':input', ele).not(':button, :submit, :reset, :hidden').val('');
        $('#contact strong').fadeIn().delay(2000).fadeOut();
    });

    function _newPos(ele, pos, speed) { //OLD
        var _dist = $window.scrollTop() - $(ele).offset().top;
        return ($(window).height() / 2 - (_dist / speed) - pos) + "px";
    }


    function newPos(ele, pos, speed) {
        var dist = $(window).scrollTop() - $(ele).offset().top;
        return (-(dist / speed) + pos) + "px";
    }

    Move();

    function Move() {
        if ($home.hasClass("inview")) {
            var posX = '800px';
            if ($(window).width() >= 1600) {
                posX = "108%";
            }
            //$("#home .food").css({ 'backgroundPosition': posX + ' ' + newPos('#home', 227, 3) });

            if ($(window).width() >= 806) {
                $("#home").removeClass('textLeft');
            }
            else {
                $("#home").addClass('textLeft');
            }
            moveSetCoordinates(
                '#home .food',
                '85%',
                '800px',
                '487px',
                '216px',
                '100px',
                newPos('#home', 227, 3),
                newPos('#home', 492, 3),
                newPos('#home', 515, 3),
                $(window).width(),
                '/images/mainHome.png',
                '/images/Tablet_mainHome.png',
                '/images/mainHome_Mobile.png'
            );


            setNavFromManualScroll('#home');
        }
        if ($whoWeAre.hasClass("inview")) {
            var posX = '-370px';
            if ($(window).width() <= 1500) {
                posX = "-700px";
            }
            if ($(window).width() >= 806) {
                $("#whoWeAre").removeClass('textLeft');
            }
            else {
                $("#whoWeAre").addClass('textLeft');
            }

            $("#whoWeAre .food").css({ 'backgroundPosition': posX + ' ' + newPos('#whoWeAre', 346, 3) });

            setNavFromManualScroll('#whoWeAre');
        }


        if ($howWereDifferent.hasClass("inview")) {
            var posX = '754px';
            if ($(window).width() >= 1600) {
                posX = "108%";
            }
            //$("#howWereDifferent .food").css({ 'backgroundPosition': posX + ' ' + newPos('#howWereDifferent', 256, 3) });
            if ($(window).width() >= 806) {
                $("#howWereDifferent").removeClass('textLeft');
            }
            else {
                $("#howWereDifferent").addClass('textLeft');
            }

            moveSetCoordinates(
                '#howWereDifferent .food',
                '85%',
                '754px',
                '487px',
                '275px',
                '100px',
                newPos('#howWereDifferent', 256, 3),
                newPos('#howWereDifferent', 256, 3),
                newPos('#howWereDifferent', 256, 3),
                $(window).width(),
                '/images/mainHowWereDifferent.png',
                '/images/mainHowWereDifferent.png',
                '/images/mainHowWereDifferent.png'
            );

            setNavFromManualScroll('#howWereDifferent');
        }


        if ($whatWeMake.hasClass("inview")) {
            var posX = '200px';
            if ($(window).width() <= 1500) {
                posX = "-100px";
            }

            if ($(window).width() >= 806) {
                $("#whatWeMake").removeClass('textLeft');
            }
            else {
                $("#whatWeMake").addClass('textLeft');
            }

            $("#whatWeMake .food").css({ 'backgroundPosition': posX + ' ' + newPos('#whatWeMake', 19, 3) });

            setNavFromManualScroll('#whatWeMake');
        }


        if ($contact.hasClass("inview")) {
            var posX = '687px';
            if ($(window).width() >= 1600) {
                posX = "167%";
            }
            //$("#contact .food").css({ 'backgroundPosition': posX + ' ' + newPos('#contact', 263, 3) });

            if ($(window).width() >= 806) {
                $("#contact").removeClass('textLeft');
            }
            else {
                $("#contact").addClass('textLeft');
            }

            moveSetCoordinates(
                '#contact .food',
                '167%',
                '687px',
                '487px',
                '275px',
                '100px',
                newPos('#contact', 256, 3),
                newPos('#contact', 256, 3),
                newPos('#contact', 256, 3),
                $(window).width(),
                '/images/mainContact.png',
                '/images/mainContact.png',
                '/images/mainContact.png'
            );

            setNavFromManualScroll('#contact');
        }
    }

    $window.resize(function () {
        Move();
        //RepositionNav();
    });

    $window.bind('scroll', function () {
        Move();
    });

});


function moveSetCoordinates(ele, x1, x2, x3, x4, x5, y, y2, y3, browserWidth, bgImage, bgImage2, bgImage3) {
    switch (true) {
        case (browserWidth >= 1600):
            //$('#home h1').html("Greater than 1600");
            $(ele).css({ 'backgroundPosition': x1 + ' ' + y });
            $(ele).css({ 'background-image': "url('" + bgImage + "')" });
            break;
        case (browserWidth <= 1599 && browserWidth >= 1281):
            //$('#home h1').html("Between 1280 and 1599");
            $(ele).css({ 'backgroundPosition': x2 + ' ' + y });
            $(ele).css({ 'background-image': "url('" + bgImage + "')" });
            break;
        case (browserWidth <= 1280 && browserWidth >= 816):
            //$('#home h1').html("Between 1280 and 769");
            $(ele).css({ 'backgroundPosition': x3 + ' ' + y });
            $(ele).css({ 'background-image': "url('" + bgImage + "')" });
            break;
        case (browserWidth <= 815 && browserWidth >= 481):
            //$('#home h1').html("Between 768 and 481");
            $(ele).css({ 'backgroundPosition': x4 + ' ' + y2 });
            $(ele).css({ 'background-image': "url('"+bgImage2+"')" });
            //alert(bgImage);
            break;
        default:
            //$('#home h1').html(browserWidth);
            $(ele).css({ 'backgroundPosition': x5 + ' ' + y3 });
            $(ele).css({ 'background-image': "url('" + bgImage3 + "')" });
    }
}

function setNavFromManualScroll(id) {
    $('header nav ul li a').removeClass('current');
    $('header nav ul li a[href$="'+id+'"]').addClass('current');
}

function navToolTipShow() {
    var ele = $('h1', this);
    $(ele).animate({ opacity: 0 },0);
    $(ele).show();
    $(ele).animate({
        left: '-=11',
        opacity: 1,
        queue: false
    }, 200);
}
function navToolTipHide() {
    $("header nav ul li h1").fadeOut(200);
    var ele = $('h1', this);
    $(ele).animate({
        left: '+=11',
        opacity: 0,
        queue: false
    }, 200);
    //$(ele).hide();
}

function videoOpen() {
    $('html, body').animate({ scrollTop: 0 }, 'slow');
    $('header nav').fadeOut('fast');

    var videoWidth = 853;
    var videoHeight = 480;
    if ($(window).width() < 815) { $("#video .wrapper").addClass('on'); }
    else {
        $("#video .wrapper").removeClass('on');
    }

    $("#video .wrapper").prepend('<iframe id="player" width="' + videoWidth + '" height="' + videoHeight + '" src="http://www.youtube.com/embed/TQHv0qQpX1Y?rel=0&autohide=1&wmode=transparent&enablejsapi=1&autoplay=1" frameborder="0" allowfullscreen></iframe>');
    $('#video').css('height', $(window).height());
    $('#video').fadeIn();
    $('html').css('overflow', 'hidden');
}

function videoClose() {

    $('header nav').fadeIn('fast');
    $('#video').fadeOut('fast');
    $('#video iframe').remove();
    $('html').css('overflow-y', 'scroll');

}


