/* Author:
	Bool&Beat
*/

$(function () {
	$('header .sublinks').hover(
		function (ev) {
			var sub = $(this).children('ul');
			sub.stop(true, true);
			sub.slideDown(200, 'easeOutCubic');
		},
		function (ev) {
			var sub = $(this).children('ul');
			sub.stop(true, true);
			sub.delay(500);
			sub.slideUp(200, 'easeInCubic');
		}
	);

	$('.legals').click(
		function (e) {
			e.preventDefault();
			var options = {
				content: $(this).attr('href'),
				width: 870,
				height: 594
			};
			$('#modal_container').modal(options);
		}
	);

	$('#home_overlay_arrow_big').hide();
	$('#home_overlay_text').hide();
	$('#home_overflow_container').hover(
		function (ev) {
			$('#home_overlay_text').delay(200).show();
			$('#home_overlay_arrow_big').delay(200).show();
			$('#home_overlay_arrow').hide();
			$('#home_overlay_background').stop().animate({marginLeft : 0 , marginTop : 0},200);
		},
		function (ev) {
			$('#home_overlay_arrow').delay(200).show();
			$('#home_overlay_text').hide();
			$('#home_overlay_arrow_big').hide();
			$('#home_overlay_background').stop().animate({marginLeft : -170 , marginTop : -170},200);
		}
	);
});

// Main lib
window.cailles = (function () {

	function cart_updated(html) {
		$("#modal_container").modal('close');
		$('#cart_outer').html(html);
		init_widgets( $('#cart_outer').children() );
	}
	function close_cart() {
		$('#cart_container').slideUp(300);
	}
	function open_cart() {
		$('#cart_container').slideDown(300);
	}
	//function refresh_cart_summary() {
	//	$.get('/cart/summary', function (res) {
	//		$('#cartbtn').replaceWith(res);
	//	});
	//}

	function init_widgets(target) {
		$('[data-widget]', target).each(function () {
			var widget = $(this);
			var widget_type = widget.data('widget');
			if ( !$.isFunction( widget[widget_type] ) ) {
				return;
			}
			var args = null;
			var options = widget.data('options');
			if ( $.isPlainObject(options) ) {
				args = [ options ];
			}
			widget[widget_type].apply(widget, args);
		});
	}

	$(function () {
		cailles.init_widgets();
	});

	return {
		cart_updated: cart_updated,
		close_cart: close_cart,
		open_cart: open_cart,

		init_widgets: init_widgets
	};
})();

(function () {
	if ( !Modernizr.canvas ) { return; }

	var konami_keys = [ 38, 38, 40, 40, 37, 39, 37, 39, 66, 65 ];
	var konami = konami_keys.join(',');

	var kkeys = [];
	$(document).keydown(function (ev) {
		kkeys.push(ev.keyCode);
		if ( kkeys.length > konami_keys.length ) {
			kkeys.splice(0, kkeys.length - konami_keys.length);
		}
		if ( kkeys.join(',') !== konami ) { return; }

		// TODO nettoyer mieux...
		var d = document.createElement('div');
		var c = document.createElement('canvas');
		d.style.position = "absolute";
		d.style.top = "0";
		d.style.zIndex = "0";
		c.width = window.screen.width;
		c.height = window.screen.height;
		c.style.margin = '0';
		c.style.padding = '0';
		document.body.style.overflow = 'hidden';
		var nbb = 200;
		var frame_rate = 30;


		document.body.appendChild(d);
		d.appendChild(c);
		var h = c.height;
		var w = c.width;

		var ctx = c.getContext('2d');
		var b_a = [];//array of birds
		var looper;

		var clear = function(){
			ctx.fillStyle = "rgba(0, 0, 0, 0)";

		   ctx.clearRect(0, 0, w, h);
		   ctx.beginPath();
		   ctx.rect(0, 0, w, h);
		   ctx.closePath();
		   ctx.fill();
		}

		var render = function (){
			clear();
			for (var i = 0, l = b_a.length; i < l ; i++){
				b_a[i].update();
			}
		}

		var start = function(){
			looper = setInterval(render, 1000 /  frame_rate);
		}
		var pause = function(){
			clearInterval(looper);
		}

		var Bird = function(){};
		Bird.prototype.x = - 20;
		Bird.prototype.y = h;
		Bird.prototype.dx = w;
		Bird.prototype.dy = -20;
		Bird.prototype.cycle = 0;
		Bird.prototype.w = 0;
		Bird.prototype.h = 0;
		Bird.prototype.random = 1000;
		Bird.prototype.current_frame = null;

		Bird.prototype.init = function(){
			this.x = - 20 - this.random * Math.random();
			this.y = h + this.random * Math.random();
			this.dx = w + this.random * Math.random();
			this.dy = -20 - this.random * Math.random();

			var image = new Image();
			image.src = "/static/images/bird.png";
			image.ref = this;
			image.onload = function() {
				var weight = 2 * Math.random();
				this.ref.w = this.width * weight;
				this.ref.h = this.height * weight;
				this.ref.i = this;
			};
			var image2 = new Image();
			image2.src = "/static/images/bird2.png";
			image2.ref = this;
			image2.onload = function() {
				this.ref.i2 = this;
			};
		}


		Bird.prototype.update = function(){
			if(this.cycle >= 19) this.cycle  = 0;
			this.x += (this.dx - this.x) / 100;
			this.y += (this.dy - this.y) / 100 + Math.random() * 10 * Math.sin(this.cycle);
			if (this.cycle % 4 == 0) {
				this.current_frame = (this.current_frame == this.i ) ? this.i2 : this.i;
			}
			ctx.drawImage(this.current_frame, this.x, this.y, this.w , this.h);
			this.cycle ++;
		}

		for(var j = 0; j < nbb ; j++){
			var b = new Bird();
			b.init();
			b_a.push(b);
		}

		start();
	});
})();

