//ie  = (document.all)? 1:0;
//moz = (document.getElementById && !ie)? 1:0
//  ______________
// | 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.
//----------------------------------------------------------------------------------- 
 
document.write("<style>.Dragbox{position:absolute}</style>");
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.isFocused = null;
      this.bottomZ =2000;              
	  this.topZ = 2000;    
	  this.superZ =20000; 
	  this.floatBlur=false;				// whether float object is blurred
	  this.lockZ=9999;             
	  this.floatZ = 10000;
	  this.floatDialogZ = 15000;
	  this.dragDropZ=20000;               // abritrary setting of top Z index 
	  this.floatObject = null;               // and the current top zIndex = 0;
	  this.dragObject = null;           // reference to object undergoing drag event
	  this.dragType = 0   // drag whole layer
      this.mouseX=0;
      this.mouseY=0;
      this.dx=0;
      this.dy=0;
      this.event = null;
      this.pageIsLoaded=false; 
      this.addObject = addObject;       // methods to add object
	  this.killObject = killObject;    
	  this.parseObjects = EMparseObjects; 
	  this.makeActive=makeActive;       
      this.recordEvent = recordEvent;
	  this.grabKeyboardEvents = EMgrabKeyboard;
	  this.releaseKeyboardEvents = EMreleaseKeyboard;
      this.init = eventManagerInit;
      this.getObjectRef = getObjectRef;
      this.getWindowSize= getWindowSize;
	  this.setBounds = setBounds;
	  this.showVideo = EMshowVideo;
	  this.hideVideo = EMhideVideo;
	  this.clearVideo = EMclearVideo;
	  this.capture = null;
	  this.video = null;
	  this.dialog = null;
	  
	  	this.showDialog = GM_showDialog;
		this.closeDialog = GM_closeDialog;
		this.openGui = GM_openGui;
		this.closeGui = GM_closeGui;
		this.lockPageUI = GM_lockPageUI;
		this.unlockPageUI = GM_unlockPageUI;
		this.show = GM_show;
		this.fromX  = new Array();
		this.fromY = new Array();
	  
	  	this.dragDrop = GM_dragDrop;
		this.drop = GM_drop;
		this.dragDropMove = GM_dragDropMove;
		this.dragDropHover = GM_dragDropHover;
		this.hoverObject = null;
		this.dragDropObject = null;

	}
function eventManagerInit()


    {    
		if(ie) 
            {  
             document.onmousedown=EMmousedown;
             document.onmousemove=EMmousemove;
             document.onmouseup=EMmouseup;
			 document.onkeypress=EMkeyboardEvent;
			 document.onkeydown=EMkeyboardEvent;
            }
        if(ns6)
            {
			// window.user_pref ("accessibility.typeaheadfind", false);
             window.addEventListener("mousedown", EMmousedown, true);
             window.addEventListener("mousemove", EMmousemove, true);
             window.addEventListener("mouseup", EMmouseup, true);
			 window.addEventListener("keypress",EMkeyboardEvent,true);
			 window.addEventListener("keydown",EMkeyboardEvent,true);
            }



       this.dragBox = new Box("GuiManager.dragBox",null,"box",1,1);
       this.dragBox.canvas.zIndex=this.superZ;
	   this.getWindowSize();
	   this.setBounds(0,0,this.w,this.h);
	   this.dark = new Sprite("GuiManager.dark",null);
	   this.dark.setSize(document.body.offsetWidth,document.body.offsetHeight);
	   this.dark.canvas.backgroundColor="#000000";
	   this.dark.setOpacity(0);
	   this.dark.canvas.zIndex=this.lockZ;
	   
	   this.pageIsLoaded = true;
	  
	  
    }
    function getWindowSize()
    {
         if (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.objects[n].type=="dialog")
	  	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!=obj)		
        	this.active.blur()
		if (this.objects[objRef]==this.floatObject){
			this.objects[objRef].focus(this.floatZ);

		}	
		else{ 
			if (this.objects[objRef].type=="static"){		
	    		this.objects[objRef].focus(getStyle(this.objects[objRef].content).zIndex-2);

			}
	    	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;         

			  this.objects[j].canvas.zIndex = tempz;

		  }
		  this.objects.sort(sortByZindex);

				
	}	
    
    // sorting function for the array objects's sort method.	
	function sortByZindex(obj1,obj2)   
	{  
	  a = obj1.canvas.zIndex;			
	  b = obj2.canvas.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;   
	    
		   this.objects[i].canvas.zIndex = this.bottomZ+i;
		        
	  }	
	  this.topZ=(this.objects.length)*20+this.bottomZ;
	  if (this.objects.length>0) 
        this.makeActive(this.objects.length-1);
	  
	}
 
 
	function EMparseObjects()
	{
      returnflag = false;
	  var p;
	  for (p=this.objects.length-1;p>-1;p--)                 											                        
	  {    
			//alert("gmx: "+this.mouseX+" gmy:"+this.mouseY+" "+this.objects[p]+" x:"+this.objects[p].x+" y"+this.objects[p].y);  	
	    if(this.mouseX<this.objects[p].x || this.mouseX > this.objects[p].x+this.objects[p].w) continue;
        if (this.mouseY<this.objects[p].y || this.mouseY > this.objects[p].y+this.objects[p].h) continue;
		//alert("gmx: "+this.mouseX+" gmy:"+this.mouseY+" "+this.objects[p]+" x:"+this.objects[p].x+" y"+this.objects[p].y);
		if (this.active != this.objects[p]){
			this.makeActive(p);
			//alert(this.objects[p]);
		}
		this.active.mouseEventDispatch(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;
			this.key = event.keyCode;
			this.eType = event.type;
        }
        if(ns || ns6)
        {   this.event = e;
            this.mouseX = e.pageX;
            this.mouseY = e.pageY;
			this.button = e.which;
			this.key = e.which;
			this.eType = e.type;
        }
        this.dx = this.mouseX - ox;
        this.dy = this.mouseY - oy;
    } 
	function EMgrabKeyboard(obj)
	{
		this.keyGrabber = obj;
	
		//alert('keygrab by '+ obj.id);
	}
	function EMreleaseKeyboard(obj)
	{
		if(this.keyGrabber ==obj)
		this.keyGrabber = null;
		//alert('released by :'+obj.id+" and it's isfocsed="+obj.isFocused);
	}
	function EMkeyboardEvent(e)
	{	
		//alert("key event");
	  // alert(GuiManager.keyGrabber);
		//if (ns6)
			//e.preventDefault();
		GuiManager.recordEvent(e);
		//alert (GuiManager.event.shiftKey);
		if(GuiManager.keyGrabber){
			return GuiManager.keyGrabber.keyEvent(GuiManager);
			
		}
		
	}
    function EMmousedown(e)
    {	

        GuiManager.recordEvent(e)
		if(this.dragDropObject)
		{
			this.drop();
		}
		if(moz){
		if(e.button==2)
		{
			if(moz) e.preventDefault();
		}
		}
        us = GuiManager.parseObjects();
        if (!us)
        {
        return false;
        }
		else
		{
		if(ie)
		document.onselectstart=new Function ("return false")
		if(moz)
		//e.preventDefault();
		window.addEventListener("selectstart",new Function("return false"),true);
		}
        return false;
        
    }
	
    function EMmousemove(e)
    {
        GuiManager.recordEvent(e)
		if(GuiManager.capture)
			GuiManager.capture();
        if (GuiManager.dragObject){
            GuiManager.dragObject.drag()
            if(ie) event.returnValue=false;
            return true;
        }else{
			if(GuiManager.dragDropObject)
			{
				GuiManager.dragDropMove();
			}
            return true;
        }
    }
    
    function EMmouseup(e)
    {   
        clearTimeout(GuiManager.scrollRepeat)
        GuiManager.recordEvent(e)
		
        if (GuiManager.dragObject){
            GuiManager.dragObject.dragStop();
            GuiManager.dragObject = null;
        }
		
		if(GuiManager.dragDropObject)
		{
			GuiManager.drop();
		}
        if(ns) routeEvent(e);
        return true;
   				if(ie)
		document.onselectstart=new Function ("return true")
		if(moz)
		window.addeventListenr("selectstart",new Function("return true"),true);
    } 
	
	function EMshowVideo()
	{
		if(this.video)
		this.video.style.visibility='inherit';
	}
	function EMhideVideo()
	{
		if(this.video)
		this.video.style.visibility="hidden";
	}
	function EMclearVideo()
	{
		if(this.video)
		{
			this.video.innerHTML="";
		}
	}
	
	
	function GM_showDialog(gui,center,source)
	{
		//alert("show dialog "+gui+" dialog"+this.dialog);
		if(center){
			this.getWindowSize();
			if(ie){
				ny =  document.body.scrollTop+this.my-gui.h/2;
				nx = document.body.scrollLeft+this.mx-gui.w/2;
			}else{
				ny = window.pageYOffset+this.my-gui.h/2;
				nx = window.pageXOffset+this.mx-gui.w/2;
			}
			gui.setXY(nx,ny)
		
		}
		if (typeof gui =="object")
		{
			gui.type="dialog";
			setTimeout(gui.id+".open();",600);
			gui.canvas.zIndex=this.dialog?this.floatDialogZ:this.floatZ;
		}
		else
		{
			getStyle(gui).position="relative";
			getStyle(gui).zIndex =this.floatZ;
			window.scrollTo(0,getPageTop(getLayer(gui))-150);
		}
		if(!this.dialog){
			this.dialog=gui;
			this.lockPageUI();
		}

	}
	function GM_show(gui,center,fromOrigin)
	{
		
		if(center){
			this.getWindowSize();
			if(ie){
				ny =  document.body.scrollTop+this.my-gui.h/2;
				nx = document.body.scrollLeft+this.mx-gui.h/2;
			}else{
				ny = window.pageYOffset+this.my-gui.h/2;
				nx = window.pageXOffset+this.mx-gui.w/2;
			}
			gui.type="dialog";
			gui.setXY(nx,ny)
			gui.open();
			if(this.dialog)
				gui.focus(this.floatDialogZ);
		
		}
	}
	function GM_closeDialog(gui){
		if(gui)
		{
			if(gui!=this.dialog)
			{   
				this.closeGui(gui);
				return;
			}
		}
		this.unlockPageUI();
		if (typeof this.dialog =="object")
		{
			this.closeGui(this.dialog);
		}
		else
		{
			getStyle(this.dialog).zIndex ='';
		}
		this.dialog=null;
		
		

	}
	
	function GM_openGui(gui,fromX,fromY,toX,toY)
	{
		gui.setOpacity(0);
		gui.show();
		gui.fadeTo(1,500);
	}
	
	function GM_closeGui(gui)
	{
		gui.fadeTo(0,500);
		setTimeout(gui.id+".hide();",510);
	}
	
	function GM_lockPageUI()
	{
		//alert('locking');
		this.dark.setSize(document.body.offsetWidth,document.body.offsetHeight+1000);
		this.dark.setOpacity(0);
		this.dark.show();
		if(typeof this.dialog == "object")
			this.dark.setOpacity(0.5);
		else
			this.dark.fadeTo(0.5,700);
	}
	
	function GM_unlockPageUI()
	{
		//
		if(typeof(this.dialog)=="object")
			this.dark.hide();
		else{
			this.dark.fadeTo(0,500);
			setTimeout("GuiManager.dark.hide()",510);
		}
		
	}
	
	function GM_dragDrop(dragDropObj)
	{ 
		//this.dragDrop = true;
		this.dragDropObject = dragDropObj;
		this.dragDropObject.canvas.zIndex=this.dragDropZ;
		this.dragDropObject.setOpacity(0.5);
		this.dragDropObject.ox = this.mouseX;
		this.dragDropObject.oy = this.mouseY;
		this.dragDropMove();	
	
	}

	function GM_dragDropMove()
	{
		this.dragDropObject.setXY(this.mouseX,this.mouseY,1);
		this.dragDropHover();
	
	}
	function GM_dragDropHover()
	{
			
			if(this.hoverObject)
			{
				xp = getPageLeft(this.hoverObject.node);
				yp = getPageTop(this.hoverObject.node);
			  	if((this.mouseX<xp || this.mouseX > xp + this.hoverObject.w) 
										&&
		        (this.mouseY<yp || this.mouseY > yp+this.hoverObject.h))
					{
						this.hoverObject.dragOut();
					}
			}
		  for (p=this.objects.length-1;p>-1;p--)                 											                        
		  {    
			xp = getPageLeft(this.objects[p].node);
			yp = getPageTop(this.objects[p].node); 	
		    if(this.mouseX<xp || this.mouseX > xp+this.objects[p].w) continue;
	        if (this.mouseY<yp || this.mouseY > yp+this.objects[p].h) continue;
	
		
			if(this.objects[p].acceptsDrop)
			{
				if(this.hoverObject && this.hoverObject!=this.objects[p])
				{
					this.hoverObject.dragOut();
					
				}
				if(this.objects[p].acceptsDrop && this.objects[p].validateDrop(this.dragDropObject))
				{
					//alert(this.objects[p]);
					this.hoverObject = this.objects[p];
					this.hoverObject.dragOver();
					return;
				}
				
			}
			
		 }
	     if(this.hoverObject){
		 	this.hoverObject.dragOut();
			this.hoverObject = null
		}		

	}
	
	function GM_drop()
	{
		
		this.dragDropObject.setOpacity(1);
		if(this.hoverObject)
		{
			this.hoverObject.dragOut();
			this.hoverObject.dragDrop(this.dragDropObject);
			this.hoverObject = null;
			this.dragDropObject.hide();
			this.dragDropObject = null;

		}else{
			dx = this.dragDropObject.ox - this.dragDropObject.x;
			dy = this.dragDropObject.oy - this.dragDropObject.y;
			diag = Math.sqrt(dx^2+dy^2);
			this.ddo = this.dragDropObject;
			this.dragDropObject = null;
			this.ddo.Move(dx,dy,400);
			this.ddo.fadeTo(0.1,400);
			setTimeout('GuiManager.ddo.hide();GuiManager.ddo.destroy();GuiManager.ddo=null',510);
		
		}
		
	}
//
//---end box object
//---------------------------------------------------

GuiManager = new EventManager("GuiManager");