/*
 *
 *	Intercare 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) {
      $('.bnav-item').removeClass('selected');
      $('#bnav-' + (current)).addClass('selected');
      $(div_id + " #text").fadeOut(100, function () {
         $(this).html(clippings[current].html);
         $('.banner-img').attr("src", clippings[current].src);
      })
      .fadeIn(100, 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(5000, function(timer) {
         current = (current === (clippings.length - 1)) ? 0 : (current + 1);
         changeBanner(current);
      });
      
      // Build and insert the list of banner nav boxes
      var banner_nav = '<div id="banner-nav">\n';
      
      // This is all crazy because the nav boxes are floated right.
      var nav_item;
      for (i = 0; i < num_clippings; i++) {
         nav_item = num_clippings - 1 - i;
         banner_nav += '<div id="bnav-' + nav_item + '" class="bnav-item';
         banner_nav += (current === nav_item) ? ' selected"></div>\n' : '"></div>\n';
      }
      
      banner_nav += '</div>\n';
      
      // Insert the nav into the DOM.
      $(div_id).append(banner_nav);
      
      // When nav item is clicked change banner and stop timer.
      $('.bnav-item').click( function () {
         var id_arr = $(this).attr("id").split('-');
         var banner_id = parseInt(id_arr[1], 10);
         changeBanner(banner_id);
         timer.stop();
      });
   });
};


