﻿          function Followed(id) {
              $("#" + id).hide();
          }
          function MoreBegin(context) {
              $("#More").hide();
              $("#MoreLoader").show();
          }

          function MoreFinish(context) {
              $("#More").show();
              $("#MoreLoader").hide();
          }

          function UpdateBegin(context) {
              $("#TweetsBoxSubmit").hide();
              $("#UpdateLoader").show();
          }

          function DMOnClick(id) {
              $(".tweetMainDM").hide();
              $(".tweetMainDMBtn").hide();

              $("#TweetsBox").val("");
              var user = removeAnchor($("#" + id + " .tweetMainUserName").html());
              $("#DirectTo").val(user);
              $("#Followers").val(user);
          }

          function FollowSuccess(context) {
              $("#followStatus a").html("You are following this user");
          }

          function isFav() {
          }

          function decHTML(str) {
              return str.replace(/&amp;/g, '&').replace(/&lt;/g, '<').replace(/&gt;/g, '>');
          }

          function removeAnchor(txt) {

              var removeahref = (txt.replace(/<a href=.*?>/gi, ""));

              return removeahref.replace(/<\/a>/gi, "");

          }

          function extractReplyName(mainReplyto, txt) {
              var returntxt = mainReplyto + " ";

              var matches = txt.match(/@[A-Za-z0-9_\-]*/g);
              if (matches != null) {
                  for (var i = 0, len = matches.length; i < len; i++) {
                      if (matches[i] != mainReplyto)
                          returntxt = returntxt + matches[i] + " ";
                  };
              };
              return returntxt;
          }

          function ClearTextbox(content) {
              //Fake the jquery to trap error the message
              $("#UpdateLoader").hide();
              $("#TweetsBoxSubmit").show();
              var errorMsg = jQuery("input#ErrorMsg", "<div>" + content.get_data() + "</div>").attr("value");

              if (errorMsg.length > 0)
                  alert(errorMsg)
              else {
                  $("div#TweetContent").prepend(content.get_data()).show("slow");
                  $("#TweetsBox").val("");
                  $("#Lat").val("");
                  $("#Lng").val("");
                  $("#ReplyTo").val("");
                  $("#DirectTo").val("");
                  $("#SendTo").html("");
                  $("#ReturnMessage").html("");
              }
          }

          $(function() {
              // Place init code here instead of $(document).ready()          

              $("#Followers").click(function() {
                  $("#DirectTo").val($("#Followers").val());
              });


              $(".tweetMainFunction .reply").live('click', function(event) {
                  var user = $(this).parent().parent().parent().find(".tweetMainUserName").html();
                  //var user = removeAnchor($("#" + id + " .tweetMainUserName").html());
                  var id = $(this).parent().parent().parent().attr("id");
                  $("#TweetsBox").val("@" + user + " ");
                  $("#ReplyTo").val(id);
                  $("#DirectTo").val("");
                  $("#SendTo").html("Reply to: " + user);
                  $("#TweetsBox").focus();
                  $("#TweetsBox").putCursorAtEnd();
              });


              $(".tweetMainFunction .replyall").live('click', function(event) {
                  var user = $(this).parent().parent().parent().find(".tweetMainUserName").html();
                  //var user = removeAnchor($("#" + id + " .tweetMainUserName").html());
                  var id = $(this).parent().parent().parent().attr("id");
                  $("#TweetsBox").val(extractReplyName("@" + user, $("#org" + id).val()));
                  $("#ReplyTo").val(id);
                  $("#DirectTo").val("");
                  $("#SendTo").html("Reply to: " + user);
                  $("#TweetsBox").focus();
                  $("#TweetsBox").putCursorAtEnd();
              });

              $(".tweetMainFunction .retweet").live('click', function(event) {
                  var id = $(this).parent().parent().parent().attr("id");
                  var tweets = $("#org" + id).val();
                  var user = $(this).parent().parent().parent().find(".tweetMainUserName").html();
                  $("#TweetsBox").val("RT @" + user + " " + decHTML(tweets));
                  $("#DirectTo").val("");
                  $("#SendTo").html("Retweet: " + user);
                  $("#TweetsBox").focus();
              });

              $(".tweetMainFunction .dm").live('click', function(event) {
                  var id = $(this).parent().parent().parent().attr("id");
                  var tweets = $("#org" + id).val();
                  var user = removeAnchor($("#" + id + " .tweetMainUserName").html());
                  $("#TweetsBox").val("");
                  $("#DirectTo").val(user);
                  $("#SendTo").html("Direct Message To: " + user);
                  $("#TweetsBox").focus();
              });


              $(".tweetMainFunction .dmreply").live('click', function(event) {
                  var id = $(this).parent().parent().parent().attr("id");
                  var tweets = $("#org" + id).val();
                  var user = $(this).parent().parent().parent().find(".tweetMainUserName").html();
                  $("#ReplyTo").val(id);
                  $("#TweetsBox").val("@" + user + " ");
                  $("#DirectTo").val(user);
                  $("#SendTo").html("Direct Message Reply To: " + user);
                  $("#TweetsBox").focus();
              });


              $(".tweetMainFunction .fav").live('click', function(event) {
                  var tweet = $(this).parent().parent().parent();
                  var id = tweet.attr("id");
                  //var id = $(this).parent().parent().parent().attr("id");                  
                  var url = '/Home/Favourite/';
                  $.get(url + id);
                  tweet.find(".fav").hide();
                  tweet.find(".unfav").show();                  

              });

              $(".tweetMainFunction .unfav").live('click', function(event) {
                  var tweet = $(this).parent().parent().parent();
                  var id = tweet.attr("id");
                  var url = '/Home/UnFavourite/';
                  $.get(url + id);
                  tweet.find(".unfav").hide();
                  tweet.find(".fav").show();

              });

              $(".tweetMainFunction .delete").live('click', function(event) {
                  var tweet = $(this).parent().parent().parent();
                  var id = tweet.attr("id");
                  var url = '/Home/Delete/';
                  $.get(url + id);
                  tweet.parent().fadeOut('slow').remove()

              });

              $(".tweetMainFunction .follow").live('click', function(event) {
                  var tweet = $(this).parent().parent().parent();
                  var username = tweet.find(".tweetMainUserName").html();
                  var url = '/Home/Follow/';
                  $.get(url + username);
                  tweet.find(".tweetMainFunction").append("<a class='unfollow'>Unfollow</a>");
                  tweet.find(".follow").hide();

              });

              $(".tweetMainFunction .unfollow").live('click', function(event) {
                  var tweet = $(this).parent().parent().parent();
                  var username = tweet.find(".tweetMainUserName").html();
                  var url = '/Home/UnFollow/';
                  $.get(url + username);
                  tweet.find(".tweetMainFunction").append("<a class='follow'>Follow</a>");
                  tweet.find(".unfollow").hide();
              });

              var auth = '<% = Request.Cookies["access"]==null ? string.Empty : Request.Cookies["access"].Value %>';


              $('#Upload').uploadify({
                  uploader: '/Scripts/uploadify.swf',
                  script: '/Upload',
                  folder: '/uploads',
                  auto: true,
                  cancelImg: '/Img/cancel.png',
                  sizelimit: 2097152,
                  buttonText: 'Upload photo',
                  fileExt: '*.jpg;*.jpeg;*.gif;*.png',
                  fileDesc: 'Please choose image (*.jpg,*.jpeg,*.gif,*.png)',
                  scriptData: { token: auth },
                  onComplete: function(event, queueID, file, ss, data) {
                      var content = $.parseJSON(ss);
                      if (content["ErrorMsg"] == null) {
                          $.modal("<div><img src='" + content['Url'].replace("com/", "com/show/thumb/") + "'/><br/><span style='color:white'>Filename : " + content['Url'] + "</span></div>",
                            { opacity: 80, overlayCss: { backgroundColor: "#000" }, overlayClose: true });
                          $("#TweetsBox").val($("#TweetsBox").val() + " " + content['Url']);
                          $("#ReturnMessage").html("Photo uploaded to: " + content['Url']);
                      }
                      else
                          $("#ReturnMessage").html(content['ErrorMsg']);
                  }
              }

            );

          });

  
