$(document).ready(function(){
	today = Today('#Today');
	today.start();
});

function Today(selector, options){
	this.init = function(){
		this.selector = selector;
		this.elements = $(selector);
		this.options  = options;
		return this;
	};
	
	this.start = function(){
		this.clear_elements();
		this.create_elements();
		this.attach_elements();
		this.update_elements();
		
		var delay  = 50;
		this.timer = setInterval(this.update_elements, delay);
	};
	
	this.destroy = function(){
		clearInterval(this.timer);
		this.clear_elements();
		this.elements.append(
			$('<p>Loading cancelled, today will never come.</p>')
		);
	}
	
	this.todays_progress = function(){
		var now         = new Date();
		var day_length  = 24 * 60 * 60;
		var current     = 
			(now.getHours() * 3600) + 
			(now.getMinutes() * 60) + 
			(now.getSeconds() * 1);
		var progress = current / day_length;
		
		return progress;
	};
	
	//All Elements		
	this.clear_elements = function(){
		this.elements.empty();
	};
	
	this.create_elements = function(){
		this.create_sun();
		this.create_progress_bar();
	};
	
	this.attach_elements = function(){
		this.attach_progress_bar();
		this.attach_sun();
	};
	
	this.update_elements = function(){
		this.update_sun();
		this.update_progress_bar();
	};
	//End Elements
	
	//Progress Bar
	this.create_progress_bar = function(){
		this.progress_elem = $('<div class="ProgressElement"/>');
		this.progress_est  = $('<div class="ProgressEstimate"/>');
		this.progress_bar  = $('<div class="ProgressValue"/>');
		this.progress_elem.append(this.progress_est);
		this.progress_elem.append(this.progress_bar);
	};
	
	this.attach_progress_bar = function(){
		this.elements.append(this.progress_elem);
	};
	
	this.update_progress_bar = function(){
		var percent   = Math.round(this.todays_progress() * 1000) / 10;
		var time_left = Math.round(
			(24 - (24 * this.todays_progress())) * 10
		) / 10;
		
		if (time_left > 1){
			this.progress_est.text('About ' + time_left + ' hours left');
		}else{
			this.progress_est.text('Less than one hour left!');
		}
		this.progress_bar.text(percent + '% Complete');
	};
	//End Progress Bar
	
	//Sun 
	this.sun_new_offset = function(){
		var parent_width  = this.sun_elem.innerWidth();
		var parent_height = this.sun_elem.innerHeight();
		var sun_width     = this.sun_img.width();
		var x = Math.round(this.todays_progress() * (parent_width - sun_width));
		var y = Math.sin(Math.PI * this.todays_progress()) * parent_height;
		y = Math.round(y);
		if (y < 0){y = 0;}
		return {'x' : x, 'y' : y}
	};
	
	this.create_sun = function(){
		this.sun_elem   = $('<div class="SunElement"/>');
		this.sun_img    = $('<img class="SunImage"/>');
		this.sun_ground = $('<div class="SunGround"/>');
		
		this.sun_img.attr('src', 'img/sun.png');
		this.sun_img.css({
			'position' : 'absolute',
		});
		this.sun_ground.css({
			'position'         : 'absolute',
			'z-index'          : '10',
			'border-top'       : '5px solid #1e5429',
		});
		this.sun_elem.css({});
		this.sun_elem.append(this.sun_img);
		this.sun_elem.append(this.sun_ground);
	};
	
	this.attach_sun = function(){
		this.elements.append(this.sun_elem);
	};
	
	this.update_sun = function(){
		var parent_position = this.sun_elem.offset();
		var parent_height   = this.sun_elem.innerHeight();
		var origin = {
			'x' : parent_position['left'],
			'y' : parent_position['top'] + parent_height
		};
				
		var offset = this.sun_new_offset();
		var top    = origin['y'] - offset['y'];
		var left   = origin['x'] + offset['x'];

		this.sun_img.css({
			'top' : top + 'px',
			'left': left + 'px'
		});
		this.sun_ground.css({
			'top'    : origin['y'] + 'px',
			'height' : this.sun_img.outerHeight(),
			'width'  : this.sun_elem.innerWidth()
		});
		this.sun_elem.css({
			'margin-bottom' : this.sun_img.outerHeight()
		});
	};
	//End Sun
	
	return this.init();
}
