/*
 * jQuery Progress Bar plugin
 * Version 2.0 (06/22/2009)
 * @requires jQuery v1.2.1 or later
 *
 * Copyright (c) 2008 Gary Teo
 * http://t.wits.sg
 
 * Modified by Eric Folley, Plus Three LLP
 * efolley@plusthree.com

*/

(function($) {
    $.fn.progressBar = function(options) {

        return this.each(function() {
            $this = $(this);
            var config;
            if ($.meta) {
                config = $.extend({}, $.fn.progressBar.defaults, options, $this.data());
            } else {
                config = $.extend({}, $.fn.progressBar.defaults, options);
            }
            
            config.runningValue = 0;
            config.increment = Math.round(config.value / config.steps);
            if (config.increment < 1) {
                config.increment = 1;
            }
            
            if (config.orientation == 'horizontal') {
                config.pixels  = config.width / 100;
            } else if (config.orientation == 'vertical') {
                config.pixels = config.height / 100;
            }

            config.barTxt = getBarText(config);
            
            var $scale, $txt;
            
            $scale = $('<div/>').attr('id', 'scale').css('height', config.height).css('width', config.width).css('background-image', 'url(' + config.barImage + ')').css('background-position', '-8888px 0');
            $this.append($scale);            
            
            if (config.showText) {
                $txt = $('<div/>').attr('id', 'scaletxt');
                $txt.text(config.barTxt);
                $this.append($txt);
            }
            
            var t = setInterval(function() {
                
                if (config.runningValue > config.value) {
                    if (config.runningValue - config.increment  < config.value) {
                        config.runningValue = config.value;
                    } else {
                        config.runningValue -= config.increment;
                    }
                } else if (config.runningValue < config.value) {
                    if (config.runningValue + config.increment  > config.value) {
                        config.runningValue = config.value;
                    } else {
                        config.runningValue += config.increment;
                    }
                }
                
                if (config.runningValue == config.value) {
                    clearInterval(t);
                }
                
                if (config.orientation == 'horizontal') {
                    $scale.css("background-position", ((config.width * -1) + (getPercentage(config) * config.pixels)) + 'px 50%');
                } else if (config.orientation == 'vertical') {
                    $scale.css("background-position", '50% ' + -((config.height * -1) + (getPercentage(config) * config.pixels)) + 'px');
                }
                
                config.barTxt = getBarText(config);
                if (config.showText) {
                    $txt.text(config.barTxt);
                }
                
                if (config.callback != null && typeof(config.callback) == 'function') {
                    config.callback(config);
                }
            }, config.stepDuration);
            
        });
    };
    
    // expose defaults
    // default values are based on the community fundraising thermometer
    $.fn.progressBar.defaults = {
        value : 0,
        steps : 50,
        stepDuration : 20,
        max : 100,
        showText : true,
        textFormat : 'percentage', // or 'fraction'
        width : 235,
        height: 315,
        orientation : 'vertical', // or 'horizontal
        barImage : '/images/thermometer_scale.gif',
        callback : null
    };
    
    // private functions
    
    function getBarText(config) {
        if (config.showText) {
            if (config.textFormat == 'percentage') {
                return Math.round(config.runningValue) + "%";
            } else if (config.textFormat == 'fraction') {
                return config.runningValue + '/' + config.max;
            }
        } else {
            return '';
        }
    }

    function getPercentage(config) {
        return config.runningValue * 100 / config.max;
    }
    
    function debug(txt) {
        if (window.console && window.console.log) {
            window.console.log(txt);
        }
    }

})(jQuery);