// JavaScript Document
// @ Author, CK 12.2.09
// @ Description: navigation window handling routines, ajax code

var strActiveDiv = '';
var hndTimer = null;
var strSendScript = strAppRoot + '/context_stream.php';
var strAjaxError = 'Sorry, your request could not be completed at this time';

window.onresize = onResize;

function onResize() 
{
	var pos = getPosition(document.getElementById('wnd_anchor'));
	setPosition(document.getElementById(strActiveDiv), pos);
}

function getPosition(e)
{
	var left = 0;
	var top  = 0;

	while (e.offsetParent){
		left += e.offsetLeft;
		top  += e.offsetTop;
		e     = e.offsetParent;
	}

	left += e.offsetLeft;
	top  += e.offsetTop;
//alert(left + '    ' + top)
	return {x:left, y:top};
}

function setPosition(e, pos)
{
	if (e) {
		var offset = {};
		var hWnd = e;
		e.top = pos.x;
		e.left = pos.y;
		
		offset.y = 0;
		offset.x = -75;
		
		hWnd.style.top = pos.y - offset.y + 'px';
		hWnd.style.left = pos.x + offset.x + 'px';
	}
}

function setActive(strDivId)
{
	strActiveDiv = strDivId
	
}

function showWnd(e)
{
	var pos = getPosition(document.getElementById('wnd_anchor')); 
	var hWnd = e;
	var strDivTag = ''
	
	if (strActiveDiv) hideWnd(strActiveDiv);
	setActive(e);
	
	// get unique div id parameter
	if (arguments.length >= 2) strDivTag = arguments[1];
	
	// initiate Ajax stream
	if (arguments.length == 3) sendRequest(arguments[2], strDivTag);

	hWnd.style.position = 'absolute';
	setPosition(e, pos);
	hWnd.style.visibility = 'visible';
}

function hideWnd(e)
{
	if (e) {
		var hWnd = e;
		
		hWnd.style.visibility = 'hidden';
		hWnd.style.top = '1px';
		hWnd.style.left = '1px';
		
		hndTimer = clearDelay(hndTimer);
		strActiveDiv = '';
	}
}

// Timer code
function setDelay()
{
	if (!hndTimer) hndTimer = window.setTimeout("hideWnd(strActiveDiv)",500);
}

function clearDelay(hndObj)
{
	if (hndObj) {
		window.clearTimeout(hndObj);
		return null;
	}
	else
		return hndObj;
}

// Ajax Code
function sendRequest(args, tag)
{
	
	var strPostData = "info=" + escape( args);
	
	//alert(strPostData);
	execUserAjaxCall(strPostData, tag);	
	return true;
}

function notifyUser(strUserMsg, tag)
{
	document.getElementById('context_data_' + tag).innerHTML = strUserMsg;
}

function execUserAjaxCall(strPostData, tag) {
  var xmlHttp;
  try {
    // Firefox, Opera 8.0+, Safari
    xmlHttp=new XMLHttpRequest();
    }
  catch (e) {
    // Internet Explorer
    try {
      xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
      }
    catch (e) {
      try {
        xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
      catch (e) {
        alert("Your browser does not support AJAX!");
        return false;
        }
      }
    }
	
	
    xmlHttp.onreadystatechange=function() {
      if(xmlHttp.readyState==4) {
			
			//user error messages
			var re = /error/ig;
			
			//server/client error messages
			var status_re = /[4-5][0-9]{2}/;
			
			//DEBUG
			//alert(xmlHttp.responseText);
			//notifyUser(xmlHttp.responseText);	
			//alert(xmlHttp.status);
			
			if (status_re.exec(xmlHttp.status)) {
				notifyUser(xmlHttp.statusText, tag);
				return;
			}
			
			if (re.exec(xmlHttp.responseText)) {
				//alert(xmlHttp.responseText);
				notifyUser(strAjaxError, tag);
			}
			else {
				notifyUser(xmlHttp.responseText, tag);
			}
      }
    }
	
    xmlHttp.open("POST",strSendScript,true);
    xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset:UTF-8");
    xmlHttp.setRequestHeader("Content-length", strPostData.length);
    xmlHttp.setRequestHeader("Connection", "close");
    xmlHttp.send(strPostData);	
 }
