function formatCurrency(num) {
num = num.toString().replace(/\$|\,/g,'');
if(isNaN(num))
num = "0";
sign = (num == (num = Math.abs(num)));
num = Math.floor(num*100+0.50000000001);
cents = num%100;
num = Math.floor(num/100).toString();
if(cents<10)
cents = "0" + cents;
for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
num = num.substring(0,num.length-(4*i+3))+','+
num.substring(num.length-(4*i+3));
return (((sign)?'':'-') + '$' + num + '.' + cents);
}

    function Browser() {

      var ua, s, i;

      this.isIE    = false;
      this.isNS    = false;
      this.version = null;

      ua = navigator.userAgent;

      s = "MSIE";
      if ((i = ua.indexOf(s)) >= 0) {
        this.isIE = true;
        this.version = parseFloat(ua.substr(i + s.length));
        return;
      }

      s = "Netscape6/";
      if ((i = ua.indexOf(s)) >= 0) {
        this.isNS = true;
        this.version = parseFloat(ua.substr(i + s.length));
        return;
      }

      // Treat any other "Gecko" browser as NS 6.1.

      s = "Gecko";
      if ((i = ua.indexOf(s)) >= 0) {
        this.isNS = true;
        this.version = 6.1;
        return;
      }
    }
    browser = new Browser(); 

DonationMeter.prototype._id = "";
DonationMeter.prototype._targetID = "";
DonationMeter.prototype._width = 233;
DonationMeter.prototype._height = 37;
DonationMeter.prototype._bgDivID = "";
DonationMeter.prototype._fgDivID = "";
DonationMeter.prototype._labelDivID = "";

DonationMeter.prototype._bgImage = "";
DonationMeter.prototype._fgImage = "url(Images/DonationIndicator/ForeGround.gif)";
DonationMeter.prototype._bgColor = "#a9bfd3";
DonationMeter.prototype._fgColor = "Green";
DonationMeter.prototype._fontFamily = "Verdana";
DonationMeter.prototype._fontSize = 18;
DonationMeter.prototype._fontWeight = "bold";
DonationMeter.prototype._fontColor = "#233052";

DonationMeter.prototype._targetAmount = 0;
DonationMeter.prototype._currentAmount = 0;
DonationMeter.prototype._updateDragMax = 2;

DonationMeter.prototype._x_correction = (browser.isIE ? 0 : 0);
DonationMeter.prototype._y_correction = ( browser.isIE ? 0 : -10 );

DonationMeter.prototype.Render = function() {
    
    var container = document.getElementById(this._targetID);
    container.style.width = this._width;
    container.style.height = this._height;
    
    var _bgDiv = document.createElement("div");
    var _fgDiv = document.createElement("div");
    var _labelDiv = document.createElement("div");

    _bgDiv.setAttribute("id", this._bgDivID);
    _fgDiv.setAttribute("id",this._fgDivID);
    _labelDiv.setAttribute("id",this._labelDivID);

    _bgDiv.style.position = 'absolute';
    _bgDiv.style.left = 0+this._x_correction+"px";
    _bgDiv.style.top = 0+this._y_correction+"px";
    _bgDiv.style.backgroundColor = this._bgColor;
    _bgDiv.style.width = this._width+"px";
    _bgDiv.style.height = this._height+"px";
    _bgDiv.style.zIndex = 100;

    _fgDiv.style.position = 'absolute';
    _fgDiv.style.left = 0+this._x_correction+"px";
    _fgDiv.style.top = 0+this._y_correction+"px";
    _fgDiv.style.backgroundColor = this._fgColor;
    _fgDiv.style.width = "1px";
    _fgDiv.style.height = this._height+"px";
    _fgDiv.style.zIndex = 101;
    _fgDiv.style.backgroundImage = this._fgImage;

    _labelDiv.style.position = 'absolute';
    _labelDiv.style.left = 0+this._x_correction+"px";
    _labelDiv.style.top = 0+this._y_correction+"px";
    _labelDiv.style.backgroundColor = "Transparent";
    _labelDiv.style.width = this._width+"px";
    _labelDiv.style.height = this._height+"px";
    _labelDiv.style.zIndex = 102;
    _labelDiv.style.textAlign = 'center';
    _labelDiv.style.paddingTop = ((this._height-this._fontSize)/2)+"px";
    _labelDiv.style.fontFamily = this._fontFamily;
    _labelDiv.style.color = this._fontColor;
    _labelDiv.style.fontSize = this._fontSize+"px";
    _labelDiv.style.fontWeight = this._fontWeight;    
    _labelDiv.innerHTML = "$0.00";

  
    container.appendChild( _bgDiv );
    container.appendChild( _fgDiv );  
    container.appendChild( _labelDiv );


}

DonationMeter.prototype._rollingAmount = 0;
DonationMeter.prototype._updateIncrement = 500;
DonationMeter.prototype._amountLeftToDisplay = -1;
DonationMeter.prototype._updateDrag = .1;
DonationMeter.prototype.Update = function() {

    var _labelDiv = document.getElementById(this._labelDivID);
    var _progressDiv = document.getElementById(this._fgDivID);
    if( this._amountLeftToDisplay == -1 ) this._amountLeftToDisplay = this._currentAmount;

    if( this._amountLeftToDisplay > 0 )
    {
        var _change = Math.floor(this._updateIncrement*this._updateDrag);
        var _amount = this._rollingAmount+_change;
        this._amountLeftToDisplay -= _change;
        this._rollingAmount += _change;

        var progressScale = (this._rollingAmount/this._currentAmount);
        var totalWidth = (this._currentAmount/this._targetAmount)*this._width;
        _progressDiv.style.width = (totalWidth*progressScale)+"px";

        if( this._updateDrag < this._updateDragMax )
            this._updateDrag *= 1.1;
        
        
        if( _amount >= this._currentAmount 
        || this._amountLeftToDisplay < 0 )
        {
            this._amountLeftToDisplay = 0;
            _amount = this._currentAmount;
        } 
//        _labelDiv.innerHTML = "$"+_amount+" / $" + this._targetAmount;
        _labelDiv.innerHTML = formatCurrency(_amount);
        
    }

}


function DonationMeter( TargetID, CurrentAmount, TargetAmount) {

    this._id = "_donation_meter_"+TargetID;
    this._bgDivID = this._id+"_bg";
    this._fgDivID = this._id+"_fg";
    this._labelDivID = this._id+"_label";
    this._targetID = TargetID;
    this._targetAmount = TargetAmount;
    this._currentAmount = CurrentAmount;
    
    
    if( this._targetAmount > 100000 )
        this._updateDragMax += Math.floor(this._targetAmount/100000);
    
    if( this._currentAmount > this._targetAmount )
        this._targetAmount = Math.floor( this._currentAmount*1.1);
};

