/**
 * Manages the missionary spotlight information on the MPO front page.
 */
var Spotlight = Class.create();
Spotlight.prototype = {

   initialize: function(varname)
   {
      // constants
      this.PICS_URL = '/mpo/pics/';
      this.PHOTO_URL = this.PICS_URL + 'view/';
      this.PB_REFRESH_COUNT = 60;
      this.PB_REFRESH_PERIOD = 500;
      this.PB_MAXWIDTH = 410;

      // object reference variables
      this.spotlight = $('missionarySpotlight');
      this.progressBar = $('progressBar');
      this.slMissionaryName = $('slMissionaryName');
      this.slLink = $('slLink');
      this.slRegion = $('slRegion');
      this.slWorldArea = $('slWorldArea');
      this.slPhotoLink = $('spotlightPhotoLink');
      this.slPhoto = $('spotlightPhoto');
      this.slShortBio = $('slShortBio');

      // list management variables
      this.loaded = false;
      this.loading = false;
      this.nextIndex = 0;
      this.missionaryInfo = null;
      this.imgPreload = new Image();

      // load first set of missionaries
      this.loadMissionaryInfo();

      // initialize the spotlight refresh timer
      this.refreshEnabled = true;
      this.refreshEvalCode = varname + '.update()';
      this.refreshCount = this.PB_REFRESH_COUNT;
      this.startTimer();
   },

   /**
    *
    */
   startTimer: function()
   {
      setTimeout(this.refreshEvalCode, this.PB_REFRESH_PERIOD);
   },

   /**
    * Displays the next missionary on the list.
    */
   update: function()
   {
      --this.refreshCount;
      if ( this.refreshCount > 0 )
      {
         // update percentage bar
         this.progressBar.style.width = (Math.floor(this.PB_MAXWIDTH*this.refreshCount/this.PB_REFRESH_COUNT) + 'px');
      }
      else if ( this.loaded == true )
      {
         // reset counter
         this.progressBar.style.width = 0;
         this.refreshCount = this.PB_REFRESH_COUNT;

         // output next missionary info
         var pkey = this.missionaryInfo[this.nextIndex].pkey;
         var name = this.missionaryInfo[this.nextIndex].name;

         this.slPhotoLink.href = this.missionaryInfo[this.nextIndex].photoUrl;
         this.slPhoto.src = this.missionaryInfo[this.nextIndex].thumbUrl;
         this.slPhoto.title = name;
         this.slPhoto.alt = 'Photo: ' + name;
         this.slMissionaryName.innerHTML = name;
         this.slRegion.innerHTML = this.missionaryInfo[this.nextIndex].region;
         this.slWorldArea.innerHTML = this.missionaryInfo[this.nextIndex].worldArea;
         this.slLink.href = 'profile.jsp?profile=' + pkey;

         // decode and output short bio
         this.slShortBio.innerHTML = this.insertProfileLink(pkey, this.missionaryInfo[this.nextIndex].shortBio);

         // setup the next update call
         ++this.nextIndex;
         if ( this.nextIndex >= this.missionaryInfo.length )
         {
            // retrieve a new list of missionaries
            this.loadMissionaryInfo();
            this.nextIndex = 0;
         }
         else
         {
            // preload the next missionary photo
            this.imgPreload.src = this.PHOTO_URL + this.missionaryInfo[this.nextIndex].pkey + '.jpg';
         }
      }
      else
      {
         // no data loaded, attempt HTTP request
         this.loadMissionaryInfo();
      }

      this.startTimer();
   },

   /**
    * 
    */
   loadMissionaryInfo: function()
   {
      // skip if already loading data
      if ( this.loading == true )
      {
         return;
      }
      // clear loaded flag until the HTTP request is successfully completed
      this.loaded = false;
      this.loading = true;

      var _this = this;
      new Ajax.Request('/MissionaryProfiles/spotlight?output=js',
         {
            method: 'post',

            onSuccess: function(transport, json)
            {
               // convert JSON response into a usable object array
               _this.missionaryInfo = json.missionaries;

               // preload first missionary image
               _this.imgPreload.src = _this.missionaryInfo.thumbUrl;

               // set loaded flag to indicate that the missionary info is available
               _this.loaded = true;
               _this.spotlight.show();
            },

            onFailure: function(httpRequest)
            {
               _this.spotlight.hide();
            }
         }
      );

      // clear loading flag
      this.loading = false;
   },

   insertProfileLink: function(pkey, bio)
   {
      return bio.replace(/\[%MORE%\]/, '<span class="moreLink">[<a href="profile.jsp?profile=' + 
         pkey + '">read more</a>]</span>');
   }

};


var g_spotlight = null;
window.onload = function() {
   g_spotlight = new Spotlight('g_spotlight');
};
