/* Author:

*/


$(document).ready(function(){
	
	// Run Mathias Bynens jQuery placeholder plugin (see plugins.js)
	if ($.fn.placeholder) {
		$('input, textarea').placeholder();		
	}

	// Display Latest Tweets
	$.getJSON('cache/twitter-json.txt', function(data){  
        $.each(data, function(index, item){  
                $('#tweet').append('<div id="tweet-item">' + '<p class="time">' + relative_time(item.created_at) + '</p><p>' + item.text.linkify() + '</p>' + '<div id="web-intent">' + '<a href="http://twitter.com/intent/retweet?tweet_id=' + item.id_str + '" id="retwitear">' + 'Retwittear</a>' + '<a href="http://twitter.com/intent/tweet?in_reply_to=' + item.id_str + '" id="responder">' + 'Responder</a>' + '<a href="http://twitter.com/intent/favorite?tweet_id=' + item.id_str + '" id="favorito">' + 'Favorito</a>' + '</div></div>');  
    });  

   
  $(".videoPlayer").fitVids();


  setTimeout(function(){
  $("div.exito").fadeOut("slow", function () {
  $("div.exito").remove();
      });

}, 8000);





}); // Ends (document).ready(function()


// Determines Twitter conected state with @Anywhere
twttr.anywhere(function (T) {  
       T.bind("authComplete", function (e, user) {  
          // triggered when auth completed successfully  
        });  
  
     T.bind("signOut", function (e) {  
          // triggered when user logs out  
        });  
     });


// Generates Twitter "Follow" button with @Anywhere
twttr.anywhere(function (T) {  
       T('#follow').followButton("juanfranciscove");  
     });


// Generates Twitter Login button with @Anywhere
/*jQuery(function () {  
        twttr.anywhere(function (T) {  
          if (T.isConnected()) {  
            $("#login-logout").append('<button id="signout" type="button">Cerrar Sesión Twitter</button>');  
            $("#signout").bind("click", function () {  
              twttr.anywhere.signOut();  
          });  
        }  
          else {  
            T("#login-logout").connectButton();  
          }  
        });  
      });


// Generates a Twitter Tweetbox with @Anywhere
twttr.anywhere(function (T) {  
      T("#tbox").tweetBox({  
          label: '<link rel="stylesheet" media="all" href="http://accessus.org/test/css/anywhere.css?v=1"><span style="font-size: 20px; color: #B2170C">¿Y tú qué le dices a JF?</span>',
          defaultContent: "@juanfranciscove "
        });  
     }); 
*/
    
// Generates Twitter Hovercards with @Anywhere
twttr.anywhere(function (T) {  
     T.hovercards({ expanded: true });  
     });


// Convert Twitter API Timestamp to "Time Ago"
function relative_time(time_value) {
  var values = time_value.split(" ");
  time_value = values[1] + " " + values[2] + ", " + values[5] + " " + values[3];
  var parsed_date = Date.parse(time_value);
  var relative_to = (arguments.length > 1) ? arguments[1] : new Date();
  var delta = parseInt((relative_to.getTime() - parsed_date) / 1000);
  delta = delta + (relative_to.getTimezoneOffset() * 60);

  var r = '';
  if (delta < 60) {
        r = 'hace <span>1</span> min';
  } else if(delta < 120) {
        r = 'hace <span>+1</span> minutos';
  } else if(delta < (45*60)) {
        r = 'hace <span>' + (parseInt(delta / 60)).toString() + '</span> minutos';
  } else if(delta < (90*60)) {
        r = 'hace <span>1</span> hora';
  } else if(delta < (24*60*60)) {
        r = 'hace <span>' + (parseInt(delta / 3600)).toString() + '</span> horas';
  } else if(delta < (48*60*60)) {
        r = 'hace <span>1</span> día';
  } else {
        r = 'hace <span>' + (parseInt(delta / 86400)).toString() + '</span> días';
  }

  	  return r;
	}

});	


// Create Usable Links
String.prototype.linkify = function() {
       return this.replace(/[A-Za-z]+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&\?\/.=]+/, function(m) {
              return m.link(m);
      });
};









 

