// lithCore_rpc.js

 /*
  * lithCore specific RPC code v0.2.9
  *
  * RPC javascript for lithCore
  * Mark Warner
  *
  * 08/30/06 - v0.0.1
  */

  /*
   * Revision History
   * -----------------------------
   * Date        Revision    Description
   * ----------- ----------- -------------------------------------------------------------------------------
   * 08/30/06    0.0.1       Initial Creation
   *             0.1.0       Changed callback syntax
   *             0.1.1       Changed interframe syntax
   *             0.1.2       Fixed debug code
   *             0.1.3       More debug code
   *             0.1.4       More syntax fixes
   *             0.1.5       Still more fixes
   *             0.1.6       Yep.
   *             0.1.7       Still.
   *             0.1.8       Fixed a REALLY stupid bug.
   *             0.1.9       Ditto.
   *             0.2.0       Messing with interframe syntax
   *             0.2.1       Ditto.
   *             0.2.2       Commented debug code
   *             0.2.3       Attempted to fix title update
   *             0.2.4       Changed source of 'topic' and 'page' parameters
   *             0.2.5       Added support for 'busy' indicator and added lithCore_toggleTitle()
   *             0.2.6       Added scriptaculous based highlight effect
   *             0.2.7       Temporarily removed highlight effect while I figure out why it doesn't like frames
   *             0.2.8       Made some changes to account for scriptaculous' apparent frame atheism
   *             0.2.9       Oops.
   *             0.3.0       Added support for update_cutoff to prevent 'back in time' updating
   *             0.3.1       Fixed busy indicator getting stuck when updates are ignored due to cutoff
   *             0.3.2       Fixed a stupid bug with the cutoff time.  Now all times are client side.
   *             0.3.3       Fixed a possible typecast bug in update()
   *
   *
   */
   
   function lithCore_handleResponse(response) {
     // handle an XML directive from the server
     //alert('A response has been received.');
     
     // discard the update if it has expired due to user action
     var dispatch_time = response.getElementsByTagName('dispatch_time');
     var update_time   = dispatch_time[0].childNodes[0].nodeValue;
     if (update_time < top.document.update_cutoff) {
       top.document.busy = 0;
       return 1;
     }

     var updates = response.getElementsByTagName('update');
     var len     = updates.length;
     //alert('Recieved ' + len + ' updates.');
     for(i = 0; i < len; i++) {
       var update = updates[i];
       
       var frame   = update.getElementsByTagName('frame')[0].childNodes[0].nodeValue;
       var element = update.getElementsByTagName('element')[0].childNodes[0].nodeValue;
       var content = String(update.getElementsByTagName('content')[0].childNodes[0].nodeValue);
       
       //alert('Update is for frame \'' + frame + '\' and element \'' + element + '\'.'); 
       
       if (frame == 'title') {
         top.document.title = content;
       }else{
         top.frames['lC_top'].frames[frame].document.getElementById(element).innerHTML = content;
         // create a momentary highlight effect on the updated element
         var frame_element  = top.frames['lC_top'].frames[frame];
         var target_element = top.frames['lC_top'].frames[frame].document.getElementById(element);
         frame_element.highlight(target_element);

       }
     }
     top.document.busy = 0;
   }
   
   function lithCore_updateForum(rpc_url) {
     // update the entire forum in one shot
     //alert('Attempting to update forum.');
     top.document.busy = 1;
     
     var date = new Date;
     var req = buildURL(rpc_url, 'op', 'lithCore', 'lC_op', 'rpc_updateForum',
                        'lC_topicID', top.document.topic, 'lC_page', top.document.page, 'lC_time', date.getTime());
     var callback = lithCore_handleResponse;
     sndRPC_interactive(req, callback);
   }
   
   // not really RPC, but to keep the template cleaner...
   var title_toggle_state = 0;
   var title_cache;
   var updated;
   function lithCore_toggleTitle() {
     // debug code
     //alert('toggleTitle was called!  Toggle state: ' + title_toggle_state);
     // make sure the page isn't being updated
     if (top.document.busy == 0) {
       // debug
       //alert('Page is not busy, so title will be swapped.');
       
       // make sure we grab the most recent title and not a stale one
       if (updated == 1) {
         // debug
         //alert('A title update was detected.');

         title_cache = top.document.title;
         updated = 0;
       }
       if (title_toggle_state == 0) {
         title_cache = top.document.title;
         var usercount = top.frames['lC_top'].frames['lC_thread'].document.getElementById('rpc_usercount').innerHTML;
         if (usercount == 1) {
           top.document.title = '(' + usercount + ' active user)';
         }else{
           top.document.title = '(' + usercount + ' active users)';
         }
         title_toggle_state = 1;
       }else{
         top.document.title = title_cache;
         title_toggle_state = 0;
       }
     }else{
       // debug
       //alert('Page is busy updating.  No title toggling.');
       
       // if the page is busy updating, make sure we know to grab the updated title for toggling later
       updated = 1;
     }
   }

