﻿$(function(){
           
    /* ------------- CAROUSEL ------------ */

    // set up scrollable
    var scrollableOptions = {
        clickable: true,
        speed: 750,       // time between slide animation
        circular: true                
    }
    
    var autoScrollOptions = {
        autoplay: true,  
        interval: 10000,   // time between slides
        autopause: true
    }

    var updateMaskLink = function( currentItem ) {             
        var currentItemLink = currentItem.find("a").attr("href");        
        $(".homepageHeroMask").attr("href", isNull ( currentItemLink ) ? "#" : currentItemLink);
    }

    // get actual no of items
    var carouselItems = $(".homepageHero li");

    // if there's more than one item turn it in to a carousel
    if (carouselItems.length > 1) {
        
        // get a reference to the api
        var api = $(".homepageHero")
                    .scrollable(scrollableOptions)
                    .autoscroll(autoScrollOptions)
                    .data("scrollable");
        
        // when it's scrolled
        api.onSeek(function() {
            updateMaskLink ( this.getItems().eq( this.getIndex() ) );            
        });

    }

    updateMaskLink ( $(".homepageHero ul li:eq(0)") );
    
    /* ------------- CLIENT QUOTES ------------ */

    function fadeClientQuotesInAndOut() {
    
         var active = $('.allQuotes a.active');
         if ( active.length == 0 ) active = $('.allQuotes a:last');

         var next = active.next().length ? active.next() : $('.allQuotes a:first');

         active.addClass('last-active');
         next.css({opacity: 0.0})
             .addClass('active')
             .animate({opacity: 1.0}, 1000, function() {
                 active.removeClass('active last-active');
         });
     } 
     
     var fadeClientQuotes = window.setInterval( fadeClientQuotesInAndOut , 5000 );
        
});
