var fbPageURL = "http://www.facebook.com/home.php#!/pages/Community-Hope-Church/132221160125743"; //The URL of the actual Facebook page
var pageURL = "https://graph.facebook.com/132221160125743";	// The Graph URL with our page's ID number

var graphURL = pageURL + "/posts?" +
				"access_token=186633281385451|2yUPfIY68VFU-MYnNNLU5noXlgU&" +
				 "callback=processResult&" +
				 "date_format=U&" +
				 "limit=2"; // This is the number of posts we want to show at a time
				 
var graphVideosURL = pageURL + "/videos?" +
				"access_token=145634995501895|2.AQBETSDnHauM4Act.3600.1311026400.1-688330263|Ij5zSFOXPkgPPCNvPHV3KzsY8yk";
				 


// Use JSONP to call the Graph API
function loadPosts() {
	var script = document.createElement("script");
	script.src = graphURL;
	document.body.appendChild(script);
}

/*----------------
UTILITIES
----------------*/

function parseFacebookDateTime(rawDateTime) {
	var timezone = jstz.determine_timezone().timezone;

	var dateTime = new Date(rawDateTime * 1000);
	//var offset = jstz.get_date_offset(dateTime) * 60000;
	//dateTime = new Date(rawDateTime * 1000 + offset);
	
	var formattedDateTime = "";
	
	// Month
	switch (dateTime.getMonth()) {
		case 0:
			formattedDateTime += "January";
			break;
		case 1:
			formattedDateTime += "February";
			break;
		case 2: 
			formattedDateTime += "March";
			break;
		case 3: 
			formattedDateTime += "April";
			break;
		case 4:
			formattedDateTime += "May";
			break;
		case 5:
			formattedDateTime += "June";
			break;
		case 6:
			formattedDateTime += "July";
			break;
		case 7:
			formattedDateTime += "August";
			break;
		case 8:
			formattedDateTime += "September";
			break;
		case 9:
			formattedDateTime += "October";
			break;
		case 10:
			formattedDateTime += "November";
			break;
		case 11:
			formattedDateTime += "December";
			break;
	}
	
	formattedDateTime += " ";
	
	// Day of month
	formattedDateTime += dateTime.getDate();
	
	formattedDateTime += " at ";
	
	// Hour
	var hours = (dateTime.getHours() - 2);
	var hours_mod = hours % 12;
	formattedDateTime += hours_mod;
	
	formattedDateTime += ":";
	
	// Minutes
	var minutes = dateTime.getMinutes();
	if (minutes < 10) {
		minutes = "0" + minutes;
	}
	formattedDateTime += minutes;
	
	// AM or PM
	if (hours > 11) {
		formattedDateTime += "pm";
	} else {
		formattedDateTime += "am";
	}
	
	return formattedDateTime;
}

/* ------------
POSTS
---------------*/

function processResult(posts) {
  if (posts.data.length == 0) {
	document.getElementById("loadMore").innerHTML =
	  "No more results";
  }
  else {
	graphURL = graphURL + "&until=" +
	  posts.data[posts.data.length-1].created_time;

	// Clear the loading notification in the fbContent div
	document.getElementById("fbContent").innerHTML = "";
	
	for (var post in posts.data) {
		// Get the post's ID
		var postID = posts.data[post].id;
		
		// Get the perma link for the post. We must trim the Facebook Page ID off the end of the post ID
		var permaLink = "http://www.facebook.com/home.php#!/permalink.php?story_fbid=" + 
						postID.substring(postID.length, postID.length - 15) + 
						"&id=132221160125743";
		
		var numLikes = 0;
		
		var postDiv = document.createElement("div");
		postDiv.className = "fb";
		
		/* Profile Picture */
		var picDiv = document.createElement("div");
		picDiv.className = "fbPic";
		picDiv.innerHTML = '<a href="' + fbPageURL + '"><img align="left" border="0" src="https://graph.facebook.com/132221160125743/picture" alt="" width="50" height="50" />';
		postDiv.appendChild(picDiv);
		
		/* The name of the Page/Person (in this case Community Hope Church) */
		var statusDiv = document.createElement("div");
		statusDiv.innerHTML = '<a href="' + fbPageURL + '" class=\"fbNameTitle\">Community Hope Church</a>';
		statusDiv.className = "fbStatus";
		
		/* The text of the post itself */
		var statusP = document.createElement("p");
		statusP.innerHTML = posts.data[post].message;
		
		statusDiv.appendChild(statusP);
		
		/* Time stamp of post */
		statusDiv.innerHTML += '<span class="fbTimestamp">' + parseFacebookDateTime(posts.data[post].created_time) + '</span> &#8226 ';
		
		/* Number of likes */
		try {
			numLikes = posts.data[post].likes.count;
		} catch (err) {
			
		}
		
		if (numLikes > 0) {
			statusDiv.innerHTML += '<a href="' + permaLink + '" class="fbLikeLink"><image src="images/fb_thumb.gif" border="0"/></a>';
			statusDiv.innerHTML += '  <a href="' + permaLink + '" class="fbLikeLink">' + numLikes + '</a>  &#8226 ';
		}
		
		/* PermaLink to post */
		statusDiv.innerHTML += '<a href="' + permaLink + '" class="fbLikeLink">View Post</a>';
		
		postDiv.appendChild(statusDiv);
		
		var clearDiv = document.createElement("div");
		clearDiv.className = "fbClear";
		postDiv.appendChild(clearDiv);
		
		document.getElementById("fbContent").appendChild(postDiv);

		
		var hr = document.createElement("hr");
		document.getElementById("fbContent").appendChild(hr);
	}
	
	//document.getElementById("fbContent").innerHTML += '<div align=\"right\"><a class=\"fbLikeLink\" onclick="loadPosts()">View More</a></div>';
	document.getElementById("fbContent").innerHTML += '<div align=\"right\"><a href="' + fbPageURL + '" class="fbLikeLink">View More</a></div>';
  }
}

/*--------------
VIDEOS
--------------*/


