/*
 *  Manages events coming from the plot image.
 */
function PlotEventBot(){
    var Cnt;
    var IE;   
    var TopM,RightM,BottomM,LeftM,Width,Height;
    var XMin,XMax,YMin,YMax;
    var PX,PY;
    var FX,FY;
    var stuck;
    var FXDif,FYDif;
    
    this.IE = document.all?true:false
    if (!this.IE) document.captureEvents(Event.MOUSEMOVE)
    this.stuck = false;
    
    this.UpdatePointer = UpdatePointer;
    this.SetMargins = SetMargins;
    this.SetLimits = SetLimits;
    

    this.Center = Center;
    this.ZoomIn = ZoomIn;
    this.ZoomOut = ZoomOut;
    this.ZoomReset = ZoomReset;
    
    this.ShowMenu = ShowMenu;
    
    function InitCanvas(){
        var canvas = document.getElementById('canvas');
        canvas.style.width = this.Width;
        canvas.style.height = this.Height-20;
        var pos = findPos(document.getElementById('pimg'));
        canvas.style.left = pos[0];
        canvas.style.top = pos[1];
        canvas.style.display = 'block';
    }
    function DisposeCanvas(){
        document.getElementById('canvas').style.display = 'none';
    }    
    function ShowMenu(){
        if(document.getElementById('plotPopup').style.visibility != 'visible'){
            if(this.FX > this.XMax || this.FX < this.XMin || this.FY > this.YMax || this.FY < this.YMin) return;
            this.stuck = true;
            document.getElementById('canvas').innerHTML = '';
            var jg = new jsGraphics('canvas');        
            jg.setColor("#ff0000");
            jg.drawLine(this.PX-4, this.PY-4, this.PX+4, this.PY+4); 
            jg.drawLine(this.PX-4, this.PY+4, this.PX+4, this.PY-4); 
            jg.paint();
            showqsnode('plotPopup');
        }else{
            clearAll();
            this.stuck = false;
        }
    }    
    function clearAll(){
        document.getElementById('canvas').innerHTML = '';
        document.getElementById('plotPopup').style.visibility = 'hidden';
    }
    function Center(){
        this.stuck = false;
        clearAll();
        var xd = this.FXDif/2;
        var yd = this.FYDif/2; 
        document.getElementById('uxmin').value = this.FX-xd;
        document.getElementById('uxmax').value = this.FX+xd;
        document.getElementById('uymin').value = this.FY-yd;
        document.getElementById('uymax').value = this.FY+yd;
        ceb.DoLimit();
        ceb.DoPlot();
        document.getElementById('ppimg').focus();
    }
    function ZoomIn(mult){        
        this.stuck = false;
        clearAll();
        var xd = this.FXDif/(mult*2);
        var yd = this.FYDif/(mult*2); 
        document.getElementById('uxmin').value = this.FX-xd;
        document.getElementById('uxmax').value = this.FX+xd;
        document.getElementById('uymin').value = this.FY-yd;
        document.getElementById('uymax').value = this.FY+yd;
        ceb.DoLimit();
        ceb.DoPlot();        
        document.getElementById('ppimg').focus();
    }
    function ZoomOut(mult){
        this.stuck = false;
        clearAll();
        var xd = (this.FXDif*mult)/2;
        var yd = (this.FYDif*mult)/2;
        document.getElementById('uxmin').value = this.FX-xd;
        document.getElementById('uxmax').value = this.FX+xd;
        document.getElementById('uymin').value = this.FY-yd;
        document.getElementById('uymax').value = this.FY+yd;
        ceb.DoLimit();
        ceb.DoPlot();
        document.getElementById('ppimg').focus();
    }
    function ZoomReset(){
        this.stuck = false;
        clearAll();
        ceb.DontLimit();
        ceb.DoPlot();
        document.getElementById('ppimg').focus();
    }
    /*
     * based off    
     * http://www.codelifter.com/main/javascript/capturemouseposition1.html
     */
    function UpdatePointer(e){
        if(this.stuck)return;
        if (this.IE) { // grab the x-y pos.s if browser is IE
            tempX = event.clientX + document.body.scrollLeft
            tempY = event.clientY + document.body.scrollTop
        } else {  // grab the x-y pos.s if browser is NS
            tempX = e.pageX
            tempY = e.pageY
        }  
        
        var pimg = document.getElementById('pimg');
        if(!pimg)return;
               
        var pos = findPos(pimg);
                
        var xoff = pos[0] + Number(this.LeftM);
        var yoff = pos[1] + Number(this.TopM);
        
        var rel_xpx = tempX - xoff;
        var rel_ypx = tempY - yoff;
        
        this.PX = rel_xpx+this.LeftM;
        this.PY = rel_ypx+this.TopM;
              
        var fx = this.FXDif * (rel_xpx / (this.Width-this.LeftM-this.RightM));
        fx+=this.XMin;
        var fy = this.FYDif * (rel_ypx / (this.Height-this.TopM - this.BottomM));       
        fy = this.YMax - fy;
               
        this.FX = fx;
        this.FY = fy;
        
        if(this.FX > this.XMax || this.FX < this.XMin || this.FY > this.YMax || this.FY < this.YMin){
            document.getElementById('positionPopup').style.visibility = 'hidden';   
            document.body.style.cursor = 'auto';
        }else{
            document.getElementById('xc').innerHTML = fx.toFixed(2);
            document.getElementById('yc').innerHTML = fy.toFixed(2);
            document.body.style.cursor = 'crosshair';            
            if(lastqs != null && lastqs.style.visibility == 'visible' && lastqs.id != 'positionPopup')return;
            showqsnode('positionPopup');
        }
    }    
    function SetMargins(tm,rm,bm,lm,width,height){
        this.TopM = Number(tm);
        this.RightM = Number(rm);
        this.BottomM = Number(bm);
        this.LeftM = Number(lm);
        this.Width = Number(width);
        this.Height = Number(height);
    }
    function SetLimits(xmin,xmax,ymin,ymax){
        this.XMin = Number(xmin);
        this.XMax = Number(xmax);
        this.YMin = Number(ymin);
        this.YMax = Number(ymax);
        this.FXDif= Math.abs(this.XMax - this.XMin);
        this.FYDif = Math.abs(this.YMax - this.YMin);
    }
    
    /*
     * findPos
     * from http://www.quirksmode.org/js/findpos.html
     */
    function findPos(obj) {
	    var curleft = curtop = 0;
	    if (obj.offsetParent) {
		    curleft = obj.offsetLeft
		    curtop = obj.offsetTop
		    while (obj = obj.offsetParent) {
			    curleft += obj.offsetLeft
			    curtop += obj.offsetTop
		    }
	    }
	    return [curleft,curtop];
    }
}
