$(document).ready(function(){
  
  var container = $("#social-media-module");
  $("#social-media-module .nav li").click(changeTab);
  
  
  var hideSpinner = function () {
    container.css({ 'background-image':'none' });
  };
  
  function changeTab (){
    $("#social-media-module .nav li").removeClass("active");
    $(this).addClass("active");
    var thisID = $(this).index()+2;
    $("#social-media-module > div.network").hide();
    $("#social-media-module > div.network:nth-child("+thisID+")").show();
    $('#social-media-module > div ul').jScrollPane();
    
    return false;
  };

  var tweetcount = 0;
  var feedcount = 0;
  var videocount = 0;
  $("#twitter").show();
  $.getJSON("https://graph.facebook.com/ncnewschools/feed?access_token=226099284070061|d2a076df9ed951069470c8f4.1-38403044|0dPBmbADCqbSwnLg3uuEgcpnk_w&callback=?",function(result){
    $.each(result.data, function(i,item){
      if(feedcount == 10){
        $('#facebook a').each(function(){
          var thisText = $(this).text();
          var thisLength = thisText.length;
          if (thisLength > 20) {
            thisText = thisText.substring(0, 25)+"...";
            $(this).text(thisText);
          }
        });
        return false;
      }
      cf = item.message;
      cn = item.from.name;
      if(cn == "North Carolina New Schools Project") {
        cf = cf.replace(/http:\/\/\S+/g,  '<a href="$&" target="_blank">$&</a>');
        $("#facebook ul").append('<li>'+cf +'<p class="date"><a href=\"http://www.facebook.com/ncnewschools#!/posted.php?id=' + item.id.replace("_","#s") + '\" target=\"_blank\">' + prettyDate(item.created_time.replace("+0000","")) +'</a></p></li>');
        feedcount = feedcount + 1;
      }
      return true;
    });
    hideSpinner();
    $("#facebook ul li:last").addClass("last");
    $('#social-media-module > div ul').jScrollPane();
  });
  
  $.getJSON("http://twitter.com/statuses/user_timeline.json?screen_name=ncnewschools&count=30&callback=?",function(data){
    $.each(data, function(i,item){
      if(tweetcount == 10) {
        $('#twitter a').each(function(){
          var thisText = $(this).text();
          var thisLength = thisText.length;
          if (thisLength > 20) {
            thisText = thisText.substring(0, 25)+"...";
            $(this).text(thisText);
          }
        });
        $('#twitter ul').jScrollPane();
        return false;
      }
      ct = item.text;
      ct = ct.replace(/http:\/\/\S+/g,  '<a href="$&" target="_blank">$&</a>');
      ct = ct.replace(/\s(@)(\w+)/g,    ' @<a onclick="javascript:pageTracker._trackPageview(\'/outgoing/twitter.com/\');" href="http://twitter.com/$2" target="_blank">$2</a>');
      ct = ct.replace(/\s(#)(\w+)/g,    ' #<a onclick="javascript:pageTracker._trackPageview(\'/outgoing/search.twitter.com/search?q=%23\');" href="http://search.twitter.com/search?q=%23$2" target="_blank">$2</a>');
      $("#twitter ul").append('<li>'+ct +'<p class="date"><a href=\"http://twitter.com/ncnewschools/status/'+ item.id + '\" target=\"_blank\">' +twitterdate(item.created_at) +'</a></p></li>');
      tweetcount = tweetcount + 1;
      return true;
    });
    hideSpinner();
    $("#twitter ul li:last").addClass("last");
  });
  
  $.getJSON("http://gdata.youtube.com/feeds/api/users/ncnewschools/uploads?max-results=10&alt=json&callback=?",function(result){
    $.each(result.feed.entry, function(i,item){
      if(videocount==10){return false;}
      $("#youtube ul").append("<li>"+item.title.$t+" <a href=\"" + item.link[0].href.replace("&feature=youtube_gdata","")+"\" title=\""+item.title.$t+"\" target=\"_blank\"><div class=\"thumbnail\"><img src=\"" + item.media$group.media$thumbnail[2].url + "\"/><span class=\"play-btn-overlay\">Play Button</span></div><p class=\"date\"><a href=\"" + item.link[0].href.replace("&feature=youtube_gdata","")+"\" title=\""+item.title.$t+"\" target=\"_blank\">"+ item.published.$t.replace(".000Z","") +"</a></p></li>");
      videocount = videocount + 1;
      return true;
    });
    $("#youtube ul li:last").addClass("last");
    $("#youtube .date").each(function(){
      $(this).find("a").text(prettyDate($(this).find("a").text()));
    });
  });
  
});
