// -------------------------------------------------------------------------------
// File: Editor.js
// Version 0.9 beta
// Programmer: Mike H
// Contains unfinished code to implement a BBcode and Emoticon editor.
//-------------------------------------------------------------------------------

var debug =true; 
var EditorId="taReviewText"; //the id of the edit Text area
var CounterId="EdCounter";
var pasted=false;



//handel Text Area Events and adds Selection info the Txt area for Ms stlye use
function getMsStyleSelection(taObj)
{  	
	
	if (taObj.createTextRange)	
		taObj.curSelection = document.selection.createRange().duplicate();
		
	UpdateCharCounter(taObj);
}




//---------------------------------------------
//Event Hndlrs
//--------------------------------------------

/*function taSelect(tArea)
{
	updateMsSelection(tArea);
	//set the char counter counter
	UpdateCharCounter(tArea);
	
}

function taClick(tArea)
{
	updateMsSelection(tArea);
	//set the char counter counter
	UpdateCharCounter(tArea);
}


function taBeforeCut(tArea)
{
	//alert("fired beforecut");
	return true;
}

//handles pastes event for suppored browsers
function taPaste(tArea)
{
	updateMsSelection(tArea);	
	pasted=true;
	
	if (window.clipboardData){
		InsertText(window.clipboardData.getData("Text"));
		UpdateCharCounter(tArea);		
		tArea.unDoText = tArea.curText;
		tArea.curText = tArea.value;
		tArea.canUndo = true;		
		return false; //kill the event, we did it ourselfs
	}//end ie olny code
	else{ //code for none ie browsers that support onpaste: safari
		UpdateCharCounter(tArea);		
		tArea.unDoText = tArea.curText;
		tArea.curText = tArea.value;
		tArea.canUndo = true;		
		return true;
	}
}




function taKeyPress(tArea)
{
	//set the char counter counter
	UpdateCharCounter(tArea);
	
	//updatems stype selection
	updateMsSelection(tArea);
	
	//set the undo 	
	tArea.unDoText = tArea.curText;
	tArea.curText = tArea.value;
	tArea.canUndo = true;
	
	if (debug){
	var spnobj = document.getElementById("spnOut");	
	spnobj.innerText = tArea.unDoText;	
	}
	
}
*/



//updates the counter with the cur len of txtarea.value
function UpdateCharCounter(taObj)
{
	var spnobj = document.getElementById("spnCount");	
	if(spnobj) spnobj.innerText = taObj.value.length;
}


function getSelText() {
	
	var taObj = document.getElementById(EditorId); 
	
	if (taObj.createTextRange && taObj.curSelection)
	{	return taObj.curSelection.text;	} 
	else if (typeof taObj.selectionStart != "undefined")		
		return taObj.value.substr(taObj.selectionStart,taObj.selectionEnd-taObj.selectionStart);
	else	
		return '';
}


function InsertText(text) {
	var taObj = document.getElementById(EditorId);
	
	//undo for inserts of text
	taObj.unDoText=taObj.value;
	
	
	if (typeof taObj.selectionStart != 'undefined'){ // if it supports DOM2
		start = taObj.selectionStart;
		end = taObj.selectionEnd;
		taObj.value = taObj.value.substr(0,taObj.selectionStart)+ text + taObj.value.substr(taObj.selectionEnd);
		taObj.focus();
		taObj.selectionStart = ((start - end) == 0) ? start + text.length : start;
		taObj.selectionEnd = start + text.length;
	} 
	else 
	{
		if (taObj.createTextRange && taObj.curSelection) 
		{
			var curSelection = taObj.curSelection;
			curSelection.text = curSelection.text.charAt(curSelection.text.length - 1) == ' ' ?   text + ' ' : text;
		}
		else
		{
			taObj.value += text;
		}
		taObj.focus(curSelection);		
	}
	
	//more undo logic
	taObj.curText = taObj.value;
	taObj.canUndo = true;
	
	UpdateCharCounter(taObj);	
	
}


/*function unDo()
{
	var taObj = document.getElementById(EditorId);
	//alert(taObj.canUndo);
	if(taObj.canUndo)
	{
		taObj.value = taObj.unDoText;
		taObj.curText = taObj.unDoText;
		
	}
		
	taObj.canUndo = false;
	taObj.focus(taObj.curSelection);		

}*/


//---------------------------------------------------------------\\
//BBcode implementation functions
//---------------------------------------------------------------\\
function bold()
{	
	InsertText("[b]"+ getSelText() +"[/b]");	
}

function underline()
{
	InsertText("[u]"+ getSelText() +"[/u]");
}

function italics()
{
	InsertText("[i]"+ getSelText() +"[/i]");
}

function quote()
{
	InsertText("[quote]"+ getSelText() +"[/quote]");
}


function doAlign(attrib)
{
	InsertText("[align = "+ attrib +"]"+ getSelText() +"[/align]");
}

function color(attrib)
{
	InsertText("[color = "+ attrib +"]"+ getSelText() +"[/color]");
}

function doSize(attrib)
{
	InsertText("[size = "+ attrib +"]"+ getSelText() +"[/size]");
}

//cap all the first letters of the selected text all other text is low case
function FirstCap()
{
	var seltext = getSelText();
	seltext=seltext.toLowerCase();
	InsertText(capFirst(seltext));
}

//private 
function capFirst(objstring) 
{        
	var newVal = '';
	objstring = objstring.split(' ');
	for(var c=0; c < objstring.length; c++) {
		newVal += objstring[c].substring(0,1).toUpperCase() +
		objstring[c].substring(1,objstring[c].length) + ' ';
	}
   return newVal;
}


//caps the sel text
function toUpCase()
{
	InsertText(getSelText().toUpperCase());
}

function toLowCase()
{
	InsertText(getSelText().toLowerCase());
}
