﻿// dependencies: jquery, webtoolkit.sprintf.js

InfoControl = function(jQuerySearchContext) {
  this.showInfo();
  this.ctx = $('#'+jQuerySearchContext)[0];
  
  var pThis = this;
  this.populate = function(data) { InfoControl.prototype.populate(pThis, data); };
  this.fetchWebcastInfo = function(id, retries) { InfoControl.prototype.fetchWebcastInfo(pThis, id, retries); }  
};

InfoControl.prototype = {

  onLocationEvent: function(lat, lon, zoom) { },

  ctx: null, // jquery search context

  fetchWebcastInfo: function(pThis, webcastID, retries) {
    if (!retries) retries = 3;
    else if (--retries == 0) { pThis.showError('err'); return; }

    pThis.showSpinner();
    pThis.clearFields();

    var ok = function(data) {
      var x = data.d || data;
      pThis.populate(x.Return);
      var wp = x.Return.WayPoint;
      if (wp) pThis.onLocationEvent(wp.Latitude, wp.Longitude, wp.Scale);
    }
    var err = function() { pThis.fetchWebcastInfo(webcastID, retries) };  // retry on error
    coreWS.GetWebcast(SID, webcastID, ok, err);
  },

  clearFields: function() {
    $('#MetadataNames > div', this.ctx).hide();
    $('#MetadataValues > div', this.ctx).hide();
  },

  populate: function(pThis, data) {

    data.has = function(f) { return typeof (data[f]) != "undefined" && data[f]; }

    if (data.has("Name")) {
      var t = data["Name"];
      if (t.length > 25) t = t.substring(0, 25) + "...";
      $('#Title', pThis.ctx).html(t); $('div[id^=Title]', pThis.ctx).show();
    }
    else {
      $('#Title', pThis.ctx).html("&nbsp;"); $('div[id^=Title]', pThis.ctx).show();
    }

    if (data.has("Duration")) {
      var d = data["Duration"];
      var sec = d % 60;
      var min = (d / 60) % 60;
      var hrs = (d / 3600);
      var show = sprintf("%02d:%02d:%02d", hrs, min, sec);
      $('#Duration', pThis.ctx).html(show); $('div[id^=Duration]', pThis.ctx).show();
    }
    else {
      $('#Duration', pThis.ctx).html("&nbsp;"); $('div[id^=Duration]', pThis.ctx).show();
    }

    if (data.has("Created")) {
      var d = data["Created"].replace(/[^\d]/g, ''); // strip non-digits
      d = new Date(parseInt(d));
      var show = sprintf("%d:%02d %02d/%02d/%02d", d.getHours(), d.getMinutes(), d.getMonth() + 1, d.getDate(), d.getFullYear());
      $('#Created', pThis.ctx).html(show); $('div[id^=Created]', pThis.ctx).show();
    }
    else {
      $('#Created', pThis.ctx).html("&nbsp;"); $('div[id^=Created]', pThis.ctx).show();
    }

    // note pattern: ABAB
    if (data.has("RealName")) {
      var rn = data["RealName"];
      if (rn.length > 25) rn = rn.substring(0, 25) + "...";
      $('#Author', pThis.ctx).html(rn); $('div[id^=Author]', pThis.ctx).show();
    }
    else {
      $('#Author', pThis.ctx).html("&nbsp;"); $('div[id^=Author]', pThis.ctx).show();
    }

    if (data.has("Device")) {
      var dev = data["Device"];
      if (dev.length > 25) dev = dev.substring(0, 25) + "...";
      $('#Device', pThis.ctx).html(dev); $('div[id^=Device]', pThis.ctx).show();
    }
    else {
      $('#Device', pThis.ctx).html("&nbsp;"); $('div[id^=Device]', pThis.ctx).show();
    }

    if (data.has("Profile")) {
      var pr = data["Profile"];
      if (pr.length > 25) pr = pr.substring(0, 25) + "...";
      $('#Bitrate', pThis.ctx).html(pr); $('div[id^=Bitrate]', pThis.ctx).show();
    }
    else {
      $('#Bitrate', pThis.ctx).html("&nbsp;"); $('div[id^=Bitrate]', pThis.ctx).show();
    }

    if (data.has("Id")) {
      var id = data["Id"];
      if (id.length > 25) id = id.substring(0, 25) + "...";
      $('#VideoID', pThis.ctx).html(id); $('div[id^=VideoID]', pThis.ctx).show();
    }
    else {
      $('#VideoID', pThis.ctx).html("&nbsp;"); $('div[id^=VideoID]', pThis.ctx).show();
    }

    if (data.has("Tags")) {
      var t = data["Tags"];
      if (t.length > 25) t = t.substring(0, 25) + "...";
      $('#Tags', pThis.ctx).html(t); $('div[id^=Tags]', pThis.ctx).show();
    }
    else {
      $('#Tags', pThis.ctx).html("&nbsp;"); $('div[id^=Tags]', pThis.ctx).show();
    }

    if (data.has("Description")) {
      var d = data["Description"];
      if (d.length > 25) d = d.substring(0, 25) + "...";
      $('#Description', pThis.ctx).html(d); $('div[id^=Description]', pThis.ctx).show();
    }
    else {
      $('#Description', pThis.ctx).html("&nbsp;"); $('div[id^=Description]', pThis.ctx).show();
    }

    if (data.has("Copyright")) {
      var cp = data["Copyright"];
      if (cp.length > 25) cp = cp.substring(0, 25) + "...";
      $('#Copyright', pThis.ctx).html(cp); $('div[id^=Copyright]', pThis.ctx).show();
    }
    else {
      $('#Copyright', pThis.ctx).html("&nbsp;"); $('div[id^=Copyright]', pThis.ctx).show();
    }

    if (data.has("Rating")) {
      var r = data["Rating"];
      if (r.length > 25) r = r.substring(0, 25) + "...";
      $('#Rating', pThis.ctx).html(r); $('div[id^=Rating]', pThis.ctx).show();
    }
    else {
      $('#Rating', pThis.ctx).html("&nbsp;"); $('div[id^=Rating]', pThis.ctx).hide(); // NOTE: if rating is added, then hide() should be changed to show()
    }

    pThis.showInfo();
  },

  showSpinner: function() {
    $('#MediaInfoSpinner', this.ctx).show();
    $('#MediaInfo', this.ctx).hide();
    $('#MediaInfoError', this.ctx).hide();
  },

  showInfo: function() {
    $('#MediaInfoSpinner', this.ctx).hide();
    $('#MediaInfo', this.ctx).show();
    $('#MediaInfoError', this.ctx).hide();
  },

  showError: function(err) {
    $('#MediaInfoSpinner', this.ctx).hide();
    $('#MediaInfo', this.ctx).hide();
    $('#MediaInfoError', this.ctx).show();
    $('#mediaInfoErrorDetail', this.ctx).text(err);
  }
};