(function($){

  $(document).ready(function(){

    /* Logo Subtitles cycle */
    if ($('.subtitles').length > 0) {
      $('.subtitles').cycle({
        speedIn: '800',
        speedOut: '200',
        timeout: '4000'
      });
    }

    /* Calendar Popup */
    if ($('.calendar-event').length > 0) {
      $('.calendar-event').hover(
        function() {
          var pos = $(this).position();
          var h = $(this).height();
          $(this).find('.tooltip-wrapper').css({
            'left': pos.left,
            'bottom': pos.top + h
          }).fadeIn();

        },
        function() {
          $(this).find('.tooltip-wrapper').fadeOut();
        }
        );
    }

    if ( $('.tile-row').length > 0) {

      var num_row = $('.tile-row').length;
      var val = 1;
      var inc = val/num_row;

      $('.tile-row').each(function(index, value){
        $(this).children('.item').css('opacity', val);
        val = val - inc;
      });

    }



    /* Headshot colour fade in/out */
    if($('.headshot').length > 0){
      $('div.headshot').hover(function(){
        $(this).find('.field-name-field-user-picture:eq(1)').fadeOut(200);
      },
      function(){
        $(this).children('.field-name-field-user-picture:eq(1)').fadeIn(200);
      });
    }


    /* Leadership colour fade in/out */
    if($('.view-leadership .views-row, .leader').length > 0){

      var config = {
        over: function(){
          $(this).find('b').stop(true, true).slideDown();
          $(this).find('img').eq(1).fadeOut()
        },
        timeout: 100,
        out: function(){
          $(this).find('b').slideUp();
          $(this).find('img').eq(1).fadeIn()

        }
      };

      $('.view-leadership .views-row, .leader').hoverIntent(config);
    }


    /* Heading resize */
    if($('.header h1').length > 0){

      while($('.header h1').height() >= ($('.header').height()*0.9) ){

        var curr = $('.header h1').css('font-size');
        var currNum = parseFloat(curr, 10);
        var newFontSize = currNum*0.9;
        $('.header h1').css('font-size', newFontSize);

      };
    }


    /* Preaching series scroll */
    if($('.media-playlist').length > 0){
      $('.media-playlist div.views-row').hover(
        function(){
          $(this).addClass('hover-back');
        },
        function(){
          $(this).removeClass('hover-back');
        }
        );
    }



  });


})(jQuery);;

