﻿var emptyCommentPrompt = "Write a comment..."

function SubmitComment(itemID) {
  if (typeof (PageMethods) === "undefined") {
    alert("Page does not inlude CommentService proxy methods");
  }
  var msg = $get("newMsg_" + itemID).value;
  if (!msg || msg == "" || msg == emptyCommentPrompt) {
    return;
  }
  $("#newMsg_" + itemID).attr("disabled", true);
  $("#postMsg_" + itemID).attr("disabled", true);

  PageMethods.PostMessage(
      $("#hRacerID").val(),
      $("#hOwnerID").val(),
      itemID,
      msg,
      OnPostSuccess,
      OnError,
      itemID // Pass to callback
      )
  return false;
}

function DeleteComment(itemID, commentID, racerID) {
  PageMethods.DeleteMessage(itemID, commentID, racerID, OnDeleteSuccess, OnError, commentID);
  return false;
}

function DelayLoadMessages(itemID, count) {
  PageMethods.DelayLoadMessages(
    itemID,
    count,
    OnDelayLoadSuccess,
    OnError,
    itemID);
  return false;
}

function OnDeleteSuccess(result, request, context) {
  $("#inlineComment_" + request).fadeOut("slow", function() { $(this).remove() });
}

function OnPostSuccess(result, context, methodName) {
  var itemID = context;
  if (!itemID) {
    return;
  }
  $("#msgPlaceholder_" + itemID).replaceWith(result + "<div id='msgPlaceholder_" + itemID + "'></div>");
  var newMsg = $("#newMsg_" + itemID);
  newMsg.val("").removeAttr("disabled");
  var postMsg = $("#postMsg_" + itemID);
  postMsg.removeAttr("disabled");
}

function OnDelayLoadSuccess(result, context, methodName) {
    var itemID = context;
  $("#msgStart_" + itemID).prev().hide().replaceWith(result);
}

function OnError(result, context, methodName) {
    if (result._timedOut) {
    alert("Message post timed out. Try again later.");
  } else {
    alert("Error: " + result._message + "\r\nError code: " + result._statusCode);
  }
  
  $("#postMsg_" + context).removeAttr("disabled");
  $("#newMsg_" + context).removeAttr("disabled");
}

function focusPostMsg(id) {
  var txt = $("#newMsg_" + id);
  if (typeof (txt) == "undefined") {
    return;
  }
  var msg = txt.val();
  if (msg == "" || msg == emptyCommentPrompt) {
    txt.val("");
  }
  txt.attr("rows", 4)
     .css("color", "black");
  $("#postMsg_" + id ).show();
}

function blurPostMsg(id) {
  var txt = $("#newMsg_" + id);
  if (typeof (txt) == "undefined") {
    return;
  }
  var msg = txt.val();
  txt
  if (msg.length == 0 || msg == emptyCommentPrompt) {
    txt.val(emptyCommentPrompt)
       .css("color", "#cccccc")
       .attr("rows", 2); 
    $("#postMsg_" + id).hide();
  }
}

function initComments() {
    $("textarea[id^='newMsg_']").css('color', '#cccccc').val(emptyCommentPrompt);
    $("[id^='startComment_']").click(function() {
        /*$(this).hide();*/
        var id = $(this).attr("id");
        id = id.substring(id.indexOf("_") + 1);
        //var b = $(this).siblings("[id^='divThreadBody']");
        var b = $("#divThreadBody_" + id);
        b.show();
        $("[id^='newMsg_']", b).focus();
    });

    $("[id^='divThreadBody']").each(function(i) {
        msgCount = $("div[id^='inlineComment']", this).length;
        if (msgCount == 0) {
            $(this).hide();
        }
        /*
        else 
        {
            $(this).siblings("[id^='startComment_']").hide();
        }
        */
    });
}
function SmartTrim(ctl) {
  $(ctl).parents().eq(1).hide().end().next(".clsSmartTrim").show();
}
//$(document).ready(function() {
//    $(".clsSmartTrimPrompt>a").click(function() {
//        $(this).parents().find(".clsSmartTrimArea").hide().parent().find(".clsSmartTrim").show();
//    }
//    );
//});