var inputs=null;
var score_total=0;
var score_div=null;
var heatColors={
	1:'#F0745B',
	2:'#F98453',
	3:'#FD954E',
	4:'#FFA74C',
	5:'#FDB84E',
	6:'#F8C953',
	7:'#EFD85C',
	8:'#E4E668',
	9:'#D5F075',
	10:'#C6F985'
};
var oldIndex=null;
var legendBox=null;

$(document).ready(function(){
	inputs=$('.score_input');
	legendBox=$('.coaching_legend');
	score_div=$('#coaching_legend_selection');
	inputs.bind('change',calculateTotals);
	inputs.bind('keyup',calculateTotals);
	calculateTotals();
});
function calculateTotals(e){
	score_total=0;
	e=$(e);
	var this_val=0;
	inputs.each(function(){
		this_val=parseIntVal($(this).children('input').attr('value'),0);
		$(this).children('input').attr('value',(this_val>0?this_val:''));
		score_total+=this_val;
		$(this).children('input').css('background-color',heatColors[this_val]);
	})
	$('#coaching_score_total').html(score_total);
	highlightLegend(score_total);
}

function parseIntVal(val,defVal){
	if(defVal==undefined){defVal=0;}
	var r=parseInt(val);
	if(r==undefined || isNaN(r)){
		r=defVal;		
	}
	if(r<0){r=0;}
	if(r>10){r=10;}
	return r;
}
function highlightLegend(score){
	var boxes=legendBox.get();
	var legend=0;
	if (score > 30 && score <= 60) {
		legend=1;
	}else if (score > 60 ){
		legend=2;
	}
	if (legend != oldIndex) {
		legendDiv = $(boxes[legend]);
		var offset = {};
		/*legendDiv.position({
			scroll: false
		}, offset);*/
		offset=legendDiv.position();
		var frstOffset=$(boxes[0]).position();
		/*var parentOffset = {};
		legendDiv.parent().offset({
			scroll: false
		}, parentOffset);
		*/
		score_div.animate({
			top: offset['top'] - frstOffset['top']-10 /*- parentOffset['top'] - 10*/,
			height: legendDiv.height() + 20
		});
		//$(legendBox).not(legendDiv).fadeTo('fast', .4);
		//$(legendDiv).fadeTo('fast', 1);
		
		legendBox.not(legendDiv).animate({color:'#999999'},600);
		legendDiv.animate({color:'#111111'},600);
		oldIndex=legend;
	}
}
