// EDIT SETTINGS

// Tags available for tooltips
var elements = new Array("span", "a");

// Attributes with text for tooltip
var attrs = new Array("rel");

// STOP EDITING

function parsePreTags(rel){
	var repl = { "[" : "<", "]" : ">" }
	for(r in repl){
		var srch = new RegExp("\\" + r, "g");
		rel = rel.replace(srch, repl[r])
	}
	return rel
}

function tableRow(val1, val2){
	return '<tr><td>' + val1 + '</td><td>' + val2 + '</td></tr>';
}

function divRow(content, cls){
	return '<div class="' + cls + '">' + content + '</div>'
}

function pluralComments(num)
{
	if(num == '')
		num = 0;
	else
		num = num*1;
		
	c = num % 10;
	
	if(c == 1 && num != 11)
		t = 'комментарий';
	else if((c == 2 || c == 3 || c == 4) && (num > 20 || num < 10))
		t = 'комментария';
	else
		t = 'комментариев';
		
	return num + ' ' + t;
}
function parseUser(rel){
	pos = rel.indexOf(':', 0)
	a = rel.slice(0, pos);
	args = rel.slice(pos + 1, rel.length);
	
	if(a == 'user'){
		ns_opacity = 0.9;
		v = args.split('|');
		
		cont = divRow('Рейтинг: ' + v[0], 'ns_work_author');
		cont += divRow('Работ: ' + v[1], 'ns_work_author');
		cont += divRow('Авардов: ' + v[2], 'ns_work_author');
		cont += divRow('Всего сообщений: ' + v[3], 'ns_work_author');
		
		ret = '<img src="/images/comments/tr.png">';
		ret += divRow(cont, 'ns_work_cont');
	} else if (a == 'work') {
		ns_opacity = 0.9;
			
		v = args.split('|');
		cont = divRow(v[0], 'ns_work_title');
		cont += divRow('Автор: ' + v[1], 'ns_work_author');
		cont += divRow(pluralComments(v[2]), '');
		
		ret = '<img src="/images/comments/tr.png">';
		ret += divRow(cont, 'ns_work_cont');
	} else {
		ns_opacity = 1.0;
		
		ret = divRow(rel, 'ns_work_cont');
	}
	
	$('#ns_tt').css({opacity: ns_opacity});
	
	return ret;
}

$(document).ready(
	function() {
		var positionTooltip = function(event) {
			var tPosX = event.pageX;
			var tPosY = event.pageY;
			$('#ns_tt').css({top: tPosY, left: tPosX});
		};
		
		$(document).mousemove(positionTooltip);
		
		$("body").prepend("<div id='ns_tt' class='ns_tt_new_class'></div>");
		$("#ns_tt").hide();
		attrs = attrs.reverse();
		for(e in elements){
			var el = elements[e];
			$(el).mouseover(function(){
				var rel = "";
				for (a in attrs){
					if(typeof($(this).attr(attrs[a])) != "undefined")
						var rel = $(this).attr(attrs[a]);
				}
				if (rel != "") {
					rel = parsePreTags(rel);
					rel = parseUser(rel);
					$("#ns_tt").empty();
					$("#ns_tt").append("<span>" + rel + "</span>");
					$("#ns_tt").show();
				}
			});
			$(el).mouseout(function(){
				$("#ns_tt").hide();
			});
		}
	}
)