﻿function initTweet() {
      $(".update-post").click(function(e) {
                $(".update-post-form").toggle("slow");
                $("#bttUpdate").attr("disabled", "");
                $("#txtTweet").val('');
      });
      $("#txtTweet").keyup(function(e) {
                var limit = 130;
                if ($(this).val().length > 0 && $(this).val().length < 131) {
                    $("#numchar").text(limit - $(this).val().length);
                    $("#bttUpdate").attr("src", "/Content/im/ico/update-btt.png");
                } else {
                    $(this).val($(this).val().substring(0, limit));
                    $("#numchar").text(limit);
                    $("#bttUpdate").attr("src", "/Content/im/ico/update-btt-dis.png");
                    return false;
                }
      });
      initEventTweet();

      $("#bttUpdate").click(function() {
          if ($("#txtTweet").length < 131) {
              $(this).attr("disabled", "disabled");
              $(this).attr("src", "/Content/im/ico/update-btt-dis.png");
              var data = $("#txtTweet").serialize() + "&";
              data += $("input[name='__RequestVerificationToken']").serialize();
              $.ajax({ type: "POST", url: "/Home/PostTweet/", data: data, dataType: "json"
                           , success: function(obj) {
                               try {
                                   if (obj) {
                                       if (obj.IsAuth) {
                                           if (obj.IsCompleted) {
                                               $(".update-post-form").toggle("slow");
                                               $("#txtTweet").val('');
                                               $("#top-tweet > UL").html(obj.HtmlString);
                                               $("#tweetPage").html(obj.Paging);
                                               initEventTweet();
                                           } else {
                                               alert(obj.ErrorMessage);
                                           }
                                       } else
                                           alert(obj.ErrorMessage);
                                   }
                                   $(this).attr("disabled", "");
                                   $(this).attr("src", "/Content/im/ico/update-btt.png");
                               } catch (e) {
                                   alert("Object JSON Invalid");
                               }
                           }, error: function() {
                               alert("Ooop! Can't process this time.");
                           }
              }); //End Ajax
          } else
              return false;
      });
  }
  function getTweet(page) {
      if (page != null) {
          $("#top-tweet > UL").block({
              message: '<img src="/content/im/site/bigWaiting.gif" />',
              css: {
                  padding: 0,
                  margin: 0,
                  width: '30%',
                  top: '40%',
                  left: '35%',
                  border: '0',
                  textAlign: 'center',
                  backgroundColor: 'transparent',
                  cursor: 'wait'
              }
          });
          var data = "page=" + page;
          $.ajax({ type: "GET", url: "/Home/GetTweet/", data: data, dataType: "json"
                           , success: function(obj) {
                               try {
                                   if (obj) {
                                       if (obj.IsCompleted) {
                                           $("#top-tweet > UL").html(obj.HtmlString);
                                           $("#tweetPage").html(obj.Paging);
                                           initEventTweet();
                                       } else {
                                           alert(obj.ErrorMessage);
                                       }
                                   }
                                  
                               } catch (e) {
                                   alert("Invalid JSON Object");
                               }
                               //$("#top-tweet > UL").unblock();
                           }, error: function() {
                               alert("Ooop! Can't process this time.");
                               //$("#top-tweet > UL").unblock();
                           }
          });      //End Ajax
      } 
  }
  function initEventTweet() {
      $("#top-tweet > ul > li").mouseover(function(e) {
          $(".delete-tweet").hide();
          $(this).find(".delete-tweet").show();
      });
      $("#top-tweet > ul > li").mouseout(function(e) {
          $(".delete-tweet").hide();
      });
  }
  function disposeTweet(theValue,page) {
    if (confirm('Do you delete this?')) {
        if (theValue != null) {
            alert('ss');
            $("#top-tweet > UL").block({
                message: '<img src="/content/im/site/bigWaiting.gif" />',
                css: {
                    padding: 0,
                    margin: 0,
                    width: '30%',
                    top: '40%',
                    left: '35%',
                    border: '0',
                    textAlign: 'center',
                    backgroundColor: 'transparent',
                    cursor: 'wait'
                }
            }
            );
            var data = "tweetId=" + theValue + "&";
            data += "page=" + page + "&";
            data += $("input[name='__RequestVerificationToken']").serialize();
            $.ajax({ type: "POST", url: "/Home/DisposeTweet/", data: data, dataType: "json"
                           , success: function(obj) {
                               try {

                                   if (obj) {
                                       if (obj.IsAuth) {
                                           if (obj.IsCompleted) {
                                               $("#top-tweet > UL").html(obj.HtmlString);
                                               $("#tweetPage").html(obj.Paging);
                                               initEventTweet();
                                           } else {
                                               alert(obj.ErrorMessage);
                                           }
                                       } else
                                           alert("Please login before delete tweet.");
                                   }
                               } catch (e) {
                                   alert("Invalid JSON Object");
                               }
                               $("#top-tweet > UL").unblock();
                           }, error: function() {
                               alert("Ooop! Can't process this time.");
                               $("#top-tweet > UL").unblock();
                           }
                           
            });     //End Ajax
        }
    }
}
