var photos = new Array();
var photoset = new Array();

function Photo(url, title, link) {
    this.url = url;
    this.title = title;
    this.link = link;
    this.loaded = false;
}

function UpdateStatus(photo) {
    var completed = 0;
    var incompleted = 0;
    photo.loaded = true;
	
    jQuery.each(photos, function() {
        switch(this.loaded) {
            case true:
                completed++;
                break;
            case false:
                incompleted++;
                break;
        }
    });
    
    percentageComplete = (completed/photos.length) * 100;

    $("#slideshow_load a").text(percentageComplete.toFixed(0) + "% complete");

    if(completed == (photos.length - 1)) {
	DisplayPhotos();
    }
}

function Randomize() {
    return (Math.round(Math.random())-0.5);
}

function DisplayPhotos() {
    $('#slideshow_load').remove();
    $("#slideshow div").hide();

    $('#slideshow').cycle({
        fx: 'fade',
        next: '#nextPhoto a',
        prev: '#previousPhoto a'
    });
}


$(document).ready(function() {
    /* place loading gif on center of slideshow */    
    $("#slideshow_load").html("<p><a href=''>Loading...</a></p>");

    /* load images */

    var defaultURL = 'http://mt.spoonstein.com/photos/';

    photos = [
      new Photo('/media/slides/slide_0000_portland-zoo.psd.jpg', "Portland Zoo", "http://www.flickr.com/photos/spoonstein/sets/72157607327247704/"),
      new Photo('/media/slides/slide_0001_kbg-detail.psd.jpg', "Kanapaha Botanical Gardens Detail", "http://www.flickr.com/photos/spoonstein/sets/72157603629513649/"),
      new Photo('/media/slides/slide_0002_portlandzengarden.psd.jpg', "Portland Japanese Garden", "http://www.flickr.com/photos/spoonstein/sets/72157601104423192/"),
      new Photo('/media/slides/slide_0003_portlandrosegardens.psd.jpg', "Portland Rose Gardens", "http://www.flickr.com/photos/spoonstein/sets/72157601059754124/"),
      new Photo('/media/slides/slide_0004_portlandchinesegardens.psd.jpg', "Portland Classical Chinese Gardens", "http://www.flickr.com/photos/spoonstein/sets/72157600966734557/"),
      new Photo('/media/slides/slide_0005_portland.psd.jpg', "Portland", "http://www.flickr.com/photos/spoonstein/sets/72157600965547709/"),
      new Photo('/media/slides/slide_0006_staugustinealligatorfarm.psd.jpg', "St. Augustine Alligator Farm", "http://www.flickr.com/photos/spoonstein/sets/72157594566297419/"),
      new Photo('/media/slides/slide_0007_seaworld.psd.jpg', "Sea World", "http://www.flickr.com/photos/spoonstein/sets/72157594566294570/"),
      new Photo('/media/slides/slide_0008_kbg.psd.jpg', "Kanapaha Botanical Gardens", "http://www.flickr.com/photos/spoonstein/sets/72157594564828061/"),
      new Photo('/media/slides/slide_0009_sanfelasco.psd.jpg', "San Felasco State Park", "http://www.flickr.com/photos/spoonstein/sets/72157594566293983/"),
      new Photo('/media/slides/slide_0010_cincinnatti-zoo.psd.jpg', "Cincinnatti Zoo", "http://www.flickr.com/photos/spoonstein/sets/72157594564765399/"),
      new Photo('/media/slides/slide_0011_butterfly-rainforest.psd.jpg', "Butterfly Rainforest", "http://www.flickr.com/photos/spoonstein/sets/72157594564760226/"),
      new Photo('/media/slides/slide_0012_animals.psd.jpg', "Animals", "http://www.flickr.com/photos/spoonstein/sets/72157594564754757/"),
      new Photo('/media/slides/slide_0013_alpena.psd.jpg', "Alpena", "http://www.flickr.com/photos/spoonstein/sets/72157594564752768/"),
        ];

/*    photos.sort(Randomize);     */
    
    jQuery.each(photos, function(index, photo) {
        $("#slideshow").append("<div id='slide_" + index + "' class='photo'><h3><a href='" + this.link + "'>" + this.title + "</a></h3></div>");
        $("#slide_" + index).css("display","none")

        var slideContent = $("#slide_" + index + " h3");
        var image = new Image();
	
	$(image).load(function() {
	   $(slideContent).after(this);
   	   UpdateStatus(photo);
	}).error(function() {
	    console.log("Error loading image " + this.title)
	}).attr('src', this.url);
    });
    
});

