/*
written by meg hourihan
http://www.megnut.com
meg@megnut.com

warning: it only works for IE4+/Win 
feel free to take it for your site
but leave this text in place.
any problems, let meg know.

*/

function mouseover(el) {
	el.className = "raise";
	}

function mouseout(el) {
	el.className = "buttons";
}

function mousedown(el) {
	el.className = "press";
}

function mouseup(el) {
	el.className = "raise";
}

/*

adapted for Moz 1.1a+ (DOM2 Text Ranging)
start of Leonard's edits
http://randomfoo.net/

*/

function shortCuts(evt) {
  if (evt.ctrlKey != true) return;
	if (evt.shiftKey != true) return;

  if(evt.charCode == 65 || evt.charCode == 97) insert_link();
  if(evt.charCode == 116 || evt.charCode == 84) format_me("i");
	if(evt.charCode == 66 || evt.charCode == 98) format_me("b");
}

// global variable for textarea and menubar (i'm lazy, what can i say?)
var ta;
var bar;	

function init() {
  // init global variables
  ta = document.getElementById("comment");
  bar = document.getElementById("formbar");

  if(ta.selectionStart != undefined) { // has support for DOM2 Ranging in textarea 
    ta.addEventListener("keypress", shortCuts, true); // add listener
  } else {
    bar.style.display = "none"; // hide the sucker
  }
}

function format_me(tag) {
  start = ta.selectionStart;
  end = ta.selectionEnd;

  // Split before, sel, after and insert tags in between 
  
  before = (ta.value).substring(0, start);
  sel = (ta.value).substring(start, end);
  after = (ta.value).substring(end, ta.textLength);

  ta.value = before + "<" + tag + ">" + sel + "</" + tag + ">" + after;
  
  // focus
  ta.focus();
  ta.selectionStart = end + 5 + (tag.length * 2);
  ta.selectionEnd = ta.selectionStart; 
}

function insert_link() {
	var my_link = prompt("Enter URL:","http://");
	if (my_link != null) {
    start = ta.selectionStart;
    end = ta.selectionEnd;
  
    // Split before, sel, after and insert tags in between 
    
    before = (ta.value).substring(0, start);
    sel = (ta.value).substring(start, end);
    after = (ta.value).substring(end, ta.textLength);
  
    ta.value = before + "<a href=\"" + my_link +  "\">" + sel + "</a>" + after;
    
    // focus
    ta.focus();
    ta.selectionStart = end + 15 + my_link.length;
    ta.selectionEnd = ta.selectionStart; 

  

		document.selection.createRange().text = "<a href=\"" + my_link + "\">" + str + "</a>";
	}
	return;
}


