
$(function(){
  $("textarea.wikitext").each(function(){
    text = $(this);
    textId = text.attr('id');

    s  = "<div id='WikiLinkToolBar" + textId + "'>";

    s += "<a targetId='" + textId + "' name='WikiLink" + textId + "' href='#WikiLink" + textId + "' id='WikiLink" + textId + "' class='wikilinkmaker-a'><img title='WikiLink' src='${href()}/chrome/wikilinkmaker/Link_Wiki_button.png' class='wikilinkmaker-button' /></a>";

    s += "<a targetId='" + textId + "' name='RepositoryLink" + textId + "' href='#RepositoryLink" + textId + "' id='RepositoryLink" + textId + "' class='wikilinkmaker-a' ><img src='${href()}/chrome/wikilinkmaker/Link_Repo_button.png' class='wikilinkmaker-button' /></a>";
    s += "</div>";
    $(this).before(s);

    $("a#WikiLink" + textId ).click(function(){
      targetId = $(this).attr('targetId');
      target = $("textarea#" + targetId);

      closebox = $.floatbox({
        content: "<div style='padding:5px 10px'><div style='text-align:left;float:left'><strong >Select Page</strong></div><div style='text-align:right;'><a role='button' href='javascript:void(0);' class='close-floatbox'>Close</a></span></div><div id='WikiLinkPanel" + targetId + "' style='padding-top:10px;padding-left:10px;overflow:auto;height:380px'/>",
        fade: true,
        button : "<span/>"
      });
      $('#WikiLinkPanel' + targetId ).fileTree({
         root: '',
         script: '${href()}/WikiLinkMaker/wiki'
       }, function(file) {
         name = file.split("/").pop();
         val = target.val();         
         index = getCurPosition(targetId);
         val = val.substr(0,index) + '[wiki:"' + file + '" ' + name + ']' + val.substr(index);
         target.val(val);
         target.focus();
         closebox();
       });
    });

    $("a#RepositoryLink" + textId ).click(function(){
      targetId = $(this).attr('targetId');
      target = $("textarea#" + targetId);

      closebox = $.floatbox({
        content: "<div style='padding:5px 10px'><div style='text-align:left;float:left'><strong >Select Page</strong></div><div style='text-align:right;'><a role='button' href='javascript:void(0);' class='close-floatbox'>Close</a></span></div><div id='RepositoryLinkPanel" + targetId + "' style='padding-top:10px;padding-left:10px;overflow:auto;height:380px'/>",
        fade: true,
        button : "<span/>"
      });
      $('#RepositoryLinkPanel' + targetId).fileTree({
         root: '',
         script: '${href()}/WikiLinkMaker/repository'
       }, function(file) {
         name = file.split("/").pop();
         val = target.val();
         index = getCurPosition(targetId);
         val = val.substr(0,index) + 'source:' + file +  val.substr(index);

         target.val(val);
         target.focus();
         closebox();
       });
    });

  });

  getCurPosition = function(val){
    ua = $.browser;
    if (ua.msie) {
        result = getCurPosition_ie(val);
    } else {
        result = getCurPosition_firefox(val);
    }
    return result;
  }

  getCurPosition_ie = function(val){
    var elm = document.getElementById(val);
    var docRange = document.selection.createRange();
    var textRange = document.body.createTextRange();
    textRange.moveToElementText(elm);
    var range = textRange.duplicate();
    range.setEndPoint("EndToStart", docRange);
    var start = range.text.length;

    return start;
  }

  getCurPosition_firefox = function(val){
    return document.getElementById(val).selectionStart;
  }

});
