var cycle = {
  init: function() {
    jQuery('.cycle').each(function(){

      var $cycle = jQuery(this);

      var id = jQuery(this).attr('id');

      // Effects
      switch(id){
        case 'spotlight':
          var fx = 'fade'; /*'scrollRight'; 'scrollLeft'; */
          break;
      }

      // Initialise the cycle
      var $cycleItems = jQuery(this).find('.items')
        .cycle({
          next: jQuery(this).find('.next'),
          prev: jQuery(this).find('.prev'),
          pager: '#spotlight-nav',
          fastOnEvent: 1,
          pause: 1,
          fx: fx,
          timeout: 6000,
          height: 343,
          width: 460
        });

      // If less or equal then 1 item hide the nav links
      if(jQuery(this).find('.items li').length <= 1){
        jQuery(this).find('.prev, .next').remove();
      }

      // Pause the sub spotlights
      switch(id){
        case 'spotlight':

          // Pause on prev/next hover
          jQuery('.prev, .next', jQuery(this))
            .data('cycle', $cycleItems)
            .hover(
              function() {
                jQuery(this).data('cycle').cycle('pause');
              },
              function() {
                jQuery(this).data('cycle').cycle('resume');
              }
            );

          break;
      }
    });

    // make spotlight items clickable as a whole
    jQuery('.cycle .items li').each(function() {
      jQuery(this).css('cursor', 'pointer');
      jQuery(this).click(function() {
        var handle = jQuery(this).find('.title a');
        if (handle.length > 0) {
          window.location = handle[0];
        }
      });
    });
  }
}

var qtip = {
  init: function() {

    jQuery('.qtip-list li').each(function(){

        var $qtipDescription = jQuery(this).find('a').attr('title');
        jQuery(this).find('a').removeAttr('title');

        jQuery(this).qtip({
          content: {
            text: '<div class="qtip-header"></div><div class="qtip-inner">' + $qtipDescription + '</div><div class="qtip-footer"></div>'
          },
          show: {
            when: {
              event: 'mouseover'
            },
            effect: {
              length: 200
            }
          },
          hide: 'mouseout',
          position: {
            corner: {
               target: 'bottomLeft'
            }
          },
          style: {
            background: 'transparent',
            border: {
              radius: 0,
              width: 0
            },
            padding: 0,
            width: 109,
            color: '#ffffff'
          }
        });
    });

    jQuery('.icon-retweet').each(function(){

        var $qtipDescription = jQuery(this).attr('title');
        jQuery(this).removeAttr('title');

        jQuery(this).qtip({
          content: {
            text: '<div class="qtip-header2"></div><div class="qtip-inner2">' + $qtipDescription + '</div><div class="qtip-footer2"></div>'
          },
          show: {
            when: {
              event: 'mouseover'
            },
            effect: {
              length: 200
            }
          },
          hide: 'mouseout',
          position: {
            container: $(this).up().up(),
            corner: {
               target: 'topMiddle'
            }
          },
          style: {
            background: 'transparent',
            border: {
              radius: 0,
              width: 0
            },
            padding: 0,
            width: 71,
            color: '#ffffff'
          }
        });
    });

  }
}

var newsletter = {
  init: function() {
    jQuery('#newsletter form').bind('submit', function(e){
      e.preventDefault();
      jQuery.ajax({
        url: jQuery(this).attr('action'),
        type: 'POST',
        data: {'email': jQuery('#newsletter_email').attr('value')},
        success: function(msg) {
          jQuery('#newsletter-response').html(msg);
        }
      });
    });
  }
}

jQuery(document).ready(function($) {

  // Now it's save to use $ alongside prototype

  cycle.init();
  qtip.init();
  newsletter.init();
  jQuery('a[rel*=external]').attr('target', '_blank');

});
