var Comment =
{
  imgSizerInit: function(){}, /*deprecated*/

  imgSizer: function( selector )
  {
    var linkText = 50;
    var clicked = function()
    {
      var srcSafe = this.src.replace("://","|||")
      document.location.href = "/en/html/forum/image.jsp?src=" + escape(srcSafe);
    };
    var sizer = function()
    {
      var img = $(this), maxW = img.parent().width()-20;
      if( img.width() <= maxW ) return;
      img.attr("title", "Image reduced to fit. Click to enlarge to " + img.width()+"x"+img.height() )
         .css({cursor:"pointer"})
         .height( img.height() * (maxW/img.width()) )
         .width(maxW)
         .click(clicked);
    };

    $( (typeof selector !== 'undefined' ? selector : "img.cntrbtd") ).each(function()
    {
      if(this.complete)
      {
        sizer.call(this);
      }
      else
      {
        $(this).bind("load",sizer);
      }
    });
  },

  imageClick: function()
  {
    var srcSafe = this.src.replace("://","|||")
    document.location.href = "/forum/image.jsp?src=" + escape(srcSafe);
  },

  linkShortner: function( selector )
  {
    $( (typeof selector !== 'undefined' ? selector : "a.cntrbtd") ).each(function()
    {
      var txt = this.text, firstIdx, lastIdx, file, fileLen;

      if(txt.indexOf("://") == -1 || txt.length <= linkText) return;

      // cull scheme
      txt = txt.substring(txt.indexOf("://") + 3);

      if(txt.length <= linkText){ $(this).text(txt); return; }

      // cull folders
      firstIdx = txt.indexOf("/") + 1;
      lastIdx  = txt.lastIndexOf("/");
      if( firstIdx < lastIdx )
      {
        txt = txt.replace(txt.substring(firstIdx, lastIdx-firstIdx), "...");
      }
      if(txt.length <= linkText){ $(this).text(txt); return; }

      // cull query string
      firstIdx = txt.indexOf("?");
      if(firstIdx > -1){ txt = txt.substring(0, firstIdx+3)+"..."; }
      if(txt.length <= linkText){ $(this).text(txt); return; }

      // cull fragment
      firstIdx = txt.indexOf("#");
      if(firstIdx > -1){ txt = txt.substring(0, firstIdx); }
      if(txt.length <= linkText){ $(this).text(txt); return; }

      // slice/splice filename
      firstIdx = txt.indexOf("/") + 1;
      lastIdx  = txt.lastIndexOf(".");
      if( lastIndex - firstIndex > 10)
      {
        file = txt.substring(firstIndex, lastIndex - firstIndex);
        fileLen = txt.length - linkText + 3;
        txt = txt.replace(file, "..." + file.substring(fileLen));
      }

      $(this).text(txt);
    });
  },

  replyTo: function( msgId, poster, domSrc )
  {
    $("#cmmntFrm").hide();
    $(".comment a").show();
    $($(domSrc).parents(".comment")[0]).append($("#cmmntFrm"));
    $("#cmmntFrm").show();
    $(domSrc).hide();
    var bdy = $("#forumpost_body");
    $("input#msgId").val(msgId);
    bdy.val( "@"+ poster.replace(/ /g,"_") +" "+ bdy.val().replace(/@[^ ]+ /g,"") );
    bdy.focus();
    return false;
  }
}

$(document).ready(function(){ Comment.imgSizer(); Comment.linkShortner(); });