window.onload = function () {
	var message = $('message');
	message.onkeypress=message.onkeyup=message.onblur=checkForm;
	checkForm();
	
	/*$('add-toast').onclick = function () {
		show('input');
		hide('add-toast');
		focus('message');
		$('key').value = '111';
	}*/
	
	if (window.location.hash == '#first') {
		var li = document.getElementsByTagName('li', $('messages'));
		
		if (li && li.length) {
			li[0].className = 'selected';
		}
	}	
	
	var zIndex = 0;
	$('messages').onclick = function (e) {
	
		var ev = e || window.event;
	
		var x = 0, y = 0;
		
		if (ev.pageX) {
			x = ev.pageX;
			y = ev.pageY;
		}
		else if (ev.clientX) {
			var body = document.documentElement || document.body;
			x = ev.clientX + body.scrollLeft;
			y = ev.clientY + body.scrollTop;
		}
	
		zIndex++;
		var mes = Math.floor($('measurer').offsetWidth * 0.2);
		var delta = 60, cx, cy, color, maxColor = 7, el;
		for (var i = 0; i < 7 + getRandom(10); i++) {
			cx = x + getRandom(delta) - delta - mes;
			cy = y + getRandom(delta) - delta;
			color = getRandom(maxColor) + 1;
			el = document.createElement('div');
			el.className = 'ball color' + color;
			el.style.marginLeft = cx + 'px';
			el.style.top = cy + 'px';
			el.style.zIndex = zIndex;
			$('sweet-balls').appendChild(el);
		}
	}
}

function checkForm()
{
	var disabled = false;
	if (stripSpaces($('message').value) == '')	disabled = true;
	
	$('submit').disabled = disabled;
}

function stripSpaces(text) {
	return text.replace(/[ \t\r\n]/g, '');
}

function toggle(id) {
	var el = $(id);
	
	if (el)
	{
		if (el.style.display == 'none')	el.style.display = 'block';
		else	el.style.display = 'none';
	}
}

function visible(id) {
	var el = $(id);
	if (el)	el.style.visibility = 'visible';
}

function focus(id) {
	var el = $(id);
	if (el && el.offsetWidth)	$(id).focus();
}

function getRandom(n) {
	return Math.floor(Math.random() * n);
}

window.$ = function(id) {
	return document.getElementById(id);
}

function show(id) {
	if (typeof id == 'string')	$(id).style.display = 'block';
	else id.style.display = 'block';
}

function hide(id) {
	if (typeof id == 'string')	$(id).style.display = 'none';
	else id.style.display = 'none';
}