var Marker = function(parameters) {
	this.markers = parameters.markers;

	this.setBehaviour();
}

Marker.prototype.setBehaviour = function() {
	this.markers.each(function(node) {
		Event.observe(node, 'click', this.doSending.bind(this, node));
	}.bind(this));
}

Marker.prototype.doSending = function(node) {
	if (node.hasClassName('unactive')) {
		node.removeClassName('unactive');
		node.addClassName('active');
		this.send('add', node.innerHTML);
	} else {
		node.removeClassName('active');
		node.addClassName('unactive');
		this.send('remove', node.innerHTML);
	}
}

Marker.prototype.send = function(action, id) {
	new Ajax.Updater($('marker_result') || new Object(), root_directory + lang + '/ajax/marker/', {
		method: 'post',
		parameters: 'action=' + action + '&id=' + id
	});
}

Marker.prototype.removekAll = function() {
	this.send('remove_all', 0);

	this.markers.each(function(node) {
		node.removeClassName('active');
		node.addClassName('unactive');
	});
}

Marker.prototype.unmarkCurrent = function() {
	var markIds = '';
	this.markers.each(function(node) {
		markIds += node.innerHTML + ',';
		node.removeClassName('active');
		node.addClassName('unactive');
	});

	this.send('unmark_many', markIds);
}

Marker.prototype.markCurrent = function() {
	var markIds = '';
	this.markers.each(function(node) {
		markIds += node.innerHTML + ',';
		node.removeClassName('unactive');
		node.addClassName('active');
	});

	this.send('mark_many', markIds);
}


// Load
Event.observe(window, 'load', function() {
	MyMarker = new Marker({
		markers: $$('div.marker')
	});
});
