Boo = new function() {
	this.mouse={x:0,y:0};
	this.sprite={x:0,y:0};
	this.acc={x:0,y:0};
	this.type=1;

	this.get_mouse_position = function(ev) {
		ev = ev || window.event;
		if (this.is_msie == false) {
			return {x: ev.pageX, y: ev.pageY};
		} else {
			return {
				x: (ev.clientX + document.documentElement.scrollLeft - document.documentElement.clientLeft),
				y: (ev.clientY + document.documentElement.scrollTop  - document.documentElement.clientTop)
			};
		}
	};

	this.moved = function(ev) {
		this.mouse = this.get_mouse_position(ev);
	};

	this.action = function() {
		var dx = this.mouse.x - this.sprite.x;
		var dy = this.mouse.y - this.sprite.y;
		dx*=dx;dy*=dy;
		if (dx+dy>22500) {
			
			if (this.mouse.x > this.sprite.x) {
				if (this.acc.x<10) this.acc.x++;
			} else if (this.mouse.x<this.sprite.x) {
				if (this.acc.x>-10) this.acc.x--;
			} else {
				if (this.acc.x>0) this.acc.x-=5; else if (this.acc.x<0) this.acc.x+=5;
			}

			if (this.mouse.y>this.sprite.y) {
				if (this.acc.y<10) this.acc.y++;
			} else if (this.mouse.y<this.sprite.y) {
				if (this.acc.y>-10) this.acc.y--;
			} else {
				if (this.acc.y>0) this.acc.y-=5; else if (this.acc.y<0) this.acc.y+=5;
			}
			
			if (this.acc.x>=0) {
				if (this.type==1) this.boo.src="img/boo1.gif";
				this.type=0;
			} else {
				if (this.type==0) this.boo.src="img/boo0.gif";
				this.type=1;
			}
			
			this.sprite.x+=this.acc.x;
			this.sprite.y+=this.acc.y;
			
			this.boo.style.left=(Math.floor(this.sprite.x-33))+'px';
			this.boo.style.top=(Math.floor(this.sprite.y-32))+'px';
		} else {
			this.acc.x=this.acc.y=0;
		}
		setTimeout("Boo.action()", 50);
	}

	this.init = function() {
		this.boo = document.getElementById('sprite');
		this.is_msie = (navigator.userAgent.search(/MSIE \d+/)>-1) ? true : false;
		document.getElementsByTagName('BODY')[0].onmousemove = function(ev) { Boo.moved(ev) };
		this.action();
	}
}

function init() {
	Boo.init();
}

function shownews(i, type) {
	var div = document.getElementById('news_'+i);
	div.style.display = (div.style.display == 'none' ? 'block' : 'none');
	if (div.firstChild.nodeValue.substring(0, 1) == 'x') {
		div.innerHTML = '<p align="center"><img src="/style/ruota.gif" alt="Attendere, prego..."/>';
		ajax.request_enqueue('/ajax/news.php', 'type='+type+'&id='+i);
	}
}

function shownews2(id) {
	var div = document.getElementById(id);
	div.style.display = (div.style.display == 'none' ? 'block' : 'none');
}

var shownewstype = null;
var shownewslist = [];
var shownewsindex = null;

function shownews3(div, el) {
	var m = div.id.match(/^news_(.*?)_(.*?)$/);
	var type = m[1];
	var topic = m[2];
	if (el) var title = el.firstChild.nodeValue;

	var div = document.getElementById('quickread');
	if (el) div.style.display = (div.style.display == 'block' ? 'none' : 'block');
	document.getElementById('quickread_title').firstChild.nodeValue = title;
	document.getElementById('quickread_body').innerHTML = '<p align="center"><img src="/style/ruota.gif" alt="Attendere, prego..."/>';
	ajax.request_enqueue('/ajax/news.php', 'type='+type+'&id='+topic);

	if (shownewstype != type) {
		shownewstype = type;
		div = document.getElementById('newscol_'+shownewstype);
		shownewslist = [];

		for (var i = 0; i < div.childNodes.length; i++) {
			if (div.childNodes[i].nodeType == 1 && div.childNodes[i].className == 'news') {
				shownewslist.push(div.childNodes[i]);
				if (div.childNodes[i] == div) shownewsindex = i;
			}
		}
	}
}

function shownewsnext() {
	shownewsindex = (shownewsindex + 1) % shownewslist.length;
	shownews3(shownewslist[shownewsindex]);
	return false;
}

function shownewsprev() {
	shownewsindex--;
	if (shownewsindex < 0) shownewsindex += shownewslist.length;
	shownews3(shownewslist[shownewsindex]);
	return false;
}

function showhint(n, b) {
	var block = document.getElementById(n);
	block.style.display = (b ? '' : 'none');
	return true;
}
