function storeCaret(text) {
  // Only bother if it will be useful.
  if (typeof(text.createTextRange) != "undefined")
    text.caretPos = document.selection.createRange().duplicate();
}
function replaceText(text, textarea) {
  // Attempt to create a text range (IE).
  if (typeof(textarea.caretPos) != "undefined" && textarea.createTextRange) {
    var caretPos = textarea.caretPos;

    caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? text + ' ' : text;
    caretPos.select();
  } else if (typeof(textarea.selectionStart) != "undefined") {
    var begin = textarea.value.substr(0, textarea.selectionStart);
    var end = textarea.value.substr(textarea.selectionEnd);
    var scrollPos = textarea.scrollTop;

    textarea.value = begin + text + end;

    if (textarea.setSelectionRange) {
      textarea.focus();
      textarea.setSelectionRange(begin.length + text.length, begin.length + text.length);
    }
    textarea.scrollTop = scrollPos;
  } else {
    textarea.value += text;
    textarea.focus(textarea.value.length - 1);
  }
}
// Surrounds the selected text with text1 and text2.
function surroundText(text1, text2, textarea) {
  // Can a text range be created?
  if (typeof(textarea.caretPos) != "undefined" && textarea.createTextRange)
  {
    var caretPos = textarea.caretPos, temp_length = caretPos.text.length;

    caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? text1 + caretPos.text + text2 + ' ' : text1 + caretPos.text + text2;

    if (temp_length == 0)
    {
      caretPos.moveStart("character", -text2.length);
      caretPos.moveEnd("character", -text2.length);
      caretPos.select();
    }
    else
      textarea.focus(caretPos);
  } else if (typeof(textarea.selectionStart) != "undefined") {
    var begin = textarea.value.substr(0, textarea.selectionStart);
    var selection = textarea.value.substr(textarea.selectionStart, textarea.selectionEnd - textarea.selectionStart);
    var end = textarea.value.substr(textarea.selectionEnd);
    var newCursorPos = textarea.selectionStart;
    var scrollPos = textarea.scrollTop;
    textarea.value = begin + text1 + selection + text2 + end;
    if (textarea.setSelectionRange) {
      if (selection.length == 0)
        textarea.setSelectionRange(newCursorPos + text1.length, newCursorPos + text1.length);
      else
        textarea.setSelectionRange(newCursorPos, newCursorPos + text1.length + selection.length + text2.length);
      textarea.focus();
    }
    textarea.scrollTop = scrollPos;
  } else {
    textarea.value += text1 + text2;
    textarea.focus(textarea.value.length - 1);
  }
}
function textformyap(textform){
  $('#gulucukler').hide();
  $('.tbi').click(function(){
    $('#gulucukler').toggle();
  });
  $('.tba').click(function(){
    var expose = new Array();
    expose = $(this).attr('title').split('-');
    surroundText(expose[0], expose[1], textform);
    return false;
  });
  $('.tbc').click(function(){
    var expose = new Array();
    expose = $(this).attr('title').split('-');
    var bil=prompt(expose[2],'http://');
    if (bil==null || bil=='http://' || bil=='') {
      surroundText(expose[0]+']', expose[1], textform);
    } else {
      surroundText(expose[0]+']'+bil, expose[1], textform);
    }
    return false;
  });
  $('.tbd').click(function(){
    var expose = new Array();
    expose = $(this).attr('title').split('-');
    var bil=prompt(expose[2],'');
    if (bil==null || bil=='') {
      surroundText(expose[0]+']', expose[1], textform);
    } else {
      surroundText(expose[0]+'='+bil+']', expose[1], textform);
    }
    return false;
  });
  $('.tbs').click(function(){
    var expose = $(this).attr('title');
    replaceText(expose, textform);
    return false;
  });
  $('#onizle').click(function(){
    var params = {};
    $('#izleme').html('<img src="/ajax-loader.gif">Önizleme hazırlanıyor');
    adresi = $('#yorumform').attr('action');
    $('#yorumform').find("input[@type='text'], input[@type='hidden'],textarea").each(function() { params[ this.name ] = this.value; });
    params[ 'onizleme' ] = '1';
    $.post(adresi, params, function(data){ $('#izleme').html(data); });
    return false;
  });
}
$(document).ready(function (){
  textformyap(document.forms.yorumform.message);
});

