	// ShutterBug.nl - Tomas Hastings - 2011

	var html_holder = fb_albums = new Array();
	var scrolder = i = j = k = 0;
	var shutterBox = function () {}
	
	shutterBox.show = function (e) {
		var scroffset = scrolder = $(window).scrollTop();
		e.preventDefault();

		$('#container')
			.css('margin-top', scroffset * -1)
			.css('overflow-y', 'hidden')
			.css('height', $(window).height() + scroffset);
			
		$('#shader').fadeIn(100, function () { $('#overlay').show(); });
		$(this).blur();
	}
	
	shutterBox.showById = function (e, id) {
		var scroffset = scrolder = $(window).scrollTop();
		e.preventDefault();

		$('#container')
			.css('margin-top', scroffset * -1)
			.css('overflow-y', 'hidden')
			.css('height', $(window).height() + scroffset);
		$('#overlay').html( html_holder[ id ] );
		$('#shader').fadeIn(100, function () { $('#overlay').show(); });
		$(this).blur();
		return false;
	}
	
	shutterBox.hider = function () {
		$('#container').css('margin-top', 0);
		$('#container').css('overflow-y', 'auto').css('height', '');
		$('#overlay,#shader').css('display', 'none');
		$(window).scrollTop(scrolder);
	}

	shutterBox.getFacebookAlbum  = function (fbid) {
		$.getJSON('https://graph.facebook.com/' + fbid  + '&callback=?', function(data) {
			$('title').text(data.name);
			$('#labeler').html('<h3>' + data.name + '</h3>' + data.count + ' foto\'s, ' + fuzzyFacebookTime (data.created_time.replace(/-/g,'/')) + ' - <a href="/albums.html">Terug</a><br /><br />');
		});

		$.getJSON('https://graph.facebook.com/' + fbid + '/photos?limit=300&callback=?', function(graph) {
			$('#FBalbum').html('');
			var inlinehtml = snippet = '';
			
			$.each(graph.data, function(id, photo) {
				var bigthumb = '';
				var thumb_h = '';
				$.each(photo.images, function (iid, image) {
					if (image.width == 180) {
						bigthumb = image.source;
						thumb_h = image.height;
					}
				});
				
				if (bigthumb == '') bigthumb = photo.picture;
				
				// todo: change color of comments
				// add 'with: x, y, and z
				// create add comment field in stead of link
				inlinehtml = '<div id="p' + id + '" style="position: relative; margin-left: auto; margin-right: auto; width: 960px"><img class="fbim" src="' + photo.source.replace(/s720x720/, '') + '" alt="" />';
				snippet = '<a href="' + photo.source.replace(/s720x720/, '') + '" rel="gallery" onclick="shutterBox.showById(event, ' + id + ')" id="pa' + id + '"><img src="' + bigthumb + '" style="margin-bottom: ' + (thumb_h > 0 ? (136 - parseInt(thumb_h)) : '1') + 'px" /></a> ';
				
				if (parseInt(photo.width) > parseInt(photo.height)) {
					// horizo
				} else {
					// vertic
					return true;
				}

				inlinehtml = inlinehtml + '<div style="background: #3B5998"><div style="padding: 6px"><a style="color: #fff; font-weight: bold; text-decoration: none; font-family: Helvetica;" href="' + photo.link + '">Klik hier om een berichtje achter te laten op Facebook!</a></div></div>';

				if (typeof photo.comments != "undefined") {
					inlinehtml = inlinehtml + '<div style="background: #fff">';
					$.each(photo.comments.data, function (cid, comment) {
						inlinehtml = inlinehtml + 
						'<table cellspacing="1" cellpadding="2" style="width: 100%;' + (k++ % 2 ? '' : 'background: #EDEFF4;') + '">' +
						'<tr>' +
						' <td style="width: 52px"><img src="http://graph.facebook.com/' + comment.from.id + '/picture" /></td>' +
						' <td style="vertical-align: top; text-align: left; padding-left: 5px; font-size: 12px; font-family: \'Lucida Grande\', tahoma, verdana"><a style="color: #3B5998; font-family: \'Lucida Grande\', tahoma, verdana" href="http://www.facebook.com/profile.php?id=' + comment.from.id + '">' +
							comment.from.name + '</a> ' + comment.message + '<br />' +
							'<span style="color: #999">' + 
								fuzzyFacebookTime (comment.created_time.replace(/-/g,'/')) +
							'</span>' +
						' </td></table>';
					});
					inlinehtml = inlinehtml + '</div>';
				}
			
				html_holder[ id ] = inlinehtml + '<br /></div>';
				$('#FBalbum').append(snippet);
			
			});
		});
	}
	
	/////
	shutterBox.getFacebookAlbums  = function (fbid, since) {
		$.getJSON('https://graph.facebook.com/' + fbid + '/albums?limit=100&since=' + since + '&callback=?', function(graph) {
			$('#FBalbum').html('');
			$.each (graph.data, function (i, album) {
				if (album.name == 'Wall Photos' || album.name == 'Profile Pictures') {
					return true;
				}
				$('#FBalbum').append (
					'<div style="padding: 4px;' + (j++ % 2 ? '' : 'background: #D2D2D2;') + '">' +
					'<table cellspacing="1" cellpadding="1"><tr>' + 
					'<td style="vertical-align: top; width: 140px; text-align: left" id="pic' + album.id + '">&nbsp;</td>' +
					'<td style="vertical-align: top; padding: 4px; text-align: left">' +
					'<h3>' + album.name + '</h3>' +
					album.count + ' foto\'s<br />' +
					fuzzyFacebookTime (album.created_time.replace(/-/g,'/')) + 
					'<br /><br /><a href="/albums.html?fbid=' + album.id + '">Bekijken</a>' +
					'</td></tr></table>' +
					'</div>'
				);
				$.getJSON('http://graph.facebook.com/' + album.cover_photo + '&callback=?', function(json) {
					$('#pic' + album.id).html ('<a href="/albums.html?fbid=' + album.id + '"><img alt="" style="border: 3px solid #fff" src="' + json.picture + '" /></a>');
				});
				///			
			});
		});
	}

	/////

	// Fuzzy wuzzy
	var fuzzyFacebookTime = (function(){
	 
	  fuzzyTime.defaultOptions={
	    // time display options
	    relativeTime : 48,
	    // language options
	    monthNames : ['Jan', 'Feb', 'Maa', 'Apr', 'Mei', 'Jun', 'Jul', 'Aug', 'Sep', 'Okt', 'Nov', 'Dec'],
	    amPm : ['AM', 'PM'],
	    ordinalSuffix : function(n) {return ['th','st','nd','rd'][n<4 || (n>20 && n % 10<4) ? n % 10 : 0]}
	  }
	 
	  function fuzzyTime (timeValue, options) {
	 
	    var options=options||fuzzyTime.defaultOptions,
	        date=parseDate(timeValue),
	        delta=parseInt(((new Date()).getTime()-date.getTime())/1000),
	        relative=options.relativeTime,
	        cutoff=+relative===relative ? relative*60*60 : Infinity;
	 
	    if (relative===false || delta>cutoff)
	      return formatTime(date, options)+' '+formatDate(date, options);
	 
	    if (delta<60) return 'minder dan een minuut geleden';
	    var minutes=parseInt(delta/60 +0.5);
	    if (minutes <= 1) return 'ongeveer een minuut geleden';
	    var hours=parseInt(minutes/60 +0.5);
	    if (hours<1) return minutes+' minuten geleden';
	    if (hours==1) return 'ongeveer een uur geleden';
	    var days=parseInt(hours/24 +0.5);
	    if (days<1) return hours+' uur geleden';
	    if (days==1) return formatTime(date, options)+' gisteren';
	    var weeks=parseInt(days/7 +0.5);
	    if (weeks<2) return formatTime(date, options)+' '+days+' dagen geleden';
	    var months=parseInt(weeks/4.34812141 +0.5);
	    if (months<2) return weeks+' weken geleden';
	    var years=parseInt(months/12 +0.5);
	    if (years<2) return months+' maanden geleden';
	    return years+' jaar geleden';
	  }
	 
	  function parseDate (str) {
	    var v=str.replace(/[T\+]/g,' ').split(' ');
	    return new Date(Date.parse(v[0] + " " + v[1] + " UTC"));
	  }
	 
	  function formatTime (date, options) {
	    var h=date.getHours(), m=''+date.getMinutes(), am=options.amPm;
	    return (h>12 ? h-12 : h)+':'+(m.length==1 ? '0' : '' )+m+' '+(h<12 ? am[0] : am[1]);
	  }
	 
	  function formatDate (date, options) {
	    var mon=options.monthNames[date.getMonth()],
	        day=date.getDate(),
	        year=date.getFullYear(),
	        thisyear=(new Date()).getFullYear(),
	        suf=options.ordinalSuffix(day);
	 
	    return mon+' '+day+suf+(thisyear!=year ? ', '+year : '');
	  }
	 
	  return fuzzyTime;
	 
	}());
	
	var fuzzyFacebookTimeEN = (function(){
	 
	  fuzzyTime.defaultOptions={
	    // time display options
	    relativeTime : 48,
	    // language options
	    monthNames : ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
	    amPm : ['AM', 'PM'],
	    ordinalSuffix : function(n) {return ['th','st','nd','rd'][n<4 || (n>20 && n % 10<4) ? n % 10 : 0]}
	  }
	 
	  function fuzzyTime (timeValue, options) {
	 
	    var options=options||fuzzyTime.defaultOptions,
	        date=parseDate(timeValue),
	        delta=parseInt(((new Date()).getTime()-date.getTime())/1000),
	        relative=options.relativeTime,
	        cutoff=+relative===relative ? relative*60*60 : Infinity;
	 
	    if (relative===false || delta>cutoff)
	      return formatTime(date, options)+' '+formatDate(date, options);
	 
	    if (delta<60) return 'less than a minute ago';
	    var minutes=parseInt(delta/60 +0.5);
	    if (minutes <= 1) return 'about a minute ago';
	    var hours=parseInt(minutes/60 +0.5);
	    if (hours<1) return minutes+' minutes ago';
	    if (hours==1) return 'about an hour ago';
	    var days=parseInt(hours/24 +0.5);
	    if (days<1) return hours+' hours ago';
	    if (days==1) return formatTime(date, options)+' yesterday';
	    var weeks=parseInt(days/7 +0.5);
	    if (weeks<2) return formatTime(date, options)+' '+days+' days ago';
	    var months=parseInt(weeks/4.34812141 +0.5);
	    if (months<2) return weeks+' weeks ago';
	    var years=parseInt(months/12 +0.5);
	    if (years<2) return months+' months ago';
	    return years+' years ago';
	  }
	 
	  function parseDate (str) {
	    var v=str.replace(/[T\+]/g,' ').split(' ');
	    return new Date(Date.parse(v[0] + " " + v[1] + " UTC"));
	  }
	 
	  function formatTime (date, options) {
	    var h=date.getHours(), m=''+date.getMinutes(), am=options.amPm;
	    return (h>12 ? h-12 : h)+':'+(m.length==1 ? '0' : '' )+m+' '+(h<12 ? am[0] : am[1]);
	  }
	 
	  function formatDate (date, options) {
	    var mon=options.monthNames[date.getMonth()],
	        day=date.getDate(),
	        year=date.getFullYear(),
	        thisyear=(new Date()).getFullYear(),
	        suf=options.ordinalSuffix(day);
	 
	    return mon+' '+day+suf+(thisyear!=year ? ', '+year : '');
	  }
	 
	  return fuzzyTime;
	 
	}());

