// (c) 2007 by Gemius SA - gemiusHeatMap
// ver. 4.7

var ghm_images = new Array();

function ghm_checktext(node) {
	if (node.nodeName == "#text") {
		return true;
	}
	if (node.childNodes) {
		for (var i = 0 ; i < node.childNodes.length ; i++) {
			if (ghm_checktext(node.childNodes[i])) {
				return true;
			}
		}
		return false;
	}
	return false;
}

function ghm_add_event(ob,evname,fn) {
	if (ob.attachEvent) {
		ob.attachEvent("on"+evname,fn);
	} else if(ob.addEventListener) {
		ob.addEventListener(evname,fn, false);
	}
}

function ghm_toutf8(str) {
	function Hex(n) {
		var hexMap = '0123456789ABCDEF';
		return '%'+hexMap.charAt(n>>4)+hexMap.charAt(n&0xF);
	}
	var c,s,uc,ul;
	var dst = '';
	for (var i=0 ; i<str.length ; i++) {
		c = str.charCodeAt(i);
		if ((c>=0xDC00)&&(c<0xE000)) continue;
		if ((c>=0xD800)&&(c<0xDC00)) {
			i++;
			if (i>=str.length) continue;
			s = str.charCodeAt(i);
			if ((s<0xDC00)||(s>=0xDE00)) continue;
			c = ((c-0xD800)<<10)+(s-0xDC00)+0x10000;
		}
		if (c<=0x20 || c==0x22 || c==0x7C) {
			uc = Hex(c);
		} else if (c<0x80) {
			uc = String.fromCharCode(c);
		} else if (c<0x800) {
			uc = Hex(0xC0+(c>>6))+Hex(0x80+(c&0x3F));
		} else if (c<0x10000) {
			uc = Hex(0xE0+(c>>12))+Hex(0x80+(c>>6&0x3F))+Hex(0x80+(c&0x3F));
		} else {
			uc = Hex(0xF0+(c>>18))+Hex(0x80+(c>>12&0x3F))+Hex(0x80+(c>>6&0x3F))+Hex(0x80+(c&0x3F));
		}
		dst+=uc;		
	}
	return dst;
}

function ghm_url_escape(str) {
	return ghm_toutf8(str).replace(/\x2520|\x2509|\x250[aA]|\x250[dD]/g,"")
}

function ghm_load() {
	if (document.getElementById && document.getElementsByTagName) {
		var allImages = document.getElementsByTagName("img");
		var allInputs = document.getElementsByTagName("input");
		var allForms = document.getElementsByTagName("form");
		var i;
		if (allImages) {
			for (i=0; i<allImages.length; i++) {
				if (typeof(allImages[i].src) != "undefined") {
					allImages[i].ghm_src = allImages[i].src;
				}
			}
		}
		if (allInputs) {
			for (i=0; i<allInputs.length; i++) {
				if (typeof(allInputs[i].value) != "undefined") {
					allInputs[i].ghm_value = allInputs[i].value;
				}
				if (typeof(allInputs[i].src) != "undefined") {
					allInputs[i].ghm_src = allInputs[i].src;
				}
			}
		}
		if (allForms) {
			for (i=0; i<allForms.length; i++) {
				if (typeof(allForms[i].action) != "undefined") {
					allForms[i].ghm_action = allForms[i].action;
				}
			}
		}
	}
}

function ghm_gettext(node) {
	var rettext="";
	if (node.nodeName=="#text") {
		return node.nodeValue;
	} else if (node.nodeName=="IMG") {
		var src="";
		var alt="";
		if (node.ghm_src) {
			src = node.ghm_src;
		} else {
			src = node.src;
		}
		if (node.alt) {
			alt = node.alt;
		}
		return "img:"+ghm_url_escape(src)+":"+alt;
	} else if (node.childNodes) {
		for (var i=0 ; i<node.childNodes.length ; i++) {
			if (node.childNodes[i].nodeName!='A') {
				rettext+=" "+ghm_gettext(node.childNodes[i]);
			}
		}
	}
	return rettext;
}

function ghm_checksum(itext,pos) {
	var cs=0;
	var b64map=".ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_";
	for (var n = pos; n < itext.length; n++) {
		cs = ((cs * 13) + itext.charCodeAt(n))&0xFFF;
	}
	return b64map.charAt((cs>>6)&0x3F)+b64map.charAt(cs&0x3F);
}


function ghm_escape(str,limit,csflag) {
	function Hex(n) {
		var hexMap = "0123456789ABCDEF";
		return hexMap.charAt(n>>4)+hexMap.charAt(n&0xF);
	}
	var c,s,uc,ul;
	var dst = "";
	for (var i=0 ; i<str.length ; i++) {
		c = str.charCodeAt(i);
		if ((c>=0xDC00)&&(c<0xE000)) continue;
		if ((c>=0xD800)&&(c<0xDC00)) {
			i++;
			if (i>=str.length) continue;
			s = str.charCodeAt(i);
			if ((s<0xDC00)||(s>=0xE000)) continue;
			c = ((c-0xD800)<<10)+(s-0xDC00)+0x10000;
		}
		if (c<0x80) {
			uc = escape(String.fromCharCode(c)).replace(/\+/g,"%2B").replace(/\//g,"%2F");
			if (c<=0x20) {
				ul=3;
			} else {
				ul=1;
			}
		} else if (c<0x800) {
			uc = "%u"+Hex(c>>8)+Hex(c&0xFF);
			ul = 2;
		} else if (c<0x10000) {
			uc = "%u"+Hex(c>>8)+Hex(c&0xFF);
			ul = 3;
		} else {
			uc = "%U"+Hex((c>>24)&0xFF)+Hex((c>>16)&0xFF)+Hex((c>>8)&0xFF)+Hex(c&0xFF);
			ul = 4;
		}
		limit -= ul;
		if (limit<0) {
			if (csflag) {
				return dst+"|"+ghm_checksum(str,i);
			} else {
				return dst;
			}
		}
		dst+=uc;		
	}
	if (csflag) {
		return dst+"|";
	} else {
		return dst;
	}
}

function ghm_send(mhref,area,ltext,lid) {
	var href=ghm_url_escape(new String(document.location.href));
	mhref=ghm_url_escape(mhref);
	ltext=ltext.replace(/[ \t\r\n]+/g," ").replace(/^ /,"").replace(/ $/,"").replace(/\x22|\||\x2520/g,"_");
	area=area.replace(/;|&|\//g,"_");
	lid=lid.replace(/;|&|\/|\|/g,"_");
	var ghm_url = document.location.protocol+"//"+ghm_hitcollector+"/_"+(new Date()).getTime()+"/redot.gif?l=1&id="+ghm_identifier+"&sarg="+ghm_escape(ltext,190,1)+"|"+ghm_escape(area,50,0)+"&ref=http%3A%2F%2F0.0.0.0%2F"+ghm_escape(mhref,230,1)+"|"+ghm_escape(lid,50,0)+"&href="+ghm_escape(href,299,0);
	var ghm_image = new Image();
	ghm_image.src = ghm_url;
	ghm_images[ghm_images.length] = ghm_image;
	var start = (new Date()).getTime();
	while (start+200>(new Date()).getTime());
}

function ghm_click(ev) {
	var p=0;
	var np=0;
	var ln=0;
	var ocln=0;
	var fform=0;
	var imgclick=0;
	var mhref="",ocmhref="",path="",ltext="",ocltext="",lid="";
	if (document.getElementById) {
		if (!window.event) { 
			p=ev.target;
		} else {
			p=window.event.srcElement;
		}
		try {
			if (p.nodeName == "A" && !ghm_checktext(p)) {
				return;
			}
			if (p.nodeName=="INPUT") {
				if (p.type=="submit") {
					var value="";
					if (p.ghm_value) {
						value = p.ghm_value;
					} else {
						value = p.value;
					}
					ltext="formsubmit:"+value;
					fform=1;
				}
				if (p.type=="image") {
					var src="";
					if (p.ghm_src) {
						src = p.ghm_src;
					} else {
						src = p.src;
					}
					ltext="formimage:"+ghm_url_escape(src);
					fform=1;
				}
			} else if (p.nodeName=="IMG") {
				imgclick=1;
			}
			while (p) {
				if (!ocln && p.attributes && p.attributes.getNamedItem) {
					ocnode = p.attributes.getNamedItem("onclick");
					if (ocnode && ocnode.nodeValue) {
						ocln=p;
						ocmhref="hmc="+ocnode.nodeValue;
						ocltext=p.nodeName+" "+ghm_gettext(p);
					}
				}
				if (!ln) {
					if (p.nodeName=="AREA" && typeof(p.href)=="string" && p.href!="") {
						ln=p;
						mhref="hma="+p.href;
						ltext = p.shape+" "+p.coords;
					}
					if (p.nodeName=="A" && typeof(p.href)=="string" && p.href!="") {
						ln=p;
						if (imgclick) {
							mhref="hmi="+p.href;
						} else {
							mhref="hml="+p.href;

						}
						if (typeof(p.className)=="string" && p.className!="") {
							ltext=p.className+" "+ghm_gettext(p);
						} else {
							ltext=ghm_gettext(p);
						}
					}
					if (fform && p.nodeName=="FORM") {
						if (typeof(p.ghm_action)=="string" && p.ghm_action!="") {
							ln=p;
							mhref="hmf="+p.ghm_action;
						} else if (p.attributes && p.attributes.getNamedItem) {
							osnode = p.attributes.getNamedItem("onsubmit");
							if (osnode && osnode.nodeValue) {
								ln=p;
								mhref="hms="+p.attributes.getNamedItem("onsubmit").nodeValue;
							}
						}
					}
				}
				if (typeof(p.id)=="string" && (ln || ocln)) {
					if (p.id.substr(0,7)=="LinkID:") {
						lid=p.id.replace(/\x2520/g,"_").substr(7,50);
					}
					if (p.id.substr(0,9)=="LinkArea:") {
						pel=p.id.replace(/\x2520/g,"_").substr(9,10);
						if (path) {
							path=pel+"|"+path;
						} else {
							path=pel; 
						} 
					}
				}
				// if there is INPUT named 'parentNode' then p.parentNode in FORM is this INPUT child not parent, so have to check it !!!
				np = 0;
				if (typeof(p.parentNode)=="object") {
					var pp = p.parentNode;
					if (pp && pp.childNodes) {
						for (var ch=0 ; ch<pp.childNodes.length ; ch++) {
							if (pp.childNodes[ch] == p) {
								np=pp;
							}
						}
					}
				}
				p=np;
			}
			if (ln) {
				ghm_send(mhref,path,ltext,lid);
			} else if (ocln) {
				ghm_send(ocmhref,path,ocltext,lid);
			}
		} catch (_ev) {
		}
	}
}

ghm_add_event(window,"load",ghm_load);
ghm_add_event(document,"click",ghm_click);
