// techtile 
// text
// setup
undo_steps = new Array ();
return_cursor = new Array();
// make sure selection model available to all functions.
select_start = 0;
select_end = 0;
gCursor = null;

// dollar function
// function $() {
//  var elements = new Array();
//  for (var i = 0; i < arguments.length; i++) {
//    var element = arguments[i];
//    if (typeof element == 'string')
//      element = document.getElementById(element);
//    if (arguments.length == 1)
//      return element;
//    elements.push(element);
//  }
//  return elements;
// }

function techtile_text_get_selection_text (myInput) {
	// update global selection model
	var aBlocks = null;
	select_start = myInput.selectionStart;
	select_end = myInput.selectionEnd;
	gCursor = select_end;
	
	all_the_text = myInput.value;
	var selected_text = (myInput.value).substring(select_start, select_end); 

	if (select_start == null) {
		// this is for IE
			selection_obj = document.selection.createRange();
			selected_text = selection_obj.text;
			if(selection_obj.parentElement() == myInput) { 
				var bookmark = "~123BooksMark098~";
				selection_obj.text = bookmark;
				cursor  = myInput.value.search( bookmark );
				myInput.value = myInput.value.replace(bookmark,[selected_text]);
				select_start = cursor;
				select_end = cursor + selected_text.length;
			}
			if (selected_text=="") {
				aBlocks = all_the_text.split("\r\n");
			}
	}  
	if (selected_text=="") {
		// it's not a selection.  Apply method to block instead.
		if (!cursor) {var cursor = myInput.selectionStart;}
		//
		// find start of block.  Will be two /n's n a row, or beginning of text whichever comes first.
		// first create a micro dom, an array of blocks.
		if (aBlocks == null) {
			aBlocks = all_the_text.split("\n\n");
		}
		
		// then find out which block we are in.
		var chars = 0;
		for (i=0; i < aBlocks.length; i++) {
			chars += aBlocks[i].length +2;
			
			if (chars>cursor) {
				selected_block = aBlocks[i];
				select_start = chars-aBlocks[i].length-2;
				select_end = chars-2;
				break;					
			}
		}
		// selected text is substring start to middle.
		selected_text = all_the_text.substring(select_start, select_end);
	}
	gCursor = cursor; // add globally accessible cursor property
	return (selected_text);
}

function techtile_text_update_input (myInput, text_middle){
	text_top = (myInput.value).substring(0, select_start); 
	text_bottom = (myInput.value).substring(select_end, myInput.value.length); 
	result_text = text_top + text_middle + text_bottom;
	myInput.value = result_text;
	return (result_text);
}

function techtile_text_make_strong (myInput,myForm) {
	myInput = $(myInput);
	all_the_text=myInput.value;
	// get selection
	selected_text = techtile_text_get_selection_text (myInput);
	// check for tags
	if (techtile_text_check_for_tags ()) {
		// add undo
		techtile_text_add_undo_step (all_the_text);
		// update text field
		new_text = "*" + selected_text + "*";
		techtile_text_update_input (myInput, new_text);
		// update "preview" and refocus
		techtile_text_return_focus(myInput);
	}
}


function techtile_text_make_em (myInput,myForm) {
	all_the_text=myInput.value;
	// get selection
	selected_text = techtile_text_get_selection_text (myInput);
	// check for tags
	if (techtile_text_check_for_tags ()) {
		// add undo
		techtile_text_add_undo_step (all_the_text);
		// update text field
		new_text = "_" + selected_text + "_";
		techtile_text_update_input (myInput, new_text);
		// update "preview" and refocus
		techtile_text_return_focus(myInput);
	}
}

function techtile_text_make_link (myInput,myForm) {
	all_the_text=myInput.value;
	// get selection
	selected_text = techtile_text_get_selection_text (myInput);
	// check for tags
	var myUrl = prompt("Please enter link location.","http\:\/\/");
	
	if (techtile_text_check_for_tags () && myUrl.length > 7) {
		// add undo
		techtile_text_add_undo_step (all_the_text);
		// update text field
		new_text = "\"" + selected_text + "\"\:" + myUrl;
		techtile_text_update_input (myInput, new_text);
		// update "preview" and refocus
		techtile_text_return_focus(myInput);
	}
}

function techtile_text_make_bullets (myInput,myForm) {
	all_the_text=myInput.value;
	// get selection
	selected_text = techtile_text_get_selection_text (myInput);
	// check for tags		
	if (techtile_text_check_for_tags ()) {
		// add undo
		techtile_text_add_undo_step (all_the_text);
		// update text field
		aBlocks = selected_text.split("\n");
		var aGoodBlocks = new Array;
		for (i=0; i < aBlocks.length; i++) {
			if (aBlocks[i].length > 0) {	
				aGoodBlocks.push(aBlocks[i]);
				}
			}
		new_text = "\* " + aGoodBlocks.join("\n\* ");
		techtile_text_update_input (myInput, new_text);
		// update "preview" and refocus
		techtile_text_return_focus(myInput);
	}
}

function techtile_text_make_h (myInput,myForm,n) {
	
	all_the_text=myInput.value;
	// get selection
	selected_text = techtile_text_get_selection_text (myInput);
	// check for tags to toggle h styles instead of simply append

	if (techtile_text_check_for_tags ()) {
		// add undo
		techtile_text_add_undo_step (all_the_text);
		// update text field
				
	text_start = selected_text.substring(0,4);
	if (text_start=="h1. " ||  text_start=="h2. " || text_start=="h3. " ||  
	text_start=="h4. " ||  text_start=="h5. " ||  text_start=="h6. ") {  
		selected_text = selected_text.substring(4,selected_text.length);
				}
	
		new_text = "h" + n + ". " + selected_text;
		techtile_text_update_input (myInput, new_text);
		// update "preview" and refocus
		techtile_text_return_focus(myInput);
	}
}

function techtile_text_return_focus(myInput) {
	if (!myInput.selectionStart) {
		// is IE, do nothing.
	}
	else {
		if (select_end > myInput.length) {
			select_end = myInput.length;
		}
		myInput.selectionStart = select_end;
		myInput.selectionEnd = select_end;
		myInput.focus();
	}
}

function techtile_text_check_for_tags (tag) {
	// 2.0 if there's a bolding tag, look for closes, make sure they match up in number.
	//     If they don't then determine which we are missing.  add to beginning such that it closes it.
	//     For now, just return true.  Work on this when we get a chance.
	return (true);
}

function techtile_text_update_preview () {

}

function techtile_text_add_undo_step (theText) {

	aRay = new Array ();
	aRay.push(theText);
	undo_steps = aRay.concat(undo_steps);
	aRay2 = new Array ();
	aRay2.push(gCursor);
	return_cursor = aRay2.concat(return_cursor);

}

function techtile_text_undo (myInput) {
  if (undo_steps.length> 0)  {
		 myInput.value = undo_steps.shift();
		 techtile_text_return_focus(myInput);
	 }	
}
