// Canvas Enviornment
var cabt = cabt || {};



// Constants
cabt.CANVAS;
cabt.STAGE;


/**
 *  Load all of the essential elements
 *  to our wonderful environment.
 */
cabt.init = function (canvas) {
	cabt.CANVAS = canvas;
	cabt.STAGE = new Stage(cabt.CANVAS);

// Load in coponents.
	this.clouds.init();
};




/**
 *  Create the clouds. Make them float.
 */
cabt.clouds = {
	_cloud_container: [],
	_random_range: function (from, to) {
		return Math.floor(Math.random() * (to - from + 1) + from);
	},
	create: function (size) {
		var cloud = new Shape(),
			size = size || 1;
		
		cloud.x = Math.floor(Math.random() * 29999);
		cloud.y = this._random_range(0, 400);
		
		for (var i = 0; 1 > i; i++) {
			cloud.graphics.
				beginRadialGradientFill(["#CCC","#FFF"], [0, 1], 50, 50, this._random_range(90, 190), 1, 1, 50).
				drawEllipse(0 , 0, this._random_range(80 + size, 120 + size), this._random_range(50 + size, 60 + size)).
				drawEllipse(this._random_range(20 + size, 50 + size), 0 , this._random_range(80 + size, 120 + size), this._random_range(50 + size, 60 + size)).
				drawEllipse(this._random_range(50 + size, 100 + size), 0 , this._random_range(80 + size, 120 + size), this._random_range(50 + size, 60 + size));
		}
		
		//cloud.alpha = 0.1; // bwteen 0 and 1..
		//cloud.shadow = new Shadow( '#CCC' , 0 , 0 , 10 );
		return cloud;
	},
	
	populate: function () {
		
		var cloud;
		
		this._cloud_container = new Container();
		
		for (var i = 0; 100 > i; i++) {
			cloud = this.create(100);
			this._cloud_container.addChild(cloud);
			cabt.STAGE.addChild(this._cloud_container);
		}
		
		cabt.STAGE.update();
		
		Ticker.addListener(cabt.clouds);
		Ticker.setFPS(5);
	},
	
	tick: function () {
		var cloud;
		
		for (var i = 0, l = this._cloud_container.getNumChildren(); i < l; i++) {
			cloud = this._cloud_container.getChildAt(i);
			if (cloud.x > 30000) {
				cloud.x = -700;
			} else {
				cloud.x = cloud.x + 2;
			}
		}
		cabt.STAGE.update();
	},
	
	init: function () {
		this.populate();
	}
};








// The hills.
// @legacy
var Hills = function (hill1, hill2, hill3, hill4) {
	this.$hill1 = hill1;
	this.$hill2 = hill2;
	this.$hill3 = hill3;
	this.$hill4 = hill4;
	
	this._internal_pos = 0;
	this.current_pos = 0;
	this.prev_pos = 0;
	
	this.init();
};
Hills.prototype.get_pos = function () {
	this.current_pos = $(window).scrollLeft();
	if (this.current_pos < this.prev_pos) {
		this._internal_pos = this._internal_pos + 1;
	} else {
		this._internal_pos = this._internal_pos - 1;
	}
	return this._internal_pos;
};
Hills.prototype.move = function () {	
	var offset = this.get_pos();
	this.$hill1[0].style.backgroundPosition = offset - offset*2 + 'px 0%';
	this.$hill2[0].style.backgroundPosition = offset + 'px 0%';
	this.$hill3[0].style.backgroundPosition = offset*4 + 'px 0%';
	this.$hill4[0].style.backgroundPosition = offset*7 + 'px 0%';
	this.prev_pos = this.current_pos;
};
Hills.prototype.init = function () {
	var that = this;
	$(window).scroll(function () {
		that.move();
	});
};




// The rain.
// @legacy
var Rain = function (elm, velocity) {
	this.velocity = velocity;
	this.elm = elm;
	this._iterator = 0;
	this._placements = ['0% 0%', '0% 100%', '0% -150%'];
	
	this.init();
};
Rain.prototype.fall = function () {
	$(this.elm).css('background-position', this._placements[this._iterator]);
	//this.elm.style.backgroundPosition = this._placements[this._iterator]; // IE is failing at this.
	this._iterator = (this._iterator === this._placements.length) ? 0 : this._iterator + 1;
};
Rain.prototype.init = function () {
	var that = this;
	(function play () {
		setTimeout(function () {
			that.fall();
			play();
		}, 60);
	})();
};




// A coud.
var Cloud = function (jContainer, leftPos, topPos) {
	this.leftPos = leftPos;
	this.topPos = topPos;
	this.container = jContainer;
	this.elm = document.createElement('div');
	this.insert();
};
Cloud.prototype.insert = function () {
	this.elm.className = 'cloud';
	this.elm.style.top = this.topPos + 'px';
	this.elm.style.left = this.leftPos + 'px';
	this.container.appendChild(this.elm);
	this.animate();
};
Cloud.prototype.animate = function () {
	var that = this;
	(function animator () {	
		var n = Math.round(Math.random() * 2),
			direction = (n > 0) ? -1000 : 1000,
			speed = 10000*(n+4);
		
		$(that.elm).animate({
			left: that.leftPos - direction
		}, speed, function () {
			animator();
		});
	})();
};


// A car.
// @legacy
var Car = function (container, model, parked, copcar) {
	this.parked = parked;
	this.model = model;
	this.cop = copcar;
	this.$container = $(container);
	this.$car = $('<div class="car ' + this.model + '"></div>');
	this.$rear_wheel = $('<div class="rear_wheel wheel"></div>');
	this.$front_wheel = $('<div class="front_wheel wheel"></div>');
	
	this.build();
	
	if (this.cop) {
		//this.animate_copcar();
	} else {
		//this.animate();
	}
};
Car.prototype.build = function () {
	if (this.cop) {
		this.distance = 5000;
		this.$rear_wheel = '';
		this.$front_wheel = '';
	} else {
		this.distance = Math.floor(Math.random() * (27000 - 1000 + 1) + 1000);
	}
	this.$car
		.css('left', this.distance + 'px')
		.append(this.$rear_wheel)
		.append(this.$front_wheel);
	
	this.$container.append(this.$car);
};
Car.prototype.animate = function () {
	var that = this;
	(function spin () {
		that.$front_wheel.animate({rotate: '+=-360deg'}, 1000, 'linear', function () {
			spin();
		});
		that.$rear_wheel.animate({rotate: '+=-360deg'}, 1000, 'linear');
	})();

	if ( ! this.parked) {
		(function drive () {
			that.$car.animate({
				left: -600
			}, that.distance * 3, 'linear', function () {
				that.distance = 28000;
				that.$car.css('left', '30000px');
				drive();
			});
		})();
	}
};
Car.prototype.animate_copcar = function () {
	var that = this,
		$message = $('#officer-message');
	
	$(window).scroll(function () {
		if (scroll_manager.x_position >= 4285 && scroll_manager.x_position <= 5200) {
			$message.slideDown(200);
		} else {
			$message.slideUp(200);
		}
	});
};


// The bird!
var Bird = function () {
	var that = this;
	this.played = false;
	this.$bird = $('#bird-scene .bird');
	this.$crack = $('#bird-scene .crack');
	this.scenes = {
		one: function () {
			that.$bird
				.css('background-image', 'url(/assets/images/birds/bird2.png)')
				.css('background-position', '530px 290px');
		},
		two: function () {
			that.$bird
				.css('background-image', 'url(/assets/images/birds/bird3.png)')
				.css('background-position', '490px 220px');
		},
		three: function () {
			that.$bird
				.css('background-image', 'url(/assets/images/birds/bird4.png)')
				.css('background-position', '400px 190px');
		},
		four: function () {
			that.$bird
				.css('background-image', 'url(/assets/images/birds/bird5.png)')
				.css('background-position', '0px 0px');
			$('#bird-scene .bird')
				.css('left', '18800px')
				.css('bottom', '200px');
		},
		five: function () { // the bang
			that.$bird
				.css('background-image', 'url(/assets/images/birds/bird6.png)')
				.css('background-position', '100% 100%');
		
			$('#bird-scene .bird')
				.css('left', '18800px')
				.css('bottom', '0px');
			
			$('#bird-scene .crack,#bird-scene .screen')
				.css('display', 'block');
			
			var i = 1, $crack = $('#bird-scene .screen');
			(function matrix () {
				setTimeout(function () {
					if (i === 1) {
						$crack.css('background-position', '0% 0%');
					} else {
						$crack.css('background-position', '0% 100%');
					}
					i = (i === 2) ? 1 : 2;
					matrix();
				}, 200);
			})();
		},
		six: function () {
			that.$bird
				.css('background-image', 'url(/assets/images/birds/bird7.png)')
				.css('background-position', '20% 50%');
		},
		seven: function () {
			that.$bird
				.css('background-image', 'url(/assets/images/birds/bird8.png)')
				.css('background-position', '20% 40%');
		},
		eight: function () {
			that.$bird
				.css('background-image', 'url(/assets/images/birds/bird9.png)')
				.css('background-position', '15% 15%');
		},
		nine: function () {
			that.$bird.css('background-image', 'none');
		}
	};
	
	this.load_images();
	this.listen();
};
Bird.prototype.listen = function () {
	var that = this;
	$(window).scroll(function () {
		if ( ! that.played) {
			if (scroll_manager.x_position >= 18819) {
				that.played = true;
				that.play();
			}
		}
	});
};
Bird.prototype.play = function () {
	var that = this;
	that.scenes['one']();
	setTimeout(function () {
		that.scenes['two']();
		setTimeout(function () {
			that.scenes['three']();
			setTimeout(function () {
				that.scenes['four']();
				setTimeout(function () {
					that.scenes['five']();
					setTimeout(function () {
						that.scenes['six']();
						setTimeout(function () {
							that.scenes['seven']();
							setTimeout(function () {
								that.scenes['eight']();
								setTimeout(function () {
									that.scenes['nine']();
								}, 200);
							}, 200);
						}, 200);
					}, 200);
				}, 200);
			}, 100);
		}, 100);
	}, 100);
};
Bird.prototype.load_images = function () {
	var o;
	for (var i = 2; 9 >= i; i++) {
		o = new Image();
		o.src = '/assets/images/birds/bird' + i + '.png';
	}
};

