function Tooltip(params) {
    for(var i in params) this[i] = params[i];
    this.staticParams = {
	'sticker':{'classname':'tipSticker','timeout':0.5},
	'avatar':{'classname':'tipAva','timeout':0.01},
	'avatarC':{'classname':'tipAva','timeout':0.01},
	'photo':{'classname':'tipPhoto','timeout':0.5},
	'video':{'classname':'tipVideo','timeout':0.5},
	'gift':{'classname':'tipGift','timeout':0.5},
	'giftLabel':{'classname':'tipGift','timeout':0.5},
	'community':{'classname':'tipCommunity','timeout':0.01,'setposition':'right'}
    };
}

Tooltip.prototype.setVars = function(type, element){
    if(!type || !element) return false;
    this.type = type;
    this.element = $(element);
    this.email = this.element.readAttribute('email') || 0;
    this.biglink = this.element.readAttribute('biglink') || 0;
    this.imagesurl = this.element.readAttribute('imagesurl') || 0;
    this.online = this.element.readAttribute('online') || 0;
    this.privat = this.element.readAttribute('privat') || 0;
    this.username = this.element.readAttribute('username') || 0;
    this.vip = this.element.readAttribute('vip') || 0;
    this.giftsRotate = $('paidGiftsMain') ? 1 : 0;
    this.timeout = this.staticParams[this.type].timeout || 0.01;
    if(this.giftsRotate) hideNewGifts();
    this.element.observe('mouseout', this.tHide.bindAsEventListener(this));
    this.tShow.bind(this).delay(this.timeout);
}

Tooltip.prototype.tShow = function(){
    this.noHide.bind(this);
    if(this.divpic) document.body.removeChild(this.divpic);
    var re = /^(.*?)@(\w+)\./;
    if (re.test(this.email)){
	this.login = RegExp.$1;
	this.domain = RegExp.$2;
    }
    var defaultHtml;
// from syberia with love
    switch(this.type) {
	case 'stiker':
	    if (this.online) var onlineneed = "<div class=\"on_90 divAuto\"><a href=\"/my/dialogues?thread=" + this.email +"\">на сайте</a></div>";
	    else var onlineneed = "";
	    var prneed = '';
	    if (this.privat) {
		if(this.vip) prneed = "Приватный VIP-стикер";
		else prneed = "Приватный стикер";
	    }
	    else {
		if(this.vip) prneed = "VIP-стикер от пользователя";
		else prneed = "Стикер от пользователя";
	    }
	    defaultHtml = "<div class=\"divAuto ava90 mt5\"><a href=\"http://" + this.mymailhost + "/" + domain + "/" + login + 
	    "/\"><i class=\"ava90\" style=\"background-image: url(http://" + this.photoavthost + "/" + domain + "/" + login + 
	    "/_avatar90);\"></i></a></div>" + onlineneed + "<div class=\"mt5 tac\"><a href=\"http://www.mail.ru/agent?message&to=" + 
	    this.email + "\" title=\"Щелкни, чтобы пообщаться с этим человеком в Mail.Ru Агенте\"><i class=\"spr-mrim-13\" email=\"" + 
	    this.email + "\"></i></a><a href=\"http://##MyMailHost##/" + this.domain + "/" + this.login + "/\">" + this.username + "</a></div>";
	    break;
	case 'avatar':
	    defaultHtml = '<i style="background-image: url(http://avt.foto.mail.ru/' + this.domain + '/' + this.login + '/_avatar);"></i>';
	    break;
	case 'avatarC':
	    defaultHtml = '<a href="/community/' + this.email + '/"><i style="background-image: url(' + this.imagesurl + ');"></i></a>';
	    break;
	case 'photo':
	    defaultHtml = '<a href="' + this.biglink + '"><i style="background-image: url(' + this.imagesurl + ');"></i></a>';
	    break;
	case 'video':
	    defaultHtml = '<a href="' + this.biglink + '"><i style="background-image: url(' + this.imagesurl + ');"></i></a>';
	    break;
	case 'gift':
	    defaultHtml = '<a href="' + this.biglink + '"><i style="background-image: url(http://img0.imgsmail.ru/r/my/gifts/' + this.imagesurl + ');"></i></a>';
	    break;
	case 'giftLabel':
	    defaultHtml = '<label for="' + this.biglink + '"><i style="background-image: url(http://img1.imgsmail.ru/r/my/gifts/' + this.imagesurl + ');"></i></label>';
	    break;
	case 'community':
	    defaultHtml = '<a href="http://www.mail.ru/agent?message&to=' + this.email + '" title="Щелкни, чтобы пообщаться с этим человеком в Mail.Ru Агенте"><i class="spr-mrim-status13" style="background-image: url(http://status.mail.ru?' + this.email + ');"></i></a><a href="/' + this.domain + '/' + this.login + '/">' + this.username + '</a><div></div>';
	    break;
	default:
	    return false;
    }
    this.divpic = document.body.appendChild(new Element('div', {'style':'display:none;position:absolute;top:-250px;left:-250px;'}));
    this.divpic.className = this.staticParams[this.type].classname || '';
    this.divpic.innerHTML = defaultHtml;
    this.divpic.observe('mouseout', this.tHide.bindAsEventListener(this));
    this.divpic.observe('mouseover', this.noHide.bindAsEventListener(this));

    var coor = this.absPosition(this.element);
    this.divpic.style.display = 'block';

    if(this.staticParams[this.type].setposition && this.staticParams[this.type].setposition == 'right') {
	this.divpic.style.right = (document.body.clientWidth - coor.left) + 'px';
	this.divpic.style.left = '';
    } else {
	if(document.body.clientWidth > (this.divpic.offsetWidth + coor.left)) this.divpic.style.left = coor.left + 'px';
	else this.divpic.style.left = (coor.left -  (this.divpic.offsetWidth - this.element.offsetWidth)) + 'px';
    }
    if(coor.top < (document.body.scrollTop + document.body.clientHeight - this.divpic.offsetHeight)) this.divpic.style.top = coor.top + 'px';
    else this.divpic.style.top = (coor.top - (this.divpic.offsetHeight - this.element.offsetHeight)) + 'px';
//    alert(this.divpic.style.top + ' ' + this.divpic.style.left);
//    alert(coor.top + ' ' + coor.left);

}

Tooltip.prototype.noHide = function(){
    if(this.hideTimeout){
	clearTimeout(this.hideTimeout);
	if(this.giftsRotate) hideNewGifts();
    }
}

Tooltip.prototype.tHide = function(event){
    if(event) {
	if(this.hideTimeout) clearTimeout(this.hideTimeout);
	this.hideTimeout = this.tHide.bind(this).delay(this.timeout);
	return;
    }
    if(this.divpic) document.body.removeChild(this.divpic);
    this.divpic = 0;
    if(this.hideTimeout) clearTimeout(this.hideTimeout);
    if(this.giftsRotate) showNewGifts();
    return;
}

Tooltip.prototype.absPosition = function(offsetTrail){
    var offsetLeft = 0;
    var offsetTop = -11;
    while (offsetTrail) {
	offsetLeft += Math.abs(offsetTrail.offsetLeft);
	offsetTop += offsetTrail.offsetTop;
	offsetTrail = offsetTrail.offsetParent;
    }
    return {
	left: offsetLeft,
	top: offsetTop
    }
}
// gifts rotate on face
function showNewGifts(){
    var topGifts = 70*giftStep;
    var p = $('podarki');
    if (p) p.style.top = '-' + topGifts + 'px';
    showNewGiftsTimer = setTimeout("showNewGifts()",4000);
    giftStep = giftStep + 1;
    if (giftStep == GiftItems){ giftStep = 0; }
}

function hideNewGifts(){ clearTimeout(showNewGiftsTimer) }

function toggleDialogImage(link) {
    var container = $(link.parentNode);
    link = container.select('a[name="imglink"]')[0];
    var img = container.select('i[name="img"]')[0];
    var imgurl = container.readAttribute('imgurl');
    var imgid = container.readAttribute('imgid');
    var imgtype = img.readAttribute('type');
    if(imgtype == 'small') {
	img.style.backgroundImage = 'url(' + imgurl + 'i-' + imgid + ')';
	img.style.width = img.readAttribute('fw') + 'px';
	img.style.height = img.readAttribute('fh') + 'px';
	img.writeAttribute('type', 'big');
	link.update('свернуть');
    } else {
	img.style.backgroundImage = 'url(' + imgurl + 'p-' + imgid + ')';
	img.style.width = '120px';
	img.style.height = '120px';
	img.writeAttribute('type', 'small');
	link.update('увеличить');
    }
    return false;
}

function toggleDialogImage2(link, pp) {
    var container = $(link.parentNode);
    var sub_container = null;
    if(pp == 1) { 
    	sub_container = container;
    	container = $(container.parentNode);
    }
    if(pp == 2) {
    	sub_container = container.select('div[name="sub"]')[0];
    }
    link = container.select('a[name="imglink"]')[0];
    var img_small = container.select('div[name="img_small"]')[0];
    var img_big = container.select('div[name="img_big"]')[0];
    var imgurl = container.readAttribute('imgurl');
    var imgid = container.readAttribute('imgid');
    var imgtype = container.readAttribute('type');
    var view_paint = null;
    if(pp) view_paint = container.select('div[name="view_paint"]')[0];
    if(imgtype == 'small') {  	
    var img = container.select('img[name="img"]')[0];
    img.src = imgurl + 'i-' + imgid;
    img_small.hide();
	img_big.show();
	container.writeAttribute('type', 'big');
	link.update('свернуть');
	if(pp)	sub_container.style.width = img_big.style.width;
	if(view_paint) view_paint.show();
	if(img_big.style.width.split("px")[0] > 444) {
		img_big.style.width = "444px";
		if(pp) sub_container.style.width = "444px";
	}
    } else {
    img_big.hide();
	img_small.show();
	container.writeAttribute('type', 'small');
	link.update('увеличить');
	if(view_paint) view_paint.hide();
	if(pp)	sub_container.style.width = img_small.style.width;
    }
    return false;
}

