// Class 
function EdiTore(textareaId)
{
	// Variables
	this.currentTags = new Array();
	this.textarea = document.getElementById(textareaId);
	
	// Method 
	this.addSmiley = function(smiley)
	{
		// Add a smiley to the end of the text or replace selected text with smiley
		this.replaceSelectedText(smiley);
		return;
	}
	
	// Method
	this.addTags = function(tag)
	{
		var text = "";
		var selectedText = this.getSelectedText();
		
		// Case 1
		// We have selected text, add the tag before and after
		if (selectedText)
		{
			text = this.getStartTag(tag) + selectedText + this.getEndTag(tag);
		}
		
		// Case 2
		// We don't have selected text, add start- or endtag
		else
		{
			// New tag, add it to the array
			if (!this.inCurrentTags(tag))
			{
				this.currentTags.push(tag);
				text = this.getStartTag(tag);
			}
			// Tag exists, close it
			else
			{
				var tempTag = "";
				var moreTags = true;
				while(moreTags)
				{
					tempTag = this.currentTags.pop();
					text += this.getEndTag(tempTag);
					
					if (tempTag == tag)
					{
						moreTags = false;
					}				
				}
			}
		}

		// Do the replace and return
		this.replaceSelectedText(text);
		return;
	}
	
	
	// Method
	this.getStartTag = function(tag)
	{
		return "[" + tag + "]";
	}	
	

	// Method
	this.getEndTag = function(tag)
	{
		return "[/" + tag + "]";		
	}
	
	
	// Method
	this.getSelectedText = function()
	{
		// Get the selection
		var selection = "";
		
		// Gecko and so
		if(this.textarea.setSelectionRange)
		{    
	        // The selected text
	        var start = this.textarea.selectionStart;
	        var end = this.textarea.selectionEnd;
	        selection = this.textarea.value.substring(start, end);
		}
		
		// IE
	    else if(document.selection)
	    { 
	        selection = document.selection.createRange().text;       
	    }
	            
	    return selection;	
	}
	
	
	// Method
	this.replaceSelectedText = function(replace)
	{
		var text = "";
			
		// Gecko and so
		if(this.textarea.setSelectionRange)
		{    
	        // The text before the selection
	        var pretext = this.textarea.value.substring(0, this.textarea.selectionStart);
	    
	        // The text after the selection
	        var posttext = this.textarea.value.substring(this.textarea.selectionEnd, this.textarea.value.length);
	        
	        // Set the hole textarea value (the text)
	        this.textarea.value = pretext + replace + posttext;
	
			// Set caret at the end of 
			this.textarea.focus();
			var position = pretext.length + replace.length;
	        this.textarea.setSelectionRange(position, position);    
		}
		
		// IE
	    else if(document.selection)
	    { 
	  		// Make sure that the textarea is in focus
	    	this.textarea.focus();
	
	    	// Replace the selected text
	    	document.selection.createRange().text = replace;
			
			// Set caret after inserted text
	    	document.selection.createRange().select();
	    }
	    
	    return;	
	}
	
	
	// Method
	this.inCurrentTags = function(value)
	{
		for (var i=0; i<this.currentTags.length; i++)
		{
			if (value == this.currentTags[i])
			{
				return true;
			}
		}
		return false;
	}
}