function CMotionSpy(){
	this.matrix=new Array();
	this.m_width=0;
	this.m_height=0;
	this.counter=0;
	this.possible=1;
	this.computedHigh=0;
	this.totalSamples=0;
	this.cubeSize=0;
	this.overlay=new Array();
}
CMotionSpy.prototype.init=function(oParent,iG){
	_gaddhandler(oParent,"onmousemove",this.handle_mousemove);
//	oParent.attachEvent("onmousemove",this.handle_mousemove);
//	var iSq=Math.ceil(iG);
//	var iSm=parseInt(iSq);
	this.matrix=new Array();
	this.cubeSize=iG;
//	if( (iG/iSm) == iSq){
		var iMaxH=document.documentElement.offsetHeight;
		var iMaxW=document.documentElement.offsetWidth;
		var iScrollH=(isNaN(document.body.scrollHeight))?0:document.body.scrollHeight;
		var iScrollW=(isNaN(document.body.scrollWidth))?0:document.body.scrollWidth;		
		iMaxH=(iMaxH > iScrollH)?iMaxH:iScrollH;
		iMaxW=(iMaxW > iScrollW)?iMaxW:iScrollW;

		var iSW=Math.ceil(iMaxW/iG);
		var iSH=Math.ceil(iMaxH/iG);
	
		var iW=iG;
		var iH=iG;	
		
		this.m_width=iW;
		this.m_height=iH;
		var iX=0;
		var iY=0;
		for(var i=0;i<iSW;i++){
			iX=(i * iW);
			
			for(var j=0;j<iSH;j++){
				iY=(j * iH);
				if(!this.matrix[i]) this.matrix[i]=new Array();
				this.matrix[i][j]=new CDimension(iX,iY,iW,iH);
			}
		}
	/*
	}
	else{
		alert("Specify a square (eg: 9, 16, 25, 36, 49,etc)");
	}
	*/
}
CMotionSpy.prototype.handle_mousemove=function(){
	CMS.handle_mouseevent(_gevt(arguments[0]));
}
CMotionSpy.prototype.handle_mouseevent=function(event){
	var iScrollT=(isNaN(document.body.scrollTop))?0:document.body.scrollTop;
	var iScrollL=(isNaN(document.body.scrollLeft))?0:document.body.scrollLeft;		

	var iX=event.clientX + iScrollL;
	var iY=event.clientY + iScrollT;
	this.addSample(iX,iY);
}

CMotionSpy.prototype.addSample=function(iX,iY){
	var iMX=Math.floor(iX/this.m_width);
	var iMY=Math.floor(iY/this.m_height);
	if(!isNaN(iMX) && !isNaN(iMY) && this.matrix[iMX] && this.matrix[iMX][iMY]){
		this.counter++;
		if(this.possible == -1 || (this.possible > 0 && this.counter==this.possible)){
			this.counter=0;
			this.matrix[iMX][iMY].sample++;		
			this.totalSamples++;
		}
	}
}

CMotionSpy.prototype.getMatrix=function(){
	var iL=this.matrix.length;
	var s="";
	var aS=new Array();
	var iComp=0;
	var iCompLevel=3;
	if(iL){
		var iLL=this.matrix[0].length;
		s="[";
		for(var i=0;i<iLL;i++){
			for(var j=0;j<iL;j++){
//				s+=this.matrix[j][i].sample + ",";
				var iP=this.matrix[j][i].sample;

				if(iP==0){
					iComp++;
				}
				else{
					if(iComp<iCompLevel){
						for(var a=0;a<iComp;a++){
							aS.push(0);
						}
					}
					else{
						aS.push("0x" + iComp);
					}
					iComp=0;
					aS.push(iP);
				}

//				aS.push(iP);				
			}
		}
		if(iComp > 0) aS.push("0x" + iComp);
//		s=s.replace(/,$/,"");
//		s+="]";
	}
	return aS;
}

CMotionSpy.prototype.refreshPercents=function(){
	var iL=this.matrix.length;
	var s="";
	var aS=new Array();
	if(iL){
		var iLL=this.matrix[0].length;
		for(var i=0;i<iLL;i++){
			for(var j=0;j<iL;j++){
				var iP=parseInt((this.matrix[j][i].computedPercent / this.computedHigh)*100);
				this.matrix[j][i].percent=iP;
				aS.push(iP);
			}
		}
//		s="[" + aS.join(",") + "]";
//		s=s.replace(/,$/,"");
//		s+="]";
	}
	return aS;
}
CMotionSpy.prototype.getMatrixPercent=function(){
	var iL=this.matrix.length;
	var s="";
	var sP="";
	var aS=new Array();
	if(iL){
		var iLL=this.matrix[0].length;	
		// total number of samples
		var iTS=this.totalSamples;
		this.computedHigh=0;
		for(var i=0;i<iLL;i++){
			for(var j=0;j<iL;j++){
				var iSP=parseInt((this.matrix[j][i].sample/iTS * 100));
				this.matrix[j][i].computedPercent=iSP;
				if(this.computedHigh < iSP) this.computedHigh=iSP;
				aS.push(iSP);
//				s+=iSP + ",";
			}
		}
	
		aP=this.refreshPercents();
//		s="[" + aS.join(",") + "]";
//		s=s.replace(/,$/,"");
	}
	return (arguments[0])?aP:aS;
}

function CDimension(iX,iY,iW,iH){
	this.x=iX;
	this.y=iY;
	this.w=iW;
	this.h=iH;
	this.sample=0;
	this.computedPercent=0;
	this.percent=0;
}

CMotionSpy.prototype.pre_init=function(){
	iNodeSize=(CMS.cubeSize > 0)?CMS.cubeSize:25;
	CMS.init(document.documentElement,iNodeSize);
}
var CMS=new CMotionSpy();

/*
	The overlay stuff is for presentation and debugging.  It should not be 
	distributed.
*/
var hexarray="0123456789ABCDEF";
function GetHex(decvalue){
	return hexarray.charAt((decvalue>>4)&0xf)+hexarray.charAt(decvalue&0xf);
}

function GetDec(hexvalue){
	return parseInt(hexvalue.toUpperCase(),16)
}

CMotionSpy.prototype.hideOverlay=function(){
	var iL=this.overlay.length;
	for(var i=0;i<iL;i++){
		this.overlay[i].parentNode.removeChild(this.overlay[i]);
	}
	this.overlay=new Array();
}

CMotionSpy.prototype.showOverlay=function(){
	this.hideOverlay();

	// forces to recompute the percents
	var sPercs=this.getMatrixPercent();
//	this.refreshPercents();
	for(var i=0;i<this.matrix.length;i++){
		for(var j=0;j<this.matrix[i].length;j++){
			if(!this.matrix[i] || !this.matrix[i][j]){
				alert("Bad value: " + i + ":" + j);
				continue;
			}
			var oD=this.matrix[i][j];
			var oE=document.createElement("SPAN");
			
			var sColor=this.getColorPercent(oD.percent);
			
			oE.style.position="absolute";
			oE.style.backgroundColor=sColor;
//			oE.style.color="#FFFFFF";
//			oE.appendChild(document.createTextNode(sColor));
			oE.style.border="1px outset";
			oE.style.zIndex=5;
			oE.style.height=oD.h + "px";
			oE.style.width=oD.w + "px";
			oE.style.top=oD.y + "px";
			oE.style.left=oD.x + "px";

			if(i==0 && j==0){
//				alert(sColor + "\n" + oD.h + ", " + oD.w + " / " + oD.x + ", " + oD.y);
			}
			
			if(arguments[0]){
				oE.style.filter="alpha(opacity=50)";
			}
			
			document.body.appendChild(oE);
			this.overlay[this.overlay.length]=oE;
		}
	}
}
CMotionSpy.prototype.getColorPercent=function(iP){

	// these are decimal values
	if(isNaN(iP)) iP=0;
	var iPrimary=255;
	var R=(iP<50)?iPrimary:0;
	var G=(iP>20)?iPrimary:0;
	var B=0;
	
	var iDecMin=80;
	var iOff=iPrimary-iDecMin;


	var iDiffR=(iP==0)?80:R;
	var iDiffG=(iP==0)?80:G;
	var iDiffB=(iP==0)?80:B;

/*
	var iDiffR=parseInt(R*(iP/100));
	var iDiffG=parseInt(G*(iP/100));
	var iDiffB=B;
*/	
//	var iDiffR=iOff + parseInt(R*(iP/100));
//	var iDiffG=iOff + parseInt(G*(iP/100));
//	var iDiffB=B;

//	var iDiffR=parseInt((255 - R)*(iP/100));
//	var iDiffG=parseInt((255 - G)*(iP/100));	
//	var iDiffB=parseInt((255 - B)*(iP/100));
	
	var sRH=GetHex(iDiffR);
	var sRG=GetHex(iDiffG);
	var sRB=GetHex(iDiffB);

	var sColor="#" + sRH + "" + sRG + "" + sRB;
	return sColor;
}

if(typeof(_gdomb)=='object' && (_gdomb.bswitch==1 || _gdomb.bswitch==2)){
	_gaddhandler(window,"onload",CMS.pre_init);
}
