//var clicktracking = new sack('wp-content/plugins/clicktracking/clicked.php');
var clicktracking = new sack('index.php');

// Detect if the browser is IE or not.
// If it is not IE, we assume that the browser is NS.
var IE = document.all?true:false

// If NS -- that is, !IE -- then set up for mouse capture
if (!IE) document.captureEvents(Event.MOUSEMOVE);

// Set-up to use getMouseXY function onMouseMove
document.onmousedown = getMouseXY;

/*
window.captureEvents(Event.CLICK);
window.onclick = function (evt) {
alert("clickERU!!");
return false;
}
*/
/*
window.addEventListener('click',
function (evt) {
getMouseXY(evt);
},
true
);

*/
var thisdate = new Date();
var sec = Math.round(thisdate.getTime()/1000);

// Temporary variables to hold mouse x-y pos.s
var tempX = 0
var tempY = 0


// Main function to retrieve mouse x-y pos.s
function getMouseXY(e) {  
  // eliminate frenzy clicking
  var thisdate = new Date();
  var sec2 = Math.round(thisdate.getTime()/1000);
  if (sec2 == sec)
    return;
  else
    sec = sec2;  

  if (IE) { // grab the x-y pos.s if browser is IE
    tempX = event.clientX + f_scrollLeft(); 
    tempY = event.clientY + f_scrollTop();
  } else {  // grab the x-y pos.s if browser is NS
    tempX = e.pageX  + f_scrollLeft(); 
    tempY = e.pageY;
  }  
  // catch possible negative values in NS4
  if (tempX < 0){tempX = 0}
  if (tempY < 0){tempY = 0}  


  // window size
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  
//  alert("width: " + myWidth + " height: " + myHeight);
  
  var myWidth = f_clientWidth();
  var myHeight = f_clientHeight();

//  alert("width: " + myWidth + " height: " + myHeight);
  
  // TODO: don't count clicks on scroller
//  if (tempX > myWidth && f_scrollLeft() == 0)
//    alert("bigger x " + tempX + " width: " + myWidth); // return true;
//  if (tempY > myHeight)
//    alert("bigger y " + tempY + " height: " + myHeight); // return true;
  
  clicktracking.setVar("ct", 1);
  clicktracking.setVar("x", tempX);
	clicktracking.setVar("y", tempY);
  clicktracking.setVar("w", myWidth);
	clicktracking.setVar("h", myHeight);
	clicktracking.setVar("url", document.location.href);
  clicktracking.setVar("post_id", wpct_post_id);

	clicktracking.method = 'GET';
	clicktracking.runAJAX();
//	clicktracking.runResponse();

  return true
}


// Few lines for getting window size in different browsers
// http://www.softcomplex.com/docs/get_window_size_and_scrollbar_position.html
function f_clientWidth() {
	return f_filterResults (
		window.innerWidth ? window.innerWidth : 0,
		document.documentElement ? document.documentElement.clientWidth : 0,
		document.body ? document.body.clientWidth : 0
	);
}
function f_clientHeight() {
	return f_filterResults (
		window.innerHeight ? window.innerHeight : 0,
		document.documentElement ? document.documentElement.clientHeight : 0,
		document.body ? document.body.clientHeight : 0
	);
}
function f_scrollLeft() {
	return f_filterResults (
		window.pageXOffset ? window.pageXOffset : 0,
		document.documentElement ? document.documentElement.scrollLeft : 0,
		document.body ? document.body.scrollLeft : 0
	);
}
function f_scrollTop() {
	return f_filterResults (
		window.pageYOffset ? window.pageYOffset : 0,
		document.documentElement ? document.documentElement.scrollTop : 0,
		document.body ? document.body.scrollTop : 0
	);
}
function f_filterResults(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;
}
