(function(d){var c=new Array;var e=new Array;d.fn.jqBarGraph=d.fn.jqbargraph=function(f){init=function(g){c[g.id]=d.extend({},d.fn.jqBarGraph.defaults,f);d(g).css({width:c[g.id].width,height:c[g.id].height,position:"relative","text-align":"center"});doGraph(g)};sum=function(g){total=0;for(val in g){total+=g[val]}return total.toFixed(2)};max=function(g){maxvalue=0;for(var h in g){value=g[h][0];if(value instanceof Array){value=sum(value)}if(parseFloat(value)>parseFloat(maxvalue)){maxvalue=value}}return maxvalue};maxMulti=function(g){maxvalue=0;maxvalue2=0;for(var j in g){ar2=g[j][0];for(var h in ar2){if(ar2[h]>maxvalue2){maxvalue2=ar2[h]}}if(maxvalue2>maxvalue){maxvalue=maxvalue2}}return maxvalue};doGraph=function(h){arr=c[h.id];data=arr.data;if(data==undefined){d(h).html("There is not enought data for graph");return}if(arr.sort=="asc"){data.sort(b)}if(arr.sort=="desc"){data.sort(a)}legend="";prefix=arr.prefix;postfix=arr.postfix;space=arr.barSpace;legendWidth=arr.legend?arr.legendWidth:0;fieldWidth=(d(h).width()-legendWidth)/data.length;totalHeight=d(h).height();var j=new Array();max=max(data);colPosition=0;for(var k in data){valueData=data[k][0];if(valueData instanceof Array){value=sum(valueData)}else{value=valueData}lbl=data[k][1];color=data[k][2];unique=k+h.id;if(color==undefined&&arr.colors==false){color=arr.color}if(arr.colors&&!color){colorsCounter=arr.colors.length;if(colorsCounter==colPosition){colPosition=0}color=arr.colors[colPosition];colPosition++}if(arr.type=="multi"){color="none"}if(lbl==undefined){lbl=arr.lbl}out="<div class='graphField"+h.id+"' id='graphField"+unique+"' style='position: absolute'>";out+="<div class='graphValue"+h.id+"' id='graphValue"+unique+"'>"+prefix+value+postfix+"</div>";out+="<div class='graphBar"+h.id+"' id='graphFieldBar"+unique+"' style='background-color:"+color+";position: relative; overflow: hidden;'></div>";if(!arr.legend||arr.legends){out+="<div class='graphLabel"+h.id+"' id='graphLabel"+unique+"'>"+lbl+"</div>"}out+="</div>";d(h).append(out);totalHeightBar=totalHeight-d(".graphLabel"+h.id).height()-d(".graphValue"+h.id).height();fieldHeight=(totalHeightBar*value)/max;d("#graphField"+unique).css({left:(fieldWidth)*k,width:fieldWidth-space,"margin-left":space});if(valueData instanceof Array){if(arr.type=="multi"){maxe=maxMulti(data);totalHeightBar=fieldHeight=totalHeight-d(".graphLabel"+h.id).height();d(".graphValue"+h.id).remove()}else{maxe=max}for(i in valueData){heig=totalHeightBar*valueData[i]/maxe;wid=parseInt((fieldWidth-space)/valueData.length);sv="";fs=0;if(arr.showValues){sv=arr.prefix+valueData[i]+arr.postfix;fs=12}o="<div class='subBars"+h.id+"' style='height:"+heig+"px; background-color: "+arr.colors[i]+"; left:"+wid*i+"px; color:"+arr.showValuesColor+"; font-size:"+fs+"px' >"+sv+"</div>";d("#graphFieldBar"+unique).prepend(o)}}if(arr.type=="multi"){d(".subBars"+h.id).css({width:wid,position:"absolute",bottom:0})}if(arr.position=="bottom"){d(".graphField"+h.id).css("bottom",0)}if(!arr.legends){j.push([color,lbl,h.id,unique])}if(arr.animate){d("#graphFieldBar"+unique).css({display:"none",height:0});d("#graphFieldBar"+unique).animate({height:fieldHeight},arr.speed*1000)}else{d("#graphFieldBar"+unique).css({height:fieldHeight})}}for(var g in arr.legends){j.push([arr.colors[g],arr.legends[g],h.id,g])}createLegend(j);if(arr.legend){d(h).append("<div id='legendHolder"+unique+"'></div>");d("#legendHolder"+unique).css({width:legendWidth,"float":"right","text-align":"left"});d("#legendHolder"+unique).append(legend);d(".legendBar"+h.id).css({"float":"left",margin:3,height:12,width:20,"font-size":0})}if(arr.title){d(h).wrap("<div id='graphHolder"+unique+"'></div>");d("#graphHolder"+unique).prepend(arr.title).css({width:arr.width+"px","text-align":"center"})}};createLegend=function(g){legend="";for(var h in g){legend+="<div id='legend"+g[h][3]+"' style='overflow: hidden; zoom: 1;'>";legend+="<div class='legendBar"+g[h][2]+"' id='legendColor"+g[h][3]+"' style='background-color:"+g[h][0]+"'></div>";legend+="<div class='legendLabel"+g[h][2]+"' id='graphLabel"+unique+"'>"+g[h][1]+"</div>";legend+="</div>"}};this.each(function(){init(this)})};d.fn.jqBarGraph.defaults={barSpace:3,width:334,height:200,color:"#000000",colors:false,lbl:"",sort:false,position:"bottom",prefix:"",postfix:"",animate:true,speed:1.5,legendWidth:100,legend:false,legends:false,type:false,showValues:true,showValuesColor:"#fff",title:false};function b(g,f){if(g[0]<f[0]){return -1}if(g[0]>f[0]){return 1}return 0}function a(g,f){if(g[0]>f[0]){return -1}if(g[0]<f[0]){return 1}return 0}})(jQuery);
