(function($) {
	/**
		@class
		@description How it works page
	*/
	REFRESH_APP.howItWorks = function() {
	
		/** private variables **/
		var self  = this,
			now   = new Date().getTime(),
			// now = new Date("March 15, 2011").getTime(), // debugging
			year  = 2011,
			trows = $('#dates table tbody tr'),
			check = '<span class="check">&nbsp;</span>';
	
		/** private functions **/
    
	    /**
	       Recursively loops through each row in the table,
	       adding classes and elements where necessary
       
	       @private
	     */
	    function compareDates(i) {
                
	        i = i || 0;
        
	        if (i === trows.length) {
	            return;
	        }
        
	        var row = $(trows[i]);

	        var submissions     = row.find('td.submissions');
	        var voting          = row.find('td.voting');
        
	        var submissionDates = submissions.find('span.date');
	        var votingDates     = voting.find('span.date');
        
	        if (submissionDates.length !== 2 || votingDates.length !== 2) {
	            return;
	        }
        
	        var startSubmission = new Date(submissionDates[0].innerHTML + ' ' + year).getTime();
	        var endSubmission   = new Date(submissionDates[1].innerHTML + ' ' + year).getTime();
	        var startVoting     = new Date(votingDates[0].innerHTML + ' ' + year).getTime();
	        var endVoting       = new Date(votingDates[1].innerHTML + ' ' + year).getTime();

	        // submissions actually end at the start of the next day
        	endSubmission = endSubmission + (86400 * 1000);

	        if (now > endVoting) {  
	            // row is past total range, add checks to both
	            submissions.prepend(check);
	            voting.prepend(check);
	            return compareDates(i+1);
	        }
        
	        if (startSubmission < now && now < endVoting) {
	            // row is "current", add class and figure out checks, then stop the loop
	            row.addClass('current');
	            if (endSubmission < now) {
	                submissions.prepend(check);
	            }
	            if (endVoting < now) {
	                voting.prepend(check);
	            } else {
	                return compareDates(i+1);
	            }
	            return;
	        }
        
	    }
	
		/**
			Initializes how it works stuff
		*/
		return {
		    init: function() {
		        compareDates();
		    }
		}

	};

	$(function() {
		var hiw = new REFRESH_APP.howItWorks();
		hiw.init();
	});
	
})(jQuery);
