google.load("visualization", "1", {packages:["piechart"]});
google.load("visualization", "1", {packages:["AnnotatedTimeLine"]});

jQuery.fn.gauge = function(config) {

	return this.each(function() {
	
		
	    var data = new google.visualization.DataTable();
	    
        data.addColumn('string', 'Label');
        data.addColumn('number', 'Value');
        data.addRows(1);
        
        data.setValue(0, 0, '');
	    data.setValue(0, 1, config.percent);
	    	
        var chart = new google.visualization.Gauge($(this)[0]);
        var options = {width: 155, height: 155, redFrom: 98, redTo: 100,
            yellowFrom:85, yellowTo: 98, minorTicks: 5};           
        chart.draw(data, options);
        
        
        
      });
	
};

jQuery.fn.spendingpie = function(config) {

	return this.each(function() {
	
		
	    var data = new google.visualization.DataTable();
	    
        data.addColumn('string', 'Label');
        data.addColumn('number', 'Value');
        data.addRows(2);
        
        var remains = (100 - config.percent);
        data.setValue(0, 0, 'Spent');
	    data.setValue(0, 1, parseInt(config.amount));
	    data.setValue(1, 0, 'Remaining');
	    data.setValue(1, 1, parseInt(config.estimate));
	    
	    var formatter = new google.visualization.NumberFormat(
	    	      {prefix: '$', negativeColor: 'red', negativeParens: true});
	    formatter.format(data, 1); // Apply formatter to second column
	    
        var chart = new google.visualization.PieChart($(this)[0]);
        var options = {width: 260, height: 205, colors:['#e9840e','#019104'],legend:'none'};           
        chart.draw(data, options);
        
        
        
      });
	
};

jQuery.fn.visualization = function(config) {
	
	return this.each(function() {

		$(this).html('');
		var data = new google.visualization.DataTable();
		
	    data.addColumn('string', 'Group');
	    data.addColumn('number', 'Value');
	    data.addRows(config.data.length);
	    
	    for(var x=0;x<config.data.length;x++) {
	    	data.setValue(x, 0, config.data[x].label);
	    	data.setValue(x, 1, config.data[x].value);
	    }

	    if (config.disableformat!==true) {
		    var formatter = new google.visualization.NumberFormat(
		    	      {prefix: '$', negativeColor: 'red', negativeParens: true});
		    formatter.format(data, 1); // Apply formatter to second column
	    }
	    
	    var chart = new google.visualization.PieChart($(this)[0]);
	    
	    var width = 400;
	    if(typeof config.width != 'undefined') {
	    	width = config.width;
	    }
	    var height = 240;
	    if(typeof config.height != 'undefined') {
	    	height = config.height;
	    }
	    
	    var legend = 'right';
	    if(typeof config.height != 'undefined') {
	    	legend = config.legend;
	    }
	    
	    var legendFontSize = 10;
	    if(typeof config.legendFontSize != 'undefined') {
	    	legendFontSize = config.legendFontSize;
	    }
	    
	    var tooltipWidth = 120;
	    if(typeof config.tooltipWidth != 'undefined') {
	    	tooltipWidth = config.tooltipWidth;
	    }
	    
	    is3D = false;
	    if(typeof config.is3D != 'undefined') {
	    	is3D = config.is3D;
	    }
	    
	    chart.draw(data, {legend:legend,legendFontSize:legendFontSize,tooltipWidth:tooltipWidth,width: width, height: height, is3D: is3D,colors:config.colors, title: config.title});				

	});
	
};

jQuery.fn.chart = function(config) {

	  return this.each(function(){
		  
		  var source = 'http://chart.apis.google.com/chart?';
		  
		  source += 'chs='  + config.size;
		  source += '&cht=' + config.type;
		  	
		  // margins set
		  if(typeof config.margins != 'undefined') {
			  source += '&chma=' + config.margins;
		  }
		  
		  if(typeof config.transparent != 'undefined') {
			  //alert('transparent');
			  source += '&chf=a,s,eeeeeeff|c,s,ffffff00';
		  }
		  
		  if(typeof config.title !='undefined') {
			  source += '&chtt=' + config.title;
		  }
		  
		  // build data and labels and colors as available
		  var max = 0;
		  var min = 1;
		  for(var x=0;x<config.data.length;x++) {
			if(config.data[x].value>max) max = parseInt(config.data[x].value);
		  }
		  
		  var data = '';
		  var colors = '';
		  var labels = '';
		  var legend = '';
		  
		  for(var x=0;x<config.data.length;x++) {

			var value = parseInt(config.data[x].value);
			var percent = Math.round((value/max)*100);
			
			if(percent<1 && value>0) percent = 1;
			data += (data.length>0) ? ',' + percent : percent;
			if(typeof config.data[x].label != 'undefined') {
				labels += (labels.length>0) ? '|' + config.data[x].label : config.data[x].label;
			}
			if(typeof config.data[x].color != 'undefined') {
				colors += (colors.length>0) ? ',' + config.data[x].color : config.data[x].color;
			}
			if(typeof config.data[x].legend != 'undefined') {
				legend += (legend.length>0) ? '|' + config.data[x].legend : config.data[x].legend;
			}
		  }
		  
		  source += '&chd=t:' + data; 
		  if(labels.length>0) {
			  source += '&chl=' + labels;
		  }
		  if(colors.length>0) {
			  source += '&chco=' + colors;
		  }
		  if(legend.length>0) {
			  source += '&chdl=' + legend;
		  }
		  // if there is a legend
		  if(legend.length>0 && typeof config.legend_position != 'undefined') {
		      source += '&chdlp='  + config.legend_position
		  }
		  source += '&agent=jChart';
		  //document.location.href=source;
		  jQuery('<img>').attr("src",source).appendTo(this);
		  
		  
		  
		  
	  });
	};

	;(function(d){var k=d.scrollTo=function(a,i,e){d(window).scrollTo(a,i,e)};k.defaults={axis:'xy',duration:parseFloat(d.fn.jquery)>=1.3?0:1};k.window=function(a){return d(window)._scrollable()};d.fn._scrollable=function(){return this.map(function(){var a=this,i=!a.nodeName||d.inArray(a.nodeName.toLowerCase(),['iframe','#document','html','body'])!=-1;if(!i)return a;var e=(a.contentWindow||a).document||a.ownerDocument||a;return d.browser.safari||e.compatMode=='BackCompat'?e.body:e.documentElement})};d.fn.scrollTo=function(n,j,b){if(typeof j=='object'){b=j;j=0}if(typeof b=='function')b={onAfter:b};if(n=='max')n=9e9;b=d.extend({},k.defaults,b);j=j||b.speed||b.duration;b.queue=b.queue&&b.axis.length>1;if(b.queue)j/=2;b.offset=p(b.offset);b.over=p(b.over);return this._scrollable().each(function(){var q=this,r=d(q),f=n,s,g={},u=r.is('html,body');switch(typeof f){case'number':case'string':if(/^([+-]=)?\d+(\.\d+)?(px|%)?$/.test(f)){f=p(f);break}f=d(f,this);case'object':if(f.is||f.style)s=(f=d(f)).offset()}d.each(b.axis.split(''),function(a,i){var e=i=='x'?'Left':'Top',h=e.toLowerCase(),c='scroll'+e,l=q[c],m=k.max(q,i);if(s){g[c]=s[h]+(u?0:l-r.offset()[h]);if(b.margin){g[c]-=parseInt(f.css('margin'+e))||0;g[c]-=parseInt(f.css('border'+e+'Width'))||0}g[c]+=b.offset[h]||0;if(b.over[h])g[c]+=f[i=='x'?'width':'height']()*b.over[h]}else{var o=f[h];g[c]=o.slice&&o.slice(-1)=='%'?parseFloat(o)/100*m:o}if(/^\d+$/.test(g[c]))g[c]=g[c]<=0?0:Math.min(g[c],m);if(!a&&b.queue){if(l!=g[c])t(b.onAfterFirst);delete g[c]}});t(b.onAfter);function t(a){r.animate(g,j,b.easing,a&&function(){a.call(this,n,b)})}}).end()};k.max=function(a,i){var e=i=='x'?'Width':'Height',h='scroll'+e;if(!d(a).is('html,body'))return a[h]-d(a)[e.toLowerCase()]();var c='client'+e,l=a.ownerDocument.documentElement,m=a.ownerDocument.body;return Math.max(l[h],m[h])-Math.min(l[c],m[c])};function p(a){return typeof a=='object'?a:{top:a,left:a}}})(jQuery);	

