﻿/* slightly modified version of Konami-JS : http://snaptortoise.com/konami-js */

window.size = function () {

	var w = 0;
	var h = 0;

	//IE
	if (!window.innerWidth) {
		//strict mode
		if (!(document.documentElement.clientWidth == 0)) {
			w = document.documentElement.clientWidth;
			h = document.documentElement.clientHeight;
		}
		//quirks mode
		else {
			w = document.body.clientWidth;
			h = document.body.clientHeight;
		}
	}
	//w3c
	else {
		w = window.innerWidth;
		h = window.innerHeight;
	}
	return { width: w, height: h };
}

var ball = {
	size: 75,
	posX: Math.random() * document.documentElement.clientWidth,
	posY: Math.random() * document.documentElement.clientHeight,
	dx: randomNo(),
	dy: randomNo()
};

function randomNo() {
	var number = Math.floor(Math.random() * 5 + 5);
	var sign = Math.floor(Math.random() * 2 - 1);
	return (sign != 0) ? number * sign : number;
}

var bounce = {
	timeout: 0,
	trigger: function () {
		bounce.timeout = setTimeout("bounce.run()", 30);
	},
	run: function () {

		ball.posX += ball.dx;
		ball.posY += ball.dy;

		var top = document.documentElement.scrollTop;
		var bottom = window.size().height + document.documentElement.scrollTop;
		var left = document.documentElement.scrollLeft;
		var right = document.documentElement.clientWidth + document.documentElement.scrollLeft;

		if (ball.posX >= (right - ball.size)) {
			if (ball.dx > 0) ball.dx = -ball.dx;
			ball.posX = right - ball.size;
		}
		if (ball.posX < left) {
			if (ball.dx < 0) ball.dx = -ball.dx;
			ball.posX = left;
		}
		if (ball.posY >= (bottom - ball.size)) {
			if (ball.dy > 0) ball.dy = -ball.dy;
			ball.posY = bottom - ball.size;
		}
		if (ball.posY < top) {
			if (ball.dy < 0) ball.dy = -ball.dy;
			ball.posY = top;
		}

		$("#ball").css({ top: ball.posY, left: ball.posX });
		
		bounce.trigger();

	},
	remove: function () {
		$("#ball").remove();
		clearTimeout(bounce.timeout);
	}
};

var konamiBounce = {
	pattern: "38384040373937396665",
	input: "",
	clear: 0,
	load: function () {
		window.document.onkeyup = function (e) {
			konamiBounce.input += e ? e.keyCode : event.keyCode;
			if (konamiBounce.input == konamiBounce.pattern) konamiBounce.code();
			clearTimeout(konamiBounce.clear);
			konamiBounce.clear = setTimeout("konamiBounce.clear_input()", 2000);
		};
	},
	code: function () {
		$("body").append('<img id="ball" style="z-index: 2001; position: absolute; width: 75px; height: 75px;" src="img/hido.gif" />');
		$("body").append('<div id="ball_overlay" style="background: #000; z-index: 2000; opacity: 0.7; filter: alpha(opacity=70); left: 0; top: 0; position: fixed; width: 100%; height: 100%;"></div>');
		clearTimeout(bounce.timeout);
		bounce.trigger();
	},
	clear_input: function () {
		konamiBounce.input = "";
		clearTimeout(konamiBounce.clear);
	}
};

konamiBounce.load();

$("#ball_overlay").live("click", function () {
	$(this).remove();
	bounce.remove();
});

