var debug = false;
var last_click_Time = "";
var browser = getBrowser();
var custnum = wm_custnum;
var server = "ctsde01.wiredminds.de";

// auto-detect page name, if necessary
    if ( typeof(wm_page_name) == 'undefined' ) {
      page_name = document.location.pathname;
    } else {
      page_name = wm_page_name;
    }
/**
 * Catch click for Heatmap
 */
 
 

function getClick(e) {
    //if (arguments.length == 0) e = event;
    var click_x = 0;
    var click_y = 0;
    /** Check if last click was at least 1 second ago */
	time = new Date();
	if (time.getTime() - last_click_Time < 1000) return;
	last_click_Time = time.getTime();
		
	/** Grab coordinate x,y of clicks */		
	if (browser == "IE6" || browser == "IE7") { // grab the x,y if browser is IE
		click_x = event.clientX;
		click_y = event.clientY;
	} else {  // grab the x,y if browser is not IE
		click_x = e.clientX;
		click_y = e.clientY;
	}
	
	scrollbarWidth = getScrollbarWidth();
    d = document.documentElement != undefined && document.documentElement.clientHeight != 0 ? document.documentElement : document.body;
    scrollx  = window.pageXOffset == undefined ? d.scrollLeft : window.pageXOffset;
    scrolly  = window.pageYOffset == undefined ? d.scrollTop : window.pageYOffset;
    client_w = window.innerWidth == undefined ? d.clientWidth : window.innerWidth;
    client_h = window.innerHeight == undefined ? d.clientHeight : window.innerHeight;
    body_w   = d.offsetWidth;
    body_h   = d.offsetHeight;
	zoomLevel = getZoomLevel();
    
    /* IE7 has new zoom function. The client/body size will be smaller if zoomed out */
    if (browser == "IE7") {
    	client_w = Math.round(client_w * zoomLevel);
    	client_h = Math.round(client_h * zoomLevel);
    	body_w = Math.round(body_w * zoomLevel);
    	body_h = Math.round(body_h * zoomLevel);
    }
	
	/* Check if clicks was on the scrollbars */
	if (body_h > client_h && click_x > (client_w - scrollbarWidth)) return;
	// TODO: Because in Firefox the body_w is constand, so it is diffcult to find out wether there is a horizontal scrollbar
	//if (body_w > client_w && click_y > (client_h - scrollbarWidth)) return;
	
    /** Add the width/height of scroll to coordinate */	
	click_x = click_x + scrollx;
	click_y = click_y + scrolly;
	
	/** Get rid of zoom */
	if (browser == "IE7") {
	    click_x = Math.round(click_x/zoomLevel);
	    click_y = Math.round(click_y/zoomLevel);
	}
	
	/**
   *  If wm_content_width provided than --> content is centered and we need to calculate the mouse pozition relative       to content width, else -> content is left aligned, no need to modify mouse position.      
   */
    
	if ((typeof (wm_content_width) != 'undefined' ) && (wm_content_width <= body_w)){
	    click_x = (click_x - (body_w - wm_content_width)/2 );	   
	}

	this_URL=document.URL;
	params = 'custnum='+custnum+'&pagename='+escape(page_name)+'&URL='+escape(page_name)+'&cx='+click_x+'&cy='+click_y+'&body_height='+body_h+'&browser='+browser;
	
if ((typeof (wm_content_width) != 'undefined')){
	   params += '&content='+wm_content_width;	
	}	
	count_heat_URL='http://'+server+'/track/countxy.php';
	heatmap_container=new Image();
  heatmap_container.src=count_heat_URL + '?' + params;
}
/**
 * Add heatmap image to current webpage
 */
function addHeatmap(base_URL, websitesel, page_name) {
 
    if ( typeof(wm_page_name) != 'undefined' ) { page_name = wm_page_name; } 
    var links = document.getElementsByTagName('a');
	var myMap = document.createElement('Map');
	myMap.name = "pageMap";
	myMap.id   = "pageMap";	
	d = document.documentElement != undefined && document.documentElement.offsetHeight != 0 ? document.documentElement : document.body;
    body_h = d.offsetHeight;
    body_w = d.offsetWidth;

	for (var index = 0; index < links.length; index++) {
	  link_left   = getPos(links[index])[0];
	  link_right  = links[index].offsetWidth + link_left;
	  link_top    = getPos(links[index])[1];
	  link_bottom = links[index].offsetHeight + link_top;
	  
	  //Check if a div is inside the link (a-tag), if so get the offset values from there. Got necessary for Firefox.
	  if (typeof(links[index].getElementsByTagName('div')[0]) == 'object') {
		tempWidth = links[index].getElementsByTagName('div')[0].offsetWidth;
		tempHeight = links[index].getElementsByTagName('div')[0].offsetHeight;
		link_right = tempWidth + link_left;
		link_bottom = tempHeight + link_top;
	  }
	  
	  area = document.createElement('area');    
	  area.shape  = 'RECT';
	  area.coords = link_left + ',' + link_top + ',' + link_right + ',' + link_bottom;
	  area.href   = base_URL + 'heatmap_temp.php?newurl=' + escape(links[index]);  
	  myMap.appendChild(area);
	}
	
  	var myDiv = document.createElement('div');
  	myDiv.id               = "WmHeatmap"
  	myDiv.style.position   = "absolute";
	myDiv.style.left       = "0";
	myDiv.style.top        = "0";
	myDiv.style.width      = "100%";
	myDiv.style.zIndex      = "999999";
	if (body_h < document.body.scrollHeight){
	  myDiv.style.height     =  document.body.scrollHeight+"px";
	}else
	{
	   myDiv.style.height     =  body_h+"px";	
	};
	myDiv.style.background = "#000000";
	myDiv.style.opacity    = "0.5";
	myDiv.style.filter     = "Alpha(opacity=50)";
	myDiv.style.visibility = "visible";
    myDiv.appendChild(myMap);
    document.body.appendChild(myMap);
    var adr	 = base_URL + "heatmap.php?websitesel=" + websitesel + "&page_name=" + page_name + "&body_height=" + body_h +"&browser=" + browser+"&body_w="+body_w;
  	var style="z-index:10000;position:relative;border:0;align=left;";
	myDiv.innerHTML = '<div align="left"><img id="wm_img00"  src="'+adr+'" style="'+style+'" usemap="#pageMap" ></div>';
	//myDiv.innerHTML = '<img id="wm_img00"  src="'+adr+'" style="'+style+'" usemap="#pageMap" >';
	document.body.appendChild(myDiv);
}
/**
 * Get coords of one web element
 */
function getPos(obj) {
	var left = 0;
	var top  = 0;
	
	if (obj.offsetParent) {
		left = obj.offsetLeft;
		top = obj.offsetTop;
		while (obj = obj.offsetParent) {
			left += obj.offsetLeft;
			top += obj.offsetTop;
		}
	}
	return [left,top];
}
/**
 * Get the width of scrollbar
 */
function getScrollbarWidth() {
    var scr = null;
    var inn = null;
    var wNoScroll = 0;
    var wScroll = 0;

    // Outer scrolling div
    scr = document.createElement('div');
    scr.style.position = 'absolute';
    scr.style.top = '-1000px';
    scr.style.left = '-1000px';
    scr.style.width = '100px';
    scr.style.height = '50px';
    // Start with no scrollbar
    scr.style.overflow = 'hidden';

    // Inner content div
    inn = document.createElement('div');
    inn.style.width = '100%';
    inn.style.height = '200px';

    // Put the inner div in the scrolling div
    scr.appendChild(inn);
    // Append the scrolling div to the doc
    document.body.appendChild(scr);

    // Width of the inner div sans scrollbar
    wNoScroll = inn.offsetWidth;
    // Add the scrollbar
    if (browser == "IE6" || browser == "IE7") {
    	scr.style.overflow = 'scroll';
    } else {
    	scr.style.overflow = 'auto';
    }
    // Width of the inner div width scrollbar
    wScroll = inn.offsetWidth;

    // Remove the scrolling div from the doc
    document.body.removeChild(document.body.lastChild);

    // Pixel width of the scroller
    return (wNoScroll - wScroll);
}
/**
 * Get the zoom level the clicker used to view the page
 * TODO: This function works only good with browsers like IE7, which has the real zoom function,
 *       Browsers like IE6,FF zoom only text not image of the page, it is diffcult to get zoom level for such kind of browser 
 */
function getZoomLevel() {
	var div = document.createElement("div");
    div.id = "zoomLevelMeasure";
    div.style.position = "absolute";
    div.style.top = "0";
    div.style.height = "100%";
  
    document.body.appendChild(div);
    
    offsetHeight = document.getElementById("zoomLevelMeasure").offsetHeight;
    
    d = document.documentElement != undefined && document.documentElement.clientHeight != 0 ? document.documentElement : document.body;
    client_h = window.innerHeight == undefined ? d.clientHeight : window.innerHeight;
    zoomLevel = Math.round(offsetHeight/client_h*10)/10;
    
	return zoomLevel;
}

/**
 * Detect the browsers the clickers used to view page.
 * TODO: Moment take care the function only 3 Browsers: IE6/IE7/FF
 */
function getBrowser() {
	var version = navigator.appVersion;
	if (version.search(/MSIE 6/i) != -1) return "IE6";
	if (version.search(/MSIE 7/i) != -1) return "IE7";
	if (version.search(/MSIE/i)   == -1) return "Others";
}
