function Vector(pX, pY)
{
	this.x = pX;
	this.y = pY;
	
	this.length = function()
		{
			return Math.sqrt(this.x * this.x + this.y * this.y);
		};
	
	return true;
}

function findPos(obj) 
{
	var curleft = curtop = 0;
	if (obj.offsetParent) 
		do 
		{
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		} while (obj = obj.offsetParent);
	return new Vector(curleft, curtop);
}

function FbLogoEye(x, y, r, s)
{
	this.center = new Vector(x, y);
	this.radius = r;
	this.size = s;
	
	return true;
}

function FbLogo()
{
	var leftEye = new FbLogoEye(143, 43.5, 8.5, 3);
	var rightEye = new FbLogoEye(172, 48, 3.3, 1.5);
	
	var headerDiv = document.getElementById("fbheader");
	var logoDiv = document.getElementById("fblogo");
	var logo = logoDiv.getElementsByTagName("a")[0];
	
	var canvas = document.createElement("canvas");
	if (!canvas.getContext)
		return true; // IE.....
	
	canvas.setAttribute("width", 257);
	canvas.setAttribute("height", 100);
	
	var canvasContext = canvas.getContext("2d");
	canvasContext.fillStyle = "rgb(0, 0, 0)";
	
	logo.appendChild(canvas);
	
	var logoPos = findPos(logo);
	
	var mouse = new Vector(0, 0);
	var blink = false;
	
	window.onresize = function(e)
		{
			logoPos = findPos(logo);
		};
	
	headerDiv.onmousemove = function(e)
		{
			logoPos = findPos(logo);
			mouse.x = e.clientX;
			mouse.y = e.clientY;
			drawEyes();
		};
	
	window.onmousedown = function(e)
		{
			blink = true;
			mouse.x = e.clientX;
			mouse.y = e.clientY;
			drawEyes();
		};
	
	window.onmouseup = function(e)
		{
			mouse.x = e.clientX;
			mouse.y = e.clientY;
			blink = false;
			drawEyes();
		};
	
	var drawEye = function(eye)
		{
			var vec = new Vector(mouse.x - eye.center.x - logoPos.x, 
								 mouse.y - eye.center.y - logoPos.y);
			var vecL = vec.length();
			
			if (vecL > eye.radius)
			{
				vec.x *= eye.radius / vecL;
				vec.y *= eye.radius / vecL;
			}
			
			canvasContext.beginPath();
			canvasContext.arc(eye.center.x + vec.x, eye.center.y + vec.y, eye.size * (blink ? 1.2 : 1), 0, Math.PI * 2, true);
			canvasContext.fill();
		};
	
	var drawEyes = function()
		{
			canvasContext.clearRect(0, 0, 257, 100);
			
			drawEye(leftEye);
			drawEye(rightEye);
		};
	
	drawEyes();
	
	return true; 
}

function Chat() {
	while (1) {
		var nick = prompt("Nickname:", "");
	
		if (nick == null) // Cancelled?
			return;

		if (nick.length == 0) {
			alert("En Nickname uginn ass essentiel fir eng sennvoll Konversatioun féiren ze kennen!");
			continue;
		}

		var invalid = nick.match(/[^a-zA-Z0-9\[\\\]\^_\{\|\}-]/);
		if (invalid) {
			alert("En Nickname dierf nemmen bestoen aus:\n"
					+ "* Buchstawen ouni Emlauter an Accenten,\n"
					+ "* Zifferen,\n"
					+ "* Folgenden Zeechen: [ \\ ] ^ _ - { | }");
			continue;
		}
		
		break; // Deen Schläifen-Design rockt!
	}

	document.location.href = "/irc?nick="+nick;
}

//var fbOldWindowOnload = window.onload;
//window.onload = function() 
//{
	new FbLogo(); 
//	if (fbOldWindowOnload != null)
//		fbOldWindowOnload();
//}