$(document).ready(function() {
   $(".loading").hide();
   
   $("ul.tweets li.tweet").each(function(i) {
      var $links = $(this).children(".corrections").children("span.links");
      
      $links.hide();      
   });
   
   //create a timer
   var timer = { };

   $(".searchbox").focus(function() {
      $(this).val("");
   });   
   
   var correctionsOrigHtml = "Mistake?";
   
   var entity_id= $(".movieinfo").attr('id');
 
   var t_enter = {};
   var t_leave = {};
   
   var correctionsEnter = function() {
      var id = $(this).parent().attr("id");

      clearTimeout(t_leave[id]);


      var self = this;
      t_enter[id] = setTimeout(function() {$(self).children(".text").hide(); $(self).children(".links").show();}, 100);
      
      return false;      
   }
   
   var correctionsLeave =  function() {
      var id = $(this).parent().attr("id");

      clearTimeout(t_enter[id]);

      var self = this;
      clearTimeout(t_enter);
      t_leave[id] = setTimeout(function() {$(self).children(".links").hide();$(self).children(".text").show();}, 1000); 
   }
   
   var clickCorrection = function() {
      var tweet_id = $(this).parent().parent().parent().attr("id");
      var sentiment= $(this).attr("class");
      
      //serialize the data
      var dataString = 'tweet_id='+ tweet_id + '&entity_id=' + entity_id + '&sentiment=' + sentiment;
      
      //display the message
      var li = $(this).parent().parent().parent();
      $(li).fadeOut('slow', function() {
         $(li).html("<h2>Your Correction Has Been Submitted. Thank You</h2>").fadeIn('slow');
         $(li).delay(500).fadeOut('slow');
      });
      
      $.getJSON("/correction.php?" + dataString, function(result) {
         //update the rating
         $.each(result, function(i, info) {
            var total = Number(info.positive) + Number(info.negative);
            var rating = Math.round(100 * Number(info.positive) / (total));
            
            $("#numpositive").fadeOut('slow', function() {
               $(this).html(info.positive).fadeIn('slow', function() {     
                  $("#numnegative").fadeOut('slow', function() {
                     $(this).html(info.negative).fadeIn('slow', function() {
                        $("#numtweets").fadeOut('slow', function() {
                           $(this).html(total).fadeIn('slow', function() {
                              $(".rating").fadeOut('slow', function() {
                                 $(this).html(rating + "%").fadeIn('slow');
                              });
                           });
                        });
                     });      
                  });
               });
            });
         });
         
      });
      
      return false;
   }
   
   var correct = function($links, sentiment) {      
      $li = $links.parent().parent().parent();
      
      //hide the misake text
      $li.children(".corrections").fadeOut('slow');
      
      //update the icon and get rid of the mistake text
      if(sentiment == "positive") {
         //change the thumb to up
         $li.children(".thumb-icon").fadeOut('slow', function() {
            $(this).html("<img src=\"/images/thumbs_up.jpg\" />").fadeIn('slow');
         });

      }
      else if(sentiment == "negative") {
         $li.children(".thumb-icon").fadeOut('slow', function() {
            $(this).html("<img src=\"/images/thumbs_down.jpg\" />").fadeIn('slow');
         });
      }
      else {
         //just get rid of it
      //               $(this).parent().parent().parent().empty();
         $li.fadeOut('slow');
      }
      
      return false;
   }
   var gotMore = function() {
   }   
   
   $(".getmore a").click(function() {
      $(".getmore").hide();
      $(".loading").show();
      var sentiment=$.query.get('filter');
      var before=$("ul.tweets li.tweet:last-child").attr("id");
      
      //serialize the data
      var dataString = 'entity_id=' + entity_id + '&before=' + before + '&sentiment=' + sentiment;
      
      $.getJSON("/getTweets.php?" + dataString, function(t) {  
         var lastTweetID;
         $.each(t, function(i, tweet) {
            var htmlcode = "<li class=\"tweet\" id=\"" + tweet.id + "\">";
            htmlcode += "<div class=\"profile-pic\">";
            htmlcode += "<a href=\"http://twitter.com/" + tweet.from_user + "\">";
            htmlcode += "<img src=\"" + tweet.profile_image_url + "\" /></a></div>";
            htmlcode += "<div class=\"text-body\"><strong><a href=\"http://twitter.com/"+tweet.from_user+"\">";
            htmlcode += tweet.from_user + "</a></strong> " +  tweet.text + "</div>";
            htmlcode += "<div class=\"thumb-icon\">";

            if (tweet.sentiment == "POSITIVE") {
               htmlcode += "<a href=\"/movie.php?id=" + entity_id + "&filter=positive\">";
               htmlcode += "<img src=\"/images/thumbs_up.jpg\" /></a>";
            }
            else {
               htmlcode += "<a href=\"/movie.php?id=" + entity_id + "&filter=negative\">";
               htmlcode += "<img src=\"/images/thumbs_down.jpg\" /></a>";
            }

            htmlcode += "</div>";
            htmlcode += "<div class=\"corrections\"><span class=\"links\">This is actually:";
            htmlcode += " <a href=\"\" class=\"positive\" rel=\"nofollow\">Positive</a> |";
            htmlcode += " <a href=\"\" class=\"negative\" rel=\"nofollow\">Negative</a> |";
            htmlcode += " <a href=\"\" class=\"spam\" rel=\"nofollow\">Spam</a> |";
            htmlcode += " <a href=\"\" class=\"notsubjective\" rel=\"nofollow\">Not a review of this movie</a>";
            htmlcode += "</span><span class=\"text\">Mistake?</span></div>";
            htmlcode += "<div class=\"line\"></div></li>";
            
            lastTweetID = tweet.id;
            $("ul.tweets").append(htmlcode);
            
            //don't forget to hide the links
            var $c = $("ul.tweets li.tweet:last-child").children(".corrections");
            $c.children("span.links").hide();
            
            //bind the callbacks
            $c.bind("mouseenter", correctionsEnter);
            $c.bind("mouseleave", correctionsLeave);
            $c.children("span.links").children("a").bind("click", clickCorrection);
          });
          
          //now check for more
         dataString = 'entity_id=' + entity_id + '&before=' + lastTweetID
                    + '&sentiment=' + sentiment +"&hasmore=1";
       
         $.getJSON("/getTweets.php?" + dataString, function(t) {
            $.each(t, function(i, result) {
               if(result.hasmore == "0") {
                  //get rid of the more link
                  $(".getmore").empty();
               }
            });
            
            $(".loading").hide();
            $(".getmore").show();      
         });
       });

      return false;
   });

   $("li.tweet .corrections").bind("mouseenter", correctionsEnter);   
   $("li.tweet .corrections").bind("mouseleave", correctionsLeave);

   $(".corrections a").click(clickCorrection);
   
});
