/*
 *
 *	Lkf jQuery functions
 *		Stuart Swope
 */

/* Enables scroller functionality on the home and stories pages. */
jQuery.enableScroller = function () {

   jQuery("<img>").attr("src", 'graphics/loading.gif'); //preload loading.gif

	// Insert scroller nav arrows into the dom.
   jQuery('.scrollable').before('<img class="prevPage browse scroller-nav" src="graphics/arrow-prev.png" alt="Previous" title="Previous" />');
   jQuery('.scrollable').after('<img class="nextPage browse scroller-nav" src="graphics/arrow-next.png" alt="Next" title="Next" />');
   
   //Hover for nav arrows
   
   jQuery('.prevPage').hover(
      function () {
         jQuery(this).attr('src', 'graphics/arrow-prev-hover.png');
      }, 
      function () {
         jQuery(this).attr('src', 'graphics/arrow-prev.png');
      }
   );
   
   jQuery('.nextPage').hover(
      function () {
         jQuery(this).attr('src', 'graphics/arrow-next-hover.png');
      }, 
      function () {
         jQuery(this).attr('src', 'graphics/arrow-next.png');
      }
   );
      
   // Initialize scrollable      
   var $scroller = jQuery("#infinite").scrollable({
      size: 5,
      item: 'a',
      loop: false,
      api: true
   });
   
   // Hide the next arrow if there are only 5 items.
   if (jQuery(".items > a").length == 5) {
      jQuery('.nextPage').addClass('disabled');
   }   
   
   // Get the clicked image and load the large image
   jQuery(".items a").click(function() { 
      var href = jQuery(this).attr("href");
      jQuery('#scroll-image-wrap').fadeTo('slow', 0.10, function () {
         jQuery.get(href, function(data) {
            jQuery('#scroll-image-wrap').html(data).fadeTo("slow", 1);
         });
      });
      return false;
   }).filter(":first").click();
   
};

/* Enable home banners */
jQuery.enableHomeBanners = function () {

   $('#banners-wrapper').append('<div id="banner-nav"><img class="prev" src="graphics/button_photoPrev.gif" alt="Previous Banner" title="Previous Banner" width="19" height="18"><img class="next" src="graphics/button_photoNext.gif" alt="Next Banner" title="Next Banner" width="19" height="18"></div>');
   
   $('.homebanner').css("display", "none");
   $('.homebanner:first').css("display", "block");
   
   var numImages = 5;
   var curImg = 1;
   var bannerTime = 5000;
   
   // Event fires on timer
   function timerEvent(direction) {
      curImg = rotateImage(numImages, curImg, direction, 600, "homebanner-");
   }
   
   // Event fires on click of button.
   function clickEvent (direction) {
      timer.reset(bannerTime);
      timerEvent(direction);
   }

   $('.next').click(function () { 
      clickEvent("next");
   });
    
   $('.prev').click(function () { 
      clickEvent("prev");
   });

   // Start timer
   var timer = $.timer(bannerTime, function(timer) {
      timerEvent("next");
   });
}; 

/* Hack to add rel to lightbox links */
jQuery.lightboxLinks = function () {
   jQuery('.lightbox-link > a').attr('rel', 'lightbox[2]');
};