VCardRef = Class.create(BasicControl, {
	initialize: function($super, config) {
		Object.extend(this, {
			config: {
				cntr: null,
				msgUrl: null,
				tpl: null,
				effectsDuration: 0.2
			},

			cntr: null,
			isPrepared: false,
			surface: null,
			inner: null
		});

		$super('vcard_ref', config);
	},

	prepare: function() {
		if (!this.isPrepared)
		{
			$(document.body).insert({'top': (
				(this.surface = new Element('div')).addClassName('vcard-ly').hide()
			)});

			this.isPrepared = true;
		}
	},

	show: function(id, el) {
		var offset, el = $(el);

		this.prepare();

		this.surface.update();

		if (offset = Position.cumulativeOffset(el))
		{
			/*this._canHide = false;

			if (this._hide)
			{
				clearTimeout(this._hide);
				this._hide = null;
			}

			this._el = el;

			el.observe('mouseout', this._el._mot = this.hide.bind(this));

			el.observe('mouseover', this._el._mov = function() {
				this._canHide = false;
			}.bind(this));

			this.surface.observe('mouseout', this.hide.bind(this));

			this.surface.observe('mouseover', function() {
				this._canHide = false;
			}.bind(this));*/

			this.surface.setStyle({
				top: (offset[1] + el.getHeight()) + 'px', left: (offset[0]) + 'px'
			});

			if (!this.surface.visible())
			{
				this.surface.show()
			}

			this.sendRequest({uid: this.id = id});
		}
	},

	hide: function(imm) {

		if (imm)
		{
			this.surface.hide();
		}
		else
		{
			if (this._hide)
			{
				clearTimeout(this._hide);
				this._hide = null;
			}

			this._canHide = true;

			this._hide = (function() {
				if (this._canHide)
				{
					this.surface.hide();
				}
			}.bind(this)).delay(0.6);
		}
	},

	initSurface: function() {
		if ((this.cntr = $(this.config.cntr)))
		{
			if (this.cntr.down('.vcard_links #connect'))
			{
				this.addAction(this.cntr.down('.vcard_links #connect'), 'click', function() {
					if (this.id)
					{
						this.sendRequest({action: 'connect', uid: this.id});
					}
				}.bind(this), true);
			}

			if (this.cntr.down('.div_connect #block'))
			{
				this.addAction(this.cntr.down('.div_connect #block'), 'click', function() {
					if (this.id)
					{
						this.sendRequest({action: 'block', uid: this.id});
					}
				}.bind(this), true);
			}

			if (this.cntr.down('.div_connect #unblock'))
			{
				this.addAction(this.cntr.down('.div_connect #unblock'), 'click', function() {
					if (this.id)
					{
						this.sendRequest({action: 'unblock', uid: this.id});
					}
				}.bind(this), true);
			}

			if (this.cntr.down('.vcard_links #send_pms'))
			{
				this.addAction(this.cntr.down('.vcard_links #send_pms'), 'click', function() {
					if (this.id && this.config.msgUrl)
					{
						document.location.href = this.config.msgUrl + '?recipient=' + this.id;
					}
				}.bind(this), true);
			}

			if (this.cntr.down('img#close'))
			{
				this.addAction(this.cntr.down('img#close'), 'click', function() {
					this.hide(true);
				}.bind(this), true);
			}
		}
	},

	sendRequest: function (get) {
		this.surface.startWaiting('bigWaiting');

		if (this.request)
		{
			this.request.transport.abort();
			this.request = null;
		}

		this.request = new Ajax.Request(this.getUrl(get), {
			onFailure: function() {
				this.surface.stopWaiting();
			}.bind(this),
			onSuccess: function(response) {
				if ((response = this.evalResponse(response)) && response.content)
				{
					this.surface.update(response.content);
					this.initSurface();
				}
				else
				{
					this.surface.hide();
				}

				this.surface.stopWaiting();
			}.bind(this)
		});
	}
});