/* COLLECTION SCROLLER */


var collection_count = 0;
var collection_pointer = 1;

function collection_rotator(event) {
	

		var scrolly = $(window).scrollTop()+$(window).height();
		
		var boxy = $(".collectionbox:last").offset().top;
		
		console.log("- Current ScrollPos:" + scrolly);
		console.log("- LastBox ScrollPos:" + boxy);
		
		
		if (scrolly >= boxy) {
			
			var clonebox = $("#collectionscroller").find('.collectionbox')[collection_pointer];
			
			collection_pointer++;
			
			$(clonebox).clone().appendTo('#collectionscroller');
			
			if (collection_pointer == collection_count) collection_pointer = 1;
			
		}
		
		
		var deleting_objects = new Array;
		
		if (Math.floor($("#collectionscroller").find('.collectionbox').length / collection_count) > 3) {
			
			for (var x = 1; x < collection_count; x++) {
							
					deleting_objects.push($("#collectionscroller").find('.collectionbox')[x]);
				
			}
			
			for (var x = 0; x < deleting_objects.length; x++) {
				
				var obj = $(deleting_objects[x]);

				var deleted_height = $(obj).height();
				
				$(obj).remove();
				
				$(window).scrollTop($(window).scrollTop()-deleted_height);
			
			}
			
		}

	
}


$(function() {
	
	collection_count = $("#collectionscroller").find('.collectionbox').length;
	
	console.log("- Collections Count:" + collection_count);
	
	$(window).bind('scroll',collection_rotator);
	
	
	$(document).keypress(function(e)
	{
		switch(e.which)
		{

				case 32:
				
					$("body, html").animate({ scrollTop: 0 },1500,'easeOutSine');
				
				break;
	
		}

	}
	);

});

