// Requests for each row
var rows = [{
	row: function() {
		// Upcoming
		var upcomingEvents = new JSONContent('upcoming_events','upcoming');
		upcomingEvents.setDelegate(upcomingDelegate);
		upcomingEvents.showResults(3);
		
		// Blog
		var blogPosts = new JSONContent('blog_posts','blog');
		blogPosts.setDelegate(blogDelegate);
		blogPosts.showResults(1);
		
		// About
		var aboutMe = new JSONContent('about_me','about');
		aboutMe.setDelegate(aboutDelegate);
		aboutMe.showResults(1);
	}
},
{
	row: function() {
		// Tumblr
		var tumblrQuotes = new JSONContent('tumblr_quotes','tumblr');
		tumblrQuotes.setDelegate(tumblrDelegate);
		tumblrQuotes.showResults(1);
		
		// Projects
		var projects = new JSONContent('projects','projects');
		projects.setDelegate(projectsDelegate);
		projects.showResults(5);
		
		// Delicious
		var deliciousBookmarks = new JSONContent('delicious_bookmarks','delicious');
		deliciousBookmarks.setDelegate(deliciousDelegate);
		deliciousBookmarks.showResults(5);
	}
},
{
	row: function() {
		// TwitterSearch
		var twitterSearch = new JSONContent('twitter_tweets','twitter');
		twitterSearch.showResults(5);
		
		// Amazon
		var amazonWishlist = new JSONContent('amazon_wishlist','amazon');
		amazonWishlist.setDelegate(amazonDelegate);
		amazonWishlist.showResults(5);
		
		// Flickr
		var flickrPhotos = new JSONContent('flickr_photos','flickr');
		flickrPhotos.setDelegate(flickrDelegate);
		flickrPhotos.showResults(5);
	}
}];
var currentRow = 0;
var isScrollable = true;

// Javacript for the index page
Event.observe(window, 'load', function() {
	var grids = $$('.grid3col'),
		viewport = document.viewport,
		windowTimeout;
	
	// first hide the rows that I'm going to display on scroll
	grids.each(function(grid,index) {
		grid.hide();
	});
	
	var isLargeWindow = function() {
		window.clearTimeout(windowTimeout);
		
		// if the height of the content is less than the viewport add a row
		if (isScrollable === true && viewport.getHeight() >= $('main').getHeight()) {
			addRow();
		}
	}
	
	var addRow = function() {
		// if there are rows to add add a row
		if (currentRow < rows.length && typeof rows[currentRow].row === 'function') {
			grids[currentRow].show();
			rows[currentRow].row();
			currentRow = currentRow + 1;
			if (currentRow >= rows.length) isScrollable = false;
		}

		windowTimeout = window.setTimeout(function() { isLargeWindow() }, 6000);
		
	}
	addRow();
	
	
	
	// check to see if we're near the bottom of the window
	Event.observe(window, 'scroll', function() {
		if (isScrollable === true && (viewport.getHeight() + viewport.getScrollOffsets().top) >= document.height) {
			grids[currentRow].show();
			window.clearTimeout(windowTimeout);
			addRow();
		}
	});

});
