var _config = 
{
	name:'eightlines',
	gallery:
	{
		width:500,
		height:390,
		padding:20,
		animating:false,
		items:0,
		count:0,
		loading:false,
		id:[]
	},
	exclude:[
		'5647959208029240497',
		'5652363603002992545',
		'5648636902111932561',
		'5639579014388054465',
		'5638552972022901265',
		'5629756782707776321',
		'5627039498927140401'
	]
};

function init()
{
	$(window).resize(resize);
	resize();
	
	window.scrollTo(0, 1);
	
	$(document).bind("touchmove",function(e)
	{
		e.preventDefault();
	});	
	
	getAlbums();
}

function resize()
{
	$('header,nav').width($(document).width());
	$('iframe').width($(document).width()).height($(document).height());
	$('section').offset({top:($(window).height() - $('section').height()) / 2, left:($(window).width() - $('section').width()) / 2});
	$('nav a.swipe').offset({top:($(window).height() - $('nav a.swipe').height()) / 2});
	$('nav a.swipe.right').offset({right:0});
	$('nav a.slide').offset({top:$('section').offset().top - $('nav a.slide').height(), left:(($(window).width() - $('nav a.slide').width()) / 2) + 200});
	$('nav a.slide.down').offset({top:$('nav a.slide.down').offset().top + 480});
}

function getAlbums()
{
	var offset;
	var albumUri = '';
		albumUri += 'https://picasaweb.google.com/data/feed/base/';
		albumUri += 'user/' + _config.name;
		albumUri += '?alt=json';
		albumUri += '&kind=album';
		albumUri += '&hl=en_US';
		albumUri += '&fields=entry(media:group,id)'
		albumUri += '&thumbsize=' + _config.gallery.width;
	
	$.getJSON(albumUri, function (data) 
	{
		var html = '';
		var albumCount = 0;
		
		$.each(data.feed.entry, function (i, item) 
		{
			var albumId = item.id.$t.split('/')[9].split('?')[0];
			var bExclude = false;
			
			_config.gallery.items = data.feed.entry.length - _config.exclude.length;

			$.each(_config.exclude, function (i, item) 
			{
				if (item == albumId)
				{
					bExclude = true;
					return;
				}
			});
			
			if (!bExclude)
			{
				_config.gallery.id.unshift(albumId);
				albumCount++;

				html = '<article style="left:' + (_config.gallery.items - albumCount) * (_config.gallery.width + (_config.gallery.padding * 2)) + 'px;">';
				html += '<div class="title">' + item.media$group.media$title.$t + '<div class="triangleShadow"></div><div class="triangle"></div></div>';
				html += '<div class="slides">';
				$.each(item.media$group.media$thumbnail, function (i, item) 
				{
					var albumThumbURL = item.url;
					html += '<div class="thumbnail" style="background-image:url(\'' + albumThumbURL + '\')"></div>';
					
				});
				html += '</div>';
				if (item.media$group.media$description.$t != "")
					html += '<div class="description">' + item.media$group.media$description.$t + '</div>';
				html += '</article>';
				
				$('section').append(html);
			}
		});
	});
	
	bindNavigation();
}

function bindNavigation()
{
	$('nav a').css('z-index','100').click(function (e) 
	{
		e.preventDefault();
		switch ($(e.target).attr('class'))
		{
			case 'swipe left': scroll(-1); break;
			case 'swipe right': scroll(1); break;
			case 'slide up': slide(-1); break;
			case 'slide down': slide(1); break;
		}
	});
	
	$(window).keydown(function (e) 
	{
		switch (e.keyCode)
		{
			case 37: e.preventDefault(); scroll(-1); break;
			case 39: e.preventDefault(); scroll(1); break;
			case 38: e.preventDefault(); slide(-1); break;
			case 40: e.preventDefault(); slide(1); break;
		}
	});

	$('section').touchwipe(
	{
		wipeLeft:function () {scroll(1)},
		wipeRight:function () {scroll(-1);},
		wipeUp:function () {slide(-1)},
		wipeDown:function () {slide(1);},
		min_move_x:40,
		min_move_y:20,
		preventDefaultEvents:true
	});
}

function scroll(dir)
{
	if (_config.gallery.animating)
		return;
		
	_config.gallery.animating = true;
	$('nav a.slide').hide();

	$('nav a.scroll').show();
	/*
	if (_config.gallery.count == 0)
		$('nav a.scroll.left').hide();
	
	if (_config.gallery.count == (_config.gallery.items - 1))
		$('nav a.scroll.right').hide();
	*/
	if ((dir == -1 && _config.gallery.count == 0) ||
		(dir == 1 && _config.gallery.count == (_config.gallery.items - 1)))
	{
		_config.gallery.animating = false;
		return;
	}
	
	_config.gallery.count = _config.gallery.count + dir;
	
	$('article').each(function (i, item)
	{
		$(this).animate(
		{
			left:(dir == 1) ? '-=' + (_config.gallery.width + _config.gallery.padding) : '+=' + (_config.gallery.width + _config.gallery.padding)
		},
		{
			queue:false, 
			duration:800,
			easing:"expoEaseOut",
			complete:function ()
			{
				_config.gallery.animating = false;
				$('nav a.slide').show();
			}
		});
	});
}

function slide(dir)
{
	var $a = $('article').eq((_config.gallery.items - 1) - _config.gallery.count).find('.slides');

	if ($a.children().length == 1)
		getImages(_config.gallery.id[_config.gallery.count]);

	if (_config.gallery.animating)
		return;
		
	_config.gallery.animating = true;
	
	var h = $a.find('.thumbnail').eq(0).css('top');
		h = parseInt(h.substr(0, h.length));

	if (isNaN(h))
		h = 391;

	if ((dir == 1 && ($a.children().length - 1) * -390 >= h - 0) ||
		(dir == -1 && ($a.find('div.thumbnail').eq(0).css('top') == 'auto' || $a.find('div.thumbnail').eq(0).css('top') == '0px')))
	{
		_config.gallery.animating = false;
		return;
	}

	$a.find('div.thumbnail').each(function (i, item)
	{
		$(this).animate(
		{
			top:((dir == 1) ? '-=' : '+=') + '390'
		},
		{
			queue:false, 
			duration:800,
			easing:"expoEaseInOut",
			complete:function ()
			{
				_config.gallery.animating = false;
			}
		});
	});
}

function getImages(id)
{
	//loading indicator

	var html = '';
	var photoUri = '';
		photoUri += 'https://picasaweb.google.com/data/feed/base/';
		photoUri += 'user/' + _config.name;
		photoUri += '/albumid/' + id;
		photoUri += '?alt=json';
		photoUri += '&kind=photo';
		photoUri += '&hl=en_US';
		photoUri += '&fields=entry(media:group)';
		photoUri += '&thumbsize=' + _config.gallery.width;

	$.getJSON(photoUri, function (data) 
	{
		$.each(data.feed.entry, function (i, item)
		{
			$.each(item.media$group.media$thumbnail, function (j, item)
			{
				var photoURL = item.url;
				html += '<div class="thumbnail" style="background-image:url(\'' + photoURL + '\');top:' + ((i == 0) ? 0 : i * (_config.gallery.height + 0)) + 'px;"></div>';
				//var photo_Title = item.media$group.media$title.$t;
				//var photo_Description = item.media$group.media$description.$t;
			});
		});
		
		$('article').eq((_config.gallery.items - 1) - _config.gallery.count).find('.slides').append(html);
	});
}

$(init);
