// Facebook status
// More documentation on: http://developers.facebook.com/docs/api

function displayMsg(link, dots, msg) {
    $('#' + link).hide();
    $('#' + dots).hide();
    $('#' + msg).fadeIn();
}

var FBStatus = function(facebookId, maxNumItems, readAllText, maxNumChars, displayComments, displayLikes, displayAll) {
    var authUrl = 'https://graph.facebook.com/oauth/access_token?client_id=174113442652753&client_secret=2d538066f4471be9bc14d8e57e559305&grant_type=client_credentials';
  
  $.ajax({url: '/usercontrols/json.ashx?url=' + escape(authUrl), dataType: 'text', success: function (data) {    
    if (data.indexOf('access_token') != -1) {
      var accessToken = data.substr(13);

            var feedUrl = 'https://graph.facebook.com/' + facebookId + '/feed?access_token=' + accessToken;
            var maxNumChars = Number(maxNumChars);

            $.getJSON('/usercontrols/json.ashx?url=' + escape(feedUrl), {}, function(json) {
                var html = '<ul class="fb-status-list">';
                var count = 1;
                $(json.data).each(function(i, fb) {
                    if ((fb.from.id == facebookId || displayAll == 1) && (fb.type == 'status' || fb.type == 'link' || fb.type == 'video' || fb.type == 'swf') && count <= maxNumItems) {
                        count++;
                        var msg = fb.message;
                        var msgLength = msg.length;
                        if (maxNumChars != '') {
                            if (msgLength > maxNumChars) {
                                msgEnd = '<span id="msg-end-' + count + '" class="fb-msg-end" style="display:none;">' + msg.substring(maxNumChars, msgLength) + '</span>';
                                msgDots = '<span id="dots-' + count + '" class="dots">...</span> <a href="javascript:displayMsg(\'link-' + count + '\',\'dots-' + count + '\',\'msg-end-' + count + '\')" id="link-' + count + '" class="readAllLink">' + readAllText + '</a>';
                                msg = msg.substring(0, maxNumChars);
                                msg += msgEnd + msgDots;
                            }
                        }
                        html += '<li class="fb-status">';
                        html += '<a class="fbTitle" href="http://facebook.com/pages/CINEads-Australia/115689201781851/">' + fb.from.name + '</a> <span class="fb-msg">' + msg + '</span>';

                        if (fb.likes && displayLikes == 1) {
                            html += '<div class="fb-likes">' + fb.likes + '</div>';
                        }

                        if (fb.comments && displayComments == 1) {
                            html += '<ul>';
                            if (fb.comments.data) {
                                $(fb.comments.data).each(function(i, fb) {
                                    html += '<li class="fb-comment"><img src="https://graph.facebook.com/' + fb.from.id + '/picture" alt="' + fb.from.name + '"/><span class="fb-name"><b>' + fb.from.name + '</b></span> <span class="fb-msg">' + fb.message + '</span></li>';
                                });
                            }
                            html += '</ul>';
                        }

                        html += '</li>';
                    }
                });
                html += '</ul>';

                $('#fbFeed').animate({ opacity: 0 }, 500, function() { $('#fbFeed').html(html); });
                $('#fbFeed').animate({ opacity: 1 }, 500);
            });
        }
    } 
    });
}
