jQuery(document).ready( function($) {
  var map_timeout = null;
  var rotate_maps = function( $element, delay ) {
    var transition = 1000;
    
    var $next = $element.next();
    if ( $next.length < 1 ) {
      $next = $element.siblings(':first-child');
    }
    if ( $next.length > 0 ) {
      $element.find('img').fadeOut(transition);
      $element.removeClass('active');
      $next.find('img').fadeIn(transition, function() {
        $next.addClass('active');
        map_timeout = setTimeout( function(){
          rotate_maps($next, delay);
        }, delay );
      });
    }
  }
  if ( $('.rotating-maps li').length > 1 ) {
    map_timeout = setTimeout( function() { 
      map_timeout = rotate_maps($('.rotating-maps li:first-child'),8000);
    }, 8000);
  }
  $('.rotating-maps li').click( function(event) {
    if ( event.target != this ) {
      return;
    }
    if ( map_timeout != null ) {
      clearTimeout(map_timeout);
    }
    if ( !$(this).hasClass('active') ) {
      $(this).siblings().removeClass('active').find('img').fadeOut(500).end().end()
            .find('img').fadeIn(500).end()
            .addClass('active');
    }
  });
  
  
  var banner_timeout = null;
  var rotate_banners = function ( $element, delay ) {
    var transition = 1000;
    
    $('#banner_controls .play').addClass('playing');
    
    var $next = $element.next();
    if ( $next.length < 1 ) {
      $next = $element.siblings(':first-child');
    }
    if ( $next.length > 0 ) {
      $element.fadeOut(transition);
      $element.removeClass('active');
      $next.fadeIn(transition, function() {
        $next.addClass('active');
        $('#banner_controls ul li').removeClass('active')
              .eq($next.index()).addClass('active');
        banner_timeout = setTimeout( function(){
          rotate_banners($next, delay);
        }, delay );
      });
    }
  }
  
  $('#banners').append('<div id="banner_controls"><a href="#" class="play">Play</a><ul></ul></div>');
  $('#banners .banner').each( function() {
    $('#banner_controls ul').append('<li><a href="#"></a></li>');
  });
  $('#banner_controls ul li:first-child').addClass('active');
  
  $('#banners .banner:not(:first-child)').hide();
  
  if ( $('#banners .banner').length > 1 ) {
    $('#banner_controls .play').addClass('playing');
    banner_timeout = setTimeout( function() { 
      banner_timeout = rotate_banners($('#banners .banner:first-child'),8000);
    }, 8000);
  }
  
  $('#banner_controls .play').click( function() {
    if ( $(this).hasClass('playing') ) {
      $(this).removeClass('playing');
      $('#banners .banner').stop(true, true);
      if ( banner_timeout != null ) {
        clearTimeout(banner_timeout);
      }
    } else {
      if ( $('#banners .banner').length > 1 ) {
        $('#banner_controls .play').addClass('playing');
        banner_timeout = rotate_banners($('#banners .banner:visible'),8000);
      }
    }
    return false;
  });
  
  $('#banner_controls ul a').click( function() {
    $play = $('#banner_controls .play');
    if ( $play.hasClass('playing') ) {
      $play.removeClass('playing');
      $('#banners .banner').stop(true, true);
      if ( banner_timeout != null ) {
        clearTimeout(banner_timeout);
      }
    }
    $('#banner_controls ul li').removeClass('active');
    $('#banners .banner').hide()
          .eq($(this).parent().index()).show().addClass('active');
    $(this).parent().addClass('active');
    return false;
  });
});