/*******************************************************
  Variables and Constants
*******************************************************/
var ie = (navigator.userAgent.indexOf('MSIE') >= 0);

//var dialogs = [];		// array of references to all of the currently open dialogs
//var dialogZIndex = 1;	// top-most z-index
//var timerFaderWindow;	// timer used to fade the fader window in and out
//var timerDialogFade;	// timer used to fade a dialog window
var currentSelector;	// reference to the current, open selectorWindow
var selectorSource;		// source object that opened the currentSelector
var lastDialog;


/*******************************************************
  Object Acquisition Functions
*******************************************************/
/*
function byClass(className)
{
	return null;
}
*/

function byId(id)
{
	if (typeof id != 'string') return id;
	else return document.getElementById(id);
}

var byName = document.getElementByName;
var byTag = document.getElementsByTagName;

/*
function childByClass(obj, tag)
{

}

function childById(obj, tag)
{

}

function childByName(obj, tag)
{

}

function childByTag(obj, tag)
{

}

function parentByClass(obj, className)
{

}

function parentById(obj, tag)
{

}

function parentByName(obj, tag)
{

}

function parentByTag(obj, tag)
{
	/\*
	obj = pobj = document.getElementById(obj);	
	its = 0;
	if (obj.tagName != 'BODY')
		return null;

	do
	{
		document.title = ++its;
		pobj = (ie) ? obj.parentElement : pobj.parentNode;
		if (pobj.tagName == tag)
			return pobj;
	}
	while (pobj.tagName != 'BODY')
	return null;
	*\/
}

function childOfClass(child, parent)
{

}

function childOfId(child, parent)
{

}

function childOfName(child, parent)
{

}

function childOfTag(child, parent)
{

}
*/

//-------------------------------//

function setOpacity(obj, alpha)
{	
	alpha = (alpha < 0) ? 0 : ((alpha > 100) ? 100 : alpha);
	obj.style.MozOpacity = alpha / 100;
	//obj.style.filter = 'progid:DXImageTransform.Microsoft.Alpha(Opacity=' + alpha + ')';
	obj.style.filter += 'alpha(opacity=' + alpha + ')';
}

/*
function getOpacity(obj)
{
	alert(obj.style.filter.opacity);
}

function incOpacity()
{
}

function decOpacity()
{
}
*/

//-------------------------------//

function getX(obj)
{
	if (typeof obj == 'string') obj = document.getElementById(obj);
	return (obj.offsetParent && obj.tagName != 'BODY') ? (obj.offsetLeft + getX(obj.offsetParent)) : 0;
}

function getY(obj)
{
	if (typeof obj == 'string') obj = document.getElementById(obj);
	return (obj.offsetParent && obj.tagName != 'BODY') ? (obj.offsetTop + getY(obj.offsetParent)) : 0;
}

//-------------------------------//

/*
function debug(obj)
{
	items = [];
	for (item in obj)
	{
		items.push(item);
	}
	win = window.open();
	win.document.write('<font face=arial>' + items.join('<br>'));
}
*/


/*******************************************************
  Dialog Functions
*******************************************************/
function openDialog(dlg, arg1, arg2, arg3)
{
	// initialize
	if (typeof dlg == 'string')
		dlg = document.getElementById(dlg);

	// set attributes
	dlg.setAttribute('open', true);
	dlg.style.display = 'block';

	// dialog-specific operations
	switch (dlg.id)
	{
		case 'htmlBuilderWindow':
			ws.document.getElementById('htmlmenu').style.display = 'none';
			document.getElementById(topworkspacename).style.display = 'none';
			document.getElementById('htmlBuilderWindow').style.display = 'block';
			if (ie) document.getElementById('windowSpacer').style.display = 'block';
			document.getElementById('htmlBuilderTextarea').value = '';
			document.getElementById('htmlBuilderTextarea').focus();
			break;

		case 'linkMoverWindow':

			// set variables
			data = lbGetData();
			linkMover = document.getElementById('linkMoverItems');

			// if no items, exit
			if (data.length == 0)
				return false;

			// update link mover
			linkMover.innerHTML = '';
			linkMover.style.height = 'auto';
			linkMover.style.overflow = 'visible';
			for (i = 0; i < data.length; i++)
			{
				name = data[i][0];
				url = data[i][1];
				border = (i != data.length-1) ? " style='border-bottom:solid 1px #DDD'" : '';
				linkMover.innerHTML += "<div class=linkMoverItem url=\"" + url + "\" onselectstart='return false'" + border + ">" + data[i][0] + "</div>";
			}

			// set link mover height and overflow
			if (getY(linkMover) + linkMover.offsetHeight > document.body.clientHeight - 75)
			{
				linkMover.style.overflow = 'auto';
				linkMover.style.height = (document.body.clientHeight - getY(linkMover) - 50) + 'px';
			}

			document.getElementById('pageFader').style.cssText += "; left: 0; top: 0; width: 100%; height: 100%; display: block; z-index: " + (document.getElementById('linkBuilderWindow').style.zIndex-1+10);
			break;
	}

	// show / hide windows
	zIndex = dlg.style.zIndex - 1;
	if (zIndex < 0) zIndex = 0;
/*
	if (lastDialog)
	{
		document.getElementById('pageFader').style.left = (lastDialog.offsetLeft + 1) + 'px';
		document.getElementById('pageFader').style.top = (lastDialog.offsetTop + 1) + 'px';
		document.getElementById('pageFader').style.width = (lastDialog.firstChild.offsetWidth - 10) + 'px';
		document.getElementById('pageFader').style.height = (lastDialog.firstChild.offsetHeight - 10) + 'px';
		document.getElementById('pageFader').style.zIndex = zIndex;
		document.getElementById('pageFader').style.display = 'block';
	}
	lastDialog = dlg;
*/
	// fade windows
	//timerFaderWindow = setInterval("setOpacity");
	//timerdialogFade = setInterval("");
}

function closeDialog(dlg, arg1, arg2, arg3)
{
	// initialize
	if (typeof dlg == 'string')
		dlg = document.getElementById(dlg);

	// set attributes
	dlg.setAttribute('open', null);

	// dialog-specific operations
	switch (dlg.id)
	{
		case 'htmlBuilderWindow':
			document.getElementById(topworkspacename).style.display = 'block';
			document.getElementById('windowSpacer').style.display = 'none';
			break;

		case 'linkBuilderWindow':
			btn = document.getElementById('lbAdvMnuPropBtn');
			if (btn.value.charAt(0) != 'A') btn.click();
			break;

		case 'linkMoverWindow':
			if (arg1)
			{
				tbl = document.getElementById('linkBuilderEditTable');
				items = document.getElementById('linkMoverItems').childNodes;
				for (i = 1; i < tbl.rows.length; i++)
				{
					tbl.rows[i].firstChild.firstChild.value = items[i-1].innerHTML;
					tbl.rows[i].firstChild.nextSibling.firstChild.value = items[i-1].getAttribute('url');
				}
			}
			document.getElementById('pageFader').style.display = 'none';
			lbUpdate();
			break;
	}

	// show / hide windows
	//document.getElementById('pageFader').style.visibility = 'hidden';
	dlg.style.display = 'none';
}

function openSelector(selector, source)
{
	// initialize
	if (currentSelector) closeSelector();
	source = byId(source);
	sourceText = (ie) ? source.innerText : source.textContent;
	selector = byId(selector);

	// select current item
	for (i = 0; i < selector.rows.length; i++)
	{
		selector.rows[i].cells[0].className = 'selectorItem';
		if (selector.rows[i].cells[0].innerHTML == sourceText)
		{
			if (ie) selector.rows[i].cells[0].className = 'selectorItemOn';
			selector.setAttribute('index', i);
		}
	}

	// open
	selector.style.display = 'block';
	x = getX(source) + (ie?1:0);
	y = getY(source) + source.offsetHeight + (ie?2:0);
	if (y + selector.offsetHeight > document.body.clientHeight)
		y -= selector.offsetHeight + source.offsetHeight - 1;
	if (y < 30) {
		x += source.offsetWidth;
		y = 30;
	}
	selector.style.left = x + 'px';
	selector.style.top = y + 'px';

	selector.style.width = source.offsetWidth;
	currentSelector = selector;
	selectorSource = source;
}

function closeSelector(update, newIndex)
{
	sel = currentSelector;
	source = selectorSource;
	if (!sel) return;

	// if update, set the new value
	if (update)
	{
		sel.setAttribute('index', null);
		cell = sel.rows[newIndex].cells[0];
		source.innerHTML = cell.innerHTML;
		source.setAttribute('value', cell.getAttribute('value'));

		if ((source.id == 'toolSelBorderStyle0' && document.getElementById('toolSelBorderWidth0').value == 0) /*||
			(source.id == 'toolSelBorderStyle1' && document.getElementById('toolSelBorderWidth1').value == 0)*/)
			alert("No border will be shown with the current settings because the border thickness is set to zero (0).");
	}

	// reset variables and styles
	document.getElementById('selectorHint').style.display = 'none';
	source.className = source.className.replace('On', '');
	sel.style.display = 'none';
	currentSelector = null;
}