/*
 * 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 NigelCohen extends Sink implements ClickListener, LoadListener {

  private static final String[][] sImages = new String[][]{
	  new String[] {"Images/dsc_1484.jpg", "Burnham Beeches, Summer 2006"},
	  new String[] {"Images/dsc_1499.jpg", "Sunbathing, Summer 2006"},
	  new String[] {"Images/dsc_1513.jpg", "Water Lilly"},
	  new String[] {"Images/dsc_0911.jpg", "Amersham, UK"},
	  new String[] {"Images/dsc_2075.jpg", "Wedding baloons"},
	  new String[] {"Images/dsc_2176.jpg", "Getting married"},
	  new String[] {"Images/dsc_3362.jpg", "Tomato plant"},
	  new String[] {"Images/dsc_3502.jpg", "Guarec, France"},
	  new String[] {"Images/dsc_3511.jpg", "Misty Woods"},
	  new String[] {"Images/IMG_0518.JPG", "Playful dogs"},
	  new String[] {"Images/IMG_0543.JPG", "Burnham Beeches, Jan 2007"},
	  new String[] {"Images/cover.jpg", "Anything Goes : Jazz Band Cover"},
	  new String[] {"Images/dsc_3236.jpg", "Sue"},
	  new String[] {"Images/dsc_3208.3.jpg", "Leah"},
	  new String[] {"Images/dsc_2850.jpg", "Sarah"},
  };

  public static SinkInfo init() {
    return new SinkInfo("Nigel Cohen",
        "All images are copyright Nigel Cohen."
        ) {
      public Sink createInstance() {
        return new NigelCohen();
      }
    };
  }

  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 NigelCohen() {

	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]);
  }
}

