(function($) {
	//public functions
	var methods = {
		init : function(options) {
			var defaults = {
				data: [],
				graphCeil: 100,//top value of graph is 100
				graphFloor: 0, //bottom value of graph is 0
				graphUnit: false,
				dollars: false
			}, t;
			
			var opts = $.extend(defaults, options);
			opts.$bars = [];
			$.extend(this, opts);
			
			//calculate graph range only once
			this.graphRange = this.graphCeil-this.graphFloor;
						
			this.$bars = $(this).find(".graphBar");
						
			return this;
		},
		show : function(){			
			clearInterval(animationInterval);
			
			//shrink and then display for displaying
			this.$bars.css("height", 0);			
			$(this).show();
			
			
			var t = this;			
			startTime = new Date();			
			animationInterval = setInterval(function(){
				onInterval.apply(t);
			}, 30);			
		}
	},
	
	/*
		private vars
	*/
	
	//CONSTANTS
	ANIMATION_TIME = 1000, MAX_HEIGHT=300,
	
	//class vars
	animationInterval, startTime, nowTime;
	
	/*
		private functions
	*/
	function onInterval(){
		nowTime = new Date();
		var diffTime = nowTime-startTime, 
		percent = diffTime/ANIMATION_TIME;
		
		if(percent > 1){
			percent = 1;
			drawGraph.apply(this, [percent]);			
			clearInterval(animationInterval);
		}
		
		drawGraph.apply(this, [percent]);
	}
	
	//draw graphs at percentage height
	function drawGraph(percent){
		var i=0, newHeight, percentHeight, adjustedPercentHeight,$bar;
		for(i; i<this.$bars.length; i++){
			$bar = $(this.$bars[i]);
			//there are floors to these graphs, so make them look like it 
			percentHeight	= percent*this.data[i];
			adjustedPercentHeight = percentHeight - percent*this.graphFloor;
			//regulate by max height and the passed value for the max height
			newHeight		= MAX_HEIGHT*adjustedPercentHeight/(this.graphRange);
						
			$bar.css("height", newHeight);
			
			//display height is correct units
			switch(this.graphUnit){
				case "k":
					percentHeight /= 1000;
					percentHeight = percentHeight.toFixed(0);
					percentHeight += "K";
					break;
				case "m":
					percentHeight /= 1000000;
					percentHeight = percentHeight.toFixed(2);
					percentHeight += "M";
					break;
				default:
					percentHeight = percentHeight.toFixed(0);
					break;				
			}
			
			if(this.dollars){
				percentHeight = "$"+percentHeight;
			}
			$bar.find(".graphNumber").text(percentHeight);
		}
	}
	
	
	$.fn.barGraph = function(method) {
		//Method Calling Login
		if (methods[method]) {
			return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
		} else if (typeof method === 'object' || !method) {
			return methods.init.apply(this, arguments);
		} else {
			$.error('Method ' + method + ' does not exist on jQuery.');
		}
	};
})(jQuery);
