var Animation = Class.create({
    initialize: function(folder, prefix, placeholder, frames, params) {
        
        //set default options
        this.folder         =   folder || this.err('Error 1');
        this.placeholder    =   $(placeholder) || this.err('Error 2');
        this.frames         =   frames || this.err('Error 3');
        this.prefix         =   prefix || this.err('Error 4');

        //set params
        if (typeof params == 'undefined') {
            var params      =   {};
        }
        
        this.interval       =   params.interval || 0.05;
        this.extension      =   params.extension || '.png';
        this.onComplete     =   params.onComplete || function() { return true; };
        this.loaded         =   0;
        
        //initialize variables
        this.current        =   -1;
        this.executor       =   '';
        this.images         =   [];
        
        //start preloading
        this.preload();
    
    },

    err: function (msg) {
        
        throw msg; 
        return 0;
    
    },

    preload: function () {
        
        for (var i = 0; i < this.frames; i ++) {
            var file = this.prefix + (i + 1) + '.png';
            var style = 'none';
            var img = new Image();
            img.onload = this.addLoaded.bind(this);
            img.style.display = style;
            img.src = this.folder + file;
            this.images[i] = img;
        }       
    
    },

    addLoaded: function () {
        
        this.loaded++;
    
    },

    start: function () {

        this.setImages();
        if (this.current + 1 < this.images.length) {
            this.executor = new PeriodicalExecuter(this.animation.bind(this), this.interval);
        } else {
            this.err('Error 5');
        }
    
    },

    setImages: function () {
        
        for(var i = 0; i < this.images.length; i++) {
            this.images[i] = this.getObject(this.images[i]);
            this.placeholder.appendChild(this.images[i]);
        }

    },

    getObject: function ( obj ) {
        
        if(Prototype.Browser.IE){
            var img = "filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + obj.src  + "', sizingMethod='crop')";
            var width = 'width:' + obj.width;
            var height = 'height:' + obj.height;
            
            var div = new Element('div', {style: img + ';' + width  + ';' + height + ';'});    
            div.style.display = 'none';
            return div;
        } else {
            return obj;
        }

    },

    destroy: function () {
        
        for(var i = 0; i < this.images.length; i++) {
            this.placeholder.removeChild(this.images[i]);
        }
    
    },

    animation: function () {
        
        if(this.images[this.current]) {
            this.images[this.current].style.display = 'none';
        }
        this.current = this.current + 1;
        this.images[this.current].style.display = 'block';
        if (this.current + 1 == this.images.length) {
            this.executor.stop();
            this.onComplete(this);
        }
        
    },

    isLoaded: function () {
        if( this.loaded == this.images.length ) {
            return true;
        }
        if ( Prototype.Browser.Opera ) {
            var trigger = 0;
            for(var i = 0; i < this.images.length; i++) {
                if ( this.images[i].complete ) {
                    trigger++;
                }
            }
            if( trigger == this.images.length) {
                return true;
            }
        }
        return false;
        
    },

    checkStatus: function () {
        
        var status = 0;
        if( this.isLoaded ) {
            status = 1;
        }
        if( this.images[this.images.length - 1].style.display == 'block' ) {
            status = 2;
        }
        return status;

    }

});

