$(document).ready(function(){
});

/**
 * Init
 */
var curRep=-1;

function limitChars(textid, limit, infodiv) {
  var text = $('#'+textid).val();
  var textlength = text.length;
  if (textlength > limit){
    $('#' + infodiv).html('You cannot write more then '+limit+' characters!');
    $('#'+textid).val(text.substr(0,limit));
    return false;
  } else {
    $('#' + infodiv).html('You have '+ (limit - textlength) +' characters left.');
    return true;
  }
}

/**
 * Set Record Count
 */
function digg(formKey,type,id,v){
  if (v>0){
    var st = $("#diggDigg_"+type+"_"+id).css("cursor");
  }
  else {
    var st = $("#diggBury_"+type+"_"+id).css("cursor");
  }
  if (st=="pointer"){
    // hide buttons
    $("#diggBury_"+type+"_"+id).attr("class","diggBury buryOff");
    $("#diggDigg_"+type+"_"+id).attr("class","diggDigg diggOff");
    $.post("/ajax.php?scr=ajax/commentDigg.php", { formKey:formKey, type: type, id: id, v: v },
      function(data){
        if (v>0){
          $("#diggBury_"+type+"_"+id).attr("class","diggBury buryOn");
        }
        else {
          $("#diggDigg_"+type+"_"+id).attr("class","diggDigg diggOn");
        };
        $("#"+(type==1?"newsDiggVal_":"commDiggVal_")+id).html( (data>0?"+":"") +"<b>"+ data + "</b> score");
    });
  }
  return false;
}

/**
 * Repond to reply button
 */
function openReplies(id) {
  $("#replies_"+id).toggle();
  if ($("#subToggleBtn_"+id).attr("class")=="replies open"){
    $("#subToggleBtn_"+id).attr("class","replies close");
  }
  else {
    $("#subToggleBtn_"+id).attr("class","replies open");
  }
}

/**
 * Repond to reply button
 */
function reply(formKey,rid){
  var id = $("#cmt_game_id").val();
  var uid = $("#cmt_user_id").val();
  var form_submit=false;
  // move comment box to main spot if it is not yet initiated
  if (curRep==-1) {
    $("#commentInput").insertAfter("#rep_"+rid);
    curRep=rid;
  }
  // determin if this is a submit on the open box
  if($("#repBtn_"+rid).val()=="Submit" || $("#repBtn_"+rid).html()=="Submit"){
    form_submit=true;
  }
  // remove the cancel button
  $("#canBtn").remove();
  //
  if (form_submit){
    $("#commentInput").slideUp();
  }
  else {
    if (curRep==0) { $("#repBtn_"+curRep).val("Post a Comment"); } else { $("#repBtn_"+curRep).html("Reply"); }
    if(curRep!=rid){
      if ($("#commentInput").is(":visible")) {
        $("#commentInput").slideUp("slow", function() {
          $("#commentInput").insertAfter("#rep_"+rid);
        });
      }
    }
    else {
      $("#commentInput").insertAfter("#rep_"+rid);
    }
    $("#commentText").val("");
    $("#commentInput").slideDown("slow", function() {
        $("#commentText").focus();
    });
  }
  //
  if(curRep!=rid){
    if (rid==0) { $("#repBtn_"+curRep).html("Reply"); } else { $("#repBtn_"+curRep).val("Post a Comment"); };
    if (rid==0) {
      $("#repBtn_"+rid).val("Submit");
      $("#repBtn_"+rid).after('&nbsp;<input id="canBtn" type="button" value="Cancel" onclick="return closeComment('+rid+');"/>');
    }
    else {
      $("#repBtn_"+rid).html("Submit");
      $("#repBtn_"+rid).before('<a id="canBtn" class="replyText" onclick="return closeComment('+rid+');" href="#">Cancel</a>');
    }
    curRep=rid;
  }
  else {
    // same box
    if(form_submit){
      // submit clicked
      var comment = $("#commentText").val();
      $.post("/ajax.php?scr=ajax/commentSave.php", { formKey:formKey, id:id, rid:rid, uid:uid, comment:comment },
        function(data){
          $("#tab_game_comment").html( '<div style="width:100%; text-align:center;">' + $("#loadingAnimation").html() + '</div>' );
          $("#tab_game_comment").load("/ajax/commentLoad/"+id, {}, function(data){
          $("#tab_game_comment").html(data);
          curRep = -1;
        });
      });
      if (rid==0) { $("#repBtn_"+rid).val("Post a Comment"); } else { $("#repBtn_"+rid).html("Reply"); }
      $("#canBtn").remove();
    }
    else {
      // post / reply clicked
      if (rid==0) {
        $("#repBtn_"+rid).val("Submit");
        $("#repBtn_"+rid).after('&nbsp;<input id="canBtn" type="button" value="Cancel" onclick="return closeComment('+rid+');" />');
      }
      else {
        $("#repBtn_"+rid).html("Submit");
        $("#repBtn_"+rid).before('<a id="canBtn" class="replyText" onclick="return closeComment('+rid+');" href="#">Cancel</a>');
      }
    }
  }
  return false;
}

/**
 * Close the comment box
 */
function closeComment(rid) {
  if (rid==0) { $("#repBtn_"+rid).val("Post a Comment"); } else { $("#repBtn_"+rid).html("Reply"); }
  $("#canBtn").remove();
  if ($("#commentInput").is(":visible")) {
      $("#commentInput").slideUp("slow")
  }
  curRep = -1;
  return false;
}

/**
 * Toggle the digg box buttons
 */
function showTogglebox(id) {
  $("#subToggleBox_"+id).toggle();
  $("#comment_"+id).toggleClass("subDigg");
  var st = $("#diggToggle_"+id).html();
  if (st=="Show"){
    $("#diggToggle_"+id).html("Hide");
    $("#diggToggle_"+id).attr("class","hide");
    $("#diggBury_"+id).attr("class","diggBury buryOn");
    $("#diggDigg_"+id).attr("class","diggDigg diggOn");
  }
  else {
    $("#diggToggle_"+id).html("Show");
    $("#diggToggle_"+id).attr("class","show");
    $("#diggBury_"+id).attr("class","diggBury buryOff");
    $("#diggDigg_"+id).attr("class","diggDigg diggOff");
  }
}
