/* Configurable Settings */

/* Server Options */
var pingFrequency = 30000;           // How often to ping the server (in milliseconds). Best range between 2500 and 3500 ms.
var pingTo        = 'ajax.php';  // The file that is the "server".

/* End Configurable Settings */

var userlist;
var recentposts;
var maildiv;
var pingTimer;
var counter = 0;
var initialLoadTimestamp = new Date();

function trim(text) 
{
   if(text == null) return null;
   return text.replace(/^[ \t]+|[ \t]+$/g, "");
}

function getElement(id) 
{
  if (document.getElementById) 
  {
    return document.getElementById(id);
  } 
  else if (window[id]) 
  {
    return window[id];
  }
  return null;
}

pingTimer = setInterval(ping, pingFrequency);

function checkLoginTimeout()
{
	var now = new Date();
	var inactiveTime = now - initialLoadTimestamp;
	if (inactiveTime > 1000 * 60 * 60)
	{
		window.location = "http://www.becauseimmom.com/index.php?logout=1";
	}
}

function ping() 
{
	checkLoginTimeout();
	
   var xhConn = new XHConn();
      
   xhConn.connect(pingTo, "POST", "call=ping&from="+user+"&access="+access,
      function(xh) {
         var i;

         if(xh.responseText == 'update_error') {
            return;
         }     
         
         if(trim(xh.responseText).length == 0) return;
         
         var response = xh.responseText.parseJSON();
         
         var from, data;
         if (!userlist)
         	userlist = getElement("onlineusers");
         html = "<b class=smalltext>Online Members: </b>";
         for(i=0; i<response.numusers; i++) 
         {
         	html = html + "<a class=smalltext href=\"../../profile.php?userid=" 
         		+ response.usersonline[i].userid + "\">" 
         		+ response.usersonline[i].login + "</a> "
         }
         
         userlist.innerHTML = html;
                  
         if (!recentposts)
         	recentposts = getElement("recentposts");
         if (recentposts)
         {
        	 html = "<table class=leftpane><tr><th colspan=2>Recent Posts</th></tr>";
        	 
        	 for(i=0; i<response.numposts; i++) 
        	 {
        	 	title = (response.recentposts[i].posttitle ? response.recentposts[i].posttitle : "No Subject");
        	 	html = html + "<tr><td><b><a class=\"smalltext\" href=\"index.php?page=posts&topicid="
        	 		+ response.recentposts[i].topicid + "&index=last\">"
        	 		+ title
        	 		+ "</a></b><div class=\"smalltext\"><b>by</b> "
        	 		+ response.recentposts[i].authorname + "<br><b>at</b> "
        	 		+ response.recentposts[i].createtime 
        	 		+ "</div><hr width=\"100%\" size=\"1\" class=\"hrcolor\" /></td></tr>"
        	 }

		html = html + "</table>";
  	       recentposts.innerHTML = html;
         }
         
	  if (!maildiv)
		maildiv = getElement("mailcountdiv");

	  if (maildiv)
	  {
		 html = "Inbox: <a class=\"smalltext\" href=\"../../mail.php\">" 
			+ response.mailcount + " New</a>";

		 maildiv.innerHTML = html;
         }
      }
   );
}



