var teButtons = TextileEditor.buttons;

teButtons.push(new TextileEditorButton('ed_strong',			'bold.png',          '*',   '*',  'b', 'Bold','s'));
teButtons.push(new TextileEditorButton('ed_emphasis',		'italic.png',        '_',   '_',  'i', 'Italicize','s'));
// teButtons.push(new TextileEditorButton('ed_underline',	'underline.png',     '+',   '+',  'u', 'Underline','s'));
// teButtons.push(new TextileEditorButton('ed_strike',     'strikethrough.png', '-',   '-',  's', 'Strikethrough','s'));
teButtons.push(new TextileEditorButton('ed_ol',					'list_numbers.png',  ' # ', '\n', ',', 'Numbered List'));
teButtons.push(new TextileEditorButton('ed_ul',					'list_bullets.png',  ' * ', '\n', '.', 'Bulleted List'));
teButtons.push(new TextileEditorButton('ed_p',					'paragraph.png',     'p',   '\n', 'p', 'Paragraph'));
// teButtons.push(new TextileEditorButton('ed_h1',					'h1.png',            'h1',  '\n', '1', 'Header 1'));
// teButtons.push(new TextileEditorButton('ed_h2',					'h2.png',            'h2',  '\n', '2', 'Header 2'));
// teButtons.push(new TextileEditorButton('ed_h3',					'h3.png',            'h3',  '\n', '3', 'Header 3'));
// teButtons.push(new TextileEditorButton('ed_h4',					'h4.png',            'h4',  '\n', '4', 'Header 4'));
// teButtons.push(new TextileEditorButton('ed_block',   		'blockquote.png',    'bq',  '\n', 'q', 'Blockquote'));
// teButtons.push(new TextileEditorButton('ed_outdent', 		'outdent.png',       ')',   '\n', ']', 'Outdent'));
// teButtons.push(new TextileEditorButton('ed_indent',  		'indent.png',        '(',   '\n', '[', 'Indent'));
//teButtons.push(new TextileEditorButton('ed_justifyl',		'left.png',          '<',   '\n', 'l', 'Left Justify'));
//teButtons.push(new TextileEditorButton('ed_justifyc',		'center.png',        '=',   '\n', 'e', 'Center Text'));
//teButtons.push(new TextileEditorButton('ed_justifyr',		'right.png',         '>',   '\n', 'r', 'Right Justify'));
//teButtons.push(new TextileEditorButton('ed_justify', 		'justify.png',       '<>',  '\n', 'j', 'Justify'));

// teButtons.push(new TextileEditorButton('ed_code','code','@','@','c','Code'));

//teButtons.push(new TextileEditorButton('ed_leftpic', 'image_left.png', 
//	" <div class='picleft' style='width:0px;'>\n ", " \n <p class='piccredit'>CREDIT</p>\n <p class='picdesc'>DESCRIPTION</p>\n </div>", '', 
//	'Float Picture Left')); 
teButtons.push("<ul id='headings'><li>Heading</li><li><ul><li><button id='ed_h1' class='standard' tagstart='h1' tagend='' open='undefined' accesskey='1' title='Header 1'><img src='/images/textile-editor/h1.png'/></button></li><li><button id='ed_h2' class='standard' tagstart='h2' tagend='' open='undefined' accesskey='2' title='Header 2'><img src='/images/textile-editor/h2.png'/></button></li><li><button id='ed_h3' class='standard' tagstart='h3' tagend='' open='undefined' accesskey='3' title='Header 3'><img src='/images/textile-editor/h3.png'/></button></li><li><button id='ed_h4' class='standard' tagstart='h4' tagend='' open='undefined' accesskey='4' title='Header 4'><img src='/images/textile-editor/h4.png'/></button></li></ul></li></ul>");
//teButtons.push("<select id='headings'><option id='ed_h1' tagstart='h1' tagend='' open='undefined' accesskey='1' title='Header 1'>Heading</option><option id='ed_h2' tagstart='h2' tagend='' open='undefined' accesskey='2' title='Header 2'>H1</option><option>H2</option><option id='ed_h3' tagstart='h3' tagend='' open='undefined' accesskey='3' title='Header 3'>H3</option><option id='ed_h4' tagstart='h4' tagend='' open='undefined' accesskey='4' title='Header4'>H4</option></select>");
//teButtons.push("<select id='headings'><option>Headings</option><option><button id='ed_h1' class='standard' tagstart='h1' tagend='' open='undefined' accesskey='1' title='Header 1'>H1</button></option><option><button id='ed_h2' class='standard' tagstart='h2' tagend='' open='undefined' accesskey='2' title='Header 2'>H2</button></option></select>");
teButtons.push("<button id=\"ed_block\" tagstart=\"bq\" tagend=\"\" open=\"undefined\" accesskey=\"q\" title=\"Quote\">Quote</button>");	
teButtons.push("<button id=\"ed_link\" onclick=\"addTextileLink(this, 'link');return false;\">Link</button>");	
// teButtons.push("<button id=\"ed_email\" onclick=\"addTextileLink(this, 'email');return false;\">Email</button>");	
teButtons.push("<button id=\"ed_image\" onclick=\"addTextileLink(this, 'image');return false;\">Image</button>");	
//teButtons.push("<button id=\"ed_hand_converter\" onclick=\"new Ajax.Updater(\'formatting-help\', \'/blog_entries/show_hand_converter\', {asynchronous:true, evalScripts:true});return false;\">Hand Converter</button>");	
teButtons.push("<button id=\"ed_hand_converter\" onclick=\"$('convert_hand_window').show(); return false;\">Hand Converter</button>");	

	// Link callback function. Adds url or mailto link
	// based on id of 'link' or 'email', respectively
	// modified by ben from code found here http://expressionengine.com/forums/viewthread/54264/
	function addTextileLink(button, id)
	{
		var myField = document.getElementById(button.canvas.id);
		myField.focus();

		// Selection testing straight from TEH script 
		var textSelected = false;
		var FF = false;

		// grab the text that's going to be manipulated, by browser
		if (document.selection) { // IE support
			sel = document.selection.createRange();

			// set-up the text vars
			var beginningText = '';
			var followupText = '';
			var selectedText = sel.text;

			// check if text has been selected
			if (sel.text.length > 0) {
				textSelected = true;	
			}

			// check if text has been selected
			if (sel.text.length > 0) {
				textSelected = true;	
			}
		}
		else if (myField.selectionStart || myField.selectionStart == '0') { // MOZ/FF/NS/S support
			// figure out cursor and selection positions
			var startPos = myField.selectionStart;
			var endPos = myField.selectionEnd;
			var cursorPos = endPos;
			var scrollTop = myField.scrollTop;
			FF = true; // note that is is a FF/MOZ/NS/S browser

			// set-up the text vars
			var beginningText = myField.value.substring(0, startPos);
			var followupText = myField.value.substring(endPos, myField.value.length);

			// figure out cursor and selection positions
			var startPos = myField.selectionStart;
			var endPos = myField.selectionEnd;

			// check if text has been selected
			if (startPos != endPos) {
				textSelected = true;
			}
		} // End selection testing

		var FinalText = '';

		// Prompt user
		switch(id) {
			case 'link':
				var link = prompt("Enter a URL:", "http://");
				if (link == "http://" || link == "" || link == null) return false;

				if (textSelected) {
					link = '"'+myField.value.substring(startPos, endPos)+'":'+link;	
				} else {
					link = '"Text Here":'+link+' ';
				}

				break;

			case 'email':
				var link = prompt("Enter an email address:");
				if (link == "" || link == null) return false;

				// Check for encoding option and build link
				// if (teh_options.encode_email === 'yes')
				// {
					if (textSelected) {
						link = '['+'email='+link+']'+myField.value.substring(startPos, endPos)+'[/email]';
					} else {
						link = '[email]'+link+'[/email]';
					}
				// } else {
				// 	if (textSelected) {
				// 		link = '"'+myField.value.substring(startPos, endPos)+'":'+link;
				// 	} else {
				// 		link = '"'+link+'":mailto:'+link;
				// 	}
				// }

			case 'image':
				var link = prompt("Enter image url:");
				if (link == "" || link == null) return false;

				// Check for encoding option and build link
				// if (teh_options.encode_email === 'yes')
				// {
					if (textSelected) {
						link = '!'+link+'('+myField.value.substring(startPos, endPos)+')!';
					} else {
						link = '!'+link+'!';
					}
				// } else {
				// 	if (textSelected) {
				// 		link = '"'+myField.value.substring(startPos, endPos)+'":'+link;
				// 	} else {
				// 		link = '"'+link+'":mailto:'+link;
				// 	}
				// }

				break;
			default:
				return false;
				break;
		}

		// set the appropriate DOM value with the final text
		finalText = beginningText+link+followupText;
		if (FF == true) {
			myField.value = finalText;
			myField.scrollTop = scrollTop;
		}
		else {
			sel.text = finalText;
		}

		button.className = 'unselected';
	}
	
	function addTextileText(button, text)
	{
		var myField = document.getElementById(button.canvas.id);
		myField.focus();

		// Selection testing straight from TEH script 
		var textSelected = false;
		var FF = false;

		// grab the text that's going to be manipulated, by browser
		if (document.selection) { // IE support
			sel = document.selection.createRange();

			// set-up the text vars
			var beginningText = '';
			var followupText = '';
			var selectedText = sel.text;

			// check if text has been selected
			if (sel.text.length > 0) {
				textSelected = true;	
			}

			// check if text has been selected
			if (sel.text.length > 0) {
				textSelected = true;	
			}
		}
		else if (myField.selectionStart || myField.selectionStart == '0') { // MOZ/FF/NS/S support
			// figure out cursor and selection positions
			var startPos = myField.selectionStart;
			var endPos = myField.selectionEnd;
			var cursorPos = endPos;
			var scrollTop = myField.scrollTop;
			FF = true; // note that is is a FF/MOZ/NS/S browser

			// set-up the text vars
			var beginningText = myField.value.substring(0, startPos);
			var followupText = myField.value.substring(endPos, myField.value.length);

			// figure out cursor and selection positions
			var startPos = myField.selectionStart;
			var endPos = myField.selectionEnd;

			// check if text has been selected
			if (startPos != endPos) {
				textSelected = true;
			}
		} // End selection testing

		var FinalText = '';

		// set the appropriate DOM value with the final text
		finalText = beginningText + text + followupText;
		if (FF == true) {
			myField.value = finalText;
			myField.scrollTop = scrollTop;
		}
		else {
			sel.text = finalText;
		}

		button.className = 'unselected';
	}