/*
 ### jQuery XML to JSON Plugin v1.0 - 2008-07-01 ###
 * http://www.fyneworks.com/ - diego@fyneworks.com
 * Dual licensed under the MIT and GPL licenses:
 *   http://www.opensource.org/licenses/mit-license.php
 *   http://www.gnu.org/licenses/gpl.html
 ###
 Website: http://www.fyneworks.com/jquery/xml-to-json/
*/
eval(function(p,a,c,k,e,r){e=function(c){return(c<a?'':e(parseInt(c/a)))+((c=c%a)>35?String.fromCharCode(c+29):c.toString(36))};if(!''.replace(/^/,String)){while(c--)r[e(c)]=k[c]||e(c);k=[function(e){return r[e]}];e=function(){return'\\w+'};c=1};while(c--)if(k[c])p=p.replace(new RegExp('\\b'+e(c)+'\\b','g'),k[c]);return p}(';5(10.M)(w($){$.N({11:w(j,k){5(!j)t{};w B(d,e){5(!d)t y;6 f=\'\',2=y,E=y;6 g=d.x,12=l(d.O||d.P);6 h=d.v||d.F||\'\';5(d.G){5(d.G.7>0){$.Q(d.G,w(n,a){6 b=a.x,u=l(a.O||a.P);6 c=a.v||a.F||\'\';5(b==8){t}z 5(b==3||b==4||!u){5(c.13(/^\\s+$/)){t};f+=c.H(/^\\s+/,\'\').H(/\\s+$/,\'\')}z{2=2||{};5(2[u]){5(!2[u].7)2[u]=p(2[u]);2[u][2[u].7]=B(a,R);2[u].7=2[u].7}z{2[u]=B(a)}}})}};5(d.I){5(d.I.7>0){E={};2=2||{};$.Q(d.I,w(a,b){6 c=l(b.14),C=b.15;E[c]=C;5(2[c]){5(!2[c].7)2[c]=p(2[c]);2[c][2[c].7]=C;2[c].7=2[c].7}z{2[c]=C}})}};5(2){2=$.N((f!=\'\'?A J(f):{}),2||{});f=(2.v)?(D(2.v)==\'16\'?2.v:[2.v||\'\']).17([f]):f;5(f)2.v=f;f=\'\'};6 i=2||f;5(k){5(f)i={};f=i.v||f||\'\';5(f)i.v=f;5(!e)i=p(i)};t i};6 l=w(s){t J(s||\'\').H(/-/g,"18")};6 m=w(s){t(D s=="19")||J((s&&D s=="K")?s:\'\').1a(/^((-)?([0-9]*)((\\.{0,1})([0-9]+))?$)/)};6 p=w(o){5(!o.7)o=[o];o.7=o.7;t o};5(D j==\'K\')j=$.S(j);5(!j.x)t;5(j.x==3||j.x==4)t j.F;6 q=(j.x==9)?j.1b:j;6 r=B(q,R);j=y;q=y;t r},S:w(a){6 b;T{6 c=($.U.V)?A 1c("1d.1e"):A 1f();c.1g=W}X(e){Y A L("Z 1h 1i 1j 1k 1l")};T{5($.U.V)b=(c.1m(a))?c:W;z b=c.1n(a,"v/1o")}X(e){Y A L("L 1p Z K")};t b}})})(M);',62,88,'||obj|||if|var|length||||||||||||||||||||||return|cnn|text|function|nodeType|null|else|new|parseXML|atv|typeof|att|nodeValue|childNodes|replace|attributes|String|string|Error|jQuery|extend|localName|nodeName|each|true|text2xml|try|browser|msie|false|catch|throw|XML|window|xml2json|nn|match|name|value|object|concat|_|number|test|documentElement|ActiveXObject|Microsoft|XMLDOM|DOMParser|async|Parser|could|not|be|instantiated|loadXML|parseFromString|xml|parsing'.split('|'),0,{}))

/*
 * 
 * jQuery Google Charts plugin 0.9
 * 
 * $Date: 2009-02-19 11:56:22 +0100 (gio, 19 feb 2009) $
 * $Rev: 46 $
 * 
 * @requires
 * Sugar Arrays - Dustin Diaz | http://www.dustindiaz.com
 * 
 * Copyright (c) 2008 Massimiliano Balestrieri
 * Examples and docs at: http://maxb.net/blog/
 * Licensed GPL licenses:
 * http://www.gnu.org/licenses/gpl.html
 *
 */

eval(function(p,a,c,k,e,r){e=function(c){return(c<62?'':e(parseInt(c/62)))+((c=c%62)>35?String.fromCharCode(c+29):c.toString(36))};if('0'.replace(0,e)==0){while(c--)r[e(c)]=k[c];k=[function(e){return r[e]||e}];e=function(){return'([2-9]|[12]\\w)'};c=1};while(c--)if(k[c])p=p.replace(new RegExp('\\b'+e(c)+'\\b','g'),k[c]);return p}('3(!18.13)13={};13.1h=4(){2 m=0;2 k=0;2 l=0;2 q=0;2 bc="http://1I.apis.google.com/1I?";2 bd={19:"cht",1i:"chs",1c:"chd",1j:"chco",1K:"chds",1k:"chxt",1L:"chxr",1l:"chxl",1m:"chdl",1n:"chbh",1M:"chf",1o:"chm",1p:"chtt",1N:"chts",1q:"chg",1O:"chls",1w:"1w"};2 O=7;2 u=7;2 be=7;2 n=7;2 P=7;2 s=7;2 Q=7;2 h="1P";2 R="300x200";2 v=7;2 w=7;2 g=7;2 r=7;2 o=[];2 x=1;2 S="x,y";2 i=7;2 j=7;2 y=7;2 z=7;2 T="1x";2 U="1x";2 A=90;2 B=90;2 C=10;2 D=10;2 E=7;2 F=7;2 V=7;2 W=7;2 t=7;2 X=20;2 G=1;2 Y=7;2 H=10;2 I=10;2 J=10;2 K=0;2 L=7;2 bf=["5131C9","FFCC00","DA1B1B","FF9900","FF6600","CCFFFF","CCFF00","CCCCCC","FF99CC","999900","999999","66FF00","66CC00","669900","660099","33CC00","333399","000000"];2 Z=\'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789\';4 bg(a,b){2 c=[];9(2 d=0;d<a.8;d++){2 e=a[d];3(!isNaN(e)&&e>=0){c.11(Z.charAt(Math.round((Z.8-1)*e/b)))}1d{c.11(\'_\')}}5 c.1S(\'\')}4 bh(){2 a=X;3(G)a+=","+G;5 a}4 bi(){3(!r.1e==12)1y 1s 1z("Legend 1V be 12");2 a="";9(2 b=0;b<r.8;b++)a+=r[b]+"|";a=p(a,"|");5 a}4 bj(){3(!g.1e==12)1y 1s 1z("1X 1V be 12");2 a=\'\';2 b=[];9(2 c=0;c<g.8;c++){3(g[c].1e!=12)g[c]=[g[c]];9(2 d=0;d<g[c].8;d++){3(!b[d])b[d]=[];b[d].11((g[c][d]));3(b[d].8>k){k=b[d].8;m=g[c].8}}3(O){M(1A(g[c]));N(1A(g[c]))}1d{M(g[c]);N(g[c])}}3(n&&t)b=bk(b);3(n&&W)b=bl(b);9(2 d=0;d<b.8;d++){a+=bg(b[d],l)+","}a=p(a,",");5"s:"+a}4 bl(a){2 b=[];9(2 c=1;c<=k;c++)b.11(0);a.11(b);5 a}4 bk(a){2 b=[];9(2 c=1;c<=k;c++)b.11(l);a.unshift(b);5 a}4 ba(){2 a="";2 b="";2 c=m;3(n&&t)c++;9(2 d=0;d<c;d++){b=1t[d]||bf[d];a+=b+","}a=p(a,",");5 a}4 bm(){5 q+","+l}4 M(a){3(a.1e==12)a.1a(4(nr){M(nr)});1d 3(l<a)l=a}4 N(a){3(a.1e==12)a.1a(4(nr){N(nr)});1d 3(q>a)q=a}4 bn(){5"0,"+q+","+l+"|1,"+q+","+l}4 bo(){2 a="";3(o.8==0&&k>10)x=17(k/10);3(u){2 b=[];9(2 c=o.8;c>0;c--)b.11(o[(c-1)]);o=b}9(2 c=0;c<k;c++){2 d=(c%x)==0?(o[c]||c):\'\';a+=d+"|"}a=p(a,"|");2 e=(u)?1:0;5 e+":|"+a}4 bp(){2 a=bb(T);2 b=bb(U);2 c="";3(i&&E)i+=17(E);3(i&&a=="s")c="bg,s,"+i;3(i&&a=="lg")c="bg,lg,"+A+","+i+",0,"+y+",1";3(i&&a=="ls")c="bg,ls,"+A+","+i+",0."+17(C/10)+","+y+",0."+17(C/10);3(j&&F)j+=17(F);3(j&&i)c+=\'|\';3(j&&b=="s")c+="c,s,"+j;3(j&&b=="lg")c+="c,lg,"+B+","+j+",0,"+z+",1";3(j&&b=="ls")c+="c,ls,"+B+","+j+",0."+17(D/10)+","+z+",0."+17(D/10);5 c}4 bb(a){3(a=="1x")5"s";3(a=="gradient")5"lg";3(a=="stripes")5"ls";5 a}4 bq(){2 c=[];2 d=\'\';3(n){c=ba(1t,t).split(",");c.1a(4(a,b){d+=\'b,\'+a+\',\'+b+\',\'+(b+1)+\',0|\'});d=p(d,"|")}5 d}4 br(){2 a=\'\';3(I>=0)a+=I;3(H>=0)a+=","+H;3(J>=0)a+=","+J;3(K>=0)a+=","+K;5 a}4 bs(){2 b="";L.1a(4(a){b+=a.1S(",")+"|"});b=p(b,"|");5 b}4 bt(a){3(13.1h.19.1b(a.19)!==-1)h=a.19;3(a.1i)R=a.1i;3(a.1c)g=a.1c;3(a.1m)r=a.1m;3(a.1l)o=a.1l;3(a.29)x=a.29;3(a.1j)1t=a.1j;1d 1t=[];3(a.2a)L=a.2a;3(a.1p)v=a.1p;3(a.2b&&a.2c)w=a.2b+","+a.2c;3(a.1n)X=a.1n;3(a.2d>=0)G=a.2d;3(a.1o)V=a.1o;3(a.2e)W=a.2e;3(a.2f)t=a.2f;3(a.1k)S=a.1k;3(a.bg)i=a.bg;3(a.2g)T=a.2g;3(a.2h)y=a.2h;3(a.2i)C=a.2i;3(a.2j>=0)A=a.2j;3(a.2k)E=a.2k;3(a.2l)j=a.2l;3(a.2m)U=a.2m;3(a.2n)z=a.2n;3(a.2o)D=a.2o;3(a.2p>=0)B=a.2p;3(a.2q)F=a.2q;3(a.1q){Y=a.1q;3(a.2r>=0)I=a.2r;3(a.2s>=0)H=a.2s;3(a.2t>=0)J=a.2t;3(a.2u>=0)K=a.2u}}4 f(a,b,c){2 d=c?"":"&";5 bd[a]+"="+b+d}4 bu(){be=h.1b("v")!==-1;u=h.1b("h")!==-1;O=h.1b("s")!==-1&&h!="ls"&&h!="lc";s=(h=="ls"||h=="lc");Q=(h=="p"||h=="p3");n=s&&V;P=h.1b("b")!==-1}4 p(a,b){5(a.1E(b)!==-1)?a.substr(0,a.1E(b)):a}5{2x:4(a){2 b=bc;bt(a);bu();b+=f("19",h);b+=f("1i",R);3(v)b+=f("1p",v);3(w)b+=f("1N",w);3(P){b+=f("1n",bh())}b+=f("1k",S);3(!Q&&r.8>0)b+=f("1m",bi());b+=f("1c",bj());b+=f("1K",bm());b+=f("1L",bn());b+=f("1l",bo());b+=f("1M",bp());b+=f("1j",ba());3(s&&L)b+=f("1O",bs());3(Y)b+=f("1q",br());3(s&&n)b+=f("1o",bq());b+=f("1w","2y",1F);5 b}}};13.1h.19=["bhs","bvs","bhg","1P","lc","ls","p","p3"];4 1A(c){5 2z(c,0,4(a,b){5 a+b})}4 2z(a,b,c){9(2 d=0;d<a.8;d++){b=c(b,a[d])}5 b}Function.1G.15=4(a,b){6.1G[a]=b;5 6};3(!12.1G.1a){12.15(\'1a\',4(a,b){2 c=b||18;9(2 d=0,e=6.8;d<e;++d){a.1g(c,6[d],d,6)}}).15(\'every\',4(a,b){2 c=b||18;9(2 d=0,e=6.8;d<e;++d){3(!a.1g(c,6[d],d,6)){5 7}}5 1F}).15(\'some\',4(a,b){2 c=b||18;9(2 d=0,e=6.8;d<e;++d){3(a.1g(c,6[d],d,6)){5 1F}}5 7}).15(\'map\',4(a,b){2 c=b||18;2 d=[];9(2 e=0,m=6.8;e<m;++e){d.11(a.1g(c,6[e],e,6))}5 d}).15(\'filter\',4(a,b){2 c=b||18;2 d=[];9(2 e=0,m=6.8;e<m;++e){3(!a.1g(c,6[e],e,6)){continue}d.11(6[e])}5 d}).15(\'1b\',4(a,b){2 b=b||0;9(2 c=b,d=6.8;c<d;++c){3(6[c]===a){5 c}}5-1}).15(\'1E\',4(a,b){2 b=b||6.8;3(b>=6.8){b=6.8}3(b<0){b=6.8+b}9(2 c=b;c>=0;--c){3(6[c]===a){5 c}}5-1})}13.2A={2B:4(c){c=1v.extend({},c);5 6.each(4(){3(!c.1c)1y 1s 1z("No 1X");2 a=1s 13.1h();2 b=a.2x(c);1v(\'<img>\').attr("src",b).2C(6);1v(\'<p>\'+b+\'</p>\').2C(6)})}};1v.fn.2y=13.2A.2B;',[],163,'||var|if|function|return|this|false|length|for||||||||||||||||||||||||||||||||||||||||||||||||||||||push|Array|jGCharts||method||parseInt|window|type|forEach|indexOf|data|else|constructor||call|Api|size|colors|axis_type|axis_labels|legend|bar_width|fillarea|title|grid||new|_colors||jQuery|agent|solid|throw|Error|sum||||lastIndexOf|true|prototype||chart||scaling|axis_range|background|title_style|line_style|bvg|||join|||must||Data||||||||||||axis_step|lines|title_color|title_size|bar_spacing|fillbottom|filltop|bg_type|bg_offset|bg_width|bg_angle|bg_trasparency|chbg|chbg_type|chbg_offset|chbg_width|chbg_angle|chbg_trasparency|grid_x|grid_y|grid_line|grid_blank|||make|jgcharts|foldl|Base|init|appendTo'.split('|'),0,{}))

$(document).ready(function(){
	/*=======================================================
	 * Widget data
	 */
	var wdata = $('#widget-quality-reporting').data();
	
	/*=======================================================
	 * Facility data
	 */
	var facility_data = new Array();
	
	$.get(wdata.xml_file_path+'cm_Regions.xml', function(data){
		var regions_json = $.xml2json(data);
		$.each(regions_json.Region, function(i, region){
			(region.RegionID != 'usTop' && region.RegionID != 'usAvg')?$('#quality_reporting_facility').append('<option value="'+region.RegionID+'">'+region.RegionName+'</option>'):'';
			facility_data.push(region.RegionID);
			facility_data[region.RegionID.toString()] = region.RegionName;
		});
	}, 'xml');
	
	/*=======================================================
	 * Measures
	 */
	var measures = new Array();
	
	$.get(wdata.xml_file_path+'cm_Categories.xml', function(data){
		var categories_json = $.xml2json(data);
		$.each(categories_json.Category, function(i, category){
			$('#quality_reporting_category').append('<option value="'+category.CategoryID+'">'+category.CategoryName+'</option>');
			// create array for the measures
			measures.push(category.CategoryID);
			measures[category.CategoryID.toString()] = new Array();
		});
	}, 'xml');
	
	$.get(wdata.xml_file_path+'cm_Measures.xml', function(data){
		var measures_json = $.xml2json(data);
		$.each(measures_json.Measure, function(i, measure){
			measures[measure.CategoryID.toString()].push(measure);
			
			$.each(facility_data, function(i, facility){
				measure_data.push(measure.MeasureCode);
				measure_data[measure.MeasureCode.toString()] = new Array();
			});
		});
	}, 'xml');
	
	/*=======================================================
	 * Measure Data
	 */
	var measure_data = new Array();
	
	$.get(wdata.xml_file_path+'cm_Percentages.xml', function(data){
		var percentages_json = $.xml2json(data);
		$.each(percentages_json.Percentage, function(i, percentage){
			measure_data[percentage.MeasureCode.toString()].push(percentage);
		});
	}, 'xml');
	
	/*=======================================================
	 * Measure Dates
	 */
	var measure_dates = new Array();
	
	$.get(wdata.xml_file_path+'cm_Dates.xml', function(data){
		var dates_json = $.xml2json(data);
		$.each(dates_json.Date, function(i, measure_date){
			measure_dates.push(measure_date);
		});
	}, 'xml');
	
	/*=======================================================
	 * Display the core measures on the page
	 */
	function display_core_measures(options){
		// get the quality measures with the given options
		var facility 		= options.facility;
		var facility_name 	= facility_data[facility];
		var category 		= options.category;
		
		// check if we got values
		if(facility != '' && category != ''){
			var results = '';
			
			$.each(measures[category], function(i, measure){
				/*
				measure.MeasureName
				measure.MeasureCode
				measure.MeasureDescription
				*/
				
				var measure_results 			= '<h4>'+measure_dates[0].DateText.toString()+'</h4>';
				var measure_results_prev_date 	= '<h4>'+measure_dates[1].DateText.toString()+'</h4>';
				
				$.each(measure_data[measure.MeasureCode], function(i, measure_item){
					/*
					measure_item.DateID
					measure_item.MeasureCode
					measure_item.Percent
					measure_item.RegionID
					*/
					
					if( measure_item.RegionID == 'usTop' || measure_item.RegionID == 'usAvg' || measure_item.RegionID == facility ){
						var percentage_value		= Math.round(parseFloat(measure_item.Percent*100));
							percentage_value		= (percentage_value<=100)?percentage_value:0;
						var formatted_percentage 	= (percentage_value>0)?percentage_value+'%':'';
						var formatted_region 		= '';
						var percentage_class 		= 'hospital-percentage';
						
						// show the right measure item
						switch(measure_item.RegionID){
							case 'usTop':
								formatted_region = 'Top 10% of all US Hospitals';
								percentage_class = 'us-top-percentage';
								break;
							case 'usAvg':
								formatted_region = 'Average for all US Hospitals';
								percentage_class = 'us-avg-percentage';
								break;
							default:
								if(measure_item.DateID == measure_dates[0].DateID.toString()){
									formatted_region = facility_name;
								}
								else{
									formatted_region = facility_name+'<span>Latest Score Available</span>';
								}
								break;
						}
						
						var measure_results_return = '';
						var percentage_bar = '<span style="width: '+percentage_value+'%;" class="bar '+percentage_class+'"></span>';
						
						if(percentage_value<=0){
							percentage_bar = '<span class="no-data">no data available</span>';
						}
						
						// build the measure details
						measure_results_return += 	'<div class="results-detail-item">'+
														'<h5>'+formatted_region+'</h5>'+
														'<div class="percentage-wrap">'+
															'<span class="bar-wrap">'+
																percentage_bar+
															'</span>'+
															'<span class="percentage">'+formatted_percentage+'</span>'+
														'</div>'+
													'</div>';
						
						if(measure_item.DateID == measure_dates[0].DateID.toString()){
							measure_results += measure_results_return;
						}
						else{
							measure_results_prev_date += measure_results_return;
						}
					}
				});
				
				// check if there is a MeasureDescription
				if(measure.MeasureDescription != ''){
					var measure_def_arrow = '<img src="//globalinc.prod.ehc.com/widgets/source/quality-reporting/v1/arrow-right.png" class="measure-def-open-close-img">';
					var measure_def_bubble = '<img class="measure-info-bubble" src="//globalinc.prod.ehc.com/widgets/source/quality-reporting/v1/help.png">';
				}
				else{
					var measure_def_arrow = '';
					var measure_def_bubble = '';
				}
				
				// build the measure results
				results += 	'<div class="results-detail">'+
								'<h3>'+
									measure_def_arrow+
									measure.MeasureName+
									measure_def_bubble+
								'</h3>'+
								'<div class="definition">'+measure.MeasureDescription+'</div>'+
								'<div class="current">'+measure_results+'</div>'+
								'<div class="past">'+measure_results_prev_date+'</div>'+
							'</div>';
			})
			
			$('.quality-reporting-display').html(
				'<div>'+
					'<h2>'+facility_name+'</h2>'+
					// '<div>'+category+'</div>'+
					'<div class="results">'+results+'</div>'+
				'</div>'
				);
			
			$(".results-detail .definition").each(function() {
				$(this).css("height", $(this).height());
				$(this).hide();
			});
		}
		else{
			$('.quality-reporting-display').html('Please choose both a facility and a category.');
		}
	}
	
	/*=======================================================
	 * React to the submit
	 */
	$('#widget-quality-reporting').submit(function(e){
		e.preventDefault();
		
		display_core_measures(
			{
				facility: $('#quality_reporting_facility').val(),
				category: $('#quality_reporting_category').val()
			}
		);
		
		return false;
	});
	
	/*=======================================================
	 * React to the change
	 */
	$('#quality_reporting_facility, #quality_reporting_category').change(function(e){
		if( $('#quality_reporting_facility').val() != '' && $('#quality_reporting_category').val() != '' ){
			display_core_measures(
				{
					facility: $('#quality_reporting_facility').val(),
					category: $('#quality_reporting_category').val()
				}
			);
		}
	});
	
	/*=======================================================
	 * Open the definition
	 */
	$('#widget-quality-reporting .results-detail h3').live('click',function(){
		// change the arrow
		if($('.measure-def-open-close-img', this).attr('src') == '//globalinc.prod.ehc.com/widgets/source/quality-reporting/v1/arrow-right.png'){
			$('.measure-def-open-close-img', this).attr('src', '//globalinc.prod.ehc.com/widgets/source/quality-reporting/v1/arrow-down.png');
		}
		else if($('.measure-def-open-close-img', this).attr('src') == '//globalinc.prod.ehc.com/widgets/source/quality-reporting/v1/arrow-down.png'){
			$('.measure-def-open-close-img', this).attr('src', '//globalinc.prod.ehc.com/widgets/source/quality-reporting/v1/arrow-right.png');
		}
		
		if($(this).next().text() != ''){
			$(this).next().slideToggle();
		}
	});
});
