// JavaScript Document

// Offset from cursor
var dialog_offsetX = 16;
var dialog_offsetY = 16;

// Minimum distance to screen border
var dialog_marginX = 5;
var dialog_marginY = 5;

var dialog_visible = false;

var dialog_documentClickEventAttached = false;

function dialog_SetPosition(e) {
	var mouseX = IEdetected ? window.event.clientX : e.pageX;
  var mouseY = IEdetected ? window.event.clientY : e.pageY;
	var obj = (IEdetected ? e.srcElement : e.target);
	var x, y, scrollY;

	scrollY = (IEdetected ? document.documentElement.scrollTop : window.pageYOffset);

	x = mouseX + dialog_offsetX;
	y = mouseY + dialog_offsetY + (IEdetected ? scrollY : 0);	

	dialog_Update(x, y, obj.tt_width);
}

function dialog_Update(offsetX, offsetY, width) {
	var obj = document.getElementById('helpDialog');
	var iframe = document.getElementById('helpDialogIframe');

	if(width)	obj.style.width = width + 'px';
	obj.style.left 	= offsetX + 'px';
	obj.style.top 	= offsetY + 'px';
	
	var windowHeight = (IEdetected ? document.documentElement.clientHeight : window.innerHeight);
	var scrollHeight = (IEdetected ? document.documentElement.scrollTop : window.pageYOffset);
	var dialogHeight = obj.clientHeight;
	var dialogWidth = obj.clientWidth;
	
	if(offsetX > (document.body.clientWidth - dialog_marginX - dialogWidth))
		offsetX = document.body.clientWidth - dialog_marginX - dialogWidth;
	if(offsetX < dialog_marginX) offsetX = dialog_marginX;

	if(offsetY > (scrollHeight + windowHeight - dialog_marginY - dialogHeight))
		offsetY = scrollHeight + windowHeight - dialog_marginY - dialogHeight;
	if(offsetY < dialog_marginY) offsetY = dialog_marginY;
	
	//window.status = offsetX + ' ' + offsetY;
	
	obj.style.left 	= offsetX + 'px';
	obj.style.top 	= offsetY + 'px';
	iframe.style.width 	=	obj.offsetWidth;
	iframe.style.height = obj.offsetHeight;
	iframe.style.top 		=	obj.style.top;
	iframe.style.left 	=	obj.style.left;
	iframe.style.zIndex = obj.style.zIndex - 1;
}

function dialog_Show() {
	dialog_visible = true;
	displayBlock('helpDialog');
	if(IEdetected)	displayBlock('helpDialogIframe');
}

function dialog_SetContent(content) {
	var obj = document.getElementById('helpDialog');
	obj.innerHTML = content;
	dialog_lastHTML = content;
}

function dialog_Hide() {
	dialog_visible = false;
	displayNone('helpDialog');
	displayNone('helpDialogIframe');
}

function dialog_CloseOnOutsideClick() {
	if(!dialog_documentClickEventAttached) {
		objAttachEvent(document, "onmousedown", "mousedown", dialog_OnMouseDown);
		dialog_documentClickEventAttached = true;
	}
}

function dialog_OnMouseDown(e) {
	var obj = (IEdetected ? e.srcElement : e.target);

	if(!elementIsWithin(obj, document.getElementById('helpDialog'))) dialog_Hide();
}

