/**
Plugin Creado por ikhuerta.
Más info en http://blog.ikhuerta.com/plugin-jquery-para-incluir-el-muro-de-una-pagina-de-facebook-en-tu-web
*/
// funciones para manejar facebook graph api con jquery...
var pagegraph = {
	pageid : "116374171720257",
	feedLimit : 5,
	fnCallBack : false,
	getFromGraph : function(connectionType, callback){
		/*var graphURL = "https://graph.facebook.com/" + 
						pagegraph.pageid + "/" + 
						connectionType + "?limit=" + pagegraph.feedLimit + 
						"&access_token=2227470867|2.AQCTsvimSDvGFPYp.3600.1307505600.0-298328590905|32PvUIKET48rHtUBl5BGXC5NSVs" +
						"&callback=?";*/
		var graphURL = siteBase + "_ajx/fb_getAccessToken.php";
		$.getJSON(	
			graphURL, 
			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);
		});
	},
	
	//for debug, prints in html the json object
	help : 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">';
		$.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> ';
			if (el.message) {
				html += '<span class="message">' + el.message + '</span>';
			}
			if (el.picture || el.link || el.name || el.description){
				html +='<div class="extra">';
				
				var picture_width;
				var picture_obj;
				if(el.picture){
					picture_obj = $('<img />');
					picture_obj.attr('src', el.picture);
					//console.log('src '+ el.picture );
					//console.log(picture_obj);
					//console.log('tamaño de imagen' + picture_obj[0].width);
					//picture_width = (picture_obj.get(0).width > 130 ) ? 130 : picture_obj[0].width; 
					picture_width = (picture_obj[0].width > 130 ) ? 130 : picture_obj[0].width;
					//console.log('tamaño calculado: ' + picture_width);
				}
				
				if (el.link && el.picture) {
					html += '<a class="image facebook-link" href="' + el.link + '" target="_blank" ><img src="' + el.picture + '" width="' + picture_width + '"/></a>';
				}else if (el.picture) {
					html += '<img src="' + el.picture + '" width="' + picture_width + '"/>';
				}
				html +='<div class="extra-right">';
				if (el.link && el.name)	{
					html +='<h4 class="title facebook-link"><a href="' +el.link + '" target="_blank" >' + el.name + '</a></h4>';
				}else if (el.link) {
					//html +='<h4 class="title"><a href="' + el.link + '"  target="_blank">' + el.link + '</a></h4>';
					html +='<h4 class="title"><a href="' + el.link + '" target="_blank" > Ver Publicaci&oacute;n</a></h4>';
				}else if (el.name) {
					html +='<h4 class="title">' + el.name + '</h4>';
				}
				//html +='<h5 class="target-link"><a href="' + el.link + '" target="_blank" >' + el.link + '</a></h5>';
				if (el.description) {
					html += '<span class="description">' + el.description + '</span>';
				}
				html +='</div>';
				html +='<div style="clear:both;"></div>';
				html +='</div>';
			}
			if (el.likes != undefined && el.likes.count > 0){ 
				var like_names = '';
				var separator = '';
				for(i = 0; i < el.likes.count; i++){
					if(el.likes.data[i] != undefined && el.likes.data[i] != ''){
						separator = (el.likes.count > 1 && i < el.likes.count - 1) ? ', ' : '';
						like_names += el.likes.data[i].name + separator;
					}
				}
				/*$.each(el.likes.data, function(i, l) {
					separator = (el.likes.count > 1 && i < el.likes.count - 1) ? ', ' : '';
					like_names += l.name + separator;
					console.log(l.name + separator);
				});*/
				var likeIcon = '<i class="likeIcon"></i>';
				if (el.likes.count == 1) {
					html += '<p class="likes ttip" title="' + like_names + '">' + likeIcon + ' A una persona le gusta esto</p>'; 
				}else{ 
					html += '<p class="likes ttip" title="' + like_names + '">' + likeIcon + ' A ' + el.likes.count + ' personas les gusta esto</p>';
				}
			}
			if (el.source) {
				html += '<p class="source"><a href="' + el.source + '">Descargar ' + el.type + '</a></p>';
			}
			if (el.comments != undefined && el.comments.count > 0){
				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) + ' <span class="message"><strong class="author author-' + c.from.id + '">' + c.from.name + '</strong> ' + c.message + '</span></li>';
				});
				html += '</ul>';
			}
			html += '</li>';
		});
		html += '</ul>';
		return html;
	}
};

// declaracion de los dos plugins...
(function($){
	
	// Plugin para crear el feed de la pagina...  $(xxx).facebookFeed(facebookpageId);
	$.fn.facebookFeed = function(pId, limit, fnCallBack) {
		var pgid = pagegraph.pageid;
		if (pId) {
			pagegraph.pageid = pId;
		}
		if(limit != undefined){
			pagegraph.feedLimit = limit;
		}
		var dom = $(this); // Container where the content will be applied to
		pagegraph.getFeed(function (data){
			//console.log(data.length);
			html = pagegraph.feedToHtml(data);
			dom.html(html);
			if (pId) {
				pagegraph.pageid = pgid;
			}
			if(fnCallBack != undefined){
				pagegraph.fnCallBack = fnCallBack;
			}
			
			if(pagegraph.fnCallBack){
				pagegraph.fnCallBack();
			}
		});
	};
	
	// Plugin para crear el feed de solo nuestras publicaciones en nuestra pagina. $(xxx).facebookOwnFeed(facebookpageId);
	$.fn.facebookOwnFeed = function(pId) {
		var pgid = pagegraph.pageid;
		if (pId) {
			pagegraph.pageid = pId;
		}
		if(limit != undefined){
			pagegraph.feedLimit = limit;
		}
		var dom = $(this); // Container where the content will be applied to
		pagegraph.getOwnFeed(function (data){
			html = pagegraph.feedToHtml(data);
			dom.html(html);
			if (pId) {
				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');
});
*/
