/*
 * textRange: ブラウザ上での選択範囲操作に関する機能を提供
 * Copyright (c) 2003- Seesaa CO.,LTD. All rights reserved.
 * 
 * mailto: hanabusa@seesaa.jp
 * url: http://seesaa.jp/
*/


function textRange (srcElement) {
	this.srcElement   = srcElement;
	if ( document.selection && document.selection.createRange ) {
		this.focus();
		this.range = document.selection.createRange();
	}
	else if ( typeof(srcElement.selectionStart) == 'number' ) {
		this.position  =
			new Array(srcElement.selectionStart, srcElement.selectionEnd);
	}

	return this;
}


textRange.prototype.focus = function () {
	if ( this.srcElement && this.srcElement.focus )
		this.srcElement.focus();
}


textRange.prototype.text = function () {
	if ( this.range ) {
		return this.range.text;
	}
	else if ( this.position ) {
		return this.srcElement.value.substring(this.position[0], this.position[1]);
	}
	return null;
}


textRange.prototype.insert = function (str) {
	var txt = this.text();
	if ( txt == null ) {
		this.srcElement.value += str;
	}
	else {
		var len_txt = txt.length;
		var len_str = str.length;
		if ( this.range ) {
			var ret = txt.match(/\r\n|\r|\n/g);
			if ( ret ) len_txt -= ret.length;
			this.range.text = str + txt;
			this.range.move('character', - len_txt);
			this.range.moveEnd('character', len_txt);
			this.range.select();
		}
		else if ( this.position ) {
			this.srcElement.value =
				this.srcElement.value.substring(0, this.position[0]) +
				str + txt +
				this.srcElement.value.substring(this.position[1], this.srcElement.value.length);
			this.srcElement.selectionStart = this.position[0] + len_str;
			this.srcElement.selectionEnd = this.position[1] + len_str;
		}
	}
	this.focus();
}


textRange.prototype.insertTag = function (startTag, endTag) {
	var txt = this.text();
	if ( txt == null ) {
		this.srcElement.value += startTag + endTag;
	}
	else {
		var len_txt  = txt.length;
		var len_stag = startTag.length;
		var len_etag = endTag.length;
		if ( this.range ) {
			var ret = txt.match(/\r\n|\r|\n/g);
			if ( ret ) len_txt -= ret.length;
			this.range.text = startTag + txt + endTag;
			this.range.move('character', - (len_txt + len_etag));
			this.range.moveEnd('character', len_txt);
			this.range.select();
		}
		else if ( this.position ) {
			this.srcElement.value =
				this.srcElement.value.substring(0, this.position[0]) +
				startTag + txt + endTag +
				this.srcElement.value.substring(this.position[1], this.srcElement.value.length);
			this.srcElement.selectionStart = this.position[0] + len_stag;
			this.srcElement.selectionEnd = this.position[1] + len_stag;
		}
	}
	this.focus();
}


textRange.prototype.replace = function (str) {
	var txt = this.text();
	if ( txt == null ) {
		this.srcElement.value += str;
	}
	else {
		var len_txt = txt.length;
		var len_str = str.length;
		if ( this.range) {
			this.range.text = str;
			var ret = txt.match(/\r\n|\r|\n/g);
			if ( ret ) len_txt -= ret.length;
			this.range.text = str + txt;
			this.range.move('character', - len_txt);
			this.range.moveEnd('character', len_txt);
			this.range.select();
		}
		else if ( this.position ) {
			this.srcElement.value =
				this.srcElement.value.substring(0, this.position[0]) +
				str +
				this.srcElement.value.substring(this.position[1], this.srcElement.value.length);
			this.srcElement.selectionStart = this.position[0] + len_str;
			this.srcElement.selectionEnd = this.position[1] + len_str;
		}
	}
	this.focus();
}
