var clouds = new Array();
var MAX_SPEED = .5;
var MIN_SPEED = .01;
var CLOUD_COUNT = 10;
var S_H = 500;

var TRANS_TIME = 20;
var cur_time = 0;

var day = true;
var day_time = null;

var colors = {
	start_c : null,
	end_c : null,
	
	sky_top_c : {
		d_r : 188,
		d_g : 233,
		d_b : 254,
		
		n_r : 41,
		n_g : 60,
		n_b : 116
	},
	
	sky_bot_c : {
		d_r : 222,
		d_g : 251,
		d_b : 255,
		
		n_r : 209,
		n_g : 106,
		n_b : 15
	},
	
	ground_c : {
		d_r : 10,
		d_g : 100,
		d_b : 30,
		
		n_r : 1,
		n_g : 55,
		n_b : 28
	},
	
	clouds_c : {
		d_r : 210,
		d_g : 240,
		d_b : 255,
		
		n_r : 80,
		n_g : 64,
		n_b : 107
	},
	
	site_c : {
		d_r : 255,
		d_g : 255,
		d_b : 255,
		
		n_r : 112,
		n_g : 119,
		n_b : 140
	},
	
	sky_top : function()
	{
		var r = Math.round((cur_time / TRANS_TIME) * (this.sky_top_c.n_r - this.sky_top_c.d_r) + this.sky_top_c.d_r);
		var g = Math.round((cur_time / TRANS_TIME) * (this.sky_top_c.n_g - this.sky_top_c.d_g) + this.sky_top_c.d_g);
		var b = Math.round((cur_time / TRANS_TIME) * (this.sky_top_c.n_b - this.sky_top_c.d_b) + this.sky_top_c.d_b);
		
		return "rgb(" + r + "," + g + "," + b + ")";
	},
	
	sky_bot : function()
	{
		var r = Math.round((cur_time / TRANS_TIME) * (this.sky_bot_c.n_r - this.sky_bot_c.d_r) + this.sky_bot_c.d_r);
		var g = Math.round((cur_time / TRANS_TIME) * (this.sky_bot_c.n_g - this.sky_bot_c.d_g) + this.sky_bot_c.d_g);
		var b = Math.round((cur_time / TRANS_TIME) * (this.sky_bot_c.n_b - this.sky_bot_c.d_b) + this.sky_bot_c.d_b);
		
		return "rgb(" + r + "," + g + "," + b + ")";
	},
	
	ground : function()
	{
		var r = Math.round((cur_time / TRANS_TIME) * (this.ground_c.n_r - this.ground_c.d_r) + this.ground_c.d_r);
		var g = Math.round((cur_time / TRANS_TIME) * (this.ground_c.n_g - this.ground_c.d_g) + this.ground_c.d_g);
		var b = Math.round((cur_time / TRANS_TIME) * (this.ground_c.n_b - this.ground_c.d_b) + this.ground_c.d_b);
		
		return "rgb(" + r + "," + g + "," + b + ")";
	},
	
	clouds : function()
	{
		var r = Math.round((cur_time / TRANS_TIME) * (this.clouds_c.n_r - this.clouds_c.d_r) + this.clouds_c.d_r);
		var g = Math.round((cur_time / TRANS_TIME) * (this.clouds_c.n_g - this.clouds_c.d_g) + this.clouds_c.d_g);
		var b = Math.round((cur_time / TRANS_TIME) * (this.clouds_c.n_b - this.clouds_c.d_b) + this.clouds_c.d_b);
		
		return "rgb(" + r + "," + g + "," + b + ")";
	},
	
	site : function()
	{
		var r = Math.round((cur_time / TRANS_TIME) * (this.site_c.n_r - this.site_c.d_r) + this.site_c.d_r);
		var g = Math.round((cur_time / TRANS_TIME) * (this.site_c.n_g - this.site_c.d_g) + this.site_c.d_g);
		var b = Math.round((cur_time / TRANS_TIME) * (this.site_c.n_b - this.site_c.d_b) + this.site_c.d_b);
		
		return "rgb(" + r + "," + g + "," + b + ")";
	}
}

for(var i = 0; i < CLOUD_COUNT; i++)
{
	clouds.push(new Cloud());
	
	clouds[i].setx(rand(-1 * C_W, window.innerWidth));
	clouds[i].sety(rand(S_H - C_H));
	clouds[i].s = randDec(MIN_SPEED, MAX_SPEED);
	clouds[i].setColor(colors.clouds());
}

function init()
{
	for(var i = 0; i < clouds.length; i++)
	{
		$("sky").insertBefore(clouds[i].cloud, $("sky").firstChild);
	}
	
	updateColors();
	wind();
}

function wind()
{
	for(var i = 0; i < clouds.length; i++)
	{
		clouds[i].move();
		if(clouds[i].x > window.innerWidth)
		{
			clouds[i].setx(-1 * C_W);
			clouds[i].sety(rand(S_H - C_H));
			clouds[i].s = randDec(MIN_SPEED, MAX_SPEED);
			clouds[i].makeCloud();
		}
	}
	
	setTimeout(wind, 10);
}

function drawTrans()
{
	var ctx = $("trans").getContext("2d");
	var grd = ctx.createLinearGradient(0, 0, 0, $("trans").height);
	grd.addColorStop(0, colors.sky_top());
	grd.addColorStop(1, colors.sky_bot());
	ctx.fillStyle = grd;
	ctx.fillRect(0, 0, $("trans").width, $("trans").height);
}

function draw()
{
	var ctx = $("trans").getContext("2d");
	ctx.canvas.width  = window.innerWidth;
	ctx.canvas.height = window.innerHeight;
}

function switchDay()
{
	clearTimeout(day_time);
	if(day)
	{
		day = false;
		day2Night();
	}else{
		day = true;
		night2Day()
	}
}

function day2Night()
{
	cur_time++;
	
	if(cur_time >= TRANS_TIME)
	{
		cur_time = TRANS_TIME;
		updateColors();
	}else{
		updateColors();
		day_time = setTimeout(day2Night, 50);
	}
}

function night2Day()
{
	cur_time--;
	
	if(cur_time <= 0)
	{
		cur_time = 0;
		updateColors();
	}else{
		updateColors();
		day_time = setTimeout(night2Day, 50);
	}
}

function updateColors()
{
	$("sky").style.background = colors.sky_top();
	$("site").style.background = colors.site();
	$("body").style.background = colors.ground();
	$("html").style.background = colors.ground();
	
	for(var i = 0; i < clouds.length; i++)
	{
		clouds[i].setColor(colors.clouds());
	}
	
	drawTrans();
}
