/* Author: James Spencer

*/


var TheAgency = {};

(function($) {

  $.root = $(document);

  TheAgency = (function() {

    var Scroller = function(scroller){
          var $scroller = $(scroller),
              scrolling = false,
              visibleCells = 4,
              scrollWidth = '-' + (($scroller.children().length-visibleCells) * $scroller.children(':first').outerWidth(true)) + 'px',
              scrollAnim = {'left': null},
              scrollTimeout = null,
              startScroll = function(){
                scrolling = true;
                scrollAnim.left = $scroller.hasClass('rtl') ? scrollWidth : 0;
                $scroller.animate(scrollAnim, {'easing': 'swing', 'duration': 2500, 'complete': nextScroll});
              },
              nextScroll = function(){
                // if(scrolling) {
                  $scroller.toggleClass('rtl');
                  scrollTimeout = setTimeout(startScroll, 800);
                // }
              },
              stopScroll = function(){
                clearTimeout(scrollTimeout);
                $scroller.stop(true);
              };
          $scroller.addClass('rtl');
          $scroller
            .closest('.cell')
              .hover(startScroll, stopScroll);
          $scroller
            .find('a')
              .hover(stopScroll, startScroll);
        },
        FeatureList = function(list){
          var $list = $(list),
              $cells = $list.find('.feature'),
              openCell = function(){
                var $wrapper = $(this),
                    $cell = $wrapper.closest('.feature'),
                    anim = {
                      'left': '-'+$cell.position().left+'px',
                      'height': '388px',
                      'width': '861px'
                    };
                if($cell.hasClass('open, opening, closing')) {
                  return;
                }
                $cells.removeClass('open animating');
                $cell.addClass('animating');
                $wrapper.stop().animate(anim, {'easing': 'swing', 'duration': 300, 'complete': function(){$cell.addClass('open').removeClass('animating');}});
              },
              closeCell = function(e){
                if($(this).is('.close-feature')) {
                  e.preventDefault();
                }
                var $wrapper = $(this).is('.close-feature') ? $(this).closest('.feature-wrapper') : $(this),
                    $cell = $wrapper.closest('.feature'),
                    anim = {
                      'left': 0,
                      'height': '353px',
                      'width': '282px'
                    };
                $cell.removeClass('open').addClass('animating');
                $wrapper.stop().animate(anim, {'easing': 'swing', 'duration': 300, 'complete': function(){$cell.removeClass('animating');}});
              };

          $list
            .find('.feature-wrapper')
              .hover(openCell, closeCell)
              .find('.close-feature')
                .click(closeCell);
        },
        
        AuthorList = function(list){
          var $list = $(list),
              $profiles = $list.find('.profile'),
              $openLinks = $profiles.children('.block-link'),
              openProfile = function(e){
                e.preventDefault();
                var $link = $(this),
                    $profile = $link.closest('.profile'),
                    $profileDetail = $profile.find('.profile-detail');
                if($profile.hasClass('open') || $list.hasClass('animating')) {
                  return;
                }
                $list.addClass('animating');
                $profiles
                  .filter('.open')
                    .removeClass('open')
                    .find('.profile-detail')
                      .animate({'height': 0}, 350);
                $profile.addClass('open');
                $profileDetail.animate({'height': '350px'}, 350, function(){$list.removeClass('animating');});
              };
              
          $profiles
            .filter('.open')
              .find('.profile-detail')
                .css({'display': 'block'});
          $profiles
            .not('.open')
              .find('.profile-detail')
                .css({'display': 'block', 'height': 0});
          
          $list.css({'min-height': $list.css('height')});
          
          $openLinks.click(openProfile);
        },
        HoverDupe = function(links){
          var $links = $(links),
              $navLinks = $.root.find('#nav a'),
              getPath = function(href){
                var parts = href.split('/'),
                    path = (parts.length >= 2) ? '/'+parts[1]+'/' : '//';
                return path;
              };
              
          $links.hover(function(){
              $navLinks
                .filter('[href="'+getPath($(this).attr('href'))+'"]')
                  .addClass('hover');
            },
            function(){
              $navLinks
                .filter('[href="'+getPath($(this).attr('href'))+'"]')
                  .removeClass('hover');
            });
        },
        SidewaysScroller = function(quoteList){
          var $quoteList = $(quoteList),
              $items = $quoteList.children(),
              $current = null,
              $next = null,
              listWidth = $quoteList.width()+'px',
              loadNext = function(){
                var nextHeight = null;
                $quoteList.css({'height': $quoteList.height()+'px'});
                $items.css({'position': 'absolute', 'top': 0, 'left': 0});
                $current = $items.filter('.open');
                $next = $current.next();
                if($next.length == 0) {
                  $next = $items.filter(':first');
                }
                $next.css({'display': 'block', 'position': 'static', 'visibility': 'hidden'});
                nextHeight = $next.outerHeight(true);
                $next.css({'position': 'absolute', 'top': 0, 'left': listWidth, 'visibility': 'visible'});
                $current.css({'position': 'absolute', 'top': 0, 'left': 0});
                $quoteList.animate({'height': nextHeight+'px'}, 1890, 'swing');
                slideItems();
              },
              slideItems = function(){
                $current.animate({'left': '-'+listWidth}, 1800, 'swing');
                $next.animate({'left': 0}, 1800, 'swing', function(){
                    $current.removeClass('open');
                    $next.addClass('open');
                    setTimeout(loadNext, 5000);
                  });
              };
          
          if($items.length <= 1) {
            return;
          }
          $items.not('.open').hide();
          $quoteList.css({'overflow': 'hidden'});
          setTimeout(loadNext, 5000);
        },
        TwitterFeed = function(selector){
          $(selector)
            .twitterSearch({
              term:   'TheAgencyUK',
              applyStyles: false,
              avatar:  false, 
              anchors: false, 
              bird:    false, 
              pause:   true, 
              time:    false, 
              timeout: 5000})
            .children(':first')
              .remove();
        };
    
    return {
            'init': function(){
                //Setup scrollers
                    $.root
                      .find('.scroller > ul')
                        .each(function(){
                          new Scroller(this);
                          return true;
                        });

                //Setup featured work list
                    new FeatureList('#featured-work');

                //Setup blog author list
                    new AuthorList('#profiles');

                //Show nav hover states when cells are hovered
                    new HoverDupe('.cell.block-link');

                // testimonials on why_us
                    new SidewaysScroller('#testimonials');      

                // twitter feed
                    new TwitterFeed('#twitter .status');      
              }
            };
    
  })();

$.root.ready(TheAgency.init);

})(jQuery)

