function rGetElementPosition(el) {
	var x = 0, y = 0;
	do {
		y += el.offsetTop  || 0;
		x += el.offsetLeft || 0;
		el = el.offsetParent;
	} while (el);

	if (navigator.userAgent.indexOf ("Opera") != -1){
    	    y = window.event.y+document.body.scrollTop;
        }
                                                                
	return {x:x,y:y};
	
}
function rMoveElement(el, left, top) {
        if (!el) return false;
        el.style.left = left + "px";
        el.style.top = top + "px";
}

function rGetDimensions(el) {
        if (el.style.display != 'none') {
              return {w:el.offsetWidth,h:el.offsetHeight};
        }
        return {w:el.clientWidth,h:el.clientHeight};
}
                                        

var viewVisitingTimeOut;
var closeVisitingTimeOut;
var lastPositionVisitingFloatBlock = {};
var rFloatBlockId = 'visiting_float_block';

function rViewUserFloatBlock(el, login, is_community) {
	var pos = rGetElementPosition(el);
    if (!pos) {
    	return false;
    }
    if (lastPositionVisitingFloatBlock.x == pos.x && lastPositionVisitingFloatBlock.y == pos.y) {
        window.clearTimeout(closeVisitingTimeOut);
    	return false;
    }

    // закрываем показанные ранее
    rCloseUserFloatBlock();
	viewVisitingTimeOut = window.setTimeout("rViewUserFloatBlockWithTimeOut('"+pos.x+"', '"+pos.y+"', '"+login+"', '"+is_community+"');", 300);
}

// ajax запрос блока и его визуализация
function rViewUserFloatBlockWithTimeOut(X, Y, login, is_community) {
	var url = '/common/';
	url += is_community==1 ? 'ajax_get_float_community_block' : 'ajax_get_float_block';
	url += '.html?login='+login+'&r='+Math.random();
	var req = new RAjaxRequest( {'url': url, callback: function(r, options){
		if ( req.responseIsSuccess ) {
			if (r.responseText) {
                        	var userFloatBlock = document.createElement('div');
                        	userFloatBlock.setAttribute('id', rFloatBlockId);
                                try {
                                        userFloatBlock.attachEvent('onmouseover', eventMouseOverUserFloatBlock);
                                        userFloatBlock.attachEvent('onmouseout', eventMouseOutUserFloatBlock);
                                } catch (e) {
                                        try {
                                        	userFloatBlock.setAttribute('onMouseOver', 'eventMouseOverUserFloatBlock();');
                                        	userFloatBlock.setAttribute('onmouseout', 'eventMouseOutUserFloatBlock();');
                                	} catch (e) {}
                        	}

                        	userFloatBlock.innerHTML = r.responseText;
                        	document.body.appendChild(userFloatBlock);
                        	userFloatBlock.style.display = 'block';
                                userFloatBlock.style.position = 'absolute';

                                // размеры блока
                                var userFloatBlockWH = rGetDimensions(userFloatBlock);

                                // позиционируем блок слева
                        	if (parseInt(X) < userFloatBlockWH.w) {
                        	        X = 0;
                        	} else {
                        	        X = parseInt(X) - userFloatBlockWH.w;
                        	}

				Y = parseInt(Y) + 15;
                        	rMoveElement(userFloatBlock, parseInt(X), parseInt(Y));
                        	lastPositionVisitingFloatBlock = {x:X,y:Y};
			}
		}
	} } );
}

function eventMouseOverUserFloatBlock() {
        window.clearTimeout(closeVisitingTimeOut);
}

function eventMouseOutUserFloatBlock() {
        rCloseUserFloatBlock(true);
}

// закрытие блока информации о пользователе
function rCloseUserFloatBlock(flag_timeout) {
        window.clearTimeout(viewVisitingTimeOut);
        window.clearTimeout(closeVisitingTimeOut);
        if (flag_timeout) {
                closeVisitingTimeOut = window.setTimeout("rCloseUserFloatBlockWithTimeOut();", 500);
        } else {
                rCloseUserFloatBlockWithTimeOut();
        }
}

function rCloseUserFloatBlockWithTimeOut() {
        var userFloatBlock = document.all ? document.all[rFloatBlockId] : document.getElementById(rFloatBlockId);
        if (!userFloatBlock) return false;
        userFloatBlock.parentNode.removeChild(userFloatBlock);
        lastPositionVisitingFloatBlock = {};
}

