﻿//****  All of our custom js/jquery goes here  ****//
//########## Resize Banner Images ##########//
var bannerHeights = $('#banners img').height();
var bannerWidths = $('#banners img').width();
//always add margin top to banner heights
var marginTop = 40;
$('#homeText').css({
    'position': 'absolute',
    'top': bannerHeights - 220 + 'px',
    'left': '0px'
});
$('#homeCarousel').css({
    'position': 'absolute',
    'top': bannerHeights - 220 + 'px',
    'left': '680px'
});
$('.home').height(bannerHeights + marginTop);
$('#homePage').height(bannerHeights + marginTop);
//set originals
var ogWinHeight = $(window).height();
var ogWinWidth = $(window).width();

$(window).resize(function () {
    var currWinHeight = $(window).height();
    var currWinWidth = $(window).width();
    var newBannerHeights = $('#banners img').height();
    //if window height is bigger
    if (newBannerHeights > bannerHeights) {
        $('.home').height(newBannerHeights + marginTop);
        $('#homePage').height(newBannerHeights + marginTop);

        //resize the spacing of the text too
        $('#homeText').css({
            'position': 'absolute',
            'top': newBannerHeights - 220 + 'px',
            'left': '0px'
        });
        $('#homeCarousel').css({
            'position': 'absolute',
            'top': newBannerHeights - 220 + 'px',
            'left': '680px'
        });
    }
    if (newBannerHeights < bannerHeights) {
        $('.home').height(newBannerHeights + marginTop);
        $('#homePage').height(newBannerHeights + marginTop);

        //resize the spacing of the text too
        $('#homeText').css({
            'position': 'absolute',
            'top': newBannerHeights - 220 + 'px',
            'left': '0px'
        });
        $('#homeCarousel').css({
            'position': 'absolute',
            'top': newBannerHeights - 220 + 'px',
            'left': '680px'
        });
    }
});
//tool tip functions
function over() {
    //$(this).find('.first').animate({ 'border-color': '#406792' }, { queue: false, duration: 500 });
    var toolTip = $(this).find('.toolTip');
    $(toolTip).css({
        'left': '-30px',
        'top': '-125px'
    });
}
function out() {
    //$(this).find('.first').animate({ 'opacity': '1.0' }, { queue: false, duration: 500 });
    $(this).find('.toolTip').css({ 'top': '-9999em' });
}

//end tooltips


$(document).ready(function () {
    /*############################ ---------------- Sitewide behavior ---------------- ############################*/
    //preload images
    if (document.images) {
        img0 = new Image();
        img0.src = "/images/banner1.jpg";
        img1 = new Image();
        img1.src = "/images/banner2.jpg";
        img2 = new Image();
        img2.src = "/images/banner3.jpg";
        img3 = new Image();
        img3.src = "/images/nav_bg.gif";
    }

    /*############################ ---------------- Homepage behavior ---------------- ############################*/
    //########## Resize Banner Images ##########//
    var bannerHeights = $('#banners img').height();
    var bannerWidths = $('#banners img').width();
    //always add margin top to banner heights
    var marginTop = 40;

    $('#homeText').css({
        'position': 'absolute',
        'top': bannerHeights - 220 + 'px',
        'left': '0px'
    });
    $('#homeCarousel').css({
        'position': 'absolute',
        'top': bannerHeights - 220 + 'px',
        'left': '680px'
    });

    $('.home').height(bannerHeights + marginTop);
    $('#homePage').height(bannerHeights + marginTop);




    //########## Header Superfish Nav ##########//
    $(".headerNav").superfish({
        delay: 300,                                             // one second delay on mouseout 
        animation: { opacity: 'show', height: 'show' },         // fade-in and slide-down animation 
        speed: 'fast'                                           // faster animation speed 
    });

    //########## Animate Banner Images On Page Load ##########//
    //we need the first image to have a class of active
    $('#banners img:first').addClass('active');
    var active = $('#banners img.active');
    $(active).css({ opacity: 0.0 }).animate({ opacity: 1.0 }, 1400, function () {
    });
    active = active.next();
    $(active).stop().delay(3000).css({ opacity: 0.0 }).animate({ opacity: 1.0 }, 1400, function () {
        //$(this).animate({ opacity: 0.0 }, 100, function () { });
    });
    active = active.next();
    $(active).stop().delay(6000).css({ opacity: 0.0 }).animate({ opacity: 1.0 }, 1400, function () {
        $('#carousel').roundabout("startAutoplay");
    });


    $('#homeText').fadeInOnLoad(6000, 1400);
    $('#homeCarousel').fadeInOnLoad(6000, 1400);

    //########## Carousel ##########//
    $('#carousel').removeClass('carouselRemove');

    if ($('#carousel').length > 0) {
        $('#carousel').roundabout({
            btnNext: "#next",
            btnPrev: "#previous",
            minOpacity: 0.1,
            minScale: 0.0,
            maxScale: 1.0,
            duration: 350,
            autoplay: false,
            autoplayDuration: 3000,
            autoplayPauseOnHover: true
        });

        $('.carouselButtons').hover(
        function () {
            $('#carousel').roundabout("stopAutoplay")
        }, function () {
            $('#carousel').roundabout("startAutoplay")
        });
    }


    /*############################ ---------------- Team Page behavior ---------------- ############################*/
    //banner animations//
    var teamActive = $('#wespeak img:first');
    $(teamActive).delay(1000).css({ opacity: 0.0 }).animate({ opacity: 1.0 }, 1400, function () {
        $(this).delay(2000).animate({ opacity: 0.0 }, 300, function () { });
    });
    teamActive = teamActive.next();
    $(teamActive).stop().delay(5000).css({ opacity: 0.0 }).animate({ opacity: 1.0 }, 1400, function () {
        $(this).delay(2000).animate({ opacity: 0.0 }, 300, function () { });
    });
    teamActive = teamActive.next();
    $(teamActive).stop().delay(9000).css({ opacity: 0.0 }).animate({ opacity: 1.0 }, 1400, function () {
        $(this).delay(2000).animate({ opacity: 0.0 }, 300, function () { });
    });
    teamActive = teamActive.next();
    $(teamActive).stop().delay(13000).css({ opacity: 0.0 }).animate({ opacity: 1.0 }, 1400, function () {
        $(this).delay(2000).animate({ opacity: 0.0 }, 300, function () { });
    });
    teamActive = teamActive.next();
    $(teamActive).stop().delay(17000).css({ opacity: 0.0 }).animate({ opacity: 1.0 }, 1400, function () {
        $(this).delay(2000).animate({ opacity: 0.0 }, 300, function () { });
    });
    teamActive = teamActive.next();
    $(teamActive).stop().delay(21000).css({ opacity: 0.0 }).animate({ opacity: 1.0 }, 1400, function () {
        $(this).delay(2000).animate({ opacity: 0.0 }, 300, function () { });
    });
    teamActive = teamActive.next();
    $(teamActive).stop().delay(25000).css({ opacity: 0.0 }).animate({ opacity: 1.0 }, 1400, function () {
        $(this).delay(2000).animate({ opacity: 0.0 }, 300, function () { });
    });
    teamActive = teamActive.next();
    $(teamActive).stop().delay(29000).css({ opacity: 0.0 }).animate({ opacity: 1.0 }, 1400, function () {
        $(this).delay(2000).animate({ opacity: 0.0 }, 300, function () { });
    });
    teamActive = teamActive.next();
    $(teamActive).stop().delay(33000).css({ opacity: 0.0 }).animate({ opacity: 1.0 }, 1400, function () {
        $(this).delay(2000).animate({ opacity: 0.0 }, 300, function () { });
    });
    teamActive = teamActive.next();
    $(teamActive).stop().delay(37000).css({ opacity: 0.0 }).animate({ opacity: 1.0 }, 1400, function () {
        $(this).delay(2000).animate({ opacity: 0.0 }, 300, function () { });
    });
    teamActive = teamActive.next();
    $(teamActive).stop().delay(41000).css({ opacity: 0.0 }).animate({ opacity: 1.0 }, 1400, function () {
    });
    teamActive = teamActive.next();
    $(teamActive).stop().delay(43000).css({ opacity: 0.0 }).animate({ opacity: 1.0 }, 1400, function () {
    });



    //*tooltip and thumb hover animations*//
    $('.bioThumb').hoverIntent({
        sensitivity: 0,
        interval: 820,
        over: over,
        timeout: 100,
        out: out
    });
    //*set the size of the UL so the border will stretch depening on how many thumbs there are*//
    var bioThumbWidth = $('.bioThumb').width();
    var bioThumbHeight = $('#bioThumbs').height();
    var thumbCount = $('.bioThumb').length;
    var ogListWidth = $('#bioThumbs').width();
    var newListWidth = ((bioThumbWidth * thumbCount) + thumbCount);
    //add thumb count to final width because of extra border pixels
    if (newListWidth < ogListWidth) {
        $('#bioThumbs').width(newListWidth);
    }
    //take off 15px from height due to clear fix div at the bottom
    $('#bioThumbs').height(bioThumbHeight - 15);

    /*############################  ---------------- Services Page behavior ---------------- ############################*/
    $('.approach:last').css({
        "border-bottom": "none"
    });

    var clientWidth = $('.clientsList').find('.client').width();
    var numClients = $('.clientsList').find('.client').length;

    if (numClients <= 3) {
        var newClientsListWidth = (clientWidth * numClients) + numClients;
        $('.clientsList').width(newClientsListWidth);
    }
});

//because webkit browser can't find image height until page loads
$(window).load(function () {
    if ($.browser.webkit) {
        var bannerHeights = $('#banners img').height();
        var bannerWidths = $('#banners img').width();
        //always add margin top to banner heights
        var marginTop = 40;

        $('#homeText').css({
            'position': 'absolute',
            'top': bannerHeights - 190 + 'px',
            'left': '0px'
        });
        $('#homeCarousel').css({
            'position': 'absolute',
            'top': bannerHeights - 190 + 'px',
            'left': '680px'
        });

        $('.home').height(bannerHeights + marginTop);
        $('#homePage').height(bannerHeights + marginTop);
    }
});




//######################## Custom Functions ########################//

//function to fade in an element
//
$.fn.fadeInOnLoad = function (delay, speed) {
    this.stop().delay(delay).css({ opacity: 0.0 }).animate({ opacity: 1.0 }, speed, function () {
        //animation complete
    });
}


//function to fade in an element
//
function slideSwitch() {
    var $active = $('#wespeak img.active');
    var $next = $active.next().length ? $active.next()
        : $('#slideshow img:first');

    $next.css({ opacity: 0.0 })
        .addClass('active')
        .animate({ opacity: 1.0 }, 1000, function () {
            $active.removeClass('active last-active');
        });
}
