// (c) 2009 VIZUS.CZ s.r.o.
// Updated: 10.4.2009 23:06:56

function Baterka()
{
	// init
	this.init = function(width, height)
	{
		this.width = width;
		this.height = height;
		this.mouseTop = 0;
		this.mouseLeft = 0;
		this.firstMovement = true;
		this.turnOn = false;

		if (navigator.userAgent.match(/Chrome/))
		{
			this.body = document.body;
			this.client = document.documentElement;
		}
		else if (document.compatMode && document.compatMode == 'CSS1Compat')
			this.body = this.client = document.documentElement;
		else
			this.body = this.client = document.body;

		this.lamp = document.createElement('DIV');
		document.body.appendChild(this.lamp);
		this.lamp.className = 'baterka-lamp';

		var sides = ['Left', 'Right', 'Top', 'Bottom'];

		for (var i = 0; i < 4; i++)
		{
			var div = document.createElement('DIV');
			document.body.appendChild(div);
			div.className = 'baterka-side';
			div.style.width = width + 'px';
			this[ 'side' + sides[i] ] = div;

			var div = document.createElement('DIV');
			document.body.appendChild(div);
			div.className = 'baterka-cone-' + sides[i].toLowerCase();
			this[ 'cone' + sides[i] ] = div;
		}

		if (window.navigator.userAgent.match(/Firefox/))
			window.onmousemove = new Function('event', 'Baterka.onMouseMoveFX(event)');
		else
			document.body.onmousemove = new Function('Baterka.onMouseMove()');

		window.onscroll = new Function('Baterka.onScroll()');
	};

	// move the lamp
	this.move = function()
	{
		if (!this.turnOn)
			return;

		var posTop = this.mouseTop - this.height / 2;
		var posLeft = this.mouseLeft - this.width / 2;
		var width2 = this.width / 2;
		var height2 = this.height / 2;

		if (posTop < 0)
			posTop = 0;

		if (posTop + this.height > this.client.clientHeight)
			posTop = this.client.clientHeight - this.height;

		if (posLeft < 0)
			posLeft = 0;

		if (posLeft + this.width > this.client.clientWidth)
			posLeft = this.client.clientWidth - this.width;

		posLeft += this.body.scrollLeft;
		posTop += this.body.scrollTop;

		this.lamp.style.top = posTop + height2 + 10 + 'px';
		this.lamp.style.left = posLeft + width2 + 10 + 'px';

		this.coneLeft.style.left = posLeft + 'px';
		this.coneLeft.style.top = posTop + 'px';
		this.coneRight.style.left = posLeft + width2 + 25 + 'px';
		this.coneRight.style.top = posTop + 'px';
		this.coneTop.style.left = posLeft + width2 - 25 + 'px';
		this.coneTop.style.top = posTop + 'px';
		this.coneBottom.style.left = posLeft + width2 - 25 + 'px';
		this.coneBottom.style.top = posTop + width2 + 25 + 'px';

		this.sideLeft.style.width = posLeft + 'px';
		this.sideLeft.style.height = this.body.scrollHeight + 'px';
		this.sideRight.style.left = posLeft + this.width + 'px';
		this.sideRight.style.width = this.client.clientWidth - (posLeft + this.width) + 'px';
		this.sideRight.style.height = this.body.scrollHeight + 'px';

		if (posTop > 0)
		{
			this.sideTop.style.display = 'block';
			this.sideTop.style.height = posTop + 'px';
			this.sideTop.style.left = posLeft + 'px';
		}
		else
			this.sideTop.style.display = 'none';

		var height = this.client.clientHeight - (posTop + this.height - this.body.scrollTop);

		if (height > 0)
		{
			this.sideBottom.style.display = 'block';
			this.sideBottom.style.height = height + 'px';
			this.sideBottom.style.left = posLeft + 'px';
			this.sideBottom.style.top = posTop + this.height + 'px';
		}
		else
			this.sideBottom.style.display = 'none';

		if (this.firstMovement)
		{
			this.firstMovement = false;
			//this.lamp.style.display = 'block';
			this.coneLeft.style.display = 'block';
			this.coneRight.style.display = 'block';
			this.coneTop.style.display = 'block';
			this.coneBottom.style.display = 'block';
			this.sideLeft.style.display = 'block';
			this.sideRight.style.display = 'block';
		}
	};

	// zapnuti/vypnuti
	this.turn = function(on)
	{
		this.turnOn = on | !this.turnOn;
		value = this.turnOn ? 'block' : 'none';

		//this.lamp.style.display = value;
		this.coneLeft.style.display = value;
		this.coneRight.style.display = value;
		this.coneTop.style.display = value;
		this.coneBottom.style.display = value;
		this.sideLeft.style.display = value;
		this.sideRight.style.display = value;
		this.sideTop.style.display = value;
		this.sideBottom.style.display = value;

		var date = new Date();
		date.setFullYear(date.getFullYear() + 1);
		document.cookie = 'baterka=' + (this.turnOn ? 'on' : 'off') + '; expires=' + date.toGMTString();

		this.move();
	}

	// mouse mooving handler
	this.onMouseMove = function()
	{
		this.mouseTop = event.clientY;
		this.mouseLeft = event.clientX;
		this.move();
	};

	// mouse mooving handler
	this.onMouseMoveFX = function(event)
	{
		this.mouseTop = event.clientY;
		this.mouseLeft = event.clientX;
		this.move();
	};

	// document scrolling handler
	this.onScroll = function()
	{
		if (!this.firstMovement)
			this.move();
	};
}
