/** 
 *  @author Fabian Nöthe - hello@insnet.de - www.insnet.de
 *  @description This script is based on my twitterWall (twitterwall.insnet.de)
 *  Attention: updateTime, playTime, buffer is not yet supported in this demo.
 *  This is just a demo to show what you can do with facebook open graph protocol (http://developers.facebook.com/docs/opengraph)
 */
FacebookWall = function() {
	
   /***************************************************************************
	 * private variables and functions
	 **************************************************************************/
	var lastid = 0;
	var isFirstRun = true;
	var count = 0;
	var position;
	var updateTimer;
	var playTimer;
	var postPool = new Array();
	var activePool = new Array();
	var isPlaying = false;
	
	// config	
	var config = {	container:"#post_container",
					id: "295956422141", 
					updateTime: 10000, 
					playTime: 1000, 
					showPosts: 6, 
					buffer: 20, 
					autoPlay: true
			  	};

   function init (){
		getPosts();
    }

    /*
     * load new json data
     */   
    function getPosts() {	
		$.jsonp({
			url: "https://graph.facebook.com/"+config.id+"/feed&limit=1",
		  	callbackParameter: "callback",
  			//cache: true,
		  	success: function(json, textStatus) {
					clearInterval(updateTimer);
					onJsonLoadHandler(json);
		  	},
		  	error: function(XMLHttpRequest, textStatus, errorThrown) {
				//console.error("onError");
		  	},
		  	complete: function(request, settings) {
		    	//console.info("onComplete");
		  	}
		});

	}	
	
    /*
     * json loading complete
     */  	
	function onJsonLoadHandler(json) {
		var posts = json.data
		
		// check for new posts
		if(json.data.length > 0) {
			
			// loop over posts
			var post = "";
			$.each(json.data.reverse(), function() {
				
				// alternate rows
				var position = (count % 2) ? "right" : "left";
				count++;
				

				// render types
				if(this.source != undefined) {
					// video
					post = renderVideo(this, position);			
				} else if (this.message != undefined) {
					// message
					post = renderMessage(this, position);			
				} else if(this.link != undefined) {
					// link
					post = renderLink(this, position);			
				} else {
					console.error("Missing renderer for post.");
				}
									
				// add post to stack
				postPool.unshift(post);
				
				// save last id
				lastid = this.id;
			});
			
		}
		
		// remove loader
		if(isFirstRun) $('#loading').remove();
		
		// set first run
		isFirstRun = false;
		
		// loadPosts if buffer is not full
		if(activePool.length < config.buffer) {
			//updateTimer = setInterval( function(){ getPosts();}, config.updateTime);	
		} 
		
		if(!isPlaying && config.autoPlay) {
			play();
		}

	}
	
	/*
	 * render methods
	 */
	 
	 function renderMessage(postVO, position) {
		
		 
		// build post
		post = '<div class="tweet '+position+'" id="'+postVO.id+'">';
		post += '<p class="post">'+postVO.message+'</p>';
		post += '<div class="meta'+position+'"><p class="pic"><a href="http://www.facebook.com/profile.php?id='+postVO.from.id+'" target="_blank" >';
		post += '<img alt="" src="https://graph.facebook.com/'+postVO.from.id+'/picture" class="author_avatar" width="48" height="48" /></a></p><p class="by"><a href="http://www.facebook.com/profile.php?id='+postVO.from.id+'" target="_blank" >'+postVO.from.name+'</a></p></div>';
		post += '<div style="clear:both;" />';
		post += '</div>';
		
		return post;
	 }

	 function renderLink(postVO, position) {
		
		 
		// build post
		post = '<div class="tweet '+position+'" id="'+postVO.id+'">';
		post += '<p class="post"><a href="'+postVO.link+'"><img src="'+postVO.picture+'" width="130" height="73" /></a> '+postVO.name+'</p>';
		post += '<div class="meta'+position+'"><p class="pic"><a href="http://www.facebook.com/profile.php?id='+postVO.from_user+'">';
		post += '<img alt="" src="https://graph.facebook.com/'+postVO.from.id+'/picture" class="author_avatar" width="48" height="48" /></a></p><p class="by"><a href="http://www.facebook.com/profile.php?id='+postVO.from.id+'">'+postVO.from.name+'</a></p></div>';
		post += '<div style="clear:both;" />';
		post += '</div>';
		
		return post;
	 }

	 function renderVideo(postVO, position) {
		
		 
		// build post
		post = '<div class="tweet '+position+'" id="'+postVO.id+'">';
		post += '<p class="post"><a class="video" href="'+postVO.link+'"><img src="'+postVO.picture+'" width="130" height="73" /><i class="video"></i></a> '+postVO.name+'</p>';
		post += '<div class="meta'+position+'"><p class="pic"><a href="http://www.facebook.com/profile.php?id='+postVO.from_user+'">';
		post += '<img alt="" src="https://graph.facebook.com/'+postVO.from.id+'/picture" class="author_avatar" width="48" height="48" /></a></p><p class="by"><a href="http://www.facebook.com/profile.php?id='+postVO.from.id+'">'+postVO.from.name+'</a></p></div>';
		post += '<div style="clear:both;" />';
		post += '</div>';
		
		return post;
	 }


    /*
     * show next post
     */  	
	function showNext() {		
			
			// add  and remove posts
			if(postPool.length  > 0) {
				post = postPool.pop();
				
				prepender = $(post);
				
				activePool.unshift(prepender);
				
				$(config.container).prepend(prepender);
				
				// workaround: delay 1 sek to load images
				prepender.hide();
				prepender.delay(1000).css({'margin-top':'-250px'});
				prepender.show().animate( { marginTop:"0" } , 800, 'easeOutQuad' );	
				
				
				// remove posts
				if(activePool.length-1 >= config.showPosts) {					
					remover = activePool.pop();			
					remover.delay(1000).fadeOut("slow", function() { $(this).remove(); })
				} 
			} 
	}
	
    /*
     * play and pause
     */  
	function play() {
		isPlaying = true;
		playTimer = setInterval( function(){ showNext();}, config.playTime);
	}
	
	function pause() {
		isPlaying = false;
		clearTimeout(playTimer);
	}
	/***************************************************************************
	 * public space
	 **************************************************************************/
	return {
		
		init: function(conf) {
			if(conf.container != undefined) config.container = conf.container;
			if(conf.id != undefined) config.id = conf.id;
			if(conf.updateTime != undefined) config.updateTime = conf.updateTime;
			if(conf.playTime != undefined) config.playTime = conf.playTime;
			if(conf.showPosts != undefined) config.showPosts = conf.showPosts;
			if(conf.buffer != undefined) config.buffer = conf.buffer;
			if(conf.autoPlay != undefined) config.autoPlay = conf.autoPlay;
			
			init();
		},
	   
		showNext : function () {
			showNext();
		},
		
		play : function () {
			play();
		},
		
		pause: function () {
			pause();
		}
   };
}();
