QBX_PRODUCTS = [

  { "name": "Admozi", "dc": ["web", "android"], "url": "/catalog/admozi",
    "free": true },

  { "name": "CipherLog", "dc": ["android"], "url": "/catalog/cipherlog",
    "free": true },

  { "name": "C.T.P.H.", "dc": ["android"], "url": "http://chronicthepothog.com",
    "free": true },

  { "name": "English", "dc": ["android"], "url": "/catalog/english",
    "free": true },

  { "name": "Neuro3D", "dc": ["android", "ios"], "url": "/catalog/neuro3d",
    "free": false },

  { "name": "PicBored", "dc": ["android"], "url": "http://picbored.com",
    "free": true },

  { "name": "SHIFTPOP", "dc": ["web"], "url": "http://shiftpop.tv",
    "free": true },

  { "name": "ZenerWave", "dc": ["android"], "url": "/catalog/zenerwave",
    "free": true }

];

QBX = new function() {

  var self = this;
  var m_news_id = 0;
  
  var m_news = [
    { msg: "Play games and watch live video at Qubaz.com!", url: "http://qubaz.com" },
    { msg: "Search live video feeds at shiftpop.tv!", url: "http://shiftpop.tv" },
    { msg: "You can make money displaying ads with Admozi!", url: "http://admozi.com" },
    { msg: "Follow us on Twitter to find out when we release new apps and games!", url: "http://twitter.com/qubinox" }
  ];
  
  this.setup = function() {
    self.update();
    setInterval(self.update, 7500);
  };

  this.update = function() {
    var news_item = m_news[m_news_id];
    $('#news').html('<h1>News</h1> | ' + '<a href="' + news_item.url + '" target="_blank">' + news_item.msg + '</a>');
    m_news_id += 1;
    if (m_news_id >= m_news.length) m_news_id = 0;
  };

  this.contact_setup = function() {
    $('#btn_contact').click(function() {
      var req = {};
      req['contact_email'] = $('#contact_email').val();
      req['contact_reason'] = $('#contact_reason').val();
      req['contact_product'] = $('#contact_product').val();
      req['contact_device'] = $('#contact_device').val();
      req['contact_msg'] = $('#contact_msg').val();
      self.contact_send(req);
    });
  };

  this.contact_send = function(obj) {
    var data = new Object();
    data.site = "qbx";
    data.db = "inbox";
    data.doc = obj;
    $('#btn_contact').fadeOut(500);
    S9.request("sub9.db.create_doc", JSON.stringify(data), function(data) {
      if (data['ok'] != true) {
        alert("There was a problem sending your message, please try again later or email us instead.");
      } else {
        $('#contact_email').val('');
        $('#contact_reason').val('support');
        $('#contact_product').val('None');
        $('#contact_device').val('');
        $('#contact_msg').val('');
        alert("Your message has been sent to Qubinox.  Thanks!");
      }
      $('#btn_contact').fadeIn(1500);
    });
  };

  this.get_neuro3d_scores = function() {
    self.get_scores("neuro", self.got_neuro3d_scores);
  };

  this.get_scores = function(db, callback) {
    req = new Object();
    req.site = "qbx";
    req.db = db;
    req.design = "players";
    req.view = "best";
    req.options = { "descending": "true", "limit": "10" };
    S9.request("sub9.db.view", JSON.stringify(req), callback);
  };

  this.got_neuro3d_scores = function(data) {
    var rows = data.rows;
    var scoretable = "<table class=\"score_table\">" +
      "<tr><td>Initials</td><td>Score</td></tr>";
    for (var score in rows) {
      scoretable += "<tr><td width=\"150\"><b>" + rows[score].value + "</b></td>" +
        "<td width=\"150\">" + rows[score].key + "</td></tr>";
    }
    $("#neuro3d_scoreboard").html(scoretable + "</table>");
  };

};

$(document).ready(function() {
  QBX.setup();
});
