/**
 * Misc.StatService library utilities
 *   Written in pure JavaScript in order to be JS library agnostic.
 */

Stats = {

  // Base URLs of the Misc.StatService
  STAT_ENDPOINT  : Config.STAT_URL,
  ARGUMENTS      : {
        "action"    : "a",
        "callback"  : "back",
        "entity"    : "e",
        "ids"       : "ids",
        "id"        : "id",
        "type"      : "t",
        "time"      : "ti",
        "contest"   : "c"
  },
  
  // Takes an array of params and concatenates into URL, then makes the remoting call.
  get : function( params ) {
    var qs = Array(params.length), i=0;
    
    for ( var x in params ) {
      qs[++i] = this.ARGUMENTS[x] + '=' + params[x];
    }
    
    Stats.remote.load( Stats.STAT_ENDPOINT + '?' + qs.join('&') );
  },

  // Object to handle remoting.
  remote : {
    counter : 0,
    current : null,
    head    : document.getElementsByTagName('head')[0],
    load    : function( url ) {
      var self = Stats.remote;
      self.counter = self.counter + 1;
      var id = '__stats_remote_id';
      var stamp = '&stamp=' + (new Date()).getTime();
      self.current = document.createElement('script');
      self.current.setAttribute('charset', 'utf-8');
      self.current.setAttribute('id', id);
      self.current.setAttribute('src', url + stamp );
      self.current.setAttribute('type', 'text/javascript');
      self.head.appendChild(self.current);
    }
  },

  insert : function( container, statObj ) {
    for ( x in statObj ) {
      var el = document.getElementById(container + x);
      if( el !== null && typeof el == 'object') el.innerHTML = statObj[x];
    }
  },

  view : function( type, typeId, callback ) {

    this.get({
      action    : 'v',
      entity    : type,
      id        : typeId,
      callback  : callback            
    });

  },

  appreciate : function( type, typeId ) {

    this.get({
      action    : 'a',
      entity    : type,
      id        : typeId
    });

  }

};

/**
Stats examples
--------------------
Example call
Stats.get({
  a     : 's',
  c     : 'itemStat',
  back  : 'function', // callback must be passed as string (in quotes)
  e     : 'item',
  t     : 'apps',
  ti    : 'all',
  i     : '281557|275360|277133|267974|274065|276142|278655|262461|274727|275369|276261|275316'
});

Example callback
callback_showItem : function ( result ) {
  if ( result == 'yes' ) {
    document.getElementById('item').style.display = 'block';
  }
}
**/
