function getSel() {
   if (window.getSelection)
      return window.getSelection();
   else if (document.getSelection)
      return document.getSelection();
   else if (document.selection)
      return document.selection.createRange().text;
   else return false;
}
function insertTag(startTag,endTag,cursorOffset,offsetFromStart) {
   var ta = document.getElementById("comment");
   if (document.selection) {
      ta.focus();
      sel = document.selection.createRange();
      sel.text = startTag+sel.text+endTag;
      //var dbug = '';
      //for(i in document.selection) {
         //dbug += i+" = "+document.selection[i]+"\n";
      //}
      //alert(dbug);
      ta.focus();
   } else if (ta.selectionStart || ta.selectionStart == 0) {
      var startPos = ta.selectionStart;
      var endPos = ta.selectionEnd;
      var textLength = startTag.length+endTag.length+endPos-startPos;
      ta.value = ta.value.substring(0, startPos) + startTag
         + ta.value.substring(startPos, endPos) + endTag
         + ta.value.substring(endPos, ta.value.length);
      ta.focus();
      var newPos;
      if(offsetFromStart) {
         newPos = startPos+cursorOffset;
      } else {
         newPos = startPos+textLength-cursorOffset;
      }
      ta.selectionStart = newPos;
      ta.selectionEnd = newPos;
   } else {
      ta.value += startTag+endTag;
      ta.focus();
   }
   return false;
   // the quick brown fox jumped over the lazy dog
}
function BBCode_bold() {
   return insertTag("[b]","[/b]",4,false);
}
function BBCode_italic() {
   return insertTag("[i]","[/i]",4,false);
}
function BBCode_url() {
   fadeMessage(document.getElementById("linkBtn"),"[url=http://yourlink.com]Your Link Text[/url]",5500);
   return insertTag("[url=","][/url]",6,false);
}
function BBCode_quote() {
   var quoteBtn = document.getElementById("quoteBtn");
   // First check if text was already selected.
   var sel = getSel();
   if(sel && sel!="") {
      insertTag("[quote=\"\"]",sel+"[/quote]",8,true);
      fadeMessage(quoteBtn,"Fill in who you're quoting");
   } else {
      quoteBtn.innerHTML = "select text (click elsewhere to cancel)";
      document.onmouseup = nextMouseUp;
   }
   return false;
}
function nextMouseUp() {
   document.onmouseup = quoteMouseUp;
}
function obj2str(obj) {
   var out="[";
   for(var i in obj) {
      out += i+"="+obj[i]+", ";
   }   return out+"]";
}
function getPoster(e) {
   if(e==null) return false;
   var commentRow = e.target.parentNode;
   if(commentRow.className.indexOf("commentRow")>=0) {
      var el = commentRow.firstChild.firstChild;
      var count = 5;
      if(el.className=="posterName") {
         var els = el.getElementsByTagName("A");
         if(els.length==1) return els[0].innerHTML;
         else return el.innerHTML;
      }
      return links[0].innerHTML;
   }
   // else, something's wrong!
   return false;
}
function quoteMouseUp() {
   var poster = getPoster(arguments[0]);
   var quoteBtn = document.getElementById("quoteBtn");
   var selection = getSel();
   quoteBtn.innerHTML = "quote";
   if(selection!="") {
      if(poster!=false) insertTag("[quote=\"" + poster + "\"]",selection+"[/quote]",0,false);
      else {
         insertTag("[quote=\"\"]",selection+"[/quote]",6,false);
         fadeMessage(quoteBtn,"Fill in who you're quoting");
      }
   } else {
      fadeMessage(quoteBtn,"Cancelled");
   }
   document.onmouseup = null;
}
function fadeMessage(attachment,text) {
   var delayMS = arguments[2];
   if(typeof(delayMS)=='undefined') delayMS = 2000;
   var msgDiv = document.createElement("div");
   msgDiv.className="fadeMessage";
   msgDiv.innerHTML=text;
   msgDiv.id="message";
   attachment.appendChild(msgDiv);
   setTimeout("fadeDiv('message',10,10)",delayMS);
}
function fadeDiv(divID,countDown,totalSteps) {
   var d = document.getElementById(divID);
   if(document.all) {
      d.style.filter="alpha(opacity="+Math.round(100*countDown/totalSteps)+")";
   } else {
      d.style.opacity=countDown/totalSteps;
   }
   if(countDown>0) {
      setTimeout("fadeDiv('message',"+(--countDown)+","+totalSteps+")",80);
   } else if(countDown==0) {
      d.parentNode.removeChild(d);
   } 
}

