//  ___________
// |           |____________________________________________________
// |                                                                |
// |     GuiManager                                                           |
// |                                                                |
// |                                                                |
// |                                                                |
// |                                                                |
// |                                                                |
// |________________________________________________________________|
//
 
    defaultScheme = new Array();
    defaultScheme["border"]="808080";
 
function GuiManager(id)
{

          // reference to object undergoing drag event
      this.inherit = EventManager;
	  this.inherit(id);
      this.startx=0;
      this.starty=0;
	  this.x=0;
	  this.y=0; 			
      this.sx = new Array();
      this.sy= new Array();
	  this.originalX=0; 					// used when dragging objects need to be returned...
	  this.originalY=0;					// to their original positions.
      this.mouseX=0;
      this.mouseY=0;
      this.dx=0;
      this.dy=0;
      this.startup = new Array();       // guis register at compile-time to be initialized onload.
      this.popup =null;
	  this.showingFlag=false;
	  this.hidingFlag=false;
	  this.box=new Array();
	  this.theme = defaultScheme;     
	  this.checkDrop =checkDrop;
	  this.initParent = this.init;
      this.init = guiManagerInit;
	  this.parseObjects = GMparseObjects
      this.addMeToStartup = addMeToStartup;
      this.initObjects = initObjects;
      this.showGUI = showGUI;
	  this.hideGUI = hideGUI;
	  this.setTheme = setTheme;
	  this.dBug = GMdBug;
	  this.dBugLN=0;
	}
function guiManagerInit(x,y,scheme,dBugPane)
    {   
	   this.initParent();
	   this.startx=x;this.starty=y;this.x=x;this.y=y;
       this.initObjects();
	   this.theme=scheme;
       if(w3){
		   for(bx=0;bx<5;bx++){
		   		this.box[bx] = new Box("guiManager.box["+bx+"]","box"+bx,1,1,this.theme["border"]);
				this.box[bx].layer[0].zIndex=this.superZ;
	       		this.box[bx].layer[0].borderWidth=2;
		   }
	   }
	   this.dragBox.layer[0].borderColor=this.theme["border"];
	   if (dBugPane)
	   	this.deBugger = dBugPane;
    }
	function GMdBug(text)
	{	
		if(!this.deBugger){
			
			return;
		}
		
		cnsl_out = getLayer(this.deBugger.content).innerHTML+"<br>"+this.dBugLN+" "+text;
		this.dBugLN++;
		this.deBugger.update(cnsl_out,1);
		if(!this.deBugger.visible)
			this.deBugger.setXY(this.w-this.deBugger.clipW-20,10,1);
	}
    function addMeToStartup(object)
    {
        this.startup[this.startup.length]=object;
    }
    function initObjects()
    {   
        for (s=0;s<this.startup.length;s++)
        {  
           this.startup[s].init() 
        }
         
    }
	function setTheme(newTheme)
	{
		this.theme = newTheme;
		if(w3){ 
		for (i=0;i<this.box.length;i++)
			this.box[i].setColor(this.theme["border"]);
		this.dragBox.setColor(this.theme["border"]);
		}
	}
	ax=null;
	ay=null;
	
    function showGUI(guiObject,ox,oy,ax,ay)
    {   
		if(!ox)
			ox = this.mouseX-10;
		if(!oy)
			oy = this.mouseY-10;
		if (this.showingFlag==true)
			return
		this.showingFlag = true;
        if(w3)
        { 
        	for(bc=0;bc<this.box.length;bc++)
			{
				if (this.box[bc].busy==false)
				break;
			}
            
        }
	   
        pointer = this.getObjectRef(guiObject);
        if (pointer!="err"){
            this.makeActive(pointer);
			this.showingFlag = false;
			return
        } 
		
           this.x+=20
           this.y+=20
		
        if (this.x>this.rightBound/4)
			this.x=this.startx;
		if (this.y>this.bottomBound/4)
			this.y=this.starty;

		//overide: open at specific position
		if (ax && ay)
		{   
		    if (ax =="center")
				ax = guiManager.mx-(guiObject.clipW/2);
		    if (ay =="center")
				ay = guiManager.my-(guiObject.clipH/2);
			this.x=(ax>-1)? ax:0;  
			this.y=(ay>20)?ay:50;
			ax=null;ay=null;
		}   
		if(w3)
		{
			this.sx[guiObject.id]=ox;
            this.sy[guiObject.id]=oy;
            this.box[bc].busy =true;
			this.box[bc].setSize(15,15)
			this.box[bc].setXY(ox,oy,1);
			this.box[bc].Move(this.x+guiObject.clipW/2-ox,this.y+guiObject.clipH/2-oy,200)
			setTimeout('guiManager.box['+bc+'].grow('+ guiObject.clipW+','+guiObject.clipH+',400,'+(-guiObject.clipW/2)+','+(-guiObject.clipH/2)+')',200);
			setTimeout('guiManager.box['+bc+'].hide();guiManager.box['+bc+'].busy=false;',600);		
	        setTimeout(guiObject.id+'.setXY('+this.x+','+this.y+',1)',600); 
		} 
		else guiObject.setXY(this.x,this.y,1); 
		this.showingFlag=false;
    }
	function hideGUI(object)
	{	
		if (this.hidingFlag)
			return
		this.hidingFlag = true;
        if(w3)
        { 
        	for(bx=0;bx<this.box.length;bx++)
			{
				if (this.box[bx].busy==false)
				break;
			}
	  		this.box[bx].busy=true;
	        this.box[bx].setSize(object.clipW,object.clipH);
			this.box[bx].setXY(object.x,object.y,1);
			guiManager.box[bx].grow(15,15,300,object.clipW/2,object.clipH/2);
			delay = 350;
			if(typeof this.sy[object.id] =="number"){
 setTimeout("guiManager.box["+bx+"].setXY();guiManager.box["+bx+"].Move(("+this.sx[object.id]+"-guiManager.box["+bx+"].x)"+",("+this.sy[object.id]+"-guiManager.box["+bx+"].y),200)",350);		
			delay+=200
			}
			setTimeout('guiManager.box['+bx+'].hide();guiManager.box['+bx+'].busy=false;',delay);	
			
	  }
	  this.hidingFlag=false;
	  this.killObject(object);
	}

	// XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
    // XX function PARSE_OBJECTS overwrite XX
	// XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
    //
	// this function supports popup menus 
	
	function GMparseObjects()
	{
      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]){
			if(this.activeMenu && this.objects[p].type!='menu'){
				if(this.activeMenu.activeChild)
					this.activeMenu.childMenu[activeChild].hideAll();
			}	
			this.makeActive(p);
		}
		this.active.parseXY(this.mouseX,this.mouseY);
        returnflag = true;  	  		 
		break;
        
	  }
	  if(returnflag==false && this.activeMenu)
	  {
	  	//alert(this.activeMenu.id)
		if(this.activeMenu.activeChild)
			this.activeMenu.childMenu[this.activeMenu.activeChild].hideAll();
	  }
	  return returnflag
	 
	}
	
	function checkDrop()						// check to see if object has been dropped on anything
	{  returnObject = null;						// simply returns object in mouse range like
	   this.dropIntoObjRef = null;				/// like parseObjects()
	   for (i=this.objects.length-1;i>-1;i--)                           
	  {
	    if(this.mouseX<this.objects[i].x || this.mouseX > this.objects[i].x+this.objects[i].clipW) continue;
		if (this.mouseY<this.objects[i].y || this.mouseY > this.objects[i].y+this.objects[i].clipH) continue; 
        if (this.objects[i]==dragObject) continue;  	  		 
	    returnObject = this.objects[i];
		this.dropIntoObjRef= i;
		break;
	  }
	  

	  return returnObject;
	
	}	
	
 

guiManager = new GuiManager("guiManager");
