var gxy_align_match = /[&;\/?]align=(center|left|right)([&;\/]|$)/.exec(gxy_url_params);
var gxy_type_match = /[&;\/?]type=(percent|absolute)([&;\/]|$)/.exec(gxy_url_params);
var gxy_align = gxy_align_match?gxy_align_match[1].substr(0,1):'x';
var gxy_type = gxy_type_match?gxy_type_match[1].substr(0,1):'x';
var gxy_heat_map_image = new Image();
var gxy_values = '';
var gxy_sarg = '';

function gxy_save() {
	if (gxy_values != '') {
		var gxy_clickvalues = gxy_values;
		gxy_values='';	// clear gxy_values as fast as possible (race condition !!!)
		var href = new String(document.location.href);
		var gxy_link = gxy_host+'arg=1&sarg='+gxy_sarg+'&href='+escape(href.substring(0,299))+'&ref=http://0.0.0.0/xy='+gxy_clickvalues;
		gxy_heat_map_image.src = gxy_link;
	}
}

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

function gxy_get_correct_data (n_win, n_docel, n_body) {
        var n_result = n_win ? n_win : 0;
        if (n_docel && (!n_result || (n_result > n_docel))) {
                n_result = n_docel;
	}
        return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}

function gxy_width() {
	return gxy_get_correct_data (window.innerWidth ? window.innerWidth : 0, document.documentElement ? document.documentElement.clientWidth : 0, document.body ? document.body.clientWidth : 0);
}

function gxy_height() {
	return gxy_get_correct_data (window.innerHeight ? window.innerHeight : 0, document.documentElement ? document.documentElement.clientHeight : 0, document.body ? document.body.clientHeight : 0);
}

function gxy_posx() {
	return gxy_get_correct_data (window.pageXOffset ? window.pageXOffset : 0, document.documentElement ? document.documentElement.scrollLeft : 0, document.body ? document.body.scrollLeft : 0);
}

function gxy_posy() {
	return gxy_get_correct_data (window.pageYOffset ? window.pageYOffset : 0, document.documentElement ? document.documentElement.scrollTop : 0, document.body ? document.body.scrollTop : 0);
}

function gxy_heat_map_refresh_window_params() {
        var wparam = 'r'+screen.width+','+screen.height+'|s'+gxy_width()+','+gxy_height()+'|a'+gxy_align.substr(0,1)+'|t'+gxy_type.substr(0,1)+'|m'+gxy_posx()+','+gxy_posy()+'|p';
        if (document.body && typeof document.body.scrollWidth!='undefined' && typeof document.body.scrollHeight!='undefined') {
                wparam += document.body.scrollWidth+','+document.body.scrollHeight;
        }
        return wparam;
}

function gxy_heat_map_click(ev) {
	if (gxy_values=='') {
		gxy_sarg = gxy_heat_map_refresh_window_params();
		gxy_values = ev.clientX+':'+ev.clientY;
	} else {
		gxy_values += '|'+ev.clientX+':'+ev.clientY;
		if (gxy_values.length > 250) {
			gxy_save();
		}
	}
}

function gxy_flash(id,xx,yy) {
	var gxy_obj = document.getElementById(id);
        if (gxy_obj) {
		var x=0;
		var y=0;
		if (gxy_obj.offsetParent) {
			do {
				x += gxy_obj.offsetLeft;
				y += gxy_obj.offsetTop;
				gxy_obj = gxy_obj.offsetParent;
			} while (gxy_obj);
		} else {
			if (gxy_obj.x) {
				x = gxy_obj.x;
			}
			if (gxy_obj.y) {
				y = gxy_obj.y;
			}
		}
		x+=xx;
		y+=yy;
    		if(gxy_values==''){
			gxy_sarg = gxy_heat_map_refresh_window_params();
			gxy_values = x+':'+y;
		} else {
			gxy_values += '|'+x+':'+y;
			if (gxy_values.length > 250) {;
				gxy_save();
			}
		}
	}
}

gxy_heat_map_add_event(document,"click",gxy_heat_map_click);
gxy_heat_map_add_event(window,"unload",gxy_save);
gxy_heat_map_add_event(window,"scroll",gxy_save);
gxy_heat_map_add_event(window,"resize",gxy_save);

