$(document).ready(function() {
	// EASING
	jQuery.easing.def = "easeOutQuad";
});

$(window).bind("load", function() {
	
	$('#rgbjoy').css({opacity:0})
	.animate({opacity:1},1200);
	
	$('#content').css({opacity:0})
	.delay('1250')
	.animate({opacity:1},1200);
	
	var MAX_NUM = 100;
	var INTERVAL_MSEC = 1000 / 60 >> 0;

	function Particle() {
	    resize()
	    this.initialize.apply(this, arguments);
	}

	Particle.prototype = {

	    initialize: function(x, y) {
	        this.x = x;
	        this.y = y;
	    },
	    x: 0,
	    y: 0,
	    vx: 0,
	    vy: 0,
	    next: null
	};

	var mouseX = -200;
	var mouseY = -200;
	var first;
	var old;
	var p;
	var rect;

	var canvas = document.getElementById("c");
	var ctx = canvas.getContext("2d");

	for (var i = 0; i < MAX_NUM; i++) {
	    p = new Particle(
	    Math.random() * window.innerWidth,
	    Math.random() * window.innerHeight
	    );

	    if (first == null) {
	        first = old = p;
	    } else {
	        old.next = p;
	        old = p;
	    }
	}

	setInterval(enterFrameHandler, INTERVAL_MSEC);
	function enterFrameHandler() {

	    ctx.fillStyle = "rgb(25, 25, 25)";
	    ctx.fillRect(0, 0, window.innerWidth, window.innerHeight);

	    ctx.fillStyle = "rgb(255, 255, 255)";

	    var gravityX = mouseX;
	    var gravityY = mouseY;

	    var n = first;

	    do {
	        var diffX = gravityX - n.x;
	        var diffY = gravityY - n.y;
	        var acc = 50 / (diffX * diffX + diffY * diffY);
	        var accX = acc * diffX;
	        var accY = acc * diffY;

	        n.vx += accX;
	        n.vy += accY;
	        n.x += n.vx;
	        n.y += n.vy;

	        n.vx *= 0.96;
	        n.vy *= 0.96;

	        if (n.x > window.innerWidth)
	        n.x = 0;
	        else if (n.x < 0)
	        n.x = window.innerWidth;
	        if (n.y > window.innerHeight)
	        n.y = 0;
	        else if (n.y < 0)
	        n.y = window.innerHeight;

	        ctx.fillRect(n.x, n.y, 1, 1);
	    }
	    while (n = n.next);
	}

	canvas.addEventListener("mouseup", mouseUp, false);
	canvas.addEventListener("mousedown", mouseDown, false);
	canvas.addEventListener("touchdown", mouseDown, true);

	function mouseDown(e) {
	    rect = e.target.getBoundingClientRect();
	    mouseX = e.clientX - rect.left;
	    mouseY = e.clientY - rect.top;
	    canvas.addEventListener("mousemove", mouseMove, false);
	}
	function mouseMove(e) {
	    rect = e.target.getBoundingClientRect();
	    mouseX = e.clientX - rect.left;
	    mouseY = e.clientY - rect.top;
	}

	function mouseUp(e) {
	    mouseX = -200;
	    mouseY = -200;
	    canvas.removeEventListener("mousemove", mouseMove, false);
	}

	window.onresize = resize;
	function resize() {
	    ctx.canvas.width = window.innerWidth;
	    ctx.canvas.height = window.innerHeight;
	}
	
});
