(function($){
    $.annualfund = function(el, options, data){
        var base = this;
        
        base.$el = $(el);
        base.el = el;
        
        base.$el.data("annualfund", base);
        
        base.init = function(){
            base.options = $.extend({},$.annualfund.defaultOptions, options);
            data = "";
            base.options.id = base.$el.attr("AFid");
            if(base.options.id.length > 0){
            	willow.getAnnualFund(base.options.id,function(d){
					data = d;
					$("#afTmpl").tmpl(data,{
						money : base.dollarAmount,
						stillNeed : base.stillNeed,
						underGoal : base.underGoal
					}).appendTo(".annualFund");
					base.setBar();
				}); 
            }      
        };
        
        base.setBar = function(){
        	var $bar = base.$el.children(".annual-fund").find(".bar");
			// parse the strings into integers so comparison works properly  
        	if(parseInt(data.af.current) <= parseInt(data.af.goal)){
        		var midHeight = Math.round(base.options.bar_height * (data.af.current/data.af.goal));
        		$bar.height(midHeight).css("margin-top",base.options.bar_height-midHeight);
				// IE does not like a height of 0
				if($.browser.msie && midHeight === 0){
					$bar.css("visibility","hidden");
				}
        	}else{
				$bar.height(base.options.bar_height);
        	}
        }
		
		base.underGoal = function(g, c){
			if(parseInt(c) < parseInt(g)){
				return true;}
			else{return false;}
		}
		
		base.stillNeed = function(g, c){
			var sn = parseInt(g) - parseInt(c);
			sn = sn+""; // convert to string so dollarAmount function works properly
			return sn;
		}
		
        base.checkNum = function(data){ 
			var valid = "0123456789.";
			var ok = 1; var checktemp;
			for (var i=0; i<data.length; i++) {
				checktemp = "" + data.substring(i, i+1);
				if (valid.indexOf(checktemp) == "-1") return 0;}
				return 1;
		}
        
        base.dollarAmount = function(Num) {
			dec = Num.indexOf(".");
			end = ((dec > -1) ? "" + Num.substring(dec,Num.length) : ".00");
			Num = "" + parseInt(Num);
			var temp1 = "";
			var temp2 = "";
			if (base.checkNum(Num) == 0) {
				alert("This does not appear to be a valid number.  Please try again.");
			}else{ 
				if (end.length == 2) end += "0";
				if (end.length == 1) end += "00";
				if (end == "") end += ".00";
				var count = 0;
				for (var k = Num.length-1; k >= 0; k--) {
					var oneChar = Num.charAt(k);
					if (count == 3) {
						temp1 += ",";
						temp1 += oneChar;
						count = 1;
						continue;
					}else{
						temp1 += oneChar;
						count ++;
					}
				}
				for (var k = temp1.length-1; k >= 0; k--) {
					var oneChar = temp1.charAt(k);
					temp2 += oneChar;
				}
				temp2 = "$" + temp2;// + end;
				return temp2;
			}
		}
        
        base.init();
    };
    
    $.annualfund.defaultOptions = {
    	bar_height: 235
    };
    
    $.fn.annualfund = function(data,options){
        return this.each(function(){
            (new $.annualfund(this, options, data));
        });
    };
    
})(jQuery);
