
	// Create a closure in which the $ variable is jQuery's again.
	(function($) {
		
		// "Global" variable definitions
		var scrollTopMargin = 400;
		var lastXHR;
		
		// If this is a mobile browser, we need to adjust the layout a bit.
		var isMobile = false;
		if(navigator.userAgent.match(/(Android|webOS|iPhone|iPod|iPad|BlackBerry|Nokia)/i)) {
			isMobile = true;
		}
		
		// On document ready.
		$(function() {
		
			// On desktop browsers, the header and footer float.
			if(!isMobile) {
				$("#header, #footer").addClass("position_fixed");
			}
			
			// When we want to load more items, execute the JS function
			$("#loadMoreForm").submit(function(e) {
				e.preventDefault();
				loadNext();
			});
			
			// Animate the "go to top" button
			$("#backUpForm").on("click", "a", function(e) {
				//$(this).blur();
				$("html,body").animate({ scrollTop: 50 }, 1000, "swing");
				
				return false;
			});
			
			// Add gradients
			$("div.gradient:nth-child(odd)").addClass("gradient_left");
			$("div.gradient:nth-child(even)").addClass("gradient_right");
			
		});
		
		
		// Load more items.
		var loadNext = function() {
			
			// If a request is already pending, abort it.
			if(lastXHR)
			{
				lastXHR.abort();
				lastXHR = null;
			}
			
			// Disable button
			$("#loadMoreForm button").prop("disabled", true);
			
			// Start loading the items.
			var jqXHR = $.ajax({
				url: "",
				method: "get",
				dataType: "html",
				data: {					
					//page: pageNumber
					show: 5,
					from: (15 + ((pageNumber+1)*5)),
				},
				success: function(data, textStatus, jqXHR) {
					// We loaded a page, so increase the counter.
					pageNumber++;
					
					// If we have no response, remove button.
					if(data.length === 0  || data.indexOf("NOTICE:NO_MORE_EXIST") > 0) {
						$("#loadMoreForm").remove();
						$(window).unbind("scroll.loadMore");
					}
					
					// On ready: Add items.
					$(function() {
					
						$(data).appendTo("#loaded_items")
							.filter("div.gradient")
							.addClass("gradient_right")
							.filter(":nth-child(odd)")
							.removeClass("gradient_right")
							.addClass("gradient_left");
							
						window.addEvent('domready', Mediabox.init.bind(Mediabox));
						window.addEvent('domready', Lightbox.init.bind(Lightbox));
					});
				},
				complete: function() {
					// When complete, show button again.
					$("#loadMoreForm button").prop("disabled", false);
					lastXHR = null;
				}
			});
			
			lastXHR = jqXHR;	
		};
		
		var loadNextScroll = function(rebind) {
		
			if(typeof rebind === "undefined")
				rebind = true;
			
			var w = $(window);
			var scrollBottom = w.scrollTop() + w.height();
			
			// If we're close to the bottom of the page, load new items.
			if(!lastXHR && scrollBottom > $(document).height() - scrollTopMargin) {
				loadNext();
			}
			
			// Rebind if we need to.
			if(rebind)
				$(window).one("scroll.loadMore", loadNextScroll);
		}
		
		
		if(!isMobile)
		{
			$(window).one("scroll.loadMore", loadNextScroll);
		}
	
	})(jQuery);
