//Configuración
//Cantidad de resultados que se muestran (hay que sumarle 1)
var cantidadAdmitida = 3;


// funciones para manejar facebook graph api con jquery...
var pagegraph = {
  pageid : "146086178746341",
  getFromGraph : function (connectionType,callback) { $.getJSON("https://graph.facebook.com/"+pagegraph.pageid+"/"+connectionType+"?callback=?&access_token=AAACEdEose0cBANDFuQ0fTBWyASO9z6TKENC6JUX9aER94JZAFDpJxcYmYjBUm2KwZC5cdZAi83J9dk8BftBZA0RUIw2ZBYi4k4EqrQGi8ZAhSUf0vWvHMq",function (fbData){callback(fbData.data);}); },
  getFeed : function(callback) {pagegraph.getFromGraph('feed',callback);},
  // you can add: getPhotos() , getEvents() , getVideos() ...
  getOwnFeed : function(callback) { // only the page messages in a feed
  	pagegraph.getFeed(function (feed) {
	  ownFeed = [];
	  for(var i=0;i<feed.length;i++) if (!feed[i].to) ownFeed.push(feed[i]);
	  callback(ownFeed); 
  });},
  help /*for debug, prints in html the json object*/ : function(el,elementName){
  	if (!elementName) elementName = "element";
  	var html = "<ul>";
  	$.each(el, function(i, val) {
  		i = (/[0-9]+/.test(i)) ? "["+i+"]" : "."+i;
  		if (typeof val === "object") html += "<li><strong>"+elementName+i+"</strong> " + pagegraph.viewElement(val,elementName+i) + "</li>";
  		else html += "<li><strong>"+elementName+i+"</strong> = \"" + val + "\"</li>";
	});
	html += "</ul>";
	return html; 
  },
  userImg : function (userId){return '<img class="user-photo user-'+userId+'" src="https://graph.facebook.com/'+userId+'/picture" />';},
	feedToHtml : function (feed){
	html = '<ul id="facebook-'+pagegraph.pageid+'" class="facebook">';
	
	var contadorCantidad = 0;
	
	$.each(feed, function(i, el) {
		html += '<li id="'+el.id+'" class="'+el.type+' from-'+el.from.id+'">';
		//if (el.icon) html += '<img class="icon" src="'+el.icon+'" /> ';
		//html += pagegraph.userImg(el.from.id) + '<strong class="author author-'+el.from.id+'">'+el.from.name+'</strong> ';
		var mensaje = el.message.replace(/\[\/?(?:b|i|u|url|quote|code|img|color|size)*?.*?\]/img, '');
		if (el.message) html += '<span class="face_text"><strong class="face_titulo">'+el.from.name+'</strong>&nbsp;'+mensaje+'</span>';
		/*if (el.picture || el.link || el.name || el.description)
		{
			html +='<div class="extra">';
			if (el.link && el.name)	html +='<h4 class="title facebook-link"><a href="'+el.link+'">'+el.name+'</a></h4>';
			else if (el.link) html +='<h4 class="title"><a href="'+el.link+'">'+el.link+'</a></h4>';
			else if (el.name) html +='<h4 class="title">'+el.name+'</h4>';
			if (el.link && el.picture) html += '<a class="image facebook-link" href="'+el.link+'"><img src="'+el.picture+'"/></a>';
			else if (el.picture) html += '<img src="'+el.picture+'"/>';
			if (el.description) html += '<span class="description">'+el.description+'</span>';
			html +='</div>';
		}
		if (el.likes && el.likes > 0) if (el.likes == 1) html += '<p class="likes">A una persona le gusta esto</p>'; else html += '<p class="likes">A '+el.likes+' personas les gusta esto</p>';
		if (el.source) html += '<p class="source"><a href="'+el.source+'">Descargar '+el.type+'</a></p>';
		if (el.comments)
		{
			html += '<p class="comment-count">'+el.comments.count+' comentario/s</p>';
			html += '<ul class="comments">';
			$.each(el.comments.data, function(i, c) {
				html += '<li>'+pagegraph.userImg(c.from.id)+'<strong class="author author-'+c.from.id+'">'+c.from.name+'</strong><span class="message">'+c.message+'</span></li>';
			});
			html += '</ul>';
		}*/
		html += '</li>';
		if(contadorCantidad == cantidadAdmitida){ return false; }else{ contadorCantidad++; }
	});
	
	html += '</ul>';
	return html;
  }
};

// declaracion de los dos plugins...
(function($){ 
  // Plugin para crear el feed de la pagina...  $(xxx).facebookFeed(facebookpageId);
  $.fn.facebookFeed = function( pageId ) {
  	  var pgid = pagegraph.pageId;
  	  if (pageId) pagegraph.pageId = pageId;
  	  var dom = $(this);
      pagegraph.getFeed(function (data){
		html = pagegraph.feedToHtml(data);
		dom.html(html);
		if (pageId) pagegraph.pageId = pgid;
	});
  };
  // Plugin para crear el feed de solo nuestras publicaciones en nuestra pagina. $(xxx).facebookOwnFeed(facebookpageId);
  $.fn.facebookOwnFeed = function( pageId ) {
  	  var pgid = pagegraph.pageId;
  	  if (pageId) pagegraph.pageId = pageId;
  	  var dom = $(this);
      pagegraph.getOwnFeed(function (data){
		html = pagegraph.feedToHtml(data);
		dom.html(html);
		if (pageId) pagegraph.pageId = pgid;
	});
  };
})(jQuery);

/* 
para añdirlo de id's concretos ...

$(document).ready(function(){
	// dentro de este div metemos el feed...
	$("#facebook-feed-container").facebookFeed('idPaginaFacebook');
	// dentro de este div metemos el feed con solo las publicaciones de nuestra página...
	$("#facebook-ownfeed-container").facebookOwnFeed('idPaginaFacebook');
});
*/
