﻿//Created by: Damaris Zarco 3/3/2010
//Last updated by: Damaris Zarco 3/8/2010

// vars
var total = 0;
var current = 0;
var missing = 0;
var piece = 0;
var rounded = 0;

$(document).ready(function() {
    doMeterAjax();
});

function doMeterAjax() {
    $.ajax({
        url: '/pledgedrive/scripts/pledge.xml',
        type: 'GET',
        dataType: 'xml',
        //timeout: 1000,
        success: function(xml) {
            $(xml).find('pledge').each(function() {
                //create the array of images
                total = $(this).find('amount').text();
                current = $(this).find('current').text();
            }); //xml

            missing = total - current;
            piece = total / 4;
            rounded = (Math.round(piece) / 1000) * (1000);

            numbers();
            color();
        }, //success
        error: function() {
            alert("error loading");
        } //error function
    });    //ajax
} //doAjax

function numbers() {
    $("#numZero").html(0);
    $("#numOne").html(styleNumber(piece * 1));
    $("#numTwo").html(styleNumber(piece * 2));
    $("#numThree").html(styleNumber(piece * 3));
    $("#numFour").html(styleNumber(piece * 4));
}

function color() {
//    $("#leftOne").css({ 'background': 'white no-repeat scroll 0 0' });
    var proportion = current / piece;
    var mod = current % piece;
    var fullBoxes = Math.floor(proportion);
    var partialBox = Math.ceil(proportion);
        
    if (fullBoxes == 4) {
        $("#rightFour").css({ 'background': 'white no-repeat scroll 0 0' });
    }

    for (var i = 0; i < fullBoxes; i++) {
        $("#over" + (i + 1)).css({ 'background': 'white no-repeat scroll 0 0' });
    }

    if ((mod > 0) && (partialBox > 0)) {
        var width = 0;
        if ((partialBox == 2) || (partialBox == 3)) { 
		//alert("2 or 3");
            width = ((130 * mod) / piece);
            $("#over" + partialBox).css({ 'background': 'white no-repeat scroll 0 0', 'width': width + 'px' });
        }else{
			width = ((130 * mod) / piece);
			if (width >9){
            	width = ((130 * mod) / piece) - 9;
            	$("#over" + partialBox).css({ 'background': 'white no-repeat scroll 0 0', 'width': width + 'px' });
			}
        }
    }    
}
//<!-- Original:  Cyanide_7 (leo7278@hotmail.com) -->
//<!-- Web Site:  http://www7.ewebcity.com/cyanide7 -->
//<!-- Function below comes from http://javascript.internet.com -->

function styleNumber(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);
}
