function locateDynamicPosition (obj) {
//--- Find the current position of an object on the page

	var curL = 0;
	var curT = 0;

	if (obj.offsetParent) {
		curL = obj.offsetLeft
		curT = obj.offsetTop
		while (obj = obj.offsetParent) {
			curL += obj.offsetLeft
			curT += obj.offsetTop
		}
	}

	return [curL, curT]; // return as array

}

function XHTTPRequest_Tags (url, handler) {
//--- Send an HTTP request

	if (window.XMLHttpRequest) {

		tagReq = new XMLHttpRequest ();
		tagReq.onreadystatechange = handler;
		tagReq.open ("GET", url, true);
		tagReq.send (null);

    } else if (window.ActiveXObject) {

		tagReq = new ActiveXObject ("Microsoft.XMLHTTP");
		if (tagReq) {
			tagReq.onreadystatechange = handler;
			tagReq.open ("GET", url, true);
			tagReq.send ();
        }

    }

}

function connectWord (whichWord) {
//--- Connects the selected word to the partial word

	whichField = document.getElementById ('Tags');
	whichField.focus ();

	//--- Get the caret position
	if (document.selection) {
		range = document.selection.createRange ();
		range.moveStart ('character', -whichField.value.length);
		CaretPos = range.text.length;
	} else
	if ((whichField.selectionStart) || (whichField.selectionStart == '0')) {
		CaretPos = whichField.selectionStart;
	}

	lastComma = whichField.value.substr (0, CaretPos).lastIndexOf (',');
	if (lastComma == -1) {
		lastComma = 0;
	}

	inclSpaces = '';
	findSpaces = lastComma;
	while ((findSpaces <= whichField.value.length) && ((whichField.value.substr (findSpaces, 1) == ',') || (whichField.value.substr (findSpaces, 1) == ' '))) {
		inclSpaces += whichField.value.substr (findSpaces, 1);
		findSpaces++;
	}
	whichField.value = whichField.value.substr (0, lastComma) + inclSpaces + whichWord + whichField.value.substr (CaretPos, whichField.value.length - CaretPos);

	//--- Reset caret
	if (whichField.setSelectionRange) {
		whichField.focus ();
		whichField.setSelectionRange (CaretPos + whichWord.length - (CaretPos - lastComma) + inclSpaces.length, CaretPos + whichWord.length - (CaretPos - lastComma) + inclSpaces.length);
	} else
	if (whichField.createTextRange) {
		range = whichField.createTextRange ();
		range.collapse (true);
		range.moveEnd ('character', CaretPos + whichWord.length - (CaretPos - lastComma) + inclSpaces.length);
		range.moveStart ('character', CaretPos + whichWord.length - (CaretPos - lastComma) + inclSpaces.length);
		range.select ();
	}

	document.getElementById ('tagSuggestions').style.visibility = 'hidden';

}

function showTags () {
//--- Loads the most recent tags returned from the server

	if (tagReq.readyState == 4) {

		switch (tagReq.status) {
			case 200:	//--- Display response text
						var tagLinks = '';
						var hasError = tagReq.responseXML.getElementsByTagName ("error");
						if (hasError.length) {
							document.getElementById ('tagSuggestions').innerHTML = "Could not download tag list."; break;
						} else {
							var tagList = tagReq.responseXML.getElementsByTagName ("tags")[0].getElementsByTagName ("tag");
						}
						if (tagList.length) {
							for (i = 0; i < tagList.length; i++) {
								tagLinks += "<a href=\"#\" onclick=\"connectWord ('" + tagList[i].firstChild.nodeValue.toString () + "'); return false\">" + tagList[i].firstChild.nodeValue.toString () + "</a><br />";
							}
						} else {
							document.getElementById ('tagSuggestions').style.visibility = 'hidden';
						}
						if (tagLinks) {
							textAreaObjPos = locateDynamicPosition (document.getElementById ('Tags'));
							document.getElementById ('tagSuggestions').innerHTML = tagLinks;
							document.getElementById ('tagSuggestions').style.width = (document.getElementById ('Tags').offsetWidth - 20) + 'px';
							document.getElementById ('tagSuggestions').style.top = (textAreaObjPos[1] + document.getElementById ('Tags').offsetHeight - 3) + 'px';
							document.getElementById ('tagSuggestions').style.left = (textAreaObjPos[0] + 3) + 'px';
							document.getElementById ('tagSuggestions').style.visibility = 'visible';
						}
						break;
			default:	document.getElementById ('tagSuggestions').innerHTML = "Could not download tag list."; break;
						break;
		}

	}

}

function hideTags () {
//--- Hides the tags popup

	document.getElementById ('tagSuggestions').style.visibility = 'hidden';

}

function lookupWord (event, whichField) {
//--- Function to lookup words

	var keyPressed = (event.keyCode ? event.keyCode : event.which);

	if (keyPressed == 27) {
		document.getElementById ('tagSuggestions').style.visibility = 'hidden';
		document.getElementById ('tagLayer').style.visibility = 'hidden';
		whichField.blur ();
		return false; //--- Escape
	}

	//--- Ignore commas and semi-colons
	if ((keyPressed == 186) || (keyPressed == 188)) {
		return false;
	}

	if (document.selection) {
		range = document.selection.createRange ();
		range.moveStart ('character', -whichField.value.length);
		CaretPos = range.text.length;
	} else
	if ((whichField.selectionStart) || (whichField.selectionStart == '0')) {
		CaretPos = whichField.selectionStart;
	}

	lastComma = whichField.value.substr (0, CaretPos).lastIndexOf (',');
	if (lastComma == -1) {
		lastComma = 0;
	}

	newWord = whichField.value.substr ((lastComma ? lastComma + 1 : 0), CaretPos - (lastComma ? lastComma + 1 : 0)).replace (/^\s+/, '');
	if ((newWord != '') && (newWord.length > 2) && (newWord != lastWord)) {
		XHTTPRequest_Tags ("pagebuilder/components/tag/phrase-lookup-xml.php?Word=" + escape (newWord) + "", showTags); lastWord = newWord;
	} else {
		hideTags (); return false;
	}

}

function showTagPopup (event) {
//--- Displays a popup for users to tag the content

	if (event.srcElement) {
		linkObj = event.srcElement;
	} else
	if (event.target) {
		linkObj = event.target;
	}
	linkObjPos = locateDynamicPosition (linkObj);
	document.getElementById ('tagLayer').style.visibility = 'visible';
	document.getElementById ('tagLayer').style.top = (linkObjPos[1] + linkObj.offsetHeight + 10) + 'px';
	document.getElementById ('tagLayer').style.left = linkObjPos[0] + 'px';
	if (document.getElementById ('tagLayer').filters) {
		document.getElementById ('tagLayer').style.filter = "progid:DXImageTransform.Microsoft.dropshadow(OffX=5, OffY=5, Color='gray', Positive='true')";
	}
	document.getElementById ('Tags').focus ();

}

function hideTagPopup () {
//--- Hides the tag popup

	document.getElementById ('tagLayer').style.visibility = 'hidden';
	document.forms['tagForm'].elements['Tags'].value = '';
	document.forms['tagForm'].elements['tagSubmit'].disabled = false;

}

function refreshAllTags (tags) {
//--- Updates the tags list

//	document.getElementById ('allTags').innerHTML = unescape (tags);

}

var lastWord = '';
