/**
 * @license GNU Affero General Public License v3 <http://www.gnu.org/licenses/agpl-3.0.txt>
 * 
 * Copyright (C) 2009  Charlie Powell <powellc@powelltechs.com>
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, version 3.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 * 
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see http://www.gnu.org/licenses/agpl-3.0.txt.
 * 
 */

/**
 * This provides the core of the moon phase logic.
 * It uses a known fixed date as a point of reference and calculates all
 * dates based on that using 2567400 seconds between each rototation of the cycles.
 * 
 * The date being used is epoch 1263557460, or roughly Jan. 15, 2010... the date of a new moon.
 * 
 * It uses 16 states to provide the 8 different cycles and 16 different images.
 * 
 * @version 2010.04.23
 * @author Charlie Powell
 */
Moon = {
	states: {
		0: 'New Moon', // CENTRE POINTE
		1: 'New Moon',
		2: 'Waxing Cresent',
		3: 'Waxing Cresent',
		4: 'Waxing Cresent',
		5: 'Waxing Cresent',
		6: 'First Quarter',
		7: 'First Quarter',
		8: 'First Quarter', // CENTRE POINTE
		9: 'First Quarter',
		10: 'First Quarter',
		11: 'Waxing Gibbous',
		12: 'Waxing Gibbous',
		13: 'Waxing Gibbous',
		14: 'Waxing Gibbous',
		15: 'Full Moon',
		16: 'Full Moon', // CENTRE POINTE
		17: 'Full Moon',
		18: 'Waning Gibbous',
		19: 'Waning Gibbous',
		20: 'Waning Gibbous',
		21: 'Waning Gibbous',
		22: 'Last Quarter',
		23: 'Last Quarter',
		24: 'Last Quarter', // CENTRE POINTE
		25: 'Last Quarter',
		26: 'Last Quarter',
		27: 'Waning Cresent',
		28: 'Waning Cresent',
		29: 'Waning Cresent',
		30: 'Waning Cresent',
		31: 'New Moon',
		length: 32
	},
	         
	getCurrentState: function(){
		var state = Math.round((((Moon.getUnixEpoch() - 1263557460) % 2567400) / 2567400) * Moon.states.length);
		if(state == Moon.states.length) state = 0; // Reset the newmoon back to 0.
		return state;
	},
	
	getUnixEpoch: function(){
		return Math.round(new Date().getTime()/1000.0);
	},
	
	getCurrentStateName: function(){
		return Moon.states[Moon.getCurrentState()];
	},
	
	getNextStateTime: function (state){
		if(!state) state = Moon.states.length;
		
		var statefrac = state / Moon.states.length;
		//Math.floor(state / 16);
		// Figure out the next time state X will occur.
		var timeNext = 2567400 * (statefrac - (((Moon.getUnixEpoch() - 1263557460) % 2567400) / 2567400));
		
		// If it's less then 0, it already passed for this cycle... provide the next one!
		if(timeNext < 0) timeNext = 2567400 - Math.abs(timeNext);
		
		return Moon.getUnixEpoch() + timeNext;
	},
	
	getNextFullTime: function(){
		return Moon.getNextStateTime(Moon.states.length / 2);
	},
	
	getNextNewTime: function(){
		return Moon.getNextStateTime(0);
	},
	
	epochToDate: function(epoch){
		var d = new Date(epoch * 1000);
		return d.toDateString()
	},
	
	getNextFullDate: function(){
		return Moon.epochToDate(Moon.getNextStateTime(Moon.states.length / 2));
	},
	
	getNextNewDate: function(){
		return Moon.epochToDate(Moon.getNextStateTime(0));
	},
	
	_dummy: false
};

