var ServerLog = new Class({
	id: null,
	data: null,
	initialize: function() {
		window.addEvent('load', this.sendEnter.bind(this));
		window.addEvent('unload', this.sendLeave.bind(this));
	},
	sendEnter: function(contentType, contentId, contentIndex) {
		this.data.action = 'enter';
		new Request.JSON({
			url: '/stats.php',
			method: 'get',
			onSuccess: function(data) { this.id = data.id; }.bind(this)
		}).get({ json: JSON.encode(this.data) });
	},
	sendLeave: function() {
		if (this.id != null) {
			new Request.JSON({
				url: '/stats.php',
				method: 'get'
			}).get({ json: JSON.encode({
				action: 'leave',
				id: this.id
			})});
		}
	}
});
var log = new ServerLog();

var FrontpageClicks = new Class({
	initialize: function(){
		window.addEvent('domready', function(){
			this.addEvents();
		}.bind(this));
	},
	addEvents: function(){
		for (var i = 1; i <= 4; i++) {
			var links = $$('#blogItem' + i + ' a');
			links.addEvent('click', this.sendClick.bind({
				type: 'babeblog',
				id: i
			}));
		}
		var menuItems = $$('.mainMenu a');
		for (var i = 0; i < menuItems.length; i++) {
			var item = menuItems[i];
			item.addEvent('click', this.sendClick.bindWithEvent({
				type: 'menubar',
				id: item.id
			}));
		}
	},
	sendClick: function(e) {
		new Request.JSON({
			url: '/stats.php',
			method: 'get'
		}).get({ json: JSON.encode({
			action: 'click',
			type: this.type,
			id: this.id
		}) });
	}
});
var frontpageClicks = new FrontpageClicks();
