function ajaxLoad(div, method, args, hourglass)
{
	// execute ajax request
	var obj = new WpciAjaxProcessor(); 
	obj.setCompleter(new AjaxDivLoader(div));
	args['__ajax_call'] = method; 
	
	obj.addPostData(args);
	obj.execute(location.href);
	
	// use hourglass?
	if (hourglass)
	{
		getElement(div).innerHTML = '<img src="/static/icons/hourglass.gif" alt="Waiting.." />';
	}
	
	return obj;
}

function htmlspecialchars(text)
{
	text = text.replace('&', '&amp;');
	text = text.replace('<', '&lt;');
	text = text.replace('>', '&gt;');
	text = text.replace('"', '&quot;');
	return text;
}

function onPageLoad()
{
	// capture events
	if (document.layers)
		document.captureEvents(Event.MOUSEMOVE);
	document.onmousemove = handleMouseMove;
}

function genUserPopup(obj, id)
{
	var upop = $('userPopup');
	upop.style.display = '';
	upop.style.left = getLeftFromObject(obj) + "px";
	upop.style.top  = getTopFromObject(obj)  + "px";
	upop.style.width = "200px";
	upop.style.height = '100px';

	// load popup
	ajaxLoad('userPopup', 'gen_userpopup', { 'user_id' : id}, true);
}

function buildTopMenu(top, left, index, text, url, icon, children)
{
	var html = '';
	html+= '<div class="layoutMenuItem" style="background: #111111; left: 50%; margin-left: ' + left + 'px; text-align: left; top: ' +  top + 'px; position: absolute; z-index: 10000;" id="' + index + 'link" >\n';
	html+= '	<table width="100%" border="0" cellspacing="0" cellpadding="0" onMouseOver="MenuDriver.showMenu(' + index + ');" onMouseOut="MenuDriver.onMenuOut(' + index + ');" >\n';
	html+= '		<tr><td  >\n';
	html+= '			<table cellspacing="0" cellpadding="0" class="layoutMenuHidden" >\n';
	html+= '				<tr class="pointer"><td><img src="/static/icons/' + icon + '.png" alt="ic" /></td>\n';
	html+= '					<td ><a href="' + url + '">' + text + '</a></td>\n';
	html+= '				</tr>\n';
	html+= '			</table>\n';
	html+= '		</td></tr>\n';
	html+= '		<tr id="menu' + index + '" style="display: none" >\n';
	html+= '			<td><table cellspacing="0" cellpadding="0" class="layoutMenuHidden" >\n';
	
	// for each of the children
	for (i = 0; i < children.length; i++)
		html+= '<tr ><td ><img src="/static/icons/' + children[i][2] + '.png" /></td><td ><a href="' + children[i][1] + '">' + children[i][0] + '</a></td></tr>';
	html+= '			</table></td>\n';
	html+= '		</tr>\n';
	html+= '	</table>\n';
	html+= '</div>\n';
	document.write(html);
}

function handleMouseMove(evt)
{
	// version checking
	var isNN = document.all ? false : true;
	var isIE = document.all ? true : false;
	
	var IE = document.all ? true : false;
	if (IE)
	{ 
		PageVars.mouseX = event.clientX;
		PageVars.mouseY = event.clientY + document.documentElement.scrollTop;
	}
	else 
	{ 
		PageVars.mouseX = evt.pageX;
		PageVars.mouseY = evt.pageY;
	}
			
	// any tracking mechanisms
	for(sKey in PageVars.drag_tabs)
	{
		if (PageVars.drag_tabs[sKey] == true)
		{
			var p = getElement(sKey);
			
			// if dragging
			if (PageVars.ie_ver != -1)
			{
				p.actual_top  = (PageVars.mouseY - document.documentElement.scrollTop - 10);
				p.actual_left = (PageVars.mouseX - 10);
			}
			else
			{
				p.actual_top  = (PageVars.mouseY - document.documentElement.scrollTop - 15);
				p.actual_left = (PageVars.mouseX - 15);
			}
			
			// assign actual
			p.style.left = p.actual_left + "px";
			p.style.top = p.actual_top + "px";
			
			// document.title = document.documentElement.scrollLeft + " " + p.style.left + " " + p.style.top;
		}
	}
	
	return true;
}

function handleScroll(e)
{
	return;
	if (PageVars.ie_ver <= 6 && PageVars.ie_ver != -1)
	{
		// any tracking mechanisms
		for(sKey in PageVars.drag_sets)
		{
			if (PageVars.drag_sets[sKey] == true)
			{
				var p = getElement(sKey);
				
				// if dragging
				document.title = document.documentElement.scrollTop;
				p.style.top  = (p.drag_top + document.documentElement.scrollTop) + "px";
			}
		}
	}
}
					
					// JavaScript Document

function getElement(id)
{
	return document.getElementById(id);
}

function getLeftFromObject(obj)
{
	var curleft = 0;
	if(obj.offsetParent)
	{
		while(1) 
		{
		  curleft += obj.offsetLeft;
		  if(!obj.offsetParent)
			break;
		  obj = obj.offsetParent;
		}
	}
	else if(obj.x)
	{
		curleft += obj.x;
	}
	else
	{
	}
	return curleft;
}

function getTopFromObject(obj)
{
	var curtop = 0;
	if(obj.offsetParent)
	{
		while(1)
		{
		  curtop += obj.offsetTop;
		  if(!obj.offsetParent)
		  {
			break;
		  }
		  obj = obj.offsetParent;
		}
	}
	else if(obj.y)
	{
		curtop += obj.y;
	}
	return curtop;
}

function getMouseX(e)
{
	if (document.all)
	{
		window.event.clientX;
	}
	else
	{
		return e.pageX;
	}
}

function getMouseY(e)
{
	if (document.all)
	{
		window.event.clientY;
	}
	else
	{
		return e.pageY;
	}
}

function PageVars()
{
	
}

//
// dragging
//

function addDragTab(v)
{
	// update drag tabs
	PageVars.drag_tabs[v] = true;
}

function deleteDragTab(v)
{
	// update drag tabs
	PageVars.drag_tabs[v] = false;
}

function dragMouseDown(v)
{
	alert('dragMouseDown');
	
	var p = getElement(v);
	var b = getElement(v + '_T');
	
	// check offset
	if (p.drag_ip == true)
	{
		deleteDragTab(v);
		p.drag_ip = false;
	}
	else
	{
		// offset
		p.drag_sx = PageVars.mouseX - (p.actual_left);
		p.drag_sy = PageVars.mouseY - (p.actual_top);
		p.drag_ip = true;
		addDragTab(v);
	}
}

function dragMouseUp(e)
{
}

function addFixedDiv(v)
{
	PageVars.drag_sets[v] = true;
}

function deleteFixedDiv(v)
{
	PageVars.drag_sets[v] = false;
}

function ensureTopVisible(o)
{	
}

// tracking the tabs
PageVars.drag_tabs = new Array();
PageVars.drag_sets = new Array();
PageVars.loading	 = '<img src="/static/icons/hourglass.gif" alt="Waiting.." />';

//
// Version Detection
//

function getIEVersion()
{
  var rv = -1; // Return value assumes failure.
  if (navigator.appName == 'Microsoft Internet Explorer')
  {
    var ua = navigator.userAgent;
    var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
    if (re.exec(ua) != null)
      rv = parseFloat( RegExp.$1 );
  }
  return rv;
}

PageVars.ie_ver = getIEVersion();
PageVars.is_ie  = PageVars.is_ver != -1;
