// http://chat4it.com/ - chat4it.com JavaScript module
// GNU Lesser General Public License (LGPLv3) http://opensource.org/licenses/lgpl-3.0.html
// ver 0.2.2
var chat4it = function(){
	var mX,mY; // mouse coordinates
	var hintDiv = document.createElement('div'); // object conatins
	if (!document.body) {
		alert('chat4it script must be in body section!');
		return ;
	}
	hintDiv.style.position = 'absolute';
	hintDiv.style.border = '1px solid #000';
	hintDiv.style.background = '#FFD';
	hintDiv.style.zIndex = 4444;
	
	var counters = { };
	var lastCounter = null;
	
	//hintDiv.innerHTML = 'abcdefgh'; //chatId;
	document.body.appendChild(hintDiv);
	var bdyMouseMove; // default mouse move handler
	function private_func(){
	
	}
	// public methods and so on
	return {
		updateMousePos: function(ev){
			if (window.pageYOffset) {
				mX = ev.clientX + window.pageXOffset;
				mY = ev.clientY + window.pageYOffset;
			} else {
				mX = ev.clientX + document.body.scrollLeft;
				mY = ev.clientY + document.body.scrollTop;
			}
		},
		updCnt: function(id, info){
			if (typeof(counters[id]) == 'undefined') counters[id] = { nodes: [ ] };
			counters[id].inf = info;
			for(var i = 0;i < counters[id].nodes.length; ++i){
				var n = counters[id].nodes[i];
				var c;
				if (typeof(n.n) == 'string') c = document.getElementById(n.n);
				else c = n.n;
				if (info.err) {
					n.h = n.t.hintErr(counters[id].inf);
					c.innerHTML = n.t.showErr(counters[id].inf);
				} else {
					n.h = n.t.hint(counters[id].inf);
					c.innerHTML = n.t.show(counters[id].inf);
				}
			}
		},
		regCnt: function(id, node, transformation){
			if (typeof(counters[id]) == 'undefined') counters[id] = { nodes: [ ] };
			counters[id].nodes.push({ n: node, t: transformation, h: '' });
		},
		showCounter: function(evnt){
			var ev = evnt || window.event;
			this.updateMousePos(ev);
			hintDiv.style.left = 15+ mX + 'px';
			hintDiv.style.top = 20+mY + 'px';
			
			var el = ev.target || ev.srcElement;
			if (el != lastCounter) 
			for(var c in counters){
				for(var j = 0; j < counters[c].nodes.length; ++j){
					var o;
					if (typeof(counters[c].nodes[j].n) == 'string') o = document.getElementById(counters[c].nodes[j].n);
					else o = counters[c].nodes[j].n;
					if (o == el) {
						hintDiv.innerHTML = counters[c].nodes[j].h;
						lastCounter = el;
						break ;
					}
				}
			}
			
			hintDiv.style.display = 'block';
		},
		hideHint: function(){
			hintDiv.style.display = 'none';
		},
		total: function(cnt){
			var r = 0;
			for(var v in cnt){
				if (cnt.hasOwnProperty(v)) r++;
			}
			return r;
		}
	};
}();

