import java.applet.Applet;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.Toolkit;

import java.io.InputStream;
import java.io.DataInputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;

import java.net.URL;
import java.net.URLConnection;

public class book extends Applet implements Runnable
{
	private int text_x = 0;
	private int width, height, pause, inc, num_ims, text_width, text_y, text_im_y, text_height;
	private String text = "";
	private Image off_screen;
	private Color back_col, text_col;
	private Thread thr = null;
	private boolean alive = true;
	private imloader iml;

	private static Font font;
	static
	{
		String f = "SansSerif";
		String[] fl = Toolkit.getDefaultToolkit().getFontList();
		for (int i = 0; i < fl.length; i++)
		{
			if ((fl[i] == f)||(fl[i] == "Helvetica"))
			{
				f = fl[i];
				break;
			}
		}
		font = new Font(f, Font.PLAIN, 11);
	}

	public void init ()
	{
		num_ims = Integer.parseInt(getParameter("num_ims"));
		URL[] u = new URL[num_ims];
		for (int i = 0; i < num_ims; i++)
		try
		{
			u[i] = new URL(getDocumentBase(), "im_" + i + ".jpg");
		}
		catch (Throwable t){}
		iml = new imloader(u, this);
		iml.start();

		back_col = new Color (Integer.parseInt(getParameter("back"),16));
		text_col = new Color (Integer.parseInt(getParameter("text"),16));
		pause = Integer.parseInt(getParameter("pause"));
		inc = Integer.parseInt(getParameter("inc"));

		setBackground(back_col);
		try {setVisible(true);} catch (Throwable t) {show();}

		try
		{
			InputStream is = (new URL(getDocumentBase(), "ik.txt")).openStream();
			BufferedReader b = new BufferedReader(new InputStreamReader(is));
			String s = b.readLine();
			while (s != null)
			{
				text = text + s;
				s = b.readLine();
			}
			is.close();
		}
		catch (Throwable t)
		{
			try
			{
				URLConnection uc = (new URL(getDocumentBase(), "ik.txt")).openConnection();
				uc.connect();
				InputStream is = uc.getInputStream();
				DataInputStream dis = new DataInputStream(is);
				String s = dis.readLine();
				while (s != null)
				{
					text = text + s;
					s = dis.readLine();
				}
				is.close();
			}
			catch (Throwable th)
			{}
		}

		Dimension dim;
		try {dim = getSize();} catch (Throwable t) {dim=size();}
		width = dim.width;
		height = dim.height;

		off_screen = createImage(width, height);
		Graphics g = off_screen.getGraphics();
		g.setColor(back_col);
		g.fillRect(0, 0, width, height);
		g.dispose();

		FontMetrics font_metrics = Toolkit.getDefaultToolkit().getFontMetrics(font);
		text_y = font_metrics.getMaxAscent();
		text_width = font_metrics.stringWidth(text);
		text_height = font_metrics.getHeight();
		text_im_y = height - text_height;

		thr = new Thread(this);
		thr.start();
	}


	private Image textIm (String s, int x)
	{
		Image im = createImage(width, text_height);
		Graphics g = im.getGraphics();
		g.setColor(back_col);
		g.fillRect(0, 0, width, text_height);
		g.setColor(text_col);
		g.setFont(font);
		g.drawString (s, x, text_y);
		g.dispose();
		return im;
	}

	public void run()
	{
		int current_x = 0, current_image = 0, next_image = 1;

		Graphics g = off_screen.getGraphics();

		while (alive)
		{

			if (iml.im_loaded[current_image])
			{
				int next_x = iml.ims[current_image].getWidth(this) + current_x;
				if (next_x < width)
				{
					if (iml.im_loaded[next_image])
					{
						g.drawImage (iml.ims[current_image], current_x, 0, this);
						g.drawImage (iml.ims[next_image], next_x, 0, this);
						current_x = current_x - inc * 2;
					}

				}
				else
				{
					g.drawImage (iml.ims[current_image], current_x, 0, this);
					current_x = current_x - inc * 2;
				}

				if (next_x < 0)
				{
					current_x = next_x;
					current_image = next_image;
					next_image++;
					if (next_image == num_ims) next_image = 0;
				}
			}
			g.drawImage (textIm (text, text_x), 0, text_im_y, this);
			text_x = text_x - inc;
			if (text_x + text_width < 0) text_x = width;

			paint(getGraphics());
			try {thr.sleep(pause);} catch (Throwable t){}
		}
	}

	public void destroy()
	{
		alive = false;
	}

	public void update(Graphics g)
	{
		paint(g);
	}

	public void paint(Graphics g)
	{
		g.drawImage(off_screen, 0, 0, this);
	}
}


class imloader extends Thread
{
	public Image[] ims;
	public boolean[] im_loaded;
	private URL[] urls;
	private Applet app;

	public imloader (URL[] u, Applet a)
	{
		app = a;
		urls = u;
		ims = new Image[urls.length];
		im_loaded = new boolean[urls.length];
		for (int i = 0; i < urls.length; i++) im_loaded[i] = false;
	}

	public void run()
	{
		for (int i = 0; i < urls.length; i++)
		{
			ims[i] = app.getImage(urls[i]);
			MediaTracker mt = new MediaTracker(app);
			mt.addImage(ims[i], 0);
			try
			{
				mt.waitForAll();
				im_loaded[i] = true;
			}
			catch (Throwable t) {ims[i] = null;}
		}
	}
}