﻿//dependencies: jquery, coreWS

Comments = function(pollingIntervalMS) {
pollingIntervalMS = 5000; 
  var pThis = this;
  this.fetch = function() { Comments.prototype.fetch(pThis); };
  this.poll = function() { Comments.prototype.poll(pThis, pollingIntervalMS); };
  this.add = function() { Comments.prototype.add(pThis); };
  $('#sayWhat').bind('keydown', function(e) { if(e.keyCode==13) { pThis.add(); return false; } });  
  this.send = function(handle, msg, webcastID, retries) { Comments.prototype.send(pThis, handle, msg, webcastID, retries); };
  this.poll();
};

Comments.prototype = {
  record: [], // IDs of onscreen comments
  webcastID: 0, // comments on-screen
  isVisible: false, // is this panel on-screen?
  isLivecast: true, // user watching livecast?
  isLiveAvailable: true,

  userPlayArchive: function(id) {
    this.isLivecast = false;
    this.webcastID = id;
    this.refresh();
  },
  
  userPlayLive: function(id) {
    this.isLivecast = true;
    this.webcastID = id;
    this.refresh();
  },
  
  refresh: function() {
    this.clear();
    this.fetch();
  },
  
  enable: function(bool) {
    this.isVisible = bool;
    if(this.isVisible) this.refresh();
  },

  setLivecast: function(liveID) {
    if(liveID) {
      this.isLiveAvailable = true;
      $('#sayWhat').attr('disabled',false);
      if(this.isLivecast) {
        if(this.webcastID != liveID) {  // new livestream
          this.webcastID = liveID;
          this.refresh();
        }
      }
    }
    else {
      this.isLiveAvailable = false;
      $('#sayWhat').attr('disabled',true);
    }
  },

  poll: function(pThis, interval) {
    if(pThis.isVisible && pThis.isLivecast && pThis.webcastID && pThis.isLiveAvailable) {
      pThis.fetch(pThis);
    }
    setTimeout(function() { pThis.poll(interval); }, interval);
  },
  

  fetch: function(pThis) {  
    var ok = function(data) { pThis.populate(data); }
    var err = null;
    coreWS.GetComments(SID, pThis.webcastID, ok, err);
  },

  populate: function(data) {
    if(!this.isVisible) return;
    if(data.d) data = data.d;
    if(data.length == 0) return;
    
    for(var i = 0; i < data.length; i++) {
      var c = data[i];
      if(! this.record[c.Id]) {
        this.record[c.Id] = 1;                
        var tag = '<p class="chatRow"><span class="commentName">WHO:&nbsp;</span><span class="commentContent">SAYWHAT</span></p>'
          .replace(/WHO/, c.Author.Name)
          .replace(/SAYWHAT/, c.Value);      
        $('#commentsContainer').append(tag);
      }
    }
  },
  
  clear: function() {
    this.record = [];
    $('#commentsContainer').empty();
  },
  
  add: function(pThis) {
    var msg = $('#sayWhat').val();
    $('#sayWhat').val('');
    if(!msg || !pThis.webcastID) return;
    var handle = $('#LiveChatName').val()
    setTimeout(function() { pThis.send(handle, msg, pThis.webcastID); }, 1);
  },
  
  send: function(pThis, handle, msg, webcastID, retries) {
    if(!retries) retries = 3;
    else if(--retries == 0) { alert("Error sending comment to server"); return; }   
    
    var sid = null;
    var ok = null;
    var err = function() { pThis.send(handle, msg, webcastID, retries); } // retry on error    
    coreWS.AddComment(sid, webcastID, msg, handle, ok, err);
  }
  
};