/* os = online status */
var osAjax = [];
var osUrl = '/ajax/os_ajax.php?nick=';

function osDoAjax(nick) {
  var ajax = new sack();
  var index = osAjax.length;
  ajax.requestFile = osUrl + escape(nick);
  ajax.onCompletion = function() { osCallback(index); };
  osAjax[index] = ajax;
  ajax.runAJAX();
}

function osCallback(index) {
  var ajax = osAjax[index];
  var json = eval("(" + ajax.xmlhttp.responseText + ")");
  updateOs(json);
}

/**
 * Updates the page according to server response.
 *
 * @param os Object containing the online status.
 */
function updateOs(os) {
  //alert(os);
  if (os.nick) {
    // update number of online messages
    var msgs = os.onlineMessages;
    
    // update title, for example: '(1) Final Dimension - Home'
    var currentTitle = document.title;
    var pos = 0;
    if ((pos = currentTitle.indexOf(') ')) > 0) {
      currentTitle = currentTitle.substr(pos + 2);
    }
    
    if (msgs > 0) {
      document.title = '(' + msgs + ') ' + currentTitle;
    } else {
      document.title = currentTitle;
    }
    
    // update online message text, for example: 'You have 1 new online
    // message(s)'
    if (msgs < 1) {
      msgsText = osTexts.noNewOnlineMessages;
    } else {
      msgsText = osTexts.youHaveOnlineMessages.replace('{0}', msgs);
    }
    
    document.getElementById("unreadOnlineMessages").innerHTML = msgsText;
    
    // update hits
    byId('hits').innerHTML = os.hits + ' ' + osTexts.hits;
    byId('hitsToday').innerHTML = os.hitsToday;
    /*byId('hitsThisWeek').innerHTML = os.hitsThisWeek;
    byId('hitsThisMonth').innerHTML = os.hitsThisMonth;
    byId('hitsThisYear').innerHTML = os.hitsThisYear;*/

    // update online user count
    var users = os.users;
    var html = '';
    if (users.length == 0) {
      html = osTexts.noUsersOnline;
    } else if (users.length == 1) {
      html = osTexts.oneUserOnline;
    } else {
      html = osTexts.nUsersOnline.replace('{0}', users.length);
    }
    byId('onlineUserCount').innerHTML = html;

    // update list of online users
    var osDiv = document.createElement('div');
    osDiv.setAttribute('id', 'onlineUsers');
    
    for (i = 0; i < users.length; i++) {
      u = users[i];
      
      if (mpData[u.uid]) {
        mpData[u.uid].lastSeen = u.lastSeen;
        mpData[u.uid].lastSeenTs = u.lastSeenTs;
      }
    
      // <a>
      var a = document.createElement('a');
      a.href = '/users/' + u.uid;
      a.onmouseover = function() { mpOn(this); };
      a.onmouseout = function() { mpOff(); };
      a.onclick = function() { removeMpNow(); };
      a.appendChild(document.createTextNode(unescape(u.nick)));
      if (u.donated >= 10) {
        a.innerHTML += '<img src="/img/star.gif" />';
      }
      if (u.donated >= 20) {
        a.innerHTML = '<b>' + a.innerHTML + '</b>';
      }

      osDiv.appendChild(a);
    
      if (i < users.length - 1) {
        // separate user names with comma
        osDiv.appendChild(document.createTextNode(', '));
      }
    }

    // replace old one with new one
    var oldOsDiv = document.getElementById("onlineUsers");
    oldOsDiv.parentNode.replaceChild(osDiv, oldOsDiv);
    
  }
}
