/*
 * Copyright 2006 Google Inc.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.sduk.client;

import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.LoadListener;
import com.google.gwt.user.client.ui.VerticalPanel;

/**
 * Demonstrates {@link com.google.gwt.user.client.ui.Image}.
 */
public class TomJonesBerney extends Sink implements ClickListener, LoadListener {

  private static final String[][] sImages = new String[][]{
	  new String[] {"Images/tjb.doodleface.jpg", "Doodleface"},
	  new String[] {"Images/tjb.trip_3.jpg", "Trip 3"},
	  new String[] {"Images/tjb.crying.jpg", "Crying"},
	  new String[] {"Images/tjb.JACK_FROST.jpg", "Jack Frost"},
	  new String[] {"Images/tjb.goodhead.jpg", "Goodhead"},
	  new String[] {"Images/tjb.hand.jpg", "Hand"},
	  new String[] {"Images/tjb.3_face.jpg", "3 faces"},
	  new String[] {"Images/tjb.me.jpg", "Me"},
	  new String[] {"Images/tjb.la_bete.jpg", "La Bete"},
	  new String[] {"Images/tjb.bruce_3.jpg", "Bruce 3"},
	  new String[] {"Images/tjb.farm.jpg", "Farm"},
	  new String[] {"Images/tjb.whatgornon.jpg", "Whatgornon"},
  };

  public static SinkInfo init() {
    return new SinkInfo("Tom Jones Berney",
        "Tom Jones Berney's drawings can be purchased from <a href=\"http://www.tomjonesberney.co.uk\">"
    	+ "www.TomJonesBerney.co.uk</a> where there is a far wider selection of his drawings."
        ) {
      public Sink createInstance() {
        return new TomJonesBerney();
      }
    };
  }

  private int curImage;
  private Image imageCentral = new Image();
  private HTML htmlTitleCentral = new HTML("", true);

  private Image loadingImage = new Image("Images/blanksearching.gif");
  private Image nextButton = new Image("Images/forward.gif");
  private Image prevButton = new Image("Images/back.gif");

  public TomJonesBerney() {

	imageCentral.addLoadListener(this);
    imageCentral.setStyleName("ks-images-Image");
    
    nextButton.addClickListener(this);
    nextButton.setStyleName("ks-images-Button");

    prevButton.addClickListener(this);
    prevButton.setStyleName("ks-images-Button");

    DockPanel topPanel = new DockPanel();
    topPanel.setVerticalAlignment(DockPanel.ALIGN_MIDDLE);
    topPanel.add(prevButton, DockPanel.WEST);
    topPanel.add(nextButton, DockPanel.EAST);
    topPanel.add(loadingImage, DockPanel.CENTER);

    VerticalPanel panel = new VerticalPanel();
    panel.setHorizontalAlignment(VerticalPanel.ALIGN_CENTER);
    panel.setWidth("100%");

    panel.add( htmlTitleCentral );
    panel.add(topPanel);
    panel.add(imageCentral);


    initWidget(panel);

    loadImage(0);
    
  }

  public void onClick(Widget sender) {
    if (sender == prevButton) {
      loadImage(curImage - 1);
    } else if (sender == nextButton) {
      loadImage(curImage + 1);
    }
  }

  public void onError(Widget sender) {
  }

  public void onLoad(Widget sender) {
    loadingImage.setUrl("Images/blanksearching.gif");
  }

  public void onShow() {
  }

  private void loadImage(int index) {
    if (index < 0) {
      index = sImages.length - 1;
    } else if (index > sImages.length - 1) {
      index = 0;
    }

    curImage = index;
    loadingImage.setUrl("Images/searching.gif");
    htmlTitleCentral.setHTML("<h2>" + sImages[curImage][1] + "</h2>");
    imageCentral.setUrl(sImages[curImage][0]);
  }
}

