
//  ______________
// | EventManager |________________________________________________
// |                                                                |
// |  EventManager is the Core of the GuiManager. It contains       |
// |  all the basic functionality to dispatch events to GUIs        |
// |  and handle their z placement.                                 |
// |________________________________________________________________|
//

// the following code was lifted from webreference.com DHTML LAB "Array Power III"
// it is essential for the EventManager to run in Internet Explorer 
//----------------------------------------------------------------------------------  
	if(Array.prototype.splice && typeof([0].splice(0))=="number")
           Array.prototype.splice = null;

       if(!Array.prototype.splice) {

           function array_splice(ind,cnt){
               if(arguments.length == 0) return ind;
               if(typeof ind != "number") ind = 0;
               if(ind < 0) ind = Math.max(0,this.length + ind);
               if(ind > this.length) {
                   if(arguments.length > 2) ind = this.length;
                   else return [];
               }
               if(arguments.length < 2) cnt = this.length-ind;
               cnt = (typeof cnt == "number") ? Math.max(0,cnt) : 0;
               removeArray = this.slice(ind,ind+cnt);
               endArray = this.slice(ind+cnt);
               this.length = ind;
               for(var i=2;i<arguments.length;i++){
                   this[this.length] = arguments[i];
               }
               for(var i=0;i<endArray.length;i++){
                   this[this.length] = endArray[i];
               }
               return removeArray;
           }

           Array.prototype.splice = array_splice;
       }
//
// end Array Power III code.
//----------------------------------------------------------------------------------- 
 
 
function EventManager(id)
{
      
	  this.id=id;
	  this.objects = new Array();       // array of objects -- contains actual pointers to the objects
	  this.active = null;               // on instantiation there are no active layers
      this.bottomZ =2000;              
	  this.topZ = 2000;    
	  this.superZ =20000; 
	  this.floatBlur=false;             // whether float object is blurred
	  this.floatZ = 10000;               // abritrary setting of top Z index 
	  this.floatObject = null;               // and the current top zIndex = 0;
	  this.dragObject = null;           // reference to object undergoing drag event// float 			// to their original positions.
      this.mouseX=0;
      this.mouseY=0;
      this.dx=0;
      this.dy=0;
      this.event = null;
      this.pageIsLoaded=false; 
      // guis register at compile-time to be initialized onload.
      this.addObject = addObject;       // methods to add object
	  this.killObject = killObject;    
	  this.parseObjects = EMparseObjects; 
	  this.makeActive=makeActive;       
      this.recordEvent = recordEvent;
      this.init = eventManagerInit;
      this.getObjectRef = getObjectRef;
      this.getWindowSize= getWindowSize;
	  this.setBounds = setBounds;

	}
function eventManagerInit()


    {    
		if(ie) 
            {  
             document.onmousedown=EMmousedown;
             document.onmousemove=EMmousemove;
             document.onmouseup=EMmouseup;
            }
        if(ns)
            {   
             window.captureEvents(Event.MOUSEDOWN|Event.MOUSEMOVE|Event.MOUSEUP);
             window.onmousedown =EMmousedown;
             window.onmousemove= EMmousemove;
             window.onmouseup=EMmouseup;
            }
        if(ns6)
            {
             window.addEventListener("mousedown", EMmousedown, true);
             window.addEventListener("mousemove", EMmousemove, true);
             window.addEventListener("mouseup", EMmouseup, true);
            }



       this.dragBox = new Box("guiManager.dragBox","box2",1,1,'000000');
       this.dragBox.layer[0].zIndex=this.superZ;
	   this.dragBox.layer[0].borderWidth=2;
	   this.getWindowSize();
	   this.setBounds(0,0,this.w,this.h);
	   this.pageIsLoaded = true;
	  
	  
    }
    function getWindowSize()
    {
         if (ns||ns6){
          this.w=self.innerWidth;
          this.h=self.innerHeight;      
       }
       if (ie){
           this.w=document.body.clientWidth; 
           this.h=document.body.clientHeight;          
       }
       this.mx = this.w/2 ;
       this.my = this.h/2 ;
    }
	function setBounds(leftX,topY,rightX,bottomY)
	{
		this.leftBound = leftX;
		this.rightBound = rightX;
		this.topBound=topY;
		this.bottomBound =bottomY;
	}
	function addObject(object) 
	{ 
	  if(this.getObjectRef(object)!="err")
	  	return;
	  n =  this.objects.length;			
	  this.objects[n] = object;
	  if (this.objects[n].type=="floating")
	  	this.floatObject = object;	
	  //this.active = object;
      this.topZ+=20;
	  this.makeActive(n);     	
	}
    function getObjectRef(object)
    {
        for (o=0;o<this.objects.length;o++)
        {
            if (this.objects[o]==object)
            break;
        }
        if (o<this.objects.length)
            return o;
        else
            return "err";
    }
	function makeActive(obj)	  
	{  
        if (typeof (obj) != "number")
            objRef = this.getObjectRef(obj);
        else
            objRef = obj;
		if (this.active)		
        	this.active.blur()
		if (this.objects[objRef]==this.floatObject){
			this.objects[objRef].focus(this.floatZ);
			//this.dBug(this.objects[objRef].id+" z:"+this.floatZ);
			//alert("set float z");
		}	
		else{ 
			if (this.objects[objRef].type=="static"){		
	    		this.objects[objRef].focus(getStyle(this.objects[objRef].content).zIndex-2);
			//this.dBug(this.objects[objRef].id+" z:"+(getStyle(this.objects[objRef].content)).zIndex); 
			}
	    	else{
			   this.objects[objRef].focus(this.topZ);
			}
		}
		this.active=this.objects[objRef];
		
		  for (j=objRef+1;j<this.objects.length;j++)         
		  { 
            tempz =j*20 +this.bottomZ;
		    if (this.objects[j] == this.floatObject) continue;
			if (this.objects[j].type == "static") continue;         
		    for(this.z=0;this.z<this.objects[j].layer.length;this.z++)
			{
			  this.objects[j].layer[this.z].zIndex = tempz+this.z;
			  //this.dBug("o:"+this.objects[j].id+" l:"+this.z+" zIndex:"+(tempz+this.z))
			}	
		  }
		  this.objects.sort(sortByZindex);
		  /*this.dBug("objects in Z stack ARRAY:: "+this.objects.length)
		  for (this.i=0;this.i<this.objects.length;this.i++)
		  {
		  	this.dBug("["+this.i+"]=>"+this.objects[this.i].id+"--Starts at Z :"+this.objects[this.i].layer[0].zIndex)
		  }*/
				
	}	
    
    // sorting function for the array objects's sort method.	
	function sortByZindex(obj1,obj2)   
	{  
	  a = obj1.layer[0].zIndex;			
	  b = obj2.layer[0].zIndex;
	  return a-b;
	}
    
	function killObject(object)                             
	{ 
      if (object==this.floatObject) this.floatObject=null;
	  for (i=0;i<this.objects.length;i++)
	  { 
	    if (this.objects[i] == object)                      
		   {
		     this.objects.splice(i,1);
			 break;
		   }
      }
	  
	  if (i>this.objects.length) return; 		                            
	  for(i=0;i<this.objects.length;i++) 					
	  { 
        if (this.objects[i] == this.floatObject) continue;
		if (this.objects[i].type == "static") continue;   
	    for(z=0;z<this.objects[i].layer.length;z++)	        
		{
		   this.objects[i].layer[z].zIndex = this.bottomZ+(i*20)+z;
		}         
	  }	
	  this.topZ=(this.objects.length)*20+this.bottomZ;
	  if (this.objects.length>0) 
        this.makeActive(this.objects.length-1);
	  
	}
 
 
	function EMparseObjects()
	{
      returnflag = false;

	  for (p=this.objects.length-1;p>-1;p--)                 											                        
	  {    
	    if(this.mouseX<this.objects[p].x || this.mouseX > this.objects[p].x+this.objects[p].clipW) continue;
        if (this.mouseY<this.objects[p].y || this.mouseY > this.objects[p].y+this.objects[p].clipH) continue;
		if (this.active != this.objects[p]){
			this.makeActive(p);
		}
		this.active.parseXY(this.mouseX,this.mouseY);
        returnflag = true;  	  		 
		break;
        
	  }
	  return returnflag
	 
	}
    function recordEvent(e)
    {   
        ox = this.mouseX;
        oy = this.mouseY;
	
        if (ie)								// first get oblig sex and wine coordinates
        {   this.event = event;
            this.mouseX = (event.clientX + document.body.scrollLeft);
            this.mouseY = (event.clientY + document.body.scrollTop);
			this.button = event.button;
        }
        if(ns || ns6)
        {   this.event = e;
            this.mouseX = e.pageX;
            this.mouseY = e.pageY;
			this.button = e.which
        }
        this.dx = this.mouseX - ox;
        this.dy = this.mouseY - oy;
    } 
    function EMmousedown(e)
    {	
        guiManager.recordEvent(e)
		
        us = guiManager.parseObjects();
        if (!us)
        {
        if(ns) routeEvent(e);
        return false;
        }
		if(ns) routeEvent(e);
        return false;
        
    }
	
    function EMmousemove(e)
    {
        guiManager.recordEvent(e)
        if (guiManager.dragObject){
            guiManager.dragObject.drag()
            if(ie) event.returnValue=false;
            return true;
        }else{
            if(ns) routeEvent(e);
            return true;
        }
    }
    
    function EMmouseup(e)
    {   
        clearTimeout(guiManager.scrollRepeat)
        guiManager.recordEvent(e)
		
        if (guiManager.dragObject){
            guiManager.dragObject.dragStop();
            guiManager.dragObject = null;
        }
        if(ns) routeEvent(e);
        return true;
   
    } 

//----------------------------------------
//box object for ie/ns6
//
function Box(id,boxid,w,h,color)
{
 this.inherit = Sprite;
 this.busy = false;
 this.color= color;
 this.clipW=w;this.clipH=h;
  el = document.createElement('DIV');
        el.id =boxid+"box";
        el.style.width = w+"px";
		el.style.height = h+"px";
        el.style.position ="absolute";
        el.style.visibility = "hidden";
		el.style.borderWidth=1;
		el.style.borderStyle="dotted";
		el.style.borderColor=this.color;
        document.body.appendChild(el);		
  this.inherit(id,boxid+"box");
  this.grow=grow;
  this.regrow=regrow;
  this.setSize=setSize;
  this.setColor=setColor;       
}
function setColor(color)
{
	this.layer[0].borderColor = color;
}
function setSize(w,h)
{
	this.layer[0].width=w;
	this.clipW=w;
	this.layer[0].height=h;
	this.clipH=h;
	this.xOffset[0]=0;
	this.yOffset[0]=0;
}
function grow(width,height,time,xoff,yoff)
{
     ft= (new Date()).getTime();
	 this.regrow(ft,time,width,height,xoff,yoff)
    
}
function regrow(fatherTime,ms,width,height,xoff,yoff)
{

   now = (new Date()).getTime(); // get now to calculate
   ratio = (now - fatherTime)/ ms;  
   if (ratio >= 1) 
   { 
    this.layer[0].width = width; 
    this.layer[0].height = height; 
	this.clipW = width;
	this.clipH = height;
	this.x = this.x + xoff;
	this.y = this.y + yoff;
	this.xOffset[0]=0;
	this.yOffset[0]=0;
	this.setXY();
    return;
   } 
    this.layer[0].width = parseInt(this.clipW+(width-this.clipW)*ratio);
    this.layer[0].height = parseInt(this.clipH+(height-this.clipH)*ratio);
	this.yOffset[0] = parseInt(yoff*ratio);
	this.xOffset[0] = parseInt(xoff*ratio);
	this.setXY();
    wastedTime = (new Date()).getTime()-now;
   	setTimeout(this.id+".regrow("+fatherTime+","+ms+","+width+","+height+","+xoff+","+yoff+")",25+wastedTime);
   
}
//
//---end box object
//---------------------------------------------------

guiManager = new EventManager("guiManager");
