// ClanSphere 2008 - www.clansphere.net
// $Id: $

function cs_ajax_setcontent (http_request, id, onfinish) {

  if (http_request.status != 200) return;

  if (!document.getElementById(id)) { window.setTimeout(function() { cs_ajax_setcontent(http_request, id, onfinish); },50); return; }
  document.getElementById(id).innerHTML = http_request.responseText;
    
  if (onfinish) window.setTimeout(onfinish,5);
}

function cs_ajax_getcontent(url, objectid, onfinish) {
  
  cs_ajax_request(url, function(http_request) { cs_ajax_setcontent(http_request, objectid, onfinish) } );
  
}

function cs_ajax_request (url, callback) {
  
	var http_request = cs_ajax_start();
	if (!http_request) return false;

	http_request.open ("GET", url, true);
	
	http_request.onreadystatechange = function() {
		if (http_request.readyState == 4) {
			http_request.onreadystatechange = function(){};
			callback (http_request);
		}
	};
	http_request.send (url);
	return true;
}

function cs_ajax_start() {
  
  if (window.XMLHttpRequest) { // Mozilla
    return new XMLHttpRequest();
  } else if (window.ActiveXObject) { // MS IE
    return new ActiveXObject("Msxml2.XMLHTTP");
  }
  
  return false;
}

var anch = "__START__";
var done = 1;
var away = 0;
var lastmove = 0;
  
function initializeAJAX(post, navinterval) {
  
  if (post) anch = window.location.hash;
  lastmove = GetMins();
  
  window.setInterval("checkanch()",50);
  
  if (!navinterval) return;
  
  window.setInterval("updatenavs()", navinterval * 1000);
  
  window.setTimeout('window.onmousemove = Movement;',60000);
  window.setInterval('CheckActivity()',120000);
  window.onunload = function() { if (typeof http_request != "undefined") http_request.abort(); }
  
}

function checkanch() {
  anch_new = window.location.hash;
  if (anch_new == anch) return;
  
  if (container = document.getElementById('content'))
    container.innerHTML = '<img src="symbols/clansphere/loading.gif" style="float:right" alt="Loading.." />' + container.innerHTML;
  
  cs_ajax_getcontent('content.php?'+anch_new.substr(1),'content', anch=="__START__" ? '' : 'updatenavs()');
  anch = anch_new;
}
function updatenavs() {
  if (!done || away) return;
  temp = document.createElement("div");
  temp.id = 'temp_div';
  temp.style.display = 'none';
  document.getElementsByTagName('body')[0].appendChild(temp);
  done = 0;
  cs_ajax_getcontent('navlists.php','temp_div','setnavs()')
}
function setnavs() {
  content = document.getElementById('temp_div').innerHTML;
  done = 1;
  parts = content.split('!33/');
  for (run = 1; run < parts.length; run+=2)
    if (document.getElementById(parts[run])) document.getElementById(parts[run]).innerHTML = parts[run+1];
}

// User activity
function GetMins() { now = new Date(); return now.getMinutes(); }

function Movement (Event) {
  lastmove = GetMins();
  away = 0;
  window.onmousemove = function() { };
  window.setTimeout('window.onmousemove = Movement;',60000);
}
function CheckActivity() { if ((GetMins() - lastmove) % 60 > 1) away = 1; }
