/*--------------------------------------------*/
/* Rotating Coach Previews (All Public Pages) */
/*--------------------------------------------*/

jQuery(document).ready(function($) {
   
  /*-------------------------------------------*/
  /* Coach Previews -- Configurable Parameters */
  /*-------------------------------------------*/
  
  // defines the amout of time between
  // transitions in the preview window
  rpr__pg.copr_interval = 8; // seconds
  
  // this is the time that is takes to 
  // fade between the current and next
  // profiles, should be less than interval
  rpr__pg.copr_transition_time = 3; // seconds
  
  /*--------------------------------*/
  /* End of Configurable Parameters */
  /*--------------------------------*/
    
  // do not proceed with coach preview unless its there
  if (!$('.rpr-coach-preview-window').size()) return;
  
  // first extract the single coach preview that was initially placed on the page
  // and place a hidden duplicate within its container in preparation for rotation 
  rpr__pg.copr_a = $('.rpr-coach-preview-window').children('div.rpr-coach-preview');
  rpr__pg.copr_b = rpr__pg.copr_a.clone();
  $('.rpr-coach-preview-window').append(rpr__pg.copr_b.hide());
  
  // additionally store the appropriate title element with each
  // preview panel so we can properly fade those in and out too
  rpr__pg.copr_a.data('title', $('h1.rpr-coach-preview-title.a'));
  rpr__pg.copr_b.data('title', $('h1.rpr-coach-preview-title.b'));
  
  // set our tracking vars to reflect that
  // currently previe panel a is being shown
  rpr__pg.copr_current = rpr__pg.copr_a;
  rpr__pg.copr_next = rpr__pg.copr_b;
  
  // establish a preloading image object
  rpr__pg.copr_preloader = new Image();
  
  // establish an update function which is to be called
  // after member profile data is retrieved from server
  rpr__pg.copr_a.data('update', function(pnl, data) {
    if (!(data instanceof Object)) data = new Object();
    if (!(data.member instanceof Object)) data.member = new Object();
    if (!data.member.id) return;
    
    // when it takes too long to retrieve the
    // data we do not want to update the current
    if (pnl == rpr__pg.copr_current) return;
    
    // now we can update the preview panel with the member information
    pnl.find('td.name').text(data.member.first_name+' '+data.member.last_name);
    pnl.find('td.country').text(data.country);
    pnl.find('tr.highlights td').html(data.highlights);
    pnl.find('td.headline').text(data.member.profile_heading);
    
    // now set the photo and preload immediately
    pnl.find('img.photo').attr('src', data.photo);
    rpr__pg.copr_preloader.src = data.photo;
  });
  
  // use the same function when the second panel needs updating
  rpr__pg.copr_b.data('update', rpr__pg.copr_a.data('update'));
  
  // establish a function which can
  // be called to rotate the preview
  rpr__pg.copr_rotate = function() {
    
    // first begin fading out the current preview panel and schedule the next request
    // to fill the current panel with new data when it has completely faded from view
    rpr__pg.copr_current.fadeOut(rpr__pg.copr_transition_time*1000, rpr__pg.copr_get);
    rpr__pg.copr_current.data('title').fadeOut(rpr__pg.copr_transition_time*1000);
    rpr__pg.copr_next.fadeIn(rpr__pg.copr_transition_time*1000);
    rpr__pg.copr_next.data('title').fadeIn(rpr__pg.copr_transition_time*1000);
    
    // swap the tracking variables
    // to indicate the panels changed
    var temp = rpr__pg.copr_current;
    rpr__pg.copr_current = rpr__pg.copr_next;
    rpr__pg.copr_next = temp;
    
    // schedule the next rotation based on the interval configured
    setTimeout('rpr__pg.copr_rotate()', rpr__pg.copr_interval*1000);
  };
  
  // establish a function which simply
  // retrieves a random profile from 
  // the server then updates the next
  // preview panel when data retrieved
  // we can use a best efforts approach
  rpr__pg.copr_get = function() {
    $.ajax({ url: "/coach_previews.php",
             type: "POST",
             dataType: 'json',   
             cache: false,          
             data: { action: 'fetch_random_profile' },
             success: function(data, status) {
                          rpr__pg.copr_next.data('update')(rpr__pg.copr_next, data);
                        }
           });
  };
  
  // prepare the first transition by
  // fetch the next profile and scheduling
  // the transition to occur after delay
  rpr__pg.copr_get();
  setTimeout('rpr__pg.copr_rotate()', rpr__pg.copr_interval*1000);

});









