var directionChangeCounter = 0;
var directionCoutLimit = 2;
var p1 = rand(-1, 1);
var p2 = rand(-1, 1);
var pb1 = rand(-1, 1);
var pb2 = rand(-1, 1);
var speed = 60;

function Fun() {

	this.dot = document.createElement('div');
	this.init();
}

Fun.prototype.init = FunInit;
Fun.prototype.banane = RUUNNNNN;

function FunInit() {

	var sizeW = document.body.offsetWidth;
	var sizeH = window.innerHeight;
	var pages = document.getElementsByTagName("body");
	var page = pages[0];

	var core = document.createElement('div');
	core.id = "core";
	core.style.width = "1em";
	core.style.height = "1em";
	core.style.backgroundColor = "black";
	core.style.top = "50%";
	core.style.left = "50%";
	core.style.marginLeft = "-0.5em";
	core.style.marginTop = "-0.5em";
	core.style.position = "absolute";

	var point = document.createElement('div');
	point.id = 'point';
	point.style.width = "4px";
	point.style.height = "4px";
	point.style.backgroundColor = "red";
	point.style.top = "50%";
	point.style.left = "50%";
	point.style.marginLeft = "-2px";
	point.style.marginTop = "-2px";
	point.style.position = "absolute";
	point.setAttribute('onclick', 'iwin();');

	this.dot.id = "dot";
	this.dot.style.backgroundColor = "clear";
	this.dot.style.width = "3em";
	this.dot.style.height = "3em";
	this.dot.style.position = 'absolute';
	this.dot.style.top = sizeH/2+"px";
	this.dot.style.left = sizeW/2+"px";
	this.dot.setAttribute('onmousemove', 'RUUNNNNN();');
	this.dot.setAttribute('webkit-transition', 'all 0.2s ease-in-out');

	core.appendChild(point);
	this.dot.appendChild(core);
	page.appendChild(this.dot);
}

function RUUNNNNN() {

	directionChangeCounter = directionChangeCounter-1;
	if (directionChangeCounter <= 0) {
		p1 = rand(0, 1);
		p2 = rand(0, 1);
		pb1 = rand(0, 1);
		pb2 = rand(0, 1);
		directionChangeCounter = directionCoutLimit;
	}

	if (p1 == 1) {
		if (p2 == 1) {
			this.dot.style.top = this.dot.offsetTop+rand(+speed, +speed);
		}else if (p2 == 0) {
			this.dot.style.top = this.dot.offsetTop+rand(+speed, -speed);
		}
	} else if (p1 == 0) {
		if (p2 == 1) {
			this.dot.style.top = this.dot.offsetTop+rand(-speed, +speed);
		} else if (p2 == 0) {
			this.dot.style.top = this.dot.offsetTop+rand(-speed, -speed);
		}
	}

	if (pb1 == 1) {
		if (pb2 == 1) {
			this.dot.style.left = this.dot.offsetLeft+rand(+speed, +speed);
		}else if (pb2 == 0) {
			this.dot.style.left = this.dot.offsetLeft+rand(+speed, -speed);
		}
	} else if (pb1 == 0) {
		if (pb2 == 1) {
			this.dot.style.left = this.dot.offsetLeft+rand(-speed, +speed);
		} else if (pb2 == 0) {
			this.dot.style.left = this.dot.offsetLeft+rand(-speed, -speed);
		}
	}

	function lolinger() {
			if (this.dot.offsetLeft-40 <= 0) {
				return true;
			}
			if (this.dot.offsetTop-40 <= 0) {
				return true;
			}
			if (this.dot.offsetLeft-40 >= s.width) {
				return true;
			}
			if (this.dot.offsetTop >= s.height) {
				return true;
			}
			return false;
		}

	s = sizeOfBody();
	if (lolinger()) {
			this.dot.style.top = s.height/2+"px";
			this.dot.style.left = s.width/2+"px";
			}
}

function iwin() {

	alert("I Am The One And Only!!");
}

function rand(min, max) {

	if (min > max) {
		return -1;
	}
	if (min == max) {
		return min;
	}

	return (min + parseInt(Math.random() * (max-min+1)));
}

function woop() {

	lolz = new Fun;
}
