/**
 * Time-lapse stitcher
 * @author Fontaine Shu fontaineshu+web@gmail.com
 * @version 1.0
 */

var totalImageCount = 45;
var currentImage = 1;
var imageOffset = 9586;
var imgAssets = null;

var transitionImg = function(index) {
    try {
        if(index < totalImageCount) {
            var transitionfx = new Fx.Tween(imgAssets[index], {
                property: 'opacity',
                duration: '600',
                transition: 'linear'
            });
            transitionfx.start(1,0).chain(function() {
                transitionImg(index+1);  
            });
        }
        else {
            var transitionfx = new Fx.Tween(imgAssets[0], {
                property: 'opacity',
                duration: '600',
                transition: 'linear'
            });
            transitionfx.start(0,1).chain(function() {
                imgAssets.each(function(el, i) {
                    el.setStyle('opacity', 1);
                });
                transitionImg(0);

            });
        }
        
        transitionfx = null;
    }
    catch(e) {
        if(console) {
            console.log(e);   
        }
    }
};

window.addEvent('domready', function() {
    // attach info event
    $('showInfo').addEvent('click', function() {
        if($('info').hasClass('closed')) {
            $('info').tween('top', 0);
            $('info').removeClass('closed');
            this.set('html', '-');
        }
        else {
            $('info').tween('top', -157);
            $('info').addClass('closed');
            this.set('html', '+');
        }
    });
    
    // create array
    var imgSources = new Array();
    for(var i = 0; i < totalImageCount; i++) {
        imgSources[i] = '/img/timelapse/IMG_' + (imageOffset + i) + '.jpg';
    }
    
    // create images
    imgAssets = new Asset.images(imgSources, {
        onProgress: function(counter, index) {
            this.setStyle('opacity', 1);
            this.setStyle('z-index', 51-index);
        },
        
        onComplete: function() {
            $('photoHolder').set('html', '');
            $('photoHolder').adopt(imgAssets);
            transitionImg(0);
        }
    });
});
