$(function() {
	$(window).scroll(function(){
		/* when reaching the element with id "last" we want to show the slidebox. Let's get the distance from the top to the element */
		var distanceTop = $('#bottom-inner').offset().top - $(window).height();
		
		if  ($(window).scrollTop() > distanceTop)
			$('#slidebox').animate({'right':'0px'},300);
		else 
			$('#slidebox').stop(true).animate({'right':'-430px'},100);	
	});
	
	/* remove the slidebox when clicking the cross */
	$('#slidebox .close').bind('click',function(){
		$(this).parent().remove();
	});
});
$(document).ready(function(){
      $('.lightbox').lightbox();
    });
	
				$(function(){

					var wrap = $('#gce-list');

					// set up click events to trigger the pagination plugins' behaviour 

					$('.prev').click(function(){
						wrap.trigger('prev.evtpaginate');
						return false;
					});

					$('.next').click(function(){
						wrap.trigger('next.evtpaginate');
						return false;
					});

					// listen out for events triggered by the plugin to update the counter

					wrap.bind( 'initialized.evtpaginate', function(e, startnum, totalnum ){
						$('#count').text(startnum);
						$('#total').text(totalnum);
					}); 

					wrap.bind( 'finished.evtpaginate', function(e, num, isFirst, isLast ){ $('#count').text(num); } ); 	

					wrap.evtpaginate({perPage:1}); // call the plugin!	

				});
				
$(document).ready(function(){
	$('#flickr_badge_uber_wrapper img').each(function (i){
		var oldurl = $(this).attr("src");
		var newurl = oldurl.replace(/_s./g, ".");
		var realurl = $(this).parent().attr('href');
		var realtitle = $(this).attr('title');
		$(this).parent().addClass('lightbox').attr('href',newurl).attr('rel','myflickr').attr('title',''+realtitle+' <a href="'+realurl+'">[Flickr]</a>');
	});
});

$(document).ready(function(){
	$.getJSON('http://twitter.com/status/user_timeline/Jens_Bullerjahn.json?count=1&callback=?', function(data){
		$.each(data, function(index, item){
			$('#twitter').append('<span class="date">Wann: ' + relative_time(item.created_at) + '</span><p>' + item.text.linkify() + '</p>');
		});
	
	});
	
	
	function relative_time(time_value) {
	  var values = time_value.split(" ");
	  time_value = values[1] + " " + values[2] + ", " + values[5] + " " + values[3];
	  var parsed_date = Date.parse(time_value);
	  var relative_to = (arguments.length > 1) ? arguments[1] : new Date();
	  var delta = parseInt((relative_to.getTime() - parsed_date) / 1000);
	  delta = delta + (relative_to.getTimezoneOffset() * 60);
	  
	  var r = '';
	  if (delta < 60) {
		r = 'vor einer Minute';
	  } else if(delta < 120) {
		r = 'vor wenigen Minuten';
	  } else if(delta < (45*60)) {
		r = 'vor ' + (parseInt(delta / 60)).toString() + ' Minuten';
	  } else if(delta < (90*60)) {
		r = 'vor einer Stunde';
	  } else if(delta < (24*60*60)) {
		r = 'vor ' + (parseInt(delta / 3600)).toString() + ' Stunden';
	  } else if(delta < (48*60*60)) {
		r = 'vor einem Tag';
	  } else {
		r = 'vor ' + (parseInt(delta / 86400)).toString() + ' Tagen';
	  }
	  
	  return r;
	}
	
	String.prototype.linkify = function() {
		return this.replace(/[A-Za-z]+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&\?\/.=]+/, function(m) {
			return m.link(m);
		});
	};

});
