var year = new Date().getFullYear();
var without_form = ['rpi'];

function updateChart(rel) {
    document.location.href = document.location.href.split('#')[0] + '#' + field_name 
    if (rel) {
        document.location.href = document.location.href + '#' + rel;
        rel = '&'+rel+'=1';
    } else {
        rel = '';
    }
	$.getJSON(chart_url+'&field=' + field_name + '&year=' + year + rel, function(json){
		$('#legend').empty();
		container = $('#chart-container-public');
		if (json.error) {
			container.html($('<p>').attr('class', 'error').text(json.error));
		} else {
			chart = $('#chart');
			if (!chart.length) {
				container.html($('<img/>').attr('src', json.url).attr('id', 'chart'));
			} else {
				chart.attr('src', json.url);
			}
		}
		$('input[name=year]').val(year);
		$.each(json, setValues);
		if (json.legend){
		    $.each(json.legend, appendLegend);
		}
	});
}

function setValues(key, item){
	$('#'+key).find('input:text').each(function(i, input) {
		name = $(input).attr('name');
		value = item[parseInt(name)-1]
		if (value.toFixed(0) == value && field_name == 'earnings') {
			value = value.toFixed(1);
		}
		$(input).val(value);
	});
}

function appendLegend(i,item){
	$('<li/>').append($('<div/>').css('background-color', '#'+item.color).attr('class', 'color'))
			.append($('<span />').attr('class', 'legend-item').text(item.agency)).appendTo('#legend');
}

function updateYear(selected) {
    if (selected.length) {
        $('#year li').removeClass('current');
        selected.addClass('current');
        year = parseInt(selected.text());
        $('input[name=year]').val(year);
        updateChart();
    }
}

$(document).ready(function() {
    var URL = document.location.href;
    var chart_name = URL.split('#')[1];
    var chart_rel = URL.split('#')[2];
    if (chart_rel == null){ chart_rel = ''; }
    if (chart_name != null){ field_name = chart_name; }
	updateChart(chart_rel); // year now
	
	$('#year-prev').click(function(){updateYear($('li.current').prev())});
    $('#year-next').click(function(){updateYear($('li.current').next())});
	$('#change_field a').click(function () {
	    $('#change_field a').removeClass('on');
	    $(this).addClass('on');
	    $('#chart-header').text($(this).text());
	    field_name = this.getAttribute('href',2); 
	    entries_box = $('#entries').parent();
	    rel = $(this).attr('rel');
	    // hide form when chart is not editable or if it displays total data
	    if (field_name.search(without_form.toString()) != -1 || rel == 'total') {
	        entries_box.slideUp('fast');
	    } else if (entries_box.css('display') == 'none') {
	        entries_box.slideDown('fast');
	    }
	    
	    $('.entry-form').attr('action', function() {
	        url = this.action.split('=')[0];
	        return url+'='+field_name;
	    });
		updateChart(rel);
		return false;
    });
	$('.show').change(function () {
		if (this.checked) {
			$(this).parent().parent().find('input:text').removeAttr('disabled');
    	} else {
			$(this).parent().parent().find('input:text').attr('disabled', 'disabled');
		};
	});
	forms = $('.entry-form')
	$('.entry-form').ajaxForm(function() { 
        updateChart();
    });
	$('#blog-copy').click(function () {
      $('#snippet').slideToggle('fast');
    });
});
