var CaretPoint = Class.create();

CaretPoint.prototype = {
	
	_start: 0,
	_end:   0,
	_textAreaElement: new Object(),
	
	initialize: function() {
	},
	
	startAndEndPoint: function(textAreaElement) {
		this._textAreaElement = textAreaElement;
		if (document.selection != undefined) {//IE
			this.ieStartAndEndPoint();
		} else if (textAreaElement.selectionStart != undefined || window.getSelection()) {
			this._start = this._textAreaElement.selectionStart;
			this._end   = this._textAreaElement.selectionEnd;
		} else {//Other
			this._start = this._end = textAreaElement.toString().length;
		}
	},
	
	ieStartAndEndPoint: function() {
		this._textAreaElement.focus();

		var docRange = document.selection.createRange();
		var txtRange = document.body.createTextRange();
		txtRange.moveToElementText(this._textAreaElement);
		
		var range = txtRange.duplicate();
		range.setEndPoint('EndToStart', docRange);
		var start = range.text.length;

		var range = txtRange.duplicate();
		range.setEndPoint('EndToEnd', docRange);
		var end = range.text.length;

		this._start = start;
		this._end   = end;
	},
	
	getStart: function() {
		return _start;
	},
	
	getEnd: function() {
		return _end;
	}
}
var caretPoint = new CaretPoint();

/*-----------------------------------------------------------------------------*/

var ItemInfoForm = Class.create();
ItemInfoForm.prototype = {

	typeIs: 'none',
	delay: 200,
	intervalID: '',
	textAreaElement: '',
	inputTextName: '',
	formType: '',
	
	initialize: function() {
	},
	
	show: function(textAreaName, formType) {
		this.textAreaElement = document.getElementsByName(textAreaName)[0];
		caretPoint.startAndEndPoint(this.textAreaElement);
		this.formType = formType;
		
		setTimeout('openItemInfoForm()', this.delay);
	},
	
	hide: function() {
		iteminfo.deactivate();
	},
	
	submit: function(radioName) {
		var itemValue = this.getItemValue();
		var tagTypeValue = this.getTagTypeValue(radioName);
		if (tagTypeValue == 1)
			itemValue = '[[' + itemValue + ']]';
		else
			itemValue = '{{' + itemValue + '}}';
		
		this.replaceNewValue(itemValue);
		this.hide();
		this.reset();
	},
	
	getItemValue: function() {
		var itemValue = document.getElementsByName(this.inputTextName)[0].value;
		return itemValue;
	},
	
	getTagTypeValue: function(radioName) {
		var radioObj  = document.getElementsByName(radioName);
		if (radioObj[0].checked)
			return 1;
		else
			return 2;
	},
	
	replaceNewValue: function(newValue) {
		var textAreaValue = this.textAreaElement.value;
		var p1 = textAreaValue.substring(0, caretPoint._start);
		var p2 = p1.length < textAreaValue.length ? textAreaValue.substring(caretPoint._end, textAreaValue.length) : '';
		var newTextAreaValue = p1 + newValue + p2;
		
		this.textAreaElement.value = newTextAreaValue;
	},
	
	reset: function() {
		this.inputTextName = '';
		caretPoint._start = 0;
		caretPoint._end = 0;
	},
	
	selectedText: function(inputTextName) {
		this.inputTextName = inputTextName;

		document.getElementsByName(this.inputTextName)[0].focus();

		var s = caretPoint._start;
		var e = caretPoint._end;
		if (s == e) return '';

		var text = this.textAreaElement.value;
		var selectedText = text.substring(s, e);

		return selectedText;
	},
	
	getFormType: function() {
		return this.formType;
	}

}
var iif = new ItemInfoForm();

/*-----------------------------------------------------------------------------*/

function openItemInfoForm() {
	iteminfo.activate();
}

function showItemInfoForm(textAreaName, formType) {
	iif.show(textAreaName, formType);
}

function hideItemInfoForm() {
	iif.hide();
}

function setSelectedText(inputTextName) {
	inputText = document.getElementsByName(inputTextName)[0];
	inputText.value = iif.selectedText(inputTextName);
}

function switchItemInfoForm(formType) {
	switch (formType) {
		case 'popup':
			$('itemInfoFormPopup').style.display = 'block';
			document.getElementsByName('tagType')[1].checked = true;
		break;
		case 'direct':
			$('itemInfoFormDirect').style.display = 'block';
			document.getElementsByName('tagType')[0].checked = true;
		break;
		default:
			$('itemInfoFormNormal').style.display = 'block';
			document.getElementsByName('tagType')[0].checked = true;
	}
}

function initItemInfoForm(inputTextName) {
	setSelectedText(inputTextName);
	switchItemInfoForm(iif.getFormType());
}

function submitItemInfoForm(radioName) {
	iif.submit(radioName);
}

/*

var itemInfoFormTypeIs = 'none';
var itemInfoFormInterval = 200;
var itemInfoFormIntervalID;
var itemInfoFormItemName = '';

function openItemInfoForm() {
	iteminfo.activate();
}

function closeItemInfoForm() {
	iteminfo.deactivate();
}

function switchItemInfoForm(infoFormType) {
	switch (itemInfoFormTypeIs) {
		case 'direct':
			$('itemInfoFormPopup').style.display = 'none';
		break;
		case 'popup':
			$('itemInfoFormDirect').style.display = 'none';
		break;
	}
}

function setSelectedText(inputTextName) {

	switchItemInfoForm();

	var s = caretPoint._start;
	var e = caretPoint._end;
	if (s == e) return;

	var text = caretPoint._textAreaElement.value;
	var selectedText = text.substring(s, e);

	itemInfoFormItemName = inputTextName;
	var inputText = document.getElementsByName(itemInfoFormItemName)[0];
	inputText.value = selectedText;
}

function showItemInfoForm(textAreaName, formType) {
	iif.show(textAreaName, formType);
	/*var textAreaElement = document.getElementsByName(textAreaName)[0];
	caretPoint.startAndEndPoint(textAreaElement);
	itemInfoFormTypeIs = formType || 'none';
	
	itemInfoFormIntervalID = setInterval('openItemInfoForm()', itemInfoFormInterval);
}

function hideItemInfoForm() {
	closeItemInfoForm();
}

function submitItemInfoForm(radioName) {
	
	var textValue = document.getElementsByName(itemInfoFormItemName)[0].value;
	itemInfoFormItemName = '';
	
	if (textValue == '') {
		closeItemInfoForm();
		return;
	}

	var radioObj  = document.getElementsByName(radioName);
	if (radioObj[0].checked || radioObj[1].checked) {
		if (radioObj[0].checked) {
			textValue = '[[' + textValue + ']]';//direct
		} else {
			textValue = '{{' + textValue + '}}';//popup
		}
	}
	
	var targetText = caretPoint._textAreaElement.value;
	var p1 = targetText.substring(0, caretPoint._start);
	var p2 = p1.length < targetText.length ? targetText.substring(caretPoint._end, targetText.length) : '';

	targetText = p1 + textValue + p2;
	
	caretPoint._textAreaElement.value = targetText;

	closeItemInfoForm();
}*/