// JavaScript DocumentinitTwitter ();


function initTwitter () {
	$("#content").tweet({
		//username: ["mnoleto", "nandico", "carol_albuquerq", "ThalesPessoa", "daniloslv", "fmarques88", "wfmarques"],
		username: ["mnoleto", "nandico", "carol_albuquerq"],
		join_text: "auto",
		avatar_size: 32,
		count: 10,
		query:"from:carol_albuquerq OR from:mnoleto OR from:nandico adjetiva",
		callback: function(){
			//alert(0);
			//$("#content").masonry({columnWidth: 190, resible: true, saveOptions: true, itemSelector: ".post:visible"});
			initMasonry () ;
		}
		
	});
}


// ********************************
// MASONRY.JS
// ********************************

function initMasonry () {
	$("#content").masonry({columnWidth: 190, resible: true, saveOptions: true, itemSelector: ".post:visible"});
	
	// Interval created for problems of the masonry loading in Safari
	/*
	var i = setInterval(function() {
		if($("#content").length) {
            $("#content").masonry({columnWidth: 190, resible: true, saveOptions: true, itemSelector: ".post:visible"});
            clearInterval(i);
        }}, 
    1);
	*/
}



	
// ********************************
// FILTERS
// ********************************

function initFilters () {
	// Show all posts in the beggining
	$(".work").show();
	$(".learn").show();
	$(".love").show();
	
	// filters buttons events
	$("#work-menu").click(function(){
		filterPosts ($(this), $(".work"));
		return false;
	});
	
	$("#learn-menu").click(function(){
		filterPosts ($(this), $(".learn"));
		return false;
	});
	
	$("#love-menu").click(function(){
		filterPosts ($(this), $(".love"));
		return false;
	});
}

// filters init function
function filterPosts (button, obj) {
	
	if (!verifyActiveFilter(obj)) {
		//alert(0);
		return false;
	}
	
	changeButtonState (button, obj);
	showHidePosts (obj);
}


// change the visual state of the filters buttons
// bottom = inactive
// top = active
function changeButtonState (button, obj) {
	if(obj.is(":visible")){
		button.css ("background-position", "bottom");
	} else {
		button.css ("background-position", "top");
	}
}


function verifyActiveFilter (obj) {
	if(obj.is(":visible")){
		var activeNum = 0;
		
		if( $(".work").is(":visible")) activeNum ++;
		if( $(".learn").is(":visible")) activeNum ++;
		if( $(".love").is(":visible")) activeNum ++;
		//alert(activeNum);
		if (activeNum <= 1) {
			return false;
		}
	}
	return true;
}


function showHidePosts (obj) {
	if(obj.is(":visible")){
		obj.hide("slow", function (){
			$("#content").masonry(); // re-run masonry after hidding posts
		});
	} else {
		obj.show("slow", function (){
			$("#content").masonry(); // re-run masonry after showing posts
		});
	}
}


$(window).load(function () {
	$(document).pngFix();
	
	initTwitter ();
	initFilters ();
});
