/*
 * $Revision: 1.22 $
 * $Date: 2012/01/11 15:37:37 $
 * Last edited by: $Author: danderson $
 */

/**
 * PollingClient class.
 *  
 * Operations: 
 *  constructor(viewUri, dataId, pollIntervalSecs)
 *  start() This will start polling.
 *  pause()  This will pause the polling.
 *  resume() This will resume polling.         
 *  onUpdateDoNotify() This will set a custom user notification string. If none
 *                     is specified.
 *  onUpdateDoCallback() This will cause polling client to make a call to the 
 *                       callback function passed in as the second parameter.  If
 *                       a callback object is specified as the first parameter
 *                       then the callback function will be called on the 
 *                       callback object.
 */
 
  /**
   * Constructor
   *
   * @param viewUri - The uri of the page that will be used to view the data
   *                  that is being polled.
   * @param dataId - A page level unique name for the data that you are 
   *                 polling.  This is useful if you are polling more than
   *                 one type of data on a single page.
   * @param pollIntervalSecs - The number of seconds between polling for new data.
   * 
   */
  var PollingClient = function( viewUri, dataId, pollIntervalSecs )
  {
    this.debug            = false;
    /* max interval is 2 minutes (in millis) */
    this.maxPollInterval  = 120000;
    this.dataId           = dataId;
    this.viewUri          = '' + viewUri;/* force to string */
    this.isPollingActive  = true;
    this.pollInterval     = pollIntervalSecs * 1000;
    this.pollIntervalSecs = pollIntervalSecs;
    this.lastModified     = null;
    this.loginVerfied     = false;
    this.w                = window;
    this.$                = this.w.jQuery;
    this.cookieGet        = this.$.cookies.get;
    /* setup default handling of update that user can override */
    this.callbackFunction = this.notifyUser;
    this.notifyMessage    = null;
    this.notifyWidget     = null;
  };

  PollingClient.prototype.onUpdateDoCallback = function( /** Object **/ callbackObject, /** String **/ callbackFunction )
  {
    this.callbackObject = callbackObject;
    this.callbackFunction = callbackFunction;
  };
  
  PollingClient.prototype.onUpdateDoNotify = function( message )
  {
    this.notifyMessage = message;
  };

  PollingClient.prototype.notifyUser = function()
  {
    var oThis = this;

    if( ! ( oThis.notifyWidget instanceof jQuery ) )
    {
      oThis.notifyWidget = $( '#meerkat' );
    }

    if( oThis.notifyWidget.length === 1 )
    {
      $( '.meerkatContent', oThis.notifyWidget ).html( oThis.notifyMessage );

      meerkat( meerkat.ActivityNotifyOptions );

      setTimeout( function ()
      {
        $( '.meerkatClose', oThis.notifyWidget ).click();
      }, 5000 );
	  
	 $('#dockedActivityMsg').addClass('docked').html('You Have New Activity');
     
    }
  };

  PollingClient.prototype.start = function()
  {
    if( ! this.loginVerfied )
    {
      this.verifyUserLoggedIn();
    }
    
    if( this.isPollingActive )
    {
      var pClient = this;
      setTimeout( function ()
      {
        pClient.pollServer();
      }, this.pollInterval );
    }
  };

  PollingClient.prototype.isActive = function() {
    return this.isPollingActive;
  };
  
  PollingClient.prototype.pause = function()
  {
    this.isPollingActive = false;
  };
  
  PollingClient.prototype.resume = function()
  {
    this.isPollingActive = true;
  };

  PollingClient.prototype.pollServer = function() {
    // this method is deprecated
  }

  PollingClient.prototype.verifyUserLoggedIn = function()
  {
    var pClient = this; 
    
    if( this.cookieGet( 'PHPSESSID' ) === null )
    {
      return false;
    }
    
    if (constant.defined('USERLOGGEDIN') && constant.value('USERLOGGEDIN') == false)
    {
      pClient.pause();
    }
  };
  
  PollingClient.prototype.initializePollImage = function()
  {
    var pClient = this;
    pClient.logit( 'initializePollImage() Inititializing polling image.' );

    this.$.ajax( {
      url: '/polling/initializePollImage.html',
      type: 'GET',
      timeout: 300000,
      async: true,
      cache: false,
      data: {
        dataType: this.dataId,
        uri: this.viewUri
      },
      success: function()
      {
        pClient.lastModified = null;
        pClient.logit('Polling image initialized.');
      },
      error: function( XMLHttpRequest, textStatus )
      {
        pClient.logit( 'initializePollImage() unable initilize polling image: readyState: ' + XMLHttpRequest.readyState + '; textStatus: ' + textStatus  );
      }
    } );
  };

  PollingClient.prototype.dumpString = function(windowName, message, theString)
  {
    var OpenWindow;

    OpenWindow = this.w.open( '', windowName, 'height=400,width=400,toolbar=no,scrollbars=yes,menubar=no' );
    OpenWindow.document.write( message );
    OpenWindow.document.write( theString );
    OpenWindow.document.close();
  };
  
  PollingClient.prototype.logit = function(message)
  {
    if( this.debug )
    {
      var logFunc = alert;

      if( typeof console === 'object' && console !== null && typeof console.log === 'function' )
      {
        logFunc = console.log;
      }

      logFunc( 'PollingClient.js::' + message );
    }
  };

