import java.applet.Applet;
import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
import java.util.*;
import java.net.*;

public class front extends Applet implements MouseListener,Runnable{
	Image alph[],pics[], small[],temp,temp1,selected,blank;
	char cc;
	int total;
	int sleep1,sleep2;
        String ss,name;
        GridLayout grid;
        int w, h,count=0;
        int wid,ht;
 	Graphics pad[];
 	photos p[];
 	frame fr;
 	Thread t;
	URL theURL;
	Label label=new Label("info");
	Button btn=new Button("Home");
	
 	public void setitems(){
 		total=getnumbers("totalnumber",26);
 		if (total>36)
 			total=36;
 		sleep1=getnumbers("stop-time-for-first-pic",4000);
 		if(sleep1<500)
 			sleep1=3000;
 		if(sleep1>15000)
 			sleep1=10000; 			
 		sleep2=getnumbers("stop-time-for-second-pic",4000);
 		if(sleep2<500)
 			sleep2=3000;
 		if(sleep2>15000)
 			sleep2=10000; 			
 		w=getnumbers("icon-width",50);
 		if(w<25 || w>150)
 			w=60;
 		h=getnumbers("icon-height",50);
 		if(h<25 || h>150)
 			h=60;
 		wid=getnumbers("picture-width",250);
 		if(wid<100 || wid>600)
 			wid=250;
 		ht=getnumbers("picture-height",250);
 		if(ht<100 || ht>600)
 			ht=250;
 	}
 	
	int getnumbers(String s1,int j){
		int i=0;
		String ss=getParameter(s1);
		if(ss==null)
			i=j;
		else
			i=Integer.parseInt(ss);
		return i;
	} 	
 	
	public void init(){	
		setitems();
		setBackground(new Color(0xff,0xf5,0xdf));
		blank=getImage(getCodeBase(),"images/blank.gif");
		alph=new Image[total];	
		pics=new Image[total];	
		small=new Image[total];
		p=new photos[total];
		fr=new frame("Pratyush's World",this);
		fr .addMouseListener(this);
		getthings();
//Layout things for the main page of applet		
		grid=new GridLayout(6,6);
		setLayout(grid);
		for (int j=0;j<36;j++){
			if (j<total){
				p[j]=new photos(this,j);
				p[j].init();
				p[j].setName(""+j);
				p[j].addMouseListener(this);
				add(p[j]);			
			}else{
				if(j==35){
					add(btn);
					btn.addMouseListener(this);
				}
				add(new Label(""));
			}
		}
		try{
			theURL=new URL("http://www.pratyush.net");
			System.out.println("URL working"+theURL);
		}catch(MalformedURLException e){System.out.println("Bad luck:"+theURL);}
		fr.init();
	}
	public void getthings(){
		for (int i=0;i<total;i++){
			cc=(char)(i+97);
			name=getParameter("alph-"+cc);
			if (name==null)
			        temp=blank;
			else{
				temp=getImage(getCodeBase(),"images/alph/"+name);
				alph[i]=temp.getScaledInstance(wid-10,ht-10,java.awt.Image.SCALE_FAST);
				small[i]=temp.getScaledInstance(w,h,java.awt.Image.SCALE_FAST);
			}
			name=null;
			temp=null;
			name=getParameter("pic-"+cc);
			
			if (name==null)
			        temp=blank;
			else{
				temp=getImage(getCodeBase(),"images/pics/"+name);
				pics[i]=temp.getScaledInstance(wid-10,ht-10,java.awt.Image.SCALE_FAST);
			}		
		 }
	}

	public void run(){
		while(t!=null){	
			repaint();
		    if(fr.isShowing()){
			fr.repaint();
			pause(sleep1);
			selected=pics[count];
			fr.repaint();
			pause(sleep2);
			fr.hide();
		    }
		}
	}
	public void start(){
		if (t==null)
			t=new Thread(this);
		t.start();	
	}
	
	public void stop(){
		if(t!=null)
			t=null;
	}	
	
	public void mouseClicked(MouseEvent e){
		Component o=e.getComponent();		
		if(o.equals(fr))
			fr.hide();
		if(o==(btn)){
			this.getAppletContext().showDocument(theURL);
			System.out.println("Bad luck:"+theURL);
		}
		else{
			photos f=(photos)o;
			String ss=f.getName();
			count=Integer.parseInt(ss);
			selected=alph[count];
			fr.show();
		}
		
	}
	public void mouseReleased(MouseEvent e){}
	public void mousePressed(MouseEvent e){}
	public void mouseExited(MouseEvent e){}
	public void mouseEntered(MouseEvent e){}	
	
	public void Update(Graphics g){
		paint(g);
	}	
	public void paint(Graphics g){}

	public void pause(int zz){
		try{
			t.sleep(zz);
		}catch (InterruptedException e){
			System.out.println("Unable to sleep");
		}
	}
}

class frame extends Frame{
	front parent;
	Image image;
	Graphics pad;
	public frame(String s, front f){
		super(s);
		parent=f;
	}
	public void init(){
		setBackground(new Color(0xff,0xf5,0xdf));
		setSize(parent.wid+5,parent.wid+5);
		hide();
		
		
	}
	public void update(Graphics g){
		paint(g);
	}
	public void paint(Graphics g){
		image=createImage(parent.wid,parent.ht);
		pad=image.getGraphics();
		pad.drawImage(parent.selected,0,0,this);
		g.drawImage(image,0,0,this);
	}
}



class photos extends Canvas {	
	int n;
	Image image,temp;
	Graphics pad;
	front parent;

	
	public photos(front scr,int num){
		parent=scr;
		n=num;		
//		b=new bigger("Pankaj",parent,n);
	}
	public void init(){}

	public void paint(Graphics g){		
//		g.drawString("this works",15,15);
		image=createImage(parent.w,parent.h);	
		pad=image.getGraphics();
		pad.drawImage(parent.small[n],0,0,this);
		g.drawImage(image,0,0,this);
	}
	
	
	
	public void update(Graphics g){
			paint(g);
	}	
}


