$(document).ready(function () {

    //CONTACT
    if ($('#contact').length) {
        $(".location-buttons li,.right_col li").css('cursor', 'pointer');
        $(".location-buttons li,.right_col li").bind('click', function (event) {
            event.preventDefault();
            $(".location-buttons a").removeClass('active');
            var myVal = $(this).children('a').attr('href');
            $('#hq-info,#daytona-info,#charlotte-info,#uk-info,#asia-info,.location-name #hq,.location-name #daytona,.location-name #charlotte,.location-name #uk,.location-name #asia').hide();
            switch (myVal) {
                case 'hq':
                    $('#hq-info,.location-name #hq').show();
                    $(".location-buttons a[href='hq']").addClass('active');
                    break;
                case 'daytona':
                    $('#daytona-info,.location-name #daytona').show();
                    $(".location-buttons a[href='daytona']").addClass('active');
                    break;
                case 'charlotte':
                    $('#charlotte-info,.location-name #charlotte').show();
                    $(".location-buttons a[href='charlotte']").addClass('active');
                    break;
                case 'uk':
                    $('#uk-info,.location-name #uk').show();
                    $(".location-buttons a[href='uk']").addClass('active');
                    break;
                case 'asia':
                    $('#asia-info,.location-name #asia').show();
                    $(".location-buttons a[href='asia']").addClass('active');
                    break;
                default:
                    $('#hq-info,.location-name #hq').show();
                    $(".location-buttons a[href='hq']").addClass('active');
                    break;
            };
        });
    };
    //END

    //WHO WE ARE->KEY PEOPLE
    if ($('#board-of-directors').length) {

        function bod_itemFirstInCallback(carousel, item, idx, state) {
            //$("#board-of-directors li").bind('click', function () {
            //    $("#bod-info-boxes #info-box" + $(this).attr('rel')).fadeIn();
            //});
        }
        function bod_initCallback(carousel) {
          //  $("#board-of-directors li").css('cursor', 'pointer');
        }
        $('#board-of-directors').jcarousel({
            scroll: 4,
            initCallback: bod_initCallback,
            itemFirstInCallback: bod_itemFirstInCallback
        });
    }
    if ($('#global-advisory-board').length) {
        function gab_itemFirstInCallback(carousel, item, idx, state) {
            $("#global-advisory-board li").bind('click', function () {
                $("#gab-info-boxes #info-box" + $(this).attr('rel')).fadeIn();
            });
        }
        function gab_initCallback(carousel) {
            $("#global-advisory-board li").css('cursor', 'pointer');
        }
        $('#global-advisory-board').jcarousel({
            scroll: 4,
            initCallback: gab_initCallback,
            itemFirstInCallback: gab_itemFirstInCallback
        });
    }
    if ($('#leadership-team').length) {
        function lt_itemFirstInCallback(carousel, item, idx, state) {
            $("#leadership-team li").bind('click', function () {
                $("#lt-info-boxes #info-box" + $(this).attr('rel')).fadeIn();
            });
        }
        function lt_initCallback(carousel) {
            $("#leadership-team li").css('cursor', 'pointer');
        }
        $('#leadership-team').jcarousel({
            scroll: 4,
            initCallback: lt_initCallback,
            itemFirstInCallback: lt_itemFirstInCallback
        });
    }
    if ($('#management-team').length) {
        function mt_itemFirstInCallback(carousel, item, idx, state) {
            $("#management-team li").bind('click', function () {
                $("#mt-info-boxes #info-box" + $(this).attr('rel')).fadeIn();
            });
        }
        function mt_initCallback(carousel) {
            $("#management-team li").css('cursor', 'pointer');
            $("#loc_key-people .title a").bind('click', function (event) {
                event.preventDefault();
                $(".headshot-info-box").hide();
                $("#loc_key-people .title a").removeClass('active');
                var myVal = $(this).attr('href');
                $('#board-of-directors-holder,#global-advisory-board-holder,#leadership-team-holder,#management-team-holder').hide();

                switch (myVal) {
                    case 'gab':
                        $('#global-advisory-board-holder').show();
                        $("#loc_key-people .title a[href='gab']").addClass('active');
                        break;
                    case 'bod':
                        $('#board-of-directors-holder').show();
                        $("#loc_key-people .title a[href='bod']").addClass('active');
                        break;
                    case 'lt':
                        $('#leadership-team-holder').show();
                        $("#loc_key-people .title a[href='lt']").addClass('active');
                        break;
                    case 'mt':
                        $('#management-team-holder').show();
                        $("#loc_key-people .title a[href='mt']").addClass('active');
                        break;
                    default:
                        $('#global-advisory-board-holder').show();
                        $("#loc_key-people .title a[href='gab']").addClass('active');
                        break;
                };
            });
        }
        $('#management-team').jcarousel({
            scroll: 4,
            initCallback: mt_initCallback,
            itemFirstInCallback: mt_itemFirstInCallback
        });
    }
    $(".headshot-info-box .close").click(function (event) {
        $(this).parent().fadeOut();
    });
    //END



    //WHO WE ARE->OURSTORY->TIMELINE
    if ($('#timeline').length) {
        $(function () {
            $().timelinr({ issuesTransparency: 1 });
        });
    }
    //END

    //WHAT WE DO->CAPABILITIES
    if ($('#capabilities-carousel').length) {
		function capabilities_initCallback(carousel) {
			$('#capabilities-matrix td').bind('click', function () {
				$(this).parent().parent().parent().parent().siblings('.overlay').show();
				$('#capabilities-matrix').hide();
				$('#capabilities-list').show();
				carousel.scroll($.jcarousel.intval($(this).attr('rel')),false);
                return false;
			});
			$('#capabilities-list .back').bind('click', function () {
				$(this).parent().parent().parent().siblings('.overlay').hide();
				$('#capabilities-matrix').show();
				$('#capabilities-list').hide();
			});
		}
        $('#capabilities-carousel').jcarousel({
            scroll: 1,
			initCallback: capabilities_initCallback
        });
    }
    //END

    //GLOBAL FUNCTIONALITY FOR BUTTONS THAT LAUNCH POP UPS
    if ($('.pop-nav').length) {
        $('.pop-nav a').click(function (event) {
            event.preventDefault();
        });
    }
    //END

    //WHAT WE DO->SERIES OVERVIEW
    if ($('.jcarousel-skin-jmi-series-overview').length) {
        var current_item = 1;


        /**
        * We use the initCallback callback
        * to assign functionality to the controls
        */
        function north_amer_initCallback(carousel) {
            $('#na-nav a').bind('click', function () {
                carousel.scroll($.jcarousel.intval($(this).attr('href')));
                return false;
            });

            $('#na-scroller .next-horizontal').bind('click', function () {
                carousel.next();
                return false;
            });

            $('#na-scroller .prev-horizontal').bind('click', function () {
                carousel.prev();
                return false;
            });
            $('#loc-nav #intl a').click(function (event) {
                event.preventDefault();
                $('#series-overview-na-carousel').hide();
                $('#series-overview-intl-carousel').show();
                $('#na-nav').hide();
                $('#intl-nav').show();
                $('#na-scroller').hide();
                $('#intl-scroller').show();
                $('#na-logos').hide();
                $('#intl-logos').show();
                $(this).addClass('active');
                $('#loc-nav #na a').removeClass('active');
            });

            $('#loc-nav #na a').click(function (event) {
                event.preventDefault();
                $('#series-overview-intl-carousel').hide();
                $('#series-overview-na-carousel').show();
                $('#intl-nav').hide();
                $('#na-nav').show();
                $('#intl-scroller').hide();
                $('#na-scroller').show();
                $('#intl-logos').hide();
                $('#na-logos').show();
                $(this).addClass('active');
                $('#loc-nav #intl a').removeClass('active');
            });
        };
        function intl_initCallback(carousel) {
            $('#intl-nav a').bind('click', function () {
                carousel.scroll($.jcarousel.intval($(this).attr('href')));
                return false;
            });

            $('#intl-scroller .next-horizontal').bind('click', function () {
                carousel.next();
                return false;
            });

            $('#intl-scroller .prev-horizontal').bind('click', function () {
                carousel.prev();
                return false;
            });
        };
        /**
        * This is the callback function which receives notification
        * when an item becomes the first one in the visible range.
        */
        function north_amer_itemFirstInCallback(carousel, item, idx, state) {
            current_item = idx;
            $('#na-nav a').removeClass('active');
            $('#na-nav a[href=' + current_item + ']').addClass('active');
            $('#na-logos .logo-card').hide();
            $('#na-logos #na-logo' + current_item).show();
            $('#na-headers div').hide();
            $('#na-headers #na-header' + current_item).show();
        };
        function intl_itemFirstInCallback(carousel, item, idx, state) {
            current_item = idx;
            $('#intl-nav a').removeClass('active');
            $('#intl-nav a[href=' + current_item + ']').addClass('active');
            $('#intl-logos .logo-card').hide();
            $('#intl-logos #intl-logo' + current_item).show();
            $('#intl-headers div').hide();
            $('#intl-headers #intl-header' + current_item).show();
        };

        $('#series-overview-na-carousel').jcarousel({
            scroll: 1,
            itemFirstInCallback: north_amer_itemFirstInCallback,
            initCallback: north_amer_initCallback,
            // This tells jCarousel NOT to autobuild prev/next buttons
            buttonNextHTML: null,
            buttonPrevHTML: null,
            wrap: 'both'
        });

        $('#series-overview-intl-carousel').jcarousel({
            scroll: 1,
            itemFirstInCallback: intl_itemFirstInCallback,
            initCallback: intl_initCallback,
            // This tells jCarousel NOT to autobuild prev/next buttons
            buttonNextHTML: null,
            buttonPrevHTML: null,
            wrap: 'both'
        });
    };

    $('.btn-schedule').click(function (event) {
        myURL = $(this).attr('href');
        $(".schedule-overlay .schedule").load("schedules/" + myURL, function () {
            $(".schedule-overlay .schedule ul").before('<div class="close"></div>');
            $(".schedule-overlay .schedule ul li:last").after('<div class="clearer"></div>');
            $(".schedule-overlay .schedule .close").click(function () {
                $(this).parent().parent().fadeOut();
            });
            $('.schedule-overlay').fadeIn();
        });
    });

    $('.btn-video').click(function (event) {
        $('.video-overlay').fadeIn();
        $('iframe').each(function () {
            try {
                $(this).get(0).api('unload');
                $(this).get(0).api('seekTo', 0);
                $(this).get(0).api('play', null);
            } catch (err) { }
        });
        $(".video-overlay .video .close").click(function () {
            $(this).parent().parent().fadeOut();
            $('iframe').each(function () {
                try {
                    $(this).get(0).api('pause', null);
                } catch (err) { }
            });
        });
    });
    //END


    //TAB-BASED NAVIGATION
    $("#tab").click(function (event) {
        $(this).parent().parent().toggleClass("shown");
    });
    $("#confidentiality").click(function (event) {
        event.preventDefault();
        $('.confidentiality-overlay').fadeIn();
        window.scrollTo(0, 0);
        $(".confidentiality-overlay .confidentiality .close").click(function () {
            $(this).parent().parent().fadeOut();
        });
        $('.confidentiality-overlay').click(function () {
            $(this).fadeOut();
        });
    });
    $("#terms").click(function (event) {
        event.preventDefault();
        $('.terms-overlay').fadeIn();
        window.scrollTo(0, 0);
        $(".terms-overlay .terms .close").click(function () {
            $(this).parent().parent().fadeOut();
        });
        $('.terms-overlay').click(function () {
            $(this).fadeOut();
        });
    });
    //END

    //GLOBAL INITIALIZATION FOR VIMEO-HOSTED CONTENT
    if ($('.video-overlay').length) {
        Froogaloop.init();
    };
    //END

    //HASH
    $("#header ul#nav li a").each(function () {
        //MODIFYING THE URL FOR DEVELOPMENT PURPOSES
        //THIS MAY BE REMOVED OR CHANGED FOR PRODUCTION
        var path = window.location.pathname
        var modifiedPath = path.substring(path.lastIndexOf("/") + 1);
        //END DEVELOPMENT URL MODIFICATION
        if ($(this).attr("href") == modifiedPath) {
            $(this).parent().addClass("current");
        }
    });
    if (window.location.hash) {
        $("#header ul#nav li.current ul li a").each(function () {
            if ($(this).attr("href") == window.location.hash) {
                $(this).parent().addClass("current").siblings(".current").removeClass("current");
            }
        });
        var modifiedHash = window.location.hash.replace("#", "#loc_");
        var destination = $(modifiedHash).offset().top - 80;
        $("html:not(:animated),body:not(:animated)").scrollTop(destination);
    }
    //END HASH



    //INITIALIZE A NEW PAGE
    $("#header ul#nav li.current span ul li:first-child").addClass("current");
});

$(window).load(function () {
    //SCROLLING
    var $sections = $('.section');  // all content sections
    var $navs = $('#nav > li.current > span > ul > li');  // all nav sections

    var topsArray = $sections.map(function () {
        return $(this).position().top - 300;  // make array of the tops of content
    }).get();                                 //   sections, with some padding to
    //   change the class a little sooner
    var len = topsArray.length;  // quantity of total sections
    var currentIndex = 0;        // current section selected

    var getCurrent = function (top) {   // take the current top position, and see which
        for (var i = 0; i < len; i++) {   // index should be displayed
            if (top >= topsArray[i] && topsArray[i + 1] && top < topsArray[i + 1]) {
                return i;
            } else if (!topsArray[i + 1]) {
                return len - 1;
            }
        }
    };

    // on scroll,  call the getCurrent() function above, and see if we are in the
    //    current displayed section. If not, add the "selected" class to the
    //    current nav, and remove it from the previous "selected" nav
    $(document).scroll(function (e) {
        var scrollTop = $(this).scrollTop();
        var checkIndex = getCurrent(scrollTop);
        if (checkIndex !== currentIndex) {
            currentIndex = checkIndex;
            $navs.eq(currentIndex).addClass("current").siblings(".current").removeClass("current");
        }
    });
    //END SCROLLING
});
