function ajax_player(container, url, params) {
	this.req = null;
	this.url = url || 'ajax.php';
	this.params = params;
	this.msgbox = container;
	this.timeout = 150000;
	this.timer = null;
	this.messages = {
		'loading': 'загрузка...',
		'error': 'Сервис временно недоступен. Пожалуйста, попробуйте позже.',
		'timeout': 'Превышено время ожидания. Пожалуйста, попробуйте еще раз.'
	};
}

ajax_player.prototype.createReq = function() {
	if (window.XMLHttpRequest) {
		try {
			this.req = new XMLHttpRequest();
		} catch (e) {}
	} else if (window.ActiveXObject) {
		try {
			this.req = new ActiveXObject('Msxml2.XMLHTTP');
		} catch (e) {
			try {
				this.req = new ActiveXObject('Microsoft.XMLHTTP');
			} catch (e) {}
		}
	}
};

ajax_player.prototype.send = function() {
	if (this.req != null) return;

	this.createReq();

	if (this.req) {
		var bind = this;
		this.onInit();
		this.req.onreadystatechange = function() {
			bind.onReqChange();
		};
		this.req.open('POST', this.url, true);
		this.req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
		this.req.setRequestHeader('If-Modified-Since', 'Sat, 1 Jan 2000 00:00:00 GMT');
		this.req.send(this.params);
		this.timer = setTimeout(function() {
			bind.onTimeout();
		}, this.timeout);
	}
};

ajax_player.prototype.onReqChange = function() {
	if (this.req.readyState == 4) {
		clearTimeout(this.timer);
		if (this.req.status == 200) {
			this.onSuccess(this.req.responseText);
		} else {
			this.onError(this.messages.error);
		}
		this.req = null;
	}
};

ajax_player.prototype.set_message = function(msg) {
	this.msgbox.innerHTML = msg;
};

ajax_player.prototype.onError = function(error) {
	this.set_message(error);
};

ajax_player.prototype.onSuccess = function(result) {
	this.set_message(result);
};

ajax_player.prototype.onInit = function() {
	this.set_message(this.messages.loading);
};

ajax_player.prototype.onTimeout = function() {
	this.req.abort();
	this.onError(this.messages.timeout);
};

function show_player() {
	var player = null;
	var container = null;

	// backward compatibility
	switch(arguments.length) {
		case 1:
			// new model - f(player);
			var id = 'ajaxplayer' + Math.floor(Math.random() * 100000000);
			document.write('<div id="' + id + '"></div>');
			container = document.getElementById(id);
			player = arguments[0];
			break;
		case 2:
			// old model - f(cont_id, player);
			container = arguments[0] ? document.getElementById(arguments[0]) : null;
			player = arguments[1];
			break;
		default:
			// unknown
			return false;
	}

	if (!player || !container) return false;
	
	var ap = new ajax_player(
		container,
		'http://www.rrpwe.com/ajax.php',
		'url=' + player
	);
	ap.send();
}
