/*------ -------*/
//@@not used
function reply_at(username) {
  var obj = document.getElementById('#comment');
  if (!obj) {
    //@@@obj = document.insertXXX, open the comment box
    obj = null;
  }
  insertAtCursor(obj, "@" + username + ": ");
  $(obj).focus();
}

function toggle_fav(tee_id) {
  $.post("/tiku/fav/" + tee_id + "/", function(msg){
    if (!isNaN(parseInt(msg, 10))) {
      $("#fav").toggleClass("favorited");
      $("#fav_count").text(msg);
    } else
      alert(msg);
  });
}

function tee_voteup(tee_id) {
  var myurl = "/tiku/voteup/" + tee_id + "/";
  $.post(myurl, function(msg) {
    if (!isNaN(parseInt(msg, 10))) {
      if (!$("#teeblock .vote_down").hasClass("voted"))
        $("#teeblock .vote_up").addClass("voted");
      $("#teeblock .vote_down").removeClass("voted");
      $("#teeblock .vote_count").text(msg);
    } else alert(msg);
  });
}

function tee_votedown(tee_id) {
  var myurl = "/tiku/votedown/" + tee_id + "/";
  $.post(myurl, function(msg) {
    if (!isNaN(parseInt(msg, 10))) {
      if (!$("#teeblock .vote_up").hasClass("voted"))
        $("#teeblock .vote_down").addClass("voted");
      $("#teeblock .vote_up").removeClass("voted");
      $("#teeblock .vote_count").text(msg);
    } else alert(msg);
  });
}

function answer_voteup(ans_id) {
  var myurl = "/tiku/answer/voteup/" + ans_id + "/";
  $.post(myurl, function(msg) {
    if (!isNaN(parseInt(msg, 10))) {
      if (!$("#ans" + ans_id + " .vote_down").hasClass("voted"))
        $("#ans" + ans_id + " .vote_up").addClass("voted");
      $("#ans" + ans_id + " .vote_down").removeClass("voted");
      $("#ans" + ans_id + " .vote_count").text(msg);
    } else alert(msg);
  });
}

function answer_votedown(ans_id) {
  var myurl = "/tiku/answer/votedown/" + ans_id + "/";
  $.post(myurl, function(msg) {
    if (!isNaN(parseInt(msg, 10))) {
      if (!$("#ans" + ans_id + " .vote_up").hasClass("voted"))
        $("#ans" + ans_id + " .vote_down").addClass("voted");
      $("#ans" + ans_id + " .vote_up").removeClass("voted");
      $("#ans" + ans_id + " .vote_count").text(msg);
    } else alert(msg);
  });
}

function check_cn() {
  var raw = $("#wmd_input").val();
  var ret = true;
  if (/^[\x00-\xff]+$/.test(raw) || raw.length < 15) {
    alert("不含中文或者少于15个字符为无效回复");
    ret = false;
  }
  $("#wmd-input").focus();
  return ret;
}

function search_title() {
  //@@@@
}

function tee_view_init() {
  prettyPrint();//syntax highlight
  //prevent data loss
  window.onbeforeunload=function(){if ($("#wmd_input").val()) return "！您刚刚写了一些内容但还没有提交！";return;};
  $("input.submit").click(function(){window.onbeforeunload=function(){return;};});
  //addcmt
  $(".cmtat").live("click", function(e){
    var teecmts = $(this).closest(".teecmts");
    teecmts.append($("#comment").show());
    $("#comment").attr("tag", teecmts.children(".addcmt").attr('tag'));
    var cmtbox = $("#cmtinput textarea");
    cmtbox.val("").focus();
    insertAtCursor(cmtbox[0], "@" + $(this).attr('tag') + ": ");
    e.preventDefault();
  });
  $(".addcmt").click(function(e){
    $(this).parent().append($("#comment").show());
    $("#comment").attr("tag", $(this).attr('tag'));
    $("#cmtinput textarea").focus();
    e.preventDefault();
  });
  $("#comment").keyup(function(){
    var space = 250 - $("#comment textarea").val().length;
    $("#cmtoper span").text(space);
    $("#cmtoper input").attr("disabled", space < 0);
  });
  $("#cmtoper input").click(function(){
    $("#cmtoper input").attr("disabled", true);
    var obj = this;
    $.ajax({
      type: "POST",
      url: "/tiku/comment/" + $("#comment").attr("tag"),
      data: {'comment':$("#comment textarea").val()},
      success: function(msg){
        eval(msg);
        var id_str = cmt.tt + "cmt" + cmt.id;
        var html = '<tr><td><div class="vimg cmtvote" id="' + id_str + '"></div></td><td class="cmtvcnt"> </td><td class="cmtbody">' + cmt.raw + ' － <a href="/tiku/user/' + cmt.uid + '/">' + cmt.uname + '</a>, 1秒前, <a class="cmtdel" tag="' + cmt.tt + '/del/' + cmt.id + '/" href="#">x</a></td></tr>';
        $(obj).closest(".teecmts").children("table:first").find("tr:last").after(html);
        $("#cmtoper input").removeAttr("disabled");
        $("#comment textarea").val("");
        $("#cmtoper span").text(250);
        $("#comment").hide("slow");
        $("#"+id_str).closest("tr").css("background-color", "green").animate({backgroundColor:"white"}, 500);
      },
      error: function(e){
        alert('操作失败:' + e.statusText + ":" + e.responseText);
        $("#cmtoper input").removeAttr("disabled");
      }
    });
  });
  //comment vote & del
  $(".teecmts").click(function(e){
    var obj = e.target;
    var myurl = null;
    if ($(obj).hasClass('cmtvote')) {
      myurl = "/tiku/comment/" + obj.id.substring(0, 3) + "/vote/" + obj.id.substring(6) + "/";
      $.post(myurl, function(msg){
        if (!isNaN(parseInt(msg, 10))) {
          $(obj).closest("td").next("td").html(msg);
        } else alert(msg);
      });
    } else if ($(obj).hasClass('cmtdel')) {
      myurl = "/tiku/comment/" + $(obj).attr("tag");
      if (confirm("你要删除这条评论吗？\r\n" + $(obj).closest(".cmtbody").children(":first").text())) {
        $.post(myurl, function(msg){
          var jqobj = $(obj).closest("tr");
          jqobj.css("background-color", "red").fadeOut("slow", function(){ $(this).remove(); });
        });
      }
      e.preventDefault();
    }
  });
  //more comment
  $(".morecmt").click(function(e){
    var myurl = "/tiku/comment/" + $(this).attr("tag");
    var obj = this;
    $.post(myurl, function(msg){
      eval(msg);
      var html = [];
      for (var i=0; i<cmts.length; i++) {
        cmt = cmts[i];
        var tr = "<tr>";//class name
        if (cmt.time.indexOf("年") == -1 && cmt.time.indexOf("月") == -1 && cmt.time.indexOf("天") == -1)
          tr = "<tr class=in24hours>";
        html.push(tr+'<td><div class="vimg cmtvote" id="' + cmt.tt + "cmt" + cmt.id + '"></div></td><td class="cmtvcnt">' + cmt.vote + '</td><td class="cmtbody">' + cmt.raw + ' － <a href="/tiku/user/' + cmt.uid + '/">' + cmt.uname + '</a>, ' + cmt.time + ', ' + '<a class="cmtdel" tag="' + cmt.tt + '/del/' + cmt.id + '/" href="#">x</a>' + (cmt.at?'<a class="cmtat" tag="'+cmt.uname+'" href="#">@</a>':'') + '</td></tr>');
      }
      $(obj).closest(".teecmts").children("table:first").find("tr:last").after(html.join(""));
      $(obj).hide();
    });
    e.preventDefault();
  });
}

function init_sorting() {
  var url = window.location.href;
  var rxlinks = /^http:.*\/.*[\&\?]sorting=([a-z_-]+).*$/;
  var m = url.match(rxlinks);
  if (m) $("#ts_" + m[1]).addClass("current");
  else $(".sortbox span a").eq(0).addClass("current");
}

/*init*/
$(function(){
  $(".tee_summary").each(function(){
    var tid = this.id.substring(3);//tee123
    $(this).children(".xx_c").click(function(){
      window.location.href = "/tiku/view/" + tid + "/";
    });
  });
  if ($(".sortbox").length)
    init_sorting();

  $("#top_search_box").keydown(function(eve){
    if (eve.target != this) return;
    if (eve.keyCode == 13) {
      if ($(this).val().length >= 2)
        window.location.href = "/tiku/search/" + $(this).val() + "/";
      else
        window.location.href = "/tiku/search/";
    }
  });
  $("#top_search_box").click(function(){$(this).select();});
  if ($("#shareholder").length)
    $("#shareholder").append($("#myshare"));
});

