﻿jQuery(function($){
	$('input[type=submit]').addClass("botao");  
	$('input[type=text]').addClass("input");
	$('input[type=email]').addClass("input");
	$('input[type=file]').addClass("inputArquivo"); 
	/* PNGs */
	var badBrowser = (/MSIE ((5\.5)|6)/.test(navigator.userAgent) && navigator.platform == "Win32");
	if (badBrowser) $('img[src$=.png]').each(function() {
		if (!this.complete) this.onload = function() { fixPng(this) };
		else fixPng(this);
	});

	// Tweets
	$.getJSON('http://api.twitter.com/1/statuses/user_timeline/kakamarinho.json?count=3&callback=?', function(data) {
		$(data).each(function() {
			$('ul.twits').append('<li><strong>' + relative_time(this.created_at) + '</strong>  <p class="fCor1">' + this.text.linkify().at().hash() + '</p></li>');
		});
	});

	// twitpic
	$.getJSON('http://api.twitpic.com/2/users/show.jsonp?username=kakamarinho&callback=?', function(data) {
		for (i=0; i <= 2; i++){
			obj = data.images[i];
			$('.seguidores').append('<a href="http://twitpic.com/' + obj.short_id + '" target="_blank" title="' + obj.message + '"><img src="http://twitpic.com/show/thumb/'+ obj.short_id +'" width="77" height="77" alt="' + obj.message + '" title="' + obj.message + '" /></a>\n\r');
		}
	});

	/*
	ações do twitter
	*/
	$('.twitter').each(function(){
		$.getJSON('http://urls.api.twitter.com/1/urls/count.json?url=' + $(this).find('a:first').attr('rel') + '&callback=?', function(data) {
			$('.twitter a[rel="' + data.url + '"] b').html(data.count);
		});
	});
	$('.twitter .tweet').click(function(){
		var Z=550,h=450;
		var c=screen.height;
		var b=screen.width;
		var a=Math.round((b/2)-(Z/2));
		var g=0;
		var Y=$(this).attr('href');
		if(c>h){
			g=Math.round((c/2)-(h/2))
		}
		var d=window.open(Y,"twitter_tweet","left="+a+",top="+g+",width="+Z+",height="+h+",personalbar=0,toolbar=0,scrollbars=1,resizable=1");
		if(d){
			d.focus()
		}else{
			window.location.href=Y
		}
		return false;
	});
	
	/*
	ações do facebook
	*/
	//appId  : '154215241299720',
	FB.init({
		status : true, // check login status
		cookie : true, // enable cookies to allow the server to access the session
		xfbml  : false  // parse XFBML
	});
	
	$('.facebook').each(function(){
		FB.api({
			method: 'fql.query',
			query: 'SELECT total_count, url FROM link_stat WHERE url = "' + $(this).find('a:first').attr('rel') + '"'
		},
		function(response) {
			$('.facebook a[rel="' + response[0].url + '"] b').html(response[0].total_count);
		});
	});
});

(function($){
  $.fn.shuffle = function() {
    return this.each(function(){
      var items = $(this).children();
      return (items.length)
        ? $(this).html($.shuffle(items))
        : this;
    });
  }
 
  $.shuffle = function(arr) {
    for(
      var j, x, i = arr.length; i;
      j = parseInt(Math.random() * i),
      x = arr[--i], arr[i] = arr[j], arr[j] = x
    );
    return arr;
  }
})(jQuery);

// Acrescenta o método linkfy no obj String. Este método transforma urls em links clicáveis
String.prototype.linkify = function() {
	return this.replace(/\b(((https*\:\/\/)|www\.)[^\"\']+?)(([!?,.\)]+)?(\s|$))/g, function(m, l, j, i, h) {
		var k = j.match(/w/) ? "http://" : "";
		return '<a class="twtr-hyperlink" target="_blank" href="' + k + l + '">' + k + l + "</a>" + h
	});
};

String.prototype.at = function(){
	return this.replace(/\B[@]([a-zA-Z0-9_]{1,20})/g,function(h,i){
		return '@<a target="_blank" class="twtr-atreply" href="http://twitter.com/'+i+'">'+i+"</a>"}
	)
}

String.prototype.hash = function () {
	return this.replace(/(^|\s+)#(\w+)/gi, function (h, i, j) {
		return i + '<a target="_blank" class="twtr-hashtag" href="http://twitter.com/search?q=%23' + j + '">#' + j + "</a>"
	})
}

// Função que retorna uma string com o tempo relativo a time_value
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 = 'um minuto atrás'; }
	else if(delta < 120) { r = 'alguns minutos atrás'; }
	else if(delta < (45*60)) { r = (parseInt(delta / 60)).toString() + ' minutos atrás'; }
	else if(delta < (90*60)) { r = 'uma hora atrás'; }
	else if(delta < (24*60*60)) { r = '' + (parseInt(delta / 3600)).toString() + ' horas atrás'; }
	else if(delta < (48*60*60)) { r = 'um dia atrás'; }
	else { r = (parseInt(delta / 86400)).toString() + ' dias atrás'; }

	return r;
}



