// JavaScript Document
// This holds all the good stuff that makes the site run

$(function(j$)
{
	var mainArray;
	
	j$.ajax({url:"libraries/com/utils/FacebookWrapper.php?feedType=wallFeed", success: singleFacebookPost});
	
	function singleFacebookPost(data)
	{
		var postIndex		= 0;
		var counter			= 0;
		
		while(counter < j$(data).children("data").children("post").length)
		{
			if(j$(j$(data).children("data").children("post")[counter]).children("from").children("id").text() === "83571217750")
			{
				postIndex	= counter;
				break;
			}
			
			counter++;
		}
		
		var mainMessage		= String(j$(j$(data).children("data").children("post")[postIndex]).children("message").text());
		var footerMessage	= mainMessage;
		var link	= "";
		mainArray	= new Array();
		var footerArray	= new Array();
		
		if(j$(j$(data).children("data").children("post")[postIndex]).children("link")[0] !== undefined)
		{
			link	= j$(j$(j$(data).children("data").children("post")[postIndex]).children("link")[0].nextSibling).text();
		}
		
		if(link !== "")
		{
			mainArray.push("<a href='"+link+"' target='_blank'>");
			footerArray.push("<a href='"+link+"' target='_blank'>");
		}
		
		if(footerMessage.length > 120)
		{
			footerArray.push(footerMessage.substring(0, 120)+"...");
		}
		else
		{
			footerArray.push(footerMessage);
		}
		
		if(mainMessage.length > 220)
		{
			mainArray.push(mainMessage.substring(0, 220)+"...");
		}
		else
		{
			mainArray.push(mainMessage);
		}
		
		if(link !== "")
		{
			mainArray.push("</a>");
			footerArray.push("</a>");
		}
		
		j$(".infoContainer .description").animate({opacity: 0}, 500, loaderFadeOutCompleteHandler);
		j$(".blogFeed").html(footerArray.join(""));
	}
	
	function loaderFadeOutCompleteHandler()
	{
		j$(this).html(mainArray.join("")).animate({opacity: 1}, 500);
	}
});
