// JQuery Embed for Facebook
// Author: Loren Heyns
// Company: DreamStudio.com
// Please send updates to http://DreamStudio.com/account/write/8
// This message must remain at top of script

var fbFolder = '111662108795'; // GL - to change
var showComments = true;

// Alternate - requires popup for user to signin via Facebook to get their individual access token.  
function fbFetchAccess() {
	//alert('1');
	FB.login(function (response) {
	    //alert('2');
	    if (response.session) {

	        FB.getLoginStatus(function (response) {
                // FireFox is not typically making it this far.
	            //alert('response');
	            if (response.session) {
	                //alert('connected');
	                //$('#connect_button').hide();
	                //$('#res_json').html('loading...');
	                token = response.session.access_token;
	                fbFetch(response.session.access_token);
	            }
	        });

	    } else {

	        alert('Not logged into Facebook.  Unable to show latest news.');

	    }

	});
}
function fbFetch(token) {
	// Callback is set with a '?' to overcome the cross domain problems with JSON

    // You can also request multiple objects in a single query using the "ids" query parameter.
    // For example, the URL https://graph.facebook.com?ids=arjun,vernal returns both profiles in the same response.
	// since=today&

	// Swicth from /posts to /feed to show everyone
	var url = "https://graph.facebook.com/" + fbFolder + "/posts?access_token=" + token + "&limit=3&callback=?";
	                        
    //alert(url);
	//Use jQuery getJSON method to fetch the data from the url and then create our unordered list with the relevant data.
	$.getJSON(url, function (json) {
	    var days = new Array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday');
	    var months = new Array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');
	    var html = "<ul>";
	    //loop through and within data array's retrieve the message variable.
	    $.each(json.data, function (i, fb) {

	        if (fb.link) {
	            html += "<li id='linked' onclick='javascript:document.location=\"" + fb.link + "\"'>";
	        } else {
	            html += "<li id='linked' onclick='javascript:document.location=\"https://www.facebook.com/" + fbFolder + "\"'>";
	        }
	        if (!fb.from.category) {
	            html += "<img style='float:left; margin-right:4px' src='https://graph.facebook.com/" + fb.from.id + "/picture' alt='' />";
	        }

	        if (fb.picture) {
	            html += "<img src='" + fb.picture + "' align='left' style='margin-right:10px; margin-bottom:2px; max-width:160px' alt='' />";
	        }
	        if (fb.name) {
				fbname = fb.name;
	            if (fb.name.indexOf(" ") == -1 && fb.name.length > 40) {
					if (fb.name.length < 80)
					{
						fbname = fb.name.substring(0,40) + '<br />' + fb.name.substring(41);
						
					} else {
						fbname = '';
					}
				}
				if (fbname.length > 0) {
					if (fb.link) {
						html += "<b><a href='javascript:document.location=\"" + fb.link + "\"'>" + fbname + "</a></b><br />";
					} else {
						html += "<b><a href='javascript:document.location=\"https://www.facebook.com/" + fbFolder + "\"'>" + fbname + "</a></b><br />";
					}
				}
	        }
	        if (fb.created_time) {
	            var startDate = new Date(formatFBTime(fb.created_time));
	            var startDateString = days[startDate.getDay()] + ' at ' + showTheHours(startDate.getHours()) + ':' + showZeroFilled(startDate.getMinutes()) + ' ' + showAmPm(startDate);
	            html += '<div class="messagePostDate">Posted ' + startDateString + '</div>';
	        }
	        if (fb.message) {
	            html += "<div >" + fb.message + "</div>";
	        }
			
	        if (fb.description) {
	            html += "<div class='newsfeedDesc'>" + fb.description.replace(fb.name, "") + "</div>";
	        }
	        // Comments
	        // Only summary of comments is displayed, just like facebook default, only 2 available unless you "read more" or "view all"... 
	        // the graph call for "view all comments" is get the post ID of that post and append "/comment"...
	        // example:
	        // FEED: https://graph.facebook.com/161439580553446/feed
	        // A POST FROM THE FEED: https://graph.facebook.com/161439580553446_177522428945161
	        // VIEW ALL COMMENTS: https://graph.facebook.com/161439580553446_177522428945161/comments


	        html += '<div style="clear:both"></div>';

	        // COMMENTS
	        var splitID = fb.id.split("_");
	        if (showComments) {
	            if (fb.comments) {
	                $.each(fb.comments, function (i, theComments) {
	                    if (i == "count") {
	                        if (theComments > 2) {
	                            html += "<a class='addComment' href='https://www.facebook.com/" + fbFolder + "/posts/" + splitID[1] + "'>" + (theComments - 2) + " more comments - Add comment</a><br />";
	                        } else if (theComments == 1) {
	                            html += "<a class='addComment' href='https://www.facebook.com/" + fbFolder + "/posts/" + splitID[1] + "'>Add comment</a><br />";
	                        }
	                    }

	                    if (i == "data") {
	                        $.each(theComments, function (i, data) {

	                            data.message = data.message.replace(new RegExp("\\n", "g"),"<br />");
	                            html += "<div class='comment'><img class='commentImage' src='https://graph.facebook.com/" + data.from.id + "/picture' alt='' />";
	                            html += data.message + "<br />";
	                            if (data.created_time) {
	                                var startDate = new Date(formatFBTime(data.created_time));
	                                var startDateString = days[startDate.getDay()] + ' at ' + showTheHours(startDate.getHours()) + ':' + showZeroFilled(startDate.getMinutes()) + ' ' + showAmPm(startDate);
	                                html += '<span style="color:#aaa">Posted ' + startDateString + '</span><br />';
	                            }

	                            html += "</div><div style='clear:both'></div>";
	                        });
	                    }

	                });
	            } else {
	                html += "<a class='addComment' href='https://www.facebook.com/" + fbFolder + "/posts/" + splitID[1] + "'>Add comment</a><br />";
	            }
	        }

	        //	                            if (fb.properties.name) {
	        //	                                    html += fb.properties.name + "<br />";
	        //                              }

	        //	                                if (fb.updated_time) {
	        //	                                    var offset = new Date(fb.updated_time);
	        //	                                    //var offset = curdate.getTimeZoneOffset();
	        //	                                    var startDateString = days[offset.getDay()] + ' at ' + offset.getHours() + ':' + offset.getMinutes();
	        //	                                    //+ ", " + months[offset.getMonth()] + ' ' + offset.getDate() + ', ' + fourdigits(offset.getYear());
	        //	                                    html += 'Updated ' + startDateString + '</a><br />';
	        //	                                }



	        html += "<div style='clear:both'></div></li>";
	    });
	    html += "</ul>";


	    //Animate - Fuzzy text with IE8
	    //	                            $('.newsfeed').animate({ opacity: 0 }, 500, function () {

	    //	                                $('.newsfeed').html(html);

	    //	                            });

	    //	                            $('.newsfeed').animate({ opacity: 1 }, 500);

	    // Temp, no animation
	    $('.newsfeed').html(html);
	});

};
function formatFBTime(fbDate) {
	// For Explorer 8 and Firefox 3
	var arrDateTime = fbDate.split("T");
	var arrDateCode = arrDateTime[0].split("-");
	var strTimeCode = arrDateTime[1].substring(0, arrDateTime[1].indexOf("+"));
	var arrTimeCode = strTimeCode.split(":");
	var valid_date = new Date()
	valid_date.setUTCFullYear(arrDateCode[0]);
	valid_date.setUTCMonth(arrDateCode[1] - 1);
	valid_date.setUTCDate(arrDateCode[2]);
	valid_date.setUTCHours(arrTimeCode[0]);
	valid_date.setUTCMinutes(arrTimeCode[1]);
	valid_date.setUTCSeconds(arrTimeCode[2]);
	return valid_date;
}
function fourdigits(number) {
	return (number < 1000) ? number + 1900 : number;
}
function showTheHours(theHour) {
	if (theHour > 0 && theHour < 13) {
	    if (theHour == "0") theHour = 12;
	    return (theHour);
	}
	if (theHour == 0) {
	    return (12);
	}
	return (theHour - 12);
}
function showZeroFilled(inValue) {
	if (inValue > 9) {
	    return "" + inValue;
	}
	return "0" + inValue;
}
function showAmPm(inDate) {
	if (inDate.getHours() < 12) {
	    return (" am");
	}
	return (" pm");
}
 
	                
