// Show more stuff
var ShowMoreComments = function(){

	/**
	 * Div containing comments
	 * @property {HTMLDivElement}
	 */
	this.commentsDiv;
	
	/**
	 * An array of comment divs
	 * @property {Array}
	 */
	this.comments = [];
	
	/**
	 * The show more link
	 * @property {HTMLSelectElement}
	 */
	this.showMoreLink;
	
	/**
	 * Number of comments showing
	 * @property {Number}
	 */
	this.commentsShowing = 0;
	
	/**
	 * Milliseconds to play each fade in animation
	 * @property {Number}
	 */
	var FADE_IN_TIME = 150;

	/**
	 * Closure
	 */
	var me = this;

	/**
	 * Create a new ShowMore
	 * @method
	 */
	this.construct = function(){

		// Get photo grid div
		this.commentsDiv = document.getElementById("all_comments");

		// Get show more link
		this.showMoreLink = document.getElementById("show_more_comments");

		// Collect the comments
		var divs = this.commentsDiv.getElementsByTagName("div")

		// Loop through divs
		for(var i=0; i<divs.length; i++){

			// Distill iteration
			var div = divs[i];

			// Look for correct class name
			if(/comment_(odd|even)/.test(div.className)){
				
				// Add them to our comments array
				this.comments.push(div);
			}
		}

		// If there are more than 10 comments...
		if(this.comments.length > 10){

			// Add click event
			this.showMoreLink.onclick = function(e){

				// Show more comments
				me.showMore();
				
				// Prevent default action
				return false;
			}
		}

		// Set original height
		var originalHeight = 0;

		// Loop through the first 10 comments
		for(var i=0; i<this.comments.length; i++){

			// Stop after the first 10
			if(i > 10) break;
			
			// Increase number of comments showing
			this.commentsShowing++;

			// Distill iteration
			var comment = this.comments[i];

			// Show the first 10 comments
			originalHeight += comment.offsetHeight;
		}
		
		// Show the first set of comments
		this.commentsDiv.style.height = originalHeight + "px";

		// No more comments to show?
		if(this.commentsShowing == this.comments.length){

			// Hide the show more link
			this.showMoreLink.style.marginLeft = "-999em";
		}

		// Return this instance
		return this;
	}

	/**
	 * Create a new ShowMore
	 * @method
	 */
	this.showMore = function(){

		// Hide the show more link
		this.showMoreLink.style.marginLeft = "-999em";

		// Get current height
		var newHeight = this.commentsDiv.offsetHeight;
		
		var limit = (this.comments.length > (this.commentsShowing + 10)) ?
			this.commentsShowing + 10:
			this.comments.length;

		// Show more comments
		for(var i=this.commentsShowing; i<limit; i++){

			// Distill iteration
			var comment = this.comments[i];

			// Add comment height
			newHeight += comment.offsetHeight;

			// Increment comments showing
			this.commentsShowing++;
		}

		// Fix firefox jumpy scroll
		window.scrollBy(0, -1);
		
		// Animate size change
		$(this.commentsDiv).animate({
			
			// Size to the new height
			height: newHeight + "px"
			
		// in .8 seconds
		}, 800);
	
		// Wait for the animation to complete
		setTimeout(function(){
			
			// No more comments to show?
			if(me.commentsShowing < me.comments.length){

				// Hide the show more link
				me.showMoreLink.style.marginLeft = "";
			}

		// After .8 seconds
		}, 800);
	}
}