// *** common.js ***
// Common JavaScript functions for the LeftOblique blog and related sites

// Pop up a window using a smaller format
//
// strURL		: the URL to open
// strName		: the name of the new window
//
function PopupWindow(strURL, strName)
{
	window.open(strURL, strName, 'width=640,height=480,scrollbars=yes,status=yes');
}

function obls_get_element(strElementName)
{
	var elAreas = new Array;
	if (strElementName.length > 0) {
		elAreas = document.getElementsByName(strElementName);
	}
	if (elAreas.length <= 0) {
		elAreas = document.getElementsByTagName('textarea');
	}
	return elAreas[0];
}

// Move the insertion point to the end of the text area
//
function obls_move_to_end(strElementName)
{
	if (strElementName === undefined) {
		strElementName = '';
	}
	var elTextArea = obls_get_element(strElementName);

	if (elTextArea.selectionStart || elTextArea.selectionStart == '0') {

		// Hope this is Firefox!
		elTextArea.focus();
		var len = elTextArea.value.length;
		elTextArea.selectionStart = len;
		elTextArea.selectionEnd = len;
	
	} else {

		// *** IE or Opera (again, I hope!) ***
		elTextArea.focus();
		var range = elTextArea.createTextRange();
		range.moveEnd('character', elTextArea.value.length);
		range.moveStart('character', elTextArea.value.length);
		range.select();
	}
}

// Apply open/close tags to selected text in the text area
// If there is no selection, sample text is added at the current position
//
// NOTE: this function is mostly cribbed from MediaWiki
//
// strOpen			: the open tag
// strClose			: the close tag
// strSample		: the sample text to use if there is no selection
//
function obls_insert_tags(strOpen, strClose, strSample, strElementName)
{
	// Get the first textarea in the document (perhaps we'll change this to be
	// something more sophisticated in the future!)
	if (strElementName === undefined) {
		strElementName = '';
	}
	var elTextArea = obls_get_element(strElementName);

	var strSelection = '', bUseSample = false;
	var oldScrollPos;

	if (document.selection && document.selection.createRange) {
	
		// *** IE or Opera ***

		// Save window scroll position
		if (document.documentElement && document.documentElement.scrollTop) {
			oldScrollPos = document.documentElement.scrollTop;
		} else if (document.body) {
			oldScrollPos = document.body.scrollTop;
		}
		
		// Get current selection  
		elTextArea.focus();
		var range = document.selection.createRange();
		strSelection = range.text;
		
		// Insert tags
		fnCheckSelectedText();
		range.text = strOpen + strSelection + strClose;
		
		// Mark sample text as selected
		if (bIsSample && range.moveStart) {
			if (window.opera) {
				strClose = strClose.replace(/\n/g,'');
			}
			range.moveStart('character', - strClose.length - strSelection.length); 
			range.moveEnd('character', - strClose.length); 
		}
		range.select();
		
		// Restore window scroll position
		if (document.documentElement && document.documentElement.scrollTop) {
			document.documentElement.scrollTop = oldScrollPos;
		} else if (document.body) {
			document.body.scrollTop = oldScrollPos;
		}

	} else if (elTextArea.selectionStart || elTextArea.selectionStart == '0') {
	
		// *** Mozilla ***

		// Save textarea scroll position
		oldScrollPos = elTextArea.scrollTop;
		
		// Get current selection
		elTextArea.focus();
		var startPos = elTextArea.selectionStart;
		var endPos = elTextArea.selectionEnd;
		strSelection = elTextArea.value.substring(startPos, endPos);
		
		// Insert tags
		fnCheckSelectedText();
		elTextArea.value =
			elTextArea.value.substring(0, startPos)
			+ strOpen + strSelection + strClose
			+ elTextArea.value.substring(endPos, elTextArea.value.length);

		// Set new selection
		if (bIsSample) {
			elTextArea.selectionStart = startPos + strOpen.length;
			elTextArea.selectionEnd = startPos + strOpen.length + strSelection.length;
		} else {
			elTextArea.selectionStart = startPos + strOpen.length + strSelection.length + strClose.length;
			elTextArea.selectionEnd = elTextArea.selectionStart;
		}
		
		// Restore textarea scroll position
		elTextArea.scrollTop = oldScrollPos;
	} 

	function fnCheckSelectedText()
	{
		if (!strSelection) {
			strSelection = strSample;
			bIsSample = true;
		} else if (strSelection.charAt(strSelection.length - 1) == ' ') {
			strSelection = strSelection.substring(0, strSelection.length - 1);
			strClose += ' ';
		} 
	}
}

// Start a new line or paragraph, including any selected text
//
// strTag		: the tag that starts the paragraph
// numCRs		: number of carriage returns to preface with
//
function obls_start_new_line(strTag, numCRs, strElementName)
{
	// Get the first textarea in the document (perhaps we'll change this to be
	// something more sophisticated in the future!)
	if (strElementName === undefined) {
		strElementName = '';
	}
	var elTextArea = obls_get_element(strElementName);

	var oldScrollPos;

	if (document.selection && document.selection.createRange) {
	
		// *** IE or Opera ***
		
		// Save window scroll position
		if (document.documentElement && document.documentElement.scrollTop) {
			oldScrollPos = document.documentElement.scrollTop;
		} else if (document.body) {
			oldScrollPos = document.body.scrollTop;
		}
		
		// Get current selection  
		elTextArea.focus();
		var range = document.selection.createRange();
		var strSelection = range.text;
		
		// We'll put in a rare symbol in order to mark the spot in the string
		// where the selection was; we'll then look up its position to evaluate
		// whether to add CRs
		range.text = '\u0002';
		var strFullText = elTextArea.value;
		var startPos = strFullText.indexOf('\u0002');
		var nTailSize = strFullText.length - startPos;
		
		var strSpacer = '';
		if (numCRs > 0) {
			var ch, i, j, nCRCount = 0;
			for (i = startPos - 1; i >= 0; --i) {
				ch = strFullText.charAt(i);
				if (ch == '\n') {
					++nCRCount;
					if (nCRCount >= numCRs) {
						break;
					}
				} else if (ch != '\r' && ch != '\t' && ch != ' ') {
					for (j = 0; j < numCRs - nCRCount; ++j) {
						strSpacer += '\n';
					}
					break;
				}
			}
		}
		
		// Insert tags
		range.moveStart('character', -1);
		range.text = range.text.replace('\u0002', strSpacer + strTag + strSelection);
		if (window.opera) {
			// Opera freaks out with CR/LF, so we have to detect Opera on Windows
			var strAgent = navigator.userAgent.toLowerCase();
			if (strAgent.indexOf("windows") != -1) {
				range.moveStart('character', strSpacer.length);
			}
		}
		range.select();
	
		// Restore window scroll position
		if (document.documentElement && document.documentElement.scrollTop) {
			document.documentElement.scrollTop = oldScrollPos;
		} else if (document.body) {
			document.body.scrollTop = oldScrollPos;
		}

	} else if (elTextArea.selectionStart || elTextArea.selectionStart == '0') {
	
		// *** Mozilla ***

		// Save textarea scroll position
		oldScrollPos = elTextArea.scrollTop;
		
		// Get current selection
		elTextArea.focus();
		var startPos = elTextArea.selectionStart;
		var endPos = elTextArea.selectionEnd;
		var strFullText = elTextArea.value;
		strSelection = strFullText.substring(startPos, endPos);
		
		var strSpacer = '';
		if (numCRs > 0) {
			var ch, i, j, nCRCount = 0;
			for (i = startPos - 1; i >= 0; --i) {
				ch = strFullText.charAt(i);
				if (ch == '\n') {
					++nCRCount;
					if (nCRCount >= numCRs) {
						break;
					}
				} else if (ch != '\r' && ch != '\t' && ch != ' ') {
					for (j = 0; j < numCRs - nCRCount; ++j) {
						strSpacer += '\n';
					}
					break;
				}
			}
		}
		
		// Insert tags
		elTextArea.value =
			elTextArea.value.substring(0, startPos) + strSpacer + strTag + strSelection
			+ elTextArea.value.substring(endPos, elTextArea.value.length);

		// Set new selection
		elTextArea.selectionStart = startPos + strSpacer.length + strTag.length + strSelection.length;
		elTextArea.selectionEnd = elTextArea.selectionStart;
		
		// Restore textarea scroll position
		elTextArea.scrollTop = oldScrollPos;
	} 
}
