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

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

dateDayArray = new Array(" januar "," februar "," marts "," april "," maj "," juni "," juli "," august "," september "," oktober "," november "," december ");


var FBStatus = function(facebookId, maxNumItems, readAllText, maxNumChars, displayComments, displayLikes, displayAll) {
    var authUrl = 'https://graph.facebook.com/oauth/access_token?client_id=124955394252487&client_secret=465ba88b70e29a8ae884bcfea6ebf3de&grant_type=client_credentials';
  
  $.ajax({url: '/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('/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 == 'photo') && count <= maxNumItems) {
                        count++;
                        var msg = fb.message;
                        var msgLength = msg.length;
                        var photo = (fb.type=='photo')?'<img class="fb-photo" src="'+fb.picture.replace(/_s\.jpg$/, '_b.jpg')+'" alt="'+fb.message+'" />':'';
                        var fb_date_pattern = /([0-9][0-9][0-9][0-9])\-([0-9][0-9])\-([0-9][0-9])T([0-9][0-9])\:([0-9][0-9])\:([0-9][0-9])\+0000/;
                        var date_array = fb_date_pattern.exec(fb.created_time);
                        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;
                            }
                        }
                        var hourInt = parseInt(date_array[4], 10);
                        hourInt = hourInt + 2;
                        html += '<li class="fb-status">';
                        html += '<strong class="">Fra Facebook</strong> | ';
                        html += '<span class="fb-date">' + date_array[3] + "." + dateDayArray[parseInt(date_array[2],10)-1] + hourInt  + ":" + date_array[5] + '</span>';  
                        html += '<p class="fb-msg">' + msg + '</p>';
                        html += photo;
                        // html += '<a href="http://www.facebook.com/luka.vejle">Facebook</a>';

                        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);
            });
        }
    } 
    });
}
