/*
 *
 *	Air Zoo javacript functions
 *		Stuart Swope
 */
 

/* Enables banner rotation functionality on the home page. */
//---------------------------------------------------------------------
// homeRotate - /* Enables image rotation functionality on the home page. */
// @param - group - The clippings group name.
// @param - div_id - The div id of the containing div.
//--------------------------------------------------------------------- 
/*
 * homeRotate - Enables banner rotation functionality on the home page.
 * @param - group - The clippings group name.
 * @param - div_id - The div id of the containing div.
*/
$.homeRotate = function (group,div_id) {
   var current = 0;
   var cache = [];
   var clippings = [];
   var num_clippings;
   var timer = '';
   
   // Triggered by timer or click. Changes banner and moves nav box.
   function changeBanner(current) {
      $(div_id + " #banner").fadeOut(600, function () {
         $(this).html(clippings[current].html);
         //$('.banner-img').attr("src", clippings[current].src);
      })
      .fadeIn(600, function () {
         $(div_id).css('backgroundImage', 'url('+clippings[current].src +')'); // Trick for cross-fade. Set last image as background.
      });
   }
   
   // Get the clippings using an ajax call and initialize timer.
   $.getJSON('includes/ajax_homebanners.php?', {'clip_group' : group }, function(data) {
      clippings = eval(data);
      num_clippings = clippings.length;
      var i;
      
      // Find the currently displayed banner and preload images.
      for(i = 0; i < num_clippings; i++) {
         if (clippings[i].src === $(div_id).find("img:first-child").attr('src')) {
            current = i;
            $(div_id).css('backgroundImage', 'url('+clippings[i].src +')');
         }
         
         cache[i] = new Image();
         cache[i].src = clippings[i].src;  
      }      
      
      // Start the timer.
      timer = $.timer(3750, function(timer) {
         current = (current === (clippings.length - 1)) ? 0 : (current + 1);
         changeBanner(current);
      });
      
   });
};


