var sch_state = 'ctrl'; // vagy 'mce'
var sch_lastctrl = null;
var sch_curpost = 0;


function insertSpecChar( chr )
{
	if( sch_state == 'ctrl' )
	{
		if( sch_lastctrl != null )
		{
			var t = sch_lastctrl.type;
			if( t == "text" || t == "textarea" )
			{
				insertAtCursor( sch_lastctrl, chr );
			}
		}
	} else
	{
		if( tinyMCE.selectedInstance )
		{
			tinyMCE.selectedInstance.execCommand("mceInsertContent", false, chr );
		}
	}
}
// document.writeln( "<script src=js/debug.js></script>" );
function rememberon( e )
{
    obj = e.target || event.srcElement;
	var t = obj.type;
	sch_state = 'ctrl';
	if( t == "text" || t == "textarea" )
	{
		sch_lastctrl = obj;
		sch_curpost = getCursorPosition( obj );
	}
}

function setupblurHander( tag, type )
{
	var inputs = document.getElementsByTagName( tag );
	for( i in inputs )
	{
		if( typeof( inputs[i] ) != "object" ) continue;
		// ezzel elméletileg meg kéne jegyezze a vezérlőt az elhagyásakor
		if( type )
		{
			if( type == inputs[i].type )
			{
				addEvent( inputs[i], "focus", rememberon );
			}
		} else
		{
			addEvent( inputs[i], "focus", rememberon );
		}
	}
}

function testblurfunc()
{
	sch_state = 'mce';
}

function setupTinyMceBlurHandler( inst )
{
	if (tinyMCE.isMSIE)
		var body = inst.getBody();
	else
		var body = inst.getDoc();
	tinyMCE.addEvent( body, "focus", testblurfunc );
	
}


function setupSpecChars()
{
	setupblurHander( 'input', 'text' );
	setupblurHander( 'input', 'checkbox' );
	setupblurHander( 'input', 'radio' );
	setupblurHander( 'select' );
//	setupTinyMceBlurHandler();
//	try{
//	} catch(e) {}
	setupblurHander( 'textarea' );
}

var CONST_IDENT = '    ';
var CONST_NL = "\n";

var CONST_CHARCODE_S = 115;
var CONST_KEYCODE_S = 83;
var CONST_KEYCODE_TAB = 9;
var CONST_KEYCODE_ENTER = 13;

// Insert text before and after the current cursor position
function insertAtCursor(textField, before, after) {
	if (after == null) {
		after = '';
	}
	if (textField.createTextRange) {
		textField.focus();
		var position = getCursorPosition(textField);

		// Adding the before/after text to the textRange
		var range = document.selection.createRange();
		range.text = before+after;

		// Moving the cursor to the right position
		if (before.length) {
			position += before.length;
		}
		setCursorPosition(textField, position);
	} else if (textField.selectionStart ||textField.selectionStart == 0) {
		var scrollTop = textField.scrollTop;
		var startPos = textField.selectionStart;
		var endPos = textField.selectionEnd;
		textField.value = textField.value.substring(0, startPos)
						+ (before + after)
						+ textField.value.substring(endPos, textField.value.length);
		setCursorPosition(textField, startPos+before.length, scrollTop);
	} else {
		var position = textField.value.length+before.length;
		textField.value +=  (before + after);
		setCursorPosition(textField, position);
	}
}


// Get the current cursor position of a text range
function getCursorPosition(textField, MSIEReturnNotLogicalPosition)
{
	// Handling this issue in case of Internet Explorer need a little hack.
	// The problem is, that IE handle the concept of new line different
	// from text field value and textRange "character" unit point of view.
	// In the value the \r,\n charcters exist separately, but when use
	// the textRange.move("charcter", 1) new line is only one step.
	// That's why we need a little correction.
	if (textField.createTextRange) {
		var newLineNumAfterCursor = 0;
		match = textField.value.match(/\n/g);
		if (match) {
			newLineNumAfterCursor = match.length;
		}

		textField.focus();
		var position = textField.value.length;
		var cursor = document.selection.createRange().duplicate();

		while (cursor.parentElement() == textField && cursor.move('character', 1)) {
			if (textField.value.charAt(position - 1) == CONST_NL) {
				position -= 1;
				newLineNumAfterCursor--;
			}
			position--;
		}
		
		// text mezőnél nem kell hozzáadni
		if (MSIEReturnNotLogicalPosition) {
			return position + ( textField.type == "textarea" ? 1 : 0 );
		} else {
			return position + ( textField.type == "textarea" ? 1 : 0 ) - (newLineNumAfterCursor);
		}
	} else if (textField.selectionStart || textField.selectionStart == 0) {
		textField.focus();
		return textField.selectionStart;
	}
}


// Set the current cursor position of a text range
function setCursorPosition(textField, position, scrollTop)
{
	if (textField.createTextRange) {
		var textRange = textField.createTextRange();
		textRange.moveStart("character", position);
		textRange.collapse();
		textRange.select();
	} else if (textField.selectionStart) {
		textField.focus();
		textField.selectionStart = textField.selectionEnd = position;
	}
	if (typeof scrollTop != "undefined" && (textField.scrollTop || textField.scrollTop == 0)) {
		textField.scrollTop = scrollTop;
	}
}

addEvent( window, "load", setupSpecChars );

