/*!
 * gallery Library v0.1
 * http://codecanyon.net/user/AA-Team/portfolio
 *
 * Copyright 2011, Andrei Dinca
 *
 * Date: 17.03.2011
 */
(function( $ ){
    // Simple wrapper around jQuery animate to simplify animating options.progress from your app
    // Inputs: options.progress as a percent, Callback
    // TODO: Add options and jQuery UI support.
    $.fn.animateProgress = function(options, callback) { 
        
        return this.each(function() {
            
            var progress = options.progress;
            $(this).animate({
                width: options.progress + '%'
            }, {
                duration: options.duration, 
        
                // swing or linear
                easing: options.easing,

                // this gets called every step of the animation, and updates the label
                step: function( progress ){
                    var labelEl = $('.ui-label'),
                    valueEl = labelEl.find('.value');
          
                    if (Math.ceil(progress) < 20 && $('.ui-label', this).is(":visible")) {
                        labelEl.hide();
                    }else{
                        if (labelEl.is(":hidden")) {
                            labelEl.fadeIn();
                        };
                    }
                    if (Math.ceil(progress) == 100) {
                        labelEl.text('Done');
                        setTimeout(function() {
                            labelEl.fadeOut();
                        }, 1000);
                    }else{
                        valueEl.text(Math.ceil(progress) + '%');
                    }
                },
                complete: function(scope, i, elem) {
                    if (callback) {
                        callback.call(this, i, elem );
                    };
                }
            });
        });
    };
})( jQuery );
