	package se.dahlberg.transliteration;

import java.awt.*;
import java.util.Observer;
import java.util.Observable;

public class TransliterationApplet extends java.applet.Applet implements Observer {

	Image theImage = null;
	Image[] img;
	String[] effect;
	Transliteration currentFade = null;
	int stage = 0;
	int pause, steps;
	int numImages;
	boolean isRunning = true;
	MediaTracker mt;

	public void start() {
		if (currentFade != null) {
			if (!currentFade.isRunning()) {
				setTransliteration(effect[0], img[0], img[1]);
			}
		} else {
			setTransliteration(effect[0], img[0], img[1]);
		}
		stage = 0;
		super.start();
		isRunning = true;
		repaint();
	}
	
	public void stop() {
		super.stop();
		isRunning = false;
	}

	public void init() {
		System.out.println(" TransliterationApplet by Per Dahlberg");
		System.out.println("  http://www.dahlberg.se/, per@dahlberg.se");
		System.out.println("  version 1.0, 1 Aug 1998");

		mt = new MediaTracker(this);

		numImages = Integer.parseInt(getParameter("numImages"));
		img = new Image[numImages];
		effect = new String[numImages];
		for (int i = 0; i < numImages; i++) {
			img[i] = getImage(getDocumentBase(), getParameter("image"+i));
			mt.addImage(img[i], i);
			effect[i] = getParameter("effect"+i).toLowerCase();
		}
		steps = Integer.parseInt(getParameter("steps"));
		pause = Integer.parseInt(getParameter("pause"));
		System.out.println("Got all parameters.");
		try {
			mt.waitForID(0);
		} catch (InterruptedException e) {
			return;
		}

		prepareImage(img[1], this);

		// Draw the first image
		theImage = img[0];
		prepareImage(theImage, this);
		repaint();
		validate();

		try {
			mt.waitForID(1);
		} catch (InterruptedException e) {
			return;
		}
		System.out.println("Loaded the first images. Starting trans.");

		setTransliteration(effect[0], img[0], img[1]);

		//for (int i = 2; i < numImages; i++)
		//	prepareImage(img[i], this);
	}

	public void update(Observable o, Object arg) {
		System.out.println("Updating");
		if (!isRunning) {
			return;
		}
		stage++;
		if (stage < (numImages-1)) {
			try {
				mt.waitForID(stage+1);
			} catch (InterruptedException e) {
				return;
			}
			setTransliteration(effect[stage], img[stage], img[stage+1]);
			repaint();
		} else {
			setTransliteration(effect[stage], img[stage], img[0]);
			stage = -1;
		}
	}

	public void update(Graphics g) {
		paint(g);
	}

	public void paint(Graphics g) {
		if (theImage != null) {
			g.drawImage(theImage, 1, 1, this);
		}
	}

	private void setTransliteration(String effect, Image i1, Image i2) {
		System.out.println("Setting trans: " + effect);
		if (effect.equals("checkbox")) {
			currentFade = new CheckBox
				(i1, i2, steps, i1.getWidth(this), 
				i1.getHeight(this), pause);
		} else if (effect.equals("scroll")) {
			currentFade = new Scroll
				(i1, i2, steps, i1.getWidth(this), 
				i1.getHeight(this), pause);
		} else if (effect.equals("diagonalscroll")) {
			currentFade = new DiagonalScroll
				(i1, i2, steps, i1.getWidth(this), 
				i1.getHeight(this), pause);
		} else {
			currentFade = new Fade
				(i1, i2, steps, i1.getWidth(this), 
				i1.getHeight(this), pause);
		}

		currentFade.addObserver(this);
		theImage = createImage(currentFade);
		prepareImage(theImage, this);
		repaint();
	}

}
