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).select('.basis')[0].insert({'top': (this.config.tpl)});

			this.content = (
				(this.surface = $('vcard-outer')).select('div.ContentPopupBoxArea')[0]
			);

			this.isPrepared = true;
		}
	},

	show: function(id, el, floatSide) {
		var offset, el = $(el);

		this.prepare();

		this.content.update();

		if (offset = Position.cumulativeOffset(el))
		{
			if ('left' == floatSide)
			{				
				this.surface.setStyle({
					top: (offset[1] + el.getHeight()) + 'px', left: (offset[0] - 5 - this.surface.getWidth()) + 'px'
				});
			}
			else
			{
				this.surface.setStyle({
					top: (offset[1] + el.getHeight()) + 'px', left: (offset[0] - 5) + 'px'
				});			
			}

			if (!this.surface.visible())
			{
				this.surface.show()
			}

			this.sendRequest({user_id: 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))
		{
			var el;

			if (el = this.cntr.down('#close'))
			{
				this.addAction(el, 'click', this.hide.bind(this, [true]), true);
			}
		}
	},

	sendRequest: function (get) {
		this.content.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.content.update(response.content);
					this.initSurface();
				}
				else
				{
					this.surface.hide();
				}

				this.content.stopWaiting();
			}.bind(this)
		});
	}
});