var url = "/cgi-bin/webbuild"

/*function socialbox_refresh_times() {
   socialbox_refresh_times(false);
}*/

var socialbox_fullrefresh_count = 0;

function socialbox_refresh_times() {
  var times = document.getElementsByName("socialtime");
  var datenow = new Date();
  var utcnow = parseInt(Date.UTC(datenow.getUTCFullYear(), datenow.getUTCMonth(), datenow.getUTCDate(),
                                 datenow.getUTCHours(), datenow.getUTCMinutes(), datenow.getUTCSeconds()) / 1000);
  for (var i = 0; i < times.length; i++) {
     if (times[i].attributes[0].name == "time") {
        var socialtime = times[i].attributes[0].value;
     } else {
        var socialtime = times[i].attributes[1].value;
     }
     var timeago = utcnow - socialtime;
     if ((times[i].innerHTML == "") ||
         (timeago <= 120) ||                                                // update every 15 secs if < 2 min
         (timeago <= 7200 && (socialbox_fullrefresh_count % 4 == 0)) ||    // update every minute if < 120 min
         (timeago <= 86400 && (socialbox_fullrefresh_count % 120 == 0)) ||  // update every thirty minutes if < 1 day
         (socialbox_fullrefresh_count >= 240))                             // else update all every hour
         {
           var daysago = parseInt(timeago / 86400);
           var hoursago = parseInt(timeago / 3600 % 24);
           var minutesago = parseInt(timeago / 60 % 60);
           var secondsago = parseInt(timeago % 60);
           times[i].innerHTML = "(";
           if (daysago > 1) times[i].innerHTML += daysago + " days";
           else if (daysago == 1) times[i].innerHTML += "1 day, " + hoursago + " hours";
           else if (hoursago > 1) times[i].innerHTML += hoursago + " hours";
           else if (hoursago == 1) times[i].innerHTML += "1 hour, " + minutesago + " minutes";
           else if (minutesago > 1) times[i].innerHTML += minutesago + " minutes";
           else if (minutesago == 1) times[i].innerHTML += "1 minute, " + secondsago + " seconds";
           else times[i].innerHTML += secondsago + " seconds";
           times[i].innerHTML += " ago) ";
     }
  }
  if (socialbox_fullrefresh_count++ >= 240) socialbox_fullrefresh_count = 0;
}

var socialbox_interval = null;
var socialbox_reloadtime = 15000;

function ajax_getvalues_refresh(request,user,user_email,secret) {
  var fromid = 0;
  if (socialbox_interval != null) {
     clearInterval(socialbox_interval);
  }
  if (document.getElementsByName("social").length > 0) {
     if (document.getElementsByName("social")[0].attributes[0].name == "sid") {
        fromid = parseInt(document.getElementsByName("social")[0].attributes[0].value) + 1;
     } else {
        fromid = parseInt(document.getElementsByName("social")[0].attributes[1].value) + 1;
     }
  }
  if (request != "")
     request=request+"&REQUEST=refresh&USER="+user+"&USER_EMAIL="+user_email+"&SECRET="+secret+"&FROMID="+fromid;
  else
     request="REQUEST=refresh&USER="+user+"&USER_EMAIL="+user_email+"&SECRET="+secret+"&FROMID="+fromid;

  if (window.XMLHttpRequest)
    xmlhttp1 = new XMLHttpRequest();
  else if (window.ActiveXObject)
    xmlhttp1 = new ActiveXObject("Microsoft.XMLHTTP");
  else
    return;

  xmlhttp1.onreadystatechange = function() {
    if (xmlhttp1.readyState != 4)
      return;
    if (xmlhttp1.responseXML == undefined) {
       return;
    }
    var xmldoc = xmlhttp1.responseXML.documentElement;
    var nodes = xmldoc.getElementsByTagName("socialbox")[0].childNodes;
    for (var i = 0; i < nodes.length; i++) {
       id = nodes[i].attributes[0].value;
       document.getElementById("socialbox").innerHTML =
          "<span name=social sid=\""+ id + "\">" + nodes[i].childNodes[0].nodeValue + "</span>" +
          document.getElementById("socialbox").innerHTML;
    }
    if (xmldoc.getElementsByTagName("processes")[0] != undefined) {
       document.getElementById("processes").innerHTML = xmldoc.getElementsByTagName("processes")[0].childNodes[0].nodeValue;
       $(function() { $('.scroll-pane3').jScrollPane({scrollbarWidth:10}); });
    }
    $(function() { $('.scroll-pane').jScrollPane({scrollbarWidth:10}); });
//    lastupdate = (+new Date());
    socialbox_refresh_times();
//alert("refreshed_times");
  }
  
  xmlhttp1.open("POST", url, true);
  xmlhttp1.setRequestHeader("Content-type","application/x-www-form-urlencoded");
  xmlhttp1.send(request);
  socialbox_interval = setInterval("ajax_getvalues_refresh(\"\",\""+user+"\",\""+user_email+"\",\""+secret+"\")",socialbox_reloadtime);
}
