YUI({
  fetchCSS : false
}).use('gallery-carousel', 'gallery-carousel-anim', 'gallery-carousel-opacity', 'substitute', 'tabview', 'overlay',  'gallery-overlay-modal', function (Y) {


  Y.on('domready', function(){

      /*
       * Opens external links (with rel="external") in a new window.
       */
      Y.delegate('click', function(e){

        e.preventDefault();
        window.open(e.currentTarget.get('href'));

      }, 'body', 'a[rel="external"]');


      if (Y.one('#tabs')) {

        var tabview = new Y.TabView({
          srcNode: '#tabs'
        });

        tabview.render();

        Y.all('.faqs .faq').addClass('ui-state-closed');

        Y.delegate('click', function(e){
          e.preventDefault();
          e.currentTarget.ancestor('.faq').toggleClass('ui-state-closed');
        }, 'body.faqs', '.faq .question');

      }

      var overlay = new Y.Overlay({
        boundingBox: "#overlay",
        contentBox: '#overlay .content',
        visible : false,
        width: 629,
        zIndex: 500,
        plugins : [
          Y.Plugin.OverlayModal
        ]
      });

      overlay.render();

      Y.one('#heart-uk-logo').on('click', function(e){
        e.halt();
        overlay.set('centered', true);
        overlay.show();
      });

      Y.delegate('click', function(e){
        e.halt();
        overlay.hide();
      }, '#overlay', '.button');

  });

  Y.on("contentready", function () {


    var carousel = new Y.Carousel({
      boundingBox: "#carousel",
      contentBox: "#carousel > ol",
      numVisible : 1,
      isCircular : true,
      autoPlayInterval : 5
    });

    carousel.plug(Y.CarouselOpacityPlugin, {
      animation: {
        speed: 1
      }
    });

    carousel.render();

  }, "#home");


  Y.on("contentready", function () {


    var carousel = new Y.Carousel({
      boundingBox: "#carousel",
      contentBox: "#carousel > ol",
      numVisible : 1,
      hidePagination : true,
      isCircular : true,
      autoPlayInterval : 5
    });

    carousel.plug(Y.CarouselOpacityPlugin, {
      animation: {
        speed: 1
      }
    });

    carousel.render();

  }, "#products");



  Y.on("contentready", function () {

    function TimelinePlugin(config) {
      TimelinePlugin.superclass.constructor.apply(this, arguments);
    }

    TimelinePlugin.NAME = 'timelinePlugin';
    TimelinePlugin.NS = 'timeline';

    Y.extend(TimelinePlugin, Y.Plugin.Base, {
      initializer: function(config) {
        this.afterHostMethod('_updateNavigation', this.insertTooltips);
      },
      insertTooltips : function(){
        var host = this.get('host');
        var titles = host.get('contentBox').all('li').getAttribute('title');
        this.get('host').get('boundingBox').all('.yui3-carousel-nav li a').each(function(item, i){
          item.append(Y.Node.create('<span class="tooltip"><span>' + titles[i] + '</span></span>'));
        });
      }
    });

    var carousel = new Y.Carousel({
      boundingBox: "#carousel",
      contentBox: "#carousel > ol",
      numVisible : 1,
      revealAmount : 25
    });

    carousel.plug(Y.CarouselAnimPlugin, {
      animation: {
        speed: 1,
        effect: Y.Easing.backOut
      }
    });

    carousel.plug(TimelinePlugin);

    carousel.render();

  }, "#timeline");


});
