// 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) {
  
	if (!(http_request = cs_ajax_start())) return;
	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);
}

function cs_ajax_start() {
  if (window.XMLHttpRequest) return new XMLHttpRequest();
  return (window.ActiveXObject) ? new ActiveXObject("Msxml2.XMLHTTP") : false;
}

var anch = "__START__";
var done = 1;
var away = 0;
var lastmove = 0;
  
function initializeAJAX(post, navinterval) {
  content = document.getElementsByTagName('body')[0].innerHTML;
  content = content.replace(/href=\"([a-z\/\.\-\_]*)\?mod=(\w.+)\"/g,"href=\"#mod=$2\"");
  document.getElementsByTagName('body')[0].innerHTML = content.replace(/#mod=users&amp;action=logout/g,"?mod=users&amp;action=logout");
  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;
  subanch = anch_new.substr(1);
  url = anch != "__START__" ? 'content.php?'+ subanch : 'content.php?'+subanch+'&first';
  
  document.getElementById('content').innerHTML = '<img src="symbols/clansphere/loading.gif" style="float:right" alt="Loading.." />' + document.getElementById('content').innerHTML;
  
  cs_ajax_getcontent(url,'content', "cloaded('"+anch+"')");
  anch = anch_new;
}

function cloaded(anch) {
  document.getElementById('content').innerHTML = document.getElementById('content').innerHTML.replace(/href=\"([a-z\/\.\-\_]*)\?mod=(\w.+)\"/g,"href=\"#mod=$2\"");
  if (anch != "__START__") setnavs(1);
}

function updatenavs() {
  if (!done || away) return;
  if (!document.getElementById('temp_div')) {
    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(cache) {
  content = cache != 1 ? document.getElementById('temp_div').innerHTML : document.getElementById('contenttemp').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; }
