/**
 *
 */
var Slideshow = {
    photos : [],
    
    init : function(){
        this.current = 0;
        this.initNav();
    },
    
    initPhotos : function(){
        var photos = Slideshow.photos;
        return photos;
    },
    
    initNav : function(){
        $D.removeClass( "photo-credits", "hidden" );

        var navPrev = $D.get( "nav-prev" ).firstChild;
        var navNext = $D.get( "nav-next" ).firstChild;

        $E.on( navPrev, "click", this.prevPhoto, this, true );
        $E.on( navNext, "click", this.nextPhoto, this, true );
        this.checkLimits();
    },
    
    displayPhoto : function( photo, metadata ){
        $D.get( 'slideshow' ).innerHTML = '<img src="' + photo.src + '" width="' + photo.width + '" height="' + photo.height + '" />';
        $D.get( 'photo-credits' ).innerHTML = metadata.title;

        this.fadeIn( 'slideshow' );

        this.checkLimits();
    },
    
    fadeIn : function( el ){
        // fade in
        var fadeIn = new $A( el, {
            opacity: { from: 0, to: 1 }
        }, 0.5 );
        fadeIn.animate();
    },
    
    fadeOut : function( el ){
        // fade out
        var fadeOut = new $A( el, {
            opacity: { from: 1, to: 0 }
        }, 0.5, YAHOO.util.Easing.easeIn );

        return fadeOut;
    },
    
    loadPhoto : function( photoObj ){
        var self = this;
        var fo = this.fadeOut( 'slideshow' );
        fo.onComplete.subscribe( function(){
            // display "loading" spinner
            $D.get( "slideshow-loader" ).style.display = "block";

            // create image object and set the onload
            // handler to hide loader, and display the
            // photo
            var preload = new Image();
            preload.onload = function(){
                $D.get( "slideshow-loader" ).style.display = "none";
                self.displayPhoto( preload, photoObj );
                // clear onload handler (for IE)
                preload.onload = function(){};
            };
            preload.src = photoObj.url;

        } );
        fo.animate();
    },
    
    nextPhoto : function( e ){
        $E.preventDefault( e );

        if ( this.current >= this.photos.length - 1 ){
            // disable next link
            // console.log( "disable next link" );
            return;
        }

        ++this.current;
        this.loadPhoto( this.photos[ this.current ] );
    },
    
    prevPhoto : function( e ){
        $E.preventDefault( e );

        if ( this.current <= 0 ){
            // disable next link
            // console.log( "disable prev link" );
            return;
        }

        --this.current;
        this.loadPhoto( this.photos[ this.current ] );
    },
    
    checkLimits : function(){
        if ( this.current <= 0 ){
            $D.addClass( "nav-prev", "disabled" );
        } else {
            $D.removeClass( "nav-prev", "disabled" );
        }

        if ( this.current >= this.photos.length - 1 ){
            $D.addClass( "nav-next", "disabled" );
        } else {
            $D.removeClass( "nav-next", "disabled" );
        }
    }
};

// Convenience shortcuts
$D = YAHOO.util.Dom;
$E = YAHOO.util.Event;
$A = YAHOO.util.Anim;

$E.onAvailable( "narration", Slideshow.init, Slideshow, true );