/*
 * JavaScript Pretty Date
 * Copyright (c) 2008 John Resig (jquery.com)
 * Licensed under the MIT license.
 */

// Takes an ISO time and returns a string representing how
// long ago the date represents.
function prettyDate(time){
	var date = new Date((time || "").replace(/-/g,"/").replace(/[TZ]/g," ")),
		diff = (((new Date()).getTime() - date.getTime()) / 1000),
		
		// add in timezone offset
		diff = diff + ((new Date).getTimezoneOffset()*60);
		day_diff = Math.floor(diff / 86400);
			
	if ( isNaN(day_diff) || day_diff < 0 || day_diff >= 31 )
		return false;
			
	return day_diff == 0 && (
			diff < 60 && "gerade" ||
			diff < 120 && "vor einer Minute" ||
			diff < 3600 && "vor " + Math.floor( diff / 60 ) + " Minuten" ||
			diff < 7200 && "vor einer Stunde" ||
			diff < 86400 && "vor " + Math.floor( diff / 3600 ) + " Stunden") ||
		day_diff == 1 && "Gestern" ||
		day_diff < 7 && "vor " + day_diff + " Tagen" ||
		day_diff < 31 && "vor " + Math.ceil( day_diff / 7 ) + " Wochen";
}

// uft8 encode and decode
function encode_utf8( s ){
  return unescape( encodeURIComponent( s ) );
}

function decode_utf8( s ){
  return decodeURIComponent( escape( s ) );
}

function isIE(){
	return /msie/i.test(navigator.userAgent) && !/opera/i.test(navigator.userAgent);
}

//======================================================================

var controller;
$(document).ready(function(){
	controller = new controller('stream');
	
	// Setup JQuery History
	$.history.init(function(){
    	var pagehash = window.location.hash.substr(1,window.location.hash.length);
      	controller.clearQueue();
      	if(pagehash!=''){
			controller.showCircle();
        	var searchterms = pagehash.split('|');
        	controller.searchterms = searchterms;
        	//console.log(controller.searchterms);
        	controller.resetSearchTimer();
      	}
  	});
  	$('#searchform').submit(function(e){
		if(!isIE()){
			e.preventDefault();
		}							 
			
		var q = $('#searchquery').attr('value');
      	var pagehash = window.location.hash.substr(1,window.location.hash.length);
		if(pagehash!=''){	
			if(pagehash !== q){
				$('#stream').html('');
				controller.clearQueue();
				window.location.hash = q;
				
				$('#button_play').removeClass('clicked');
				$('#button_pause').removeClass('clicked');
				if(isIE()){
					$('#stream').html('');
					controller.clearQueue();
					document.location.href = window.location;
				}
				
   		
			}
		}
		window.location.hash = q;
		return false;
  	});
	
	$('#button_play').click(function(e){
		e.preventDefault();
		$(this).addClass('clicked');
		$('#button_pause').removeClass('clicked');
		controller.restartQueueTimer();
  	});
	
	$('#button_pause').click(function(e){
		e.preventDefault();
		$(this).addClass('clicked');
		controller.pauseQueueTimer();
		$('#button_play').removeClass('clicked');
		
  	});
	
	
});

function controller(id){
	this.obj = document.getElementById(id);
	this.queue = [];
	this.searchterms = []
	
	this.pause_queue = false;

	this.searchtimer = null;
	this.queuetimer = null;
	this.timestamptimer = null;
  
  	this.startSearchTimer();
  
  	var that = this;
  	this.queuePop();
	
	this.queuetimer = setInterval(function(){
   		that.queuePop();
  	}, 1000); // every 1 secs
  
  	this.timestamptimer = setInterval(function(){
    	that.updateTimestamps();
  	}, 5000); // every 5 secs
}


controller.prototype.showCircle = function(){
	document.getElementById('loading_circle').style.display = "block";
}

controller.prototype.hideCircle = function(){
	document.getElementById('loading_circle').style.display = "none";
}


controller.prototype.startSearchTimer = function(){
	var that = this;
  	this.doSearches(); // start now
  	this.searchtimer = setInterval(function(){
		that.doSearches();
  	}, 20000); // every 20 secs
}

controller.prototype.stopSearchTimer = function(){
	clearInterval(this.searchtimer)
}

controller.prototype.pauseQueueTimer = function(){
	clearInterval(this.queuetimer)
}

controller.prototype.restartQueueTimer = function(){
	var that = this;
	this.queuetimer = setInterval(function(){
   		that.queuePop();
  	}, 1000);
}

controller.prototype.resetSearchTimer = function(){
	this.stopSearchTimer();
  	this.startSearchTimer();
}

controller.prototype.clearQueue = function(){
  	this.queue = [];
}

controller.prototype.doSearches = function(){
	var that = this;
  	if(this.searchterms.length < 1) return false;
  	$(this.searchterms).each(function(){
  		//console.log('searching for: '+this);
    	$.getJSON('http://graph.facebook.com/search?q='+escape(this)+'&callback=?', function(data){
			that.hideCircle();																				 
      		that.handleSearchResults(data.data);
    	}); 
  	})
}

controller.prototype.updateTimestamps = function(){
  $('li div.timestamp-raw', this.obj).each(function(){
    var raw = $(this).html();
    $(this).prev("span.spantist").html(prettyDate(raw))
  })
}

controller.prototype.handleSearchResults = function(data){
  var that = this;
  var tempqueue = [];
  $(data).each(function(){
    if(!that.isPostAlreadyInDom(this.id)){
      //no, carry on
      if(!that.isPostAlreadyInQueue(this.id)){
        //no, good to add to queue
        //console.log('adding to queue: '+this.id)
        if(this.message){
          tempqueue.push({
            id: this.id,
            html: that.renderListItem(this),
            raw: this
          })
        }
      }
    }    
  })
  this.queue = this.queue.concat(tempqueue.reverse());
}

controller.prototype.isPostAlreadyInQueue = function(post_id){
  var found = false;
  $(this.queue).each(function(){
    if (this.id == post_id){
      //console.log(post_id+' WAS found in queue');
      found = true;
    }
  })
  //console.log(post_id+' not found in queue');
  return found;
}

controller.prototype.isPostAlreadyInDom = function(post_id){
  var found = false;
  $('li', this.obj).each(function(){
    if ($(this).attr('id') == post_id){
      //console.log(post_id+' WAS found in dom');
      found = true;
    }
  })
  //console.log(post_id+' not found in dom');
  return found;
}

controller.prototype.renderListItem = function(post){
  var html = '<li id="'+post.id+'">';
  html+= '<img height="50" width="50" src="http://graph.facebook.com/'+post.from.id+'/picture">';
 	 html+= '<div class="spitze"></div>'
 html+= '<div class="body">'
  html+= '  <div class="user"><a href="http://www.facebook.com/profile.php?id='+post.from.id+'">'+post.from.name+'</a>&nbsp;</div>';
  html+= '  <div class="message">'+post.message.substr(0,500)+'</div>';
  var datestr = post.created_time.substr(0,post.created_time.length-5)+'Z';
  //console.log(datestr);
  html+= '  <div class="timestamp"><a href="http://www.facebook.com/permalink.php?story_fbid='+post.id+'&id='+post.from.id+'" title="Permalink"><span class="spantist">'+prettyDate(datestr)+'</span></a></div>';
  html+= '  <div class="timestamp-raw" style="display:none;">'+datestr+'</div>';
  html+= '</div></li>';
  return html;
}

controller.prototype.queuePop = function(){
  var post = this.queue.shift();
  if (post){
    //console.log('shifting from queue: '+post.id)
    var newnode = $(post.html);
    
    // put it in the test and get the height
    var heighttest = $('#heighttest');
    heighttest.empty();
    heighttest.append(newnode);
    var height = heighttest.height();

    // prepare for insertion
    newnode.css('opacity', 0);
    newnode.css('top', -200);
    newnode.css('height', 0);
    
    // insert
    $(this.obj).prepend(newnode);
    
    //animate in
    newnode.animate({
      opacity: 1,
      top: 0,
      height: (height-10)
    });

    if($('li',this.obj).size()>50){
      $('li:last', this.obj).remove();
    }

  }

}
