//adds the right tags to the parameter text and returns the changed parameter text
function changeText(type, text) {
	switch (type) 
	{
		case 'smiley':
			text = ' ' + text + ' ';
			break;
		case 'bold':
			text = '[b]' + text + '[/b]';
			break;
		case 'italic':
			text = '[i]' + text + '[/i]';
			break;
		case 'underscore':
			text = '[u]' + text + '[/u]';
			break;
		case 'url':
			if(text == 'http:\/\/') {
				text = '';
			}
			text = '[url]' + text + '[/url]';
			break;
		case 'email':
			text = '[email]' + text + '[/email]';	
			break;
		case 'red':
			text = '[red]' + text + '[/red]';
			break;
		case 'blue':
			text = '[blue]' + text + '[/blue]';
			break;
		case 'green':
			text = '[green]' + text + '[/green]';
			break;
		case 'orange':
			text = '[orange]' + text + '[/orange]';
			break;
		case 'image':
			if(text == 'http:\/\/') {
				text = '';
			}
			text = '[img]' + text + '[/img]';
			break;
		case 'yellow':
			text = '[yellow]' + text + '[/yellow]';
			break;
		default:
			break;
	}
	
	return text;
}

//insert smileys/tags in a textarea with the id 'comment'
function insertText(type, str) {
	var textarea = $('#tx');	// jquery
	//	var textarea = $('tx');	// scriptaculous
	if(!str) {
		str = '';
	}
	//Internet Explorer
	if (document.selection) {
		textarea.focus();
		var cursor = document.selection.createRange();
		var text = (cursor.text == '') ? str : cursor.text;
		if(type == 'smiley' || type == 'url' || type == 'image') {
			text = str;
		}
		cursor.text = changeText(type,text);
	}
	//Mozilla
	else if (typeof(textarea.attr('selectionStart')) != "undefined") {
		var scrollTop = textarea.attr('scrollTop');
		var begin = textarea.attr('selectionStart');
        var end = textarea.attr('selectionEnd');
		// substr substring - in javascript substr arg2=nrchars, substring arg2=endpos
		var text = (type == 'smiley' || type == 'url' || type == 'image') ? str : textarea.attr('value').substring(begin,end);
		text = changeText(type,text);
        textarea.attr('value', textarea.attr('value').substr(0, begin) + text + textarea.attr('value').substr(end));
		var newEnd = begin + text.length;
		if(begin != end) {
			// jquery - to get the "real" dom element, dereference to its first element using [0]
			textarea[0].setSelectionRange(begin, newEnd);
		}
		else {
			textarea[0].setSelectionRange(begin+text.length, newEnd);
		}
        textarea.attr('scrollTop',scrollTop);
	}
	//Safari en andere browsers die niets van cursor positie weten
	else {
		textarea.attr('value', function () {
			 	return this.value+changeText(type,str);
			}
		);
	}

	textarea.focus();
	
	if (typeof textarea.attr('cursorPos') != 'undefined') {
	 	textarea[0].onselect();
	}

}

function showIcon(object,id, number,text1,text2) {
	// scriptaculous Effect.toggle(id, 'appear', { delay: 0.1, duration: 0.5});
	var obj=$('#'+id);
	if (obj.css('display')=='block') {
		obj.fadeOut('slow',function () {});
	}
	else 
		obj.fadeIn('slow',function () {});
}
