/* -------------------------------------------------------------
This applet creates a few different user interface components
and detects when the user interacted with them.

Java's classes: Applet        (applet)
                System        (lang)
                Button        (awt)
                Choice        (awt)
                TextField     (awt)
                Checkbox      (awt)
                CheckboxGroup (awt)
                Label         (awt)
                Event         (awt)

Custom classes: UIApplet

------------------------------------------------------------- */
import java.applet.Applet;
import java.applet.AppletContext;
import java.awt.Image;
import java.awt.Label;
import java.awt.Event;
import java.awt.Graphics;
import java.awt.Color;
import java.awt.Dialog;
import java.awt.Font;
import java.awt.MediaTracker;
import java.net.URL;
import java.net.MalformedURLException;
import netscape.javascript.JSObject;
import gjt.Util;
import java.util.Vector;
import GraphicLoader;
import gjt.DialogClient;
import java.awt.Rectangle;



/*
invisibleButton1 = new InvisibleButton();
		invisibleButton1.reshape(95,46,119,87);
		add(invisibleButton1);
		*/

public class MasterApplet extends Applet implements Runnable, DialogClient
{   	
	ClickButton				bSoftware,bMultimedia, bGraphics, bWeb, bInfo, lastClickButton;
	
	RollButton				rJava, rInterface, rDirector,rTools,rKiosks,rGames,rTraining;
	RollButton				rPresentation,rAnimation,rIllustration,rPublication,rMarketing;
	RollButton				rSite,rServices,rPhone,rEmail, rSelected;
	PlateCanvas 			cnPlate;
	DisplayString			sDisplayType;
	Background 				cnBackground;
	JSObject				MasterWin;   	
   	public Image			mainGraphic,composite, pattern1; 
   	public final int 		appletWidth = 640;
   	public final int		appletHeight = 68;
   	Graphics				gComposite;
   	Image 					logo, display, plate, tile; 
   	Image 					softwareA,softwareB,multimediaA,multimediaB,graphicA,graphicB,webA,webB,contactA,contactB;
   	ImageCanvas				cnByrdmanLogo, cnButtons;	
   	Label					zType;
	GraphicLoader			loaderThreadOne, loaderThreadTwo, loaderThreadThree,loaderThreadFour;
	public int				mode;
	DisplayCanvas			dDisplay;
	public URL 				codebase;
	Thread					masterThread;
	
   	boolean					InterfaceLoaded;
   	long					startTime, loadTime;	
   	float					secondTime;			
   	MediaTracker			tracker;
   	Typer					myTyper;
   	boolean					badURL;
   	Rectangle				typeArea, buttonArea;
   	Vector 					Group1,Group2,Group3,Group4,Group5, Group0;
	
	
	/** Create a user interface. */
   	public void buildInterface()
   	{
   		setLayout(null);
   		setForeground(Color.red);
   		setBackground(Color.black);
   		
   		try{codebase = new URL(getCodeBase(),"../00main/art/");}
		catch ( MalformedURLException e)
		{
			System.out.println("Bad URL: " + codebase);
		}
   		/*
   		loaderThreadOne = new GraphicLoader(this, 1);
		loaderThreadOne.start();
		
		*/
		
   		
   		
		
		loadMainGraphic();
		sDisplayType = new DisplayString(this,50,7,208,22);
   		myTyper = new Typer(this,50,7,208,22);
		myTyper.start();
		
		composite = this.createImage(appletWidth,appletHeight);
		gComposite = composite.getGraphics();
		
		cnBackground = new Background(this, 0,0,640,68,41,37,245,0);
		typeArea = new Rectangle(240,0,400,45);
		//typeArea = new Rectangle(267,4,321,33);
		buttonArea = new Rectangle(0,40,appletWidth,appletHeight - 40);
		rSelected = null;
		
		cnByrdmanLogo = new ImageCanvas(this, 3,3,39,61,206,0);
		dDisplay = new DisplayCanvas(this, 48,5,212,26,0,100);
		
		Group0 = new Vector(0,1);
   		bSoftware = new ClickButton(this, Group0,48,42,103,20,0,0,103,0);
   		bMultimedia = new ClickButton(this, Group0,157,42,103,20,0,20,103,20);
   		bGraphics = new ClickButton(this, Group0,267,42,103,20,0,40,103,40);
   		bWeb = new ClickButton(this, Group0,376,42,103,20,0,60,103,60);
   		bInfo = new ClickButton(this, Group0,485,42,103,20,0,80,103,80);
   		
   		
   		System.out.println("------>firstTracker Start");
   		while(tracker.checkAll(true) == false)
		{
         	try { Thread.currentThread().sleep(50); }
        	catch(InterruptedException ex) { }
        }
       
        bSoftware.makeButton();
   		bMultimedia.makeButton();
   		bGraphics.makeButton();
   		bWeb.makeButton();
   		bInfo.makeButton();
   		cnByrdmanLogo.makeCanvas();
   		cnBackground.makeBackground();
   		dDisplay.makeDisplay();
   		
   		
   		gComposite.drawImage(cnBackground.background, 0,0,this);
   		gComposite.drawImage(dDisplay.display, dDisplay.locH,dDisplay.locV,this);
   		gComposite.drawImage(cnByrdmanLogo.image, cnByrdmanLogo.locH,cnByrdmanLogo.locV,this);
   		gComposite.drawImage(bSoftware.upButton, bSoftware.locH,bSoftware.locV,this);
   		gComposite.drawImage(bMultimedia.upButton, bMultimedia.locH,bMultimedia.locV,this);
   		gComposite.drawImage(bGraphics.upButton, bGraphics.locH,bGraphics.locV, this);
   		gComposite.drawImage(bWeb.upButton, bWeb.locH,bWeb.locV, this);
   		gComposite.drawImage(bInfo.upButton, bInfo.locH, bInfo.locV, this);
   		
		
   		//MasterWin = JSObject.getWindow(this);
		
		myTyper.halt();
       	InterfaceLoaded = true;
		repaint();
		
		cnPlate = new PlateCanvas(this, 267,4,321,33,0,100);
		cnPlate.makePlate();
		makeRollButtons();
		//loadGroup1();
	}  
	
	
	public boolean GoToPage(String pageNum)
   	{
   		badURL = false;
  		String hrefPage = "../" + pageNum + "/01Page.htm";
  		
  		try
		{
			URL hrefURL = new URL(getDocumentBase(),hrefPage);
			AppletContext context = getAppletContext();
			context.showDocument(hrefURL, "Display");
		}
		catch (MalformedURLException f)
		{
			badURL = true;
		}
		return true;
	}
	
   
   /** Respond to user input events. */
   
   public void buildPage(String sApplet, String sPicture) 
   {
   		System.out.println("BUILDPAGE");
   		MasterWin.eval("parent.MasterFrame.document.clear()");
   		MasterWin.eval("parent.MasterFrame.document.open()");
		MasterWin.eval("parent.MasterFrame.document.write('<HTML><FRAMESET rows = *,40><FRAME SRC= pBlank.htm NAME = DisplayFrameOne SCROLLING = AUTO MARGINHEIGHT = 0 MARGINWIDTH NORESIZE ><FRAME SRC= pBlank.htm NAME = ControlFrameOne SCROLLING = NO MARGINHEIGHT = 0 MARGINWIDTH = 0 NORESIZE ></FRAMESET></HTML>')");
		MasterWin.eval("parent.MasterFrame.document.close()");
		MasterWin.eval("parent.MasterFrame.ControlFrameOne.document.open()");
		MasterWin.eval("parent.MasterFrame.ControlFrameOne.document.write('<applet codebase = ../UIApplet code =" + sApplet +".class name = The" + sApplet + " width=816 height=40 MAYSCRIPT></applet>')");
		MasterWin.eval("parent.MasterFrame.ControlFrameOne.document.close()");
		MasterWin.eval("parent.MasterFrame.DisplayFrameOne.document.open()");
		//MasterWin.eval("parent.MasterFrame.DisplayFrameOne.document.write('<HTML><BODY BGCOLOR = #ffffff><P ALIGN=CENTER><IMG SRC= ../"+ sPicture +".gif WIDTH=620 HEIGHT=320 ALIGN=TOP NATURALSIZEFLAG=0></HTML>')");
		//MasterWin.eval("parent.MasterFrame.DisplayFrameOne.document.write('<HTML><BODY BGCOLOR = #000000><CENTER><TABLE CELLSPACING=0 WIDTH=450 VALIGN= MIDDLE><TR><TD WIDTH=100%><CENTER><P &nbsp><IMG SRC= ../"+ sPicture +".gif WIDTH=620 HEIGHT=320 ALIGN=CENTER VSPACE = 70 NATURALSIZEFLAG=0></P></CENTER></TD></TR></TABLE></CENTER></BODY></HTML>')");
		MasterWin.eval("parent.MasterFrame.DisplayFrameOne.document.write('<HTML><BODY BGCOLOR = #000000><CENTER><TABLE CELLSPACING=0 WIDTH=450 VALIGN= MIDDLE><TR><TD WIDTH=100%><CENTER><P &nbsp><IMG SRC= ../"+ sPicture +".gif WIDTH=620 HEIGHT=320 ALIGN=CENTER NATURALSIZEFLAG=0></P></CENTER></TD></TR></TABLE></CENTER></BODY></HTML>')");
		//MasterWin.eval("parent.MasterFrame.DisplayFrameOne.document.write('<HTML><IMG SRC= ../"+ sPicture +".gif WIDTH=620 HEIGHT=320 ALIGN=CENTER NATURALSIZEFLAG=0></HTML>')");
		MasterWin.eval("parent.MasterFrame.DisplayFrameOne.document.close()");	
	}
	
		
	/*
	public void update(Graphics g)
	{
		System.out.println("MASTER update");
		paint(g);
	}
	*/
	
	public void buildNetPage(String sApplet, String sPicture)    
	{  
	 		
		MasterWin.eval("parent.MasterFrame.document.clear()");   		
		MasterWin.eval("parent.MasterFrame.document.open()");   		
		//MasterWin.eval("parent.MasterFrame.document.write('<HTML><FRAMESET rows = *,40><FRAME SRC= pBlank.htm NAME = DisplayFrameOne SCROLLING = AUTO MARGINHEIGHT = 0 MARGINWIDTH NORESIZE ><FRAME SRC= pBlank.htm NAME = ControlFrameOne SCROLLING = NO MARGINHEIGHT = 0 MARGINWIDTH = 0 NORESIZE ></FRAMESET></HTML>')");		MasterWin.eval("parent.MasterFrame.document.close()");
		//System.out.println("BUILDNETPAGE");
		//MasterWin.eval("parent.MasterFrame.ControlFrameOne.document.open()");
		//MasterWin.eval("parent.MasterFrame.ControlFrameOne.document.write('<script>function getLocation() { var System = java.lang.System; var loc = parent.DisplayFrameOne.document.location; System.err.println(loc);}</script><applet codebase = ../UIApplet code =" + sApplet +".class name=\"The" + sApplet + "\" width=100% height=30 MAYSCRIPT></applet>')");
		//MasterWin.eval("parent.MasterFrame.ControlFrameOne.document.close()");	
		//MasterWin.eval("parent.MasterFrame.DisplayFrameOne.document.open()");
		MasterWin.eval("parent.MasterFrame.DisplayFrameOne.document.write('<HTML><BODY BGCOLOR = #000000><CENTER><TABLE CELLSPACING=0 WIDTH=450 VALIGN= MIDDLE><TR><TD WIDTH=100%><CENTER><P &nbsp><IMG SRC= ../"+ sPicture +".gif WIDTH=620 HEIGHT=320 ALIGN=CENTER NATURALSIZEFLAG=0></P></CENTER></TD></TR></TABLE></CENTER></BODY></HTML>')");
		//MasterWin.eval("parent.MasterFrame.DisplayFrameOne.document.write('<HTML><BODY BGCOLOR = #000000><P ALIGN=CENTER><IMG SRC= ../"+ sPicture +".gif WIDTH=620 HEIGHT=320 ALIGN=TOP NATURALSIZEFLAG=0></HTML>')");
		MasterWin.eval("parent.MasterFrame.DisplayFrameOne.document.close()");	
	
	}
	
	public boolean handleEvent(Event event) 
	{
		if (event.id == Event.MOUSE_ENTER)
		{ 
			/*
			if (event.target == bMap) 
			{
				getAppletContext().showStatus("MAP - master control panel to open any window of the course.");
				return true;
			}
			else if (event.target == bNotes) 
			{
				getAppletContext().showStatus("NOTES - review and save HTML links, typed notes and window configurations.");
				return true;
			}
			else if (event.target == bGuide) 
			{
				getAppletContext().showStatus("GUIDE - tracks the course material covered and it's recommended viewing order.");
				return true;
			}
			else if (event.target == bBack) 
			{
				getAppletContext().showStatus("PREVIOUS - moves backward one page or section in the current GUIDE selection.");
				return true;
			}
			else if (event.target == bForward) 
			{
				getAppletContext().showStatus("NEXT - moves forward one page or section in the current GUIDE selection..");
				return true;
			}
			else if (event.target == bFBack) 
			{
				getAppletContext().showStatus("STEP BACK - jumps backard one item in the GUIDE list.");
				return true;
			}
			else if (event.target == bFForward) 
			{
				getAppletContext().showStatus("STEP FORWARD - jumps forward one item in the GUIDE list.");
				return true;
			}
			else if (event.target == cnLocation||event.target == zGuide) 
			{
				getAppletContext().showStatus("GUIDE LOCATION - clicking here will automatically reopen the location listed.");
				return true;
			}
			*/
		}
		
		return super.handleEvent(event);
	}
	
	
	
	
	public boolean mouseEnter(Event e, int x, int y)
    {
    	return true;
    }
	
	
   
   public boolean action(Event e, Object arg) 
   {
   		if (e.target instanceof ClickButton)
		{
			System.out.println("Instance of ClickButton at MasterApplet");
			
			
   			return true;
		}
		else
		{
			System.out.println("Unrecognized event");
		}
		return super.action(e, arg);
		
   }
   
   public boolean mouseMove(Event evt, int x , int y)
	{
		if(typeArea.inside(x,y))
		{
			switch (mode)
			{
				case 1:
					checkRollover(Group1,x,y);
					break;
				case 2:
					checkRollover(Group2,x,y);
					break;
				case 3:
					checkRollover(Group3,x,y);
					break;
				case 4:
					checkRollover(Group4,x,y);
					break;
				case 5:
					checkRollover(Group5,x,y);
					break;
			}
					
		}
		return true;
	}
	
	public boolean mouseDown(Event evt, int x , int y)
	{
		if(buttonArea.inside(x,y))
		{
			buttonDown(x,y);
		}
		else if(typeArea.inside(x,y))
		{
			switch (mode)
			{
				case 1:
					rollDown(Group1,x,y);
					break;
				case 2:
					rollDown(Group2,x,y);
					break;
				case 3:
					rollDown(Group3,x,y);
					break;
				case 4:
					rollDown(Group4,x,y);
					break;
				case 5:
					rollDown(Group5,x,y);
					break;
			}
					
		}
		return true;
	}
	
	public void rollDown(Vector myVector, int x, int y)
	{
		for(int i = 0; i < myVector.size(); i++)
		{
			RollButton b = (RollButton)myVector.elementAt(i);
			
			if(b.rollRegion.inside(x,y))
			{
				if(rSelected != null)
				{
					rSelected.selectState = false;
					rSelected.setState(false);
				}
				rSelected = b;
				//b.setState(true);
				rSelected.selectState = true;
				GoToPage(b.pageName);
				break;
			}
		}
	
	}
	
	public void buttonDown(int x, int y)
	{
		for(int i = 0; i < Group0.size(); i++)
		{
			ClickButton b = (ClickButton)Group0.elementAt(i);
			if(b.buttonRegion.inside(x,y))
			{
				if (lastClickButton != null) 
				{
					if(lastClickButton == b)
					{
						mode = 0;
						lastClickButton.setState(false);
						lastClickButton	= null;
						cnPlate.growPlate(false);
						break;
					}
					else
					{
						b.setState(true);
						lastClickButton.setState(false);
						lastClickButton = b;
						cnPlate.clearRolls();
						break;
					}
				}
				else
				{
					b.setState(true);
					cnPlate.growPlate(true);
					lastClickButton = b;
					break;
					
				}
			}
		}
		if(lastClickButton != null)
		{
			int p = 1000;
			if(lastClickButton == bSoftware)
			{
				sDisplayType.typeMessage("Single Design for All Platforms");
				growRolls(true,1);
			}
			else if(lastClickButton == bMultimedia)
			{
				sDisplayType.typeMessage("Exciting, Fun and Informative");
				growRolls(true,2);
			}
			else if (lastClickButton == bGraphics)
			{
				sDisplayType.typeMessage("Merging Technology and Art");
				growRolls(true,3);
			}
			else if (lastClickButton == bWeb)
			{
					
				sDisplayType.typeMessage("Establish Your Global Presence");
				growRolls(true,4);
					
					//dDisplay.setMessage("Provide Information Globally");
					//try { Thread.currentThread().sleep(p); }
	        		//catch(InterruptedException ex) { }
					//dDisplay.setMessage("Improve Customer Service");
					//try { Thread.currentThread().sleep(p); }
	        		//catch(InterruptedException ex) { }
					//dDisplay.setMessage("Automate Tech Support");
					//try { Thread.currentThread().sleep(p); }
	        		//catch(InterruptedException ex) { }
					//dDisplay.setMessage("Simplify Fullfilment Process");
					//try { Thread.currentThread().sleep(p); }
	        		//catch(InterruptedException ex) { }
					//dDisplay.setMessage("IntraNet Employee Training");
					//try { Thread.currentThread().sleep(p); }
	        		//catch(InterruptedException ex) { }
					//dDisplay.setMessage("Supplement Marketing Efforts");
					
			}
			else if (lastClickButton == bInfo)
			{
				sDisplayType.typeMessage("Thank You for Visiting.");
				growRolls(true,5);
					//try { Thread.currentThread().sleep(p); }
	        		//catch(InterruptedException ex) { }
					//dDisplay.setMessage("Hope to hear from you soon.");
			}
			
				//buildPage(b.applet,b.picture);
			
		} 
	}
	
	
	
	public void checkRollover(Vector myVector, int x, int y)
	{
		for(int i = 0; i < myVector.size(); i++)
		{
			RollButton b = (RollButton)myVector.elementAt(i);
			if(b.rollRegion.inside(x,y))
			{
				if(!b.getState())
				{
					b.setState(true);
				}
			}
			else if (b.getState())
			{
				b.setState(false);
			}
		}
	
	}
	
	public void makeRollButtons()
	{
		
		Group1 = new Vector(0,1);
		Group2 = new Vector(0,1);
		Group3 = new Vector(0,1);
		Group4 = new Vector(0,1);
		Group5 = new Vector(0,1);
		
		rJava = new RollButton(this, Group1, 4,11,26,12,0,133,"01Software/01Java");
		rInterface = new RollButton(this, Group1, 40,11,56,12,27,133,"01Software/02Interface");
		rDirector = new RollButton(this, Group1, 104,11,93,12,83,133,"01Software/03Director");
		rTools = new RollButton(this, Group1, 205,11,106,12,176,133,"01Software/04Tools");
		
		rKiosks = new RollButton(this, Group2, 10,11,43,12,0,157,"02Multimedia/01Kiosks");
		rGames = new RollButton(this, Group2, 71,11,37,12,43,157,"02Multimedia/02Games");
		rTraining = new RollButton(this, Group2, 124,11,80,12,80,157,"02Multimedia/03Training");
		rPresentation = new RollButton(this, Group2, 223,11,85,12,161,157,"02Multimedia/04Presentation");
		
		rAnimation = new RollButton(this, Group3, 10,11,67,12,0,181,"03Graphics/01Animations");
		rIllustration = new RollButton(this, Group3, 111,11,82,12,68,181,"03Graphics/02Illustrations");
		rPublication = new RollButton(this, Group3,224,11,79,12,149,181,"03Graphics/03Publications");
		
		rMarketing = new RollButton(this, Group4, 6,11,133,12,0,205,"04Web/01Marketing");
		rSite = new RollButton(this, Group4,155,11,159,12,133,205,"04Web/02Site");
		
		rServices = new RollButton(this, Group5,38,11,97,12,206,76,"05Contact/01Services");
		rPhone = new RollButton(this, Group5,168,11,35,12,248,42,"05Contact/02Phone");
		rEmail = new RollButton(this, Group5,234,11,33,12,282,42,"05Contact/03Email");
	}
	
	public void paintRolls(Vector myVector, Graphics g)
	{
		for(int i = 0; i < myVector.size(); i++)
		{
			try { Thread.currentThread().sleep(100); }
        	catch(InterruptedException ex) { }
			RollButton b = (RollButton)myVector.elementAt(i);
			b.paint(g);
		}
	}
	
	public void drawRolls(Vector myVector, boolean x)
	{
		for(int i = 0; i < myVector.size(); i++)
		{
			try { Thread.currentThread().sleep(100); }
        	catch(InterruptedException ex) { }
			RollButton b = (RollButton)myVector.elementAt(i);
			b.growRoll(x);
		}
	}
	
	
	
	
	
	public void makePlate()
	{
		cnPlate = new PlateCanvas(this, 267,4,321,33,0,100);
		cnPlate.makePlate();
		add(cnPlate);
		
	}
	
	
	public void growRolls(boolean x, int newMode)
	{
		mode = newMode;
		if(rSelected !=null)
		{
			rSelected.selectState = false;
			rSelected = null;
		}
		switch(mode)
		{
			case 1:
			{
				drawRolls(Group1,x);
				break;
			}
			case 2:
			{
				drawRolls(Group2,x);
				break;
			}
			case 3:
			{
				drawRolls(Group3,x);
				break;
				
			}
			case 4:
			{
				drawRolls(Group4,x);
				break;
			}
			case 5:
			{
				drawRolls(Group5,x);
				break;
			}
		}
	
	
	}
	
	
	public void updateRolls(Graphics g)
	{
		switch(mode)
		{
			case 1:
			{
				paintRolls(Group1,g);
				break;
			}
			case 2:
			{
				paintRolls(Group2,g);
				break;
			}
			case 3:
			{
				paintRolls(Group3,g);
				break;
				
			}
			case 4:
			{
				paintRolls(Group4,g);
				break;
			}
			case 5:
			{
				paintRolls(Group5,g);
				break;
			}
		}
	
	
	}
	
	public void SendBookMessage()
   {
   /*
   		MasterWin.eval("parent.MasterFrame.ControlFrameOne.document.TheBookApplet.setPage()");
  */
   }
   
   public void returnPage()
   {
   		System.out.println("returnPage Message");
   }
   
   
   
   public void dialogDismissed(Dialog d)
   {
   	}
   
	public void loadMainGraphic()
   	{
   		mainGraphic = getImage(codebase,"all.gif");
   		tracker.addImage(mainGraphic, 0);
   	}
   	public void loadGroup1()
   	{
   		pattern1 = getImage(codebase,"../Patterns/iceburg.gif");
   		//tracker.addImage(mainGraphic, 0);
   	}
   	
   	public String getLoadTime()
	{
		loadTime = (System.currentTimeMillis() - startTime);
		secondTime = (float)loadTime/1000;
		String lt = Float.toString(secondTime);
		return lt;
	}
	
	
	
	public void update(Graphics g)
	{
		//System.out.println("MasterApplet Update");
		paint(g);
	}
	
	
	public void updateClip(int x, int y, int w, int h)
	{
		Graphics g = this.getGraphics();
		g.clipRect(x,y,w,h);
		g.drawImage(composite, 0, 0, this);
	}
	
	public void paint(Graphics g)
	{
		//System.out.println("MasterApplet Paint");
		if(InterfaceLoaded)
		{
			g.drawImage(composite, 0, 0, this);
		}
	}
	
	public  void start()
	{
		System.out.println("------>start Called");
		if (masterThread == null)
		{
			masterThread = new Thread(this);
			masterThread.start();
		}
	}
	
	public void showDialogInfo(String s)
	{
		getAppletContext().showStatus(s);
	}
	
	
	
	public void run()
	{
		//setBackground(Color.black);
		//repaint();
		if(!InterfaceLoaded)
		{
			startTime = System.currentTimeMillis();
			tracker = new MediaTracker(this);
			mode = 0;
			buildInterface();
			loadTime = (System.currentTimeMillis() - startTime);
			secondTime = (float)loadTime/1000;
			sDisplayType.typeMessage("Load Time:  " + secondTime + " seconds");
		}
	}
	
	public  void stop()
	{
		System.out.println("------>stop Called");
		if (masterThread != null)
		{
			
			masterThread.stop();
			masterThread = null;
		}
	}
   	
}



