/**
 * @author daniel
 */
var crc = new Array()

var index_crc = 7
var index_uf = 4


$(document).ready(function(){
	for(x=0; x< arr.length; x++)		
		if( crc[ arr[x][index_crc] ] )
		    crc[ arr[x][index_crc] ] = parseInt(crc[ arr[x][index_crc] ]) + 1
	    else
		    crc[ arr[x][index_crc] ] = 1
	
	uf_sigla = document.location.hash.split('#')[1]
	if(!uf_sigla){
		overview(arr.length, 'Brasil', crc)
	}else{
		mostra_tabela(uf_sigla)	
	}
		
});

function zera_crc(){
	crc['GAMA'] = 0
	crc['OXIGENIO'] = 0
	crc['CESMAR'] = 0
	crc['BH Digital'] = 0
}
function mostra_tabela(uf_sigla, total){
	$('#overview').hide()
	$('#tab').hide('fast')
	tab = '<thead><tr><th>Entidade</th><th>Projeto</th><th>Município</th><th>CRC</th><th>Aprovado em</th></tr></thead>'
	tab += '<tbody>'
	zera_crc()
	projetos = 0
	for(x=0; x< arr.length; x++){
		uf = arr[x][index_uf]
		if(total || uf==uf_sigla){
			tab += monta_tr(x)
			crc[arr[x][index_crc]] = crc[arr[x][index_crc]] + 1
			projetos ++
		}
	}
	
	tab += '</tbody>'
	if(projetos){
		$('#tab').html('<table id="tabela_beneficiados">'+tab+'</table>')
		$('#tab').show('fast')
		$('#tabela_beneficiados').tablesorter()
		overview(projetos, uf_sigla, crc)
	}else{
		$('#overview').html('Nenhuma entidade foi beneficiada nesta UF')
		$('#overview').show('fast')
	}
		
}
function overview(projetos, uf_sigla, crc){		
	html = '<h2 class="uf_sigla">'+uf_sigla+'</h2><p><strong>'+projetos+'</strong> projetos aprovados</p>'
	html += '<h2 class="h2">Projetos atendidos por</h2>'
	if(crc['GAMA'] > 0)
		html += '<p><strong>CRC Gama</strong> (DF): '+crc['GAMA']+'</p>'
	if(crc['OXIGENIO'] > 0)
		html += '<p><strong>CRC Oxigênio</strong> (SP): '+crc['OXIGENIO']+'</p>'
	if(crc['CESMAR'] > 0)
		html += '<p><strong>CRC Cesmar</strong> (RS): '+crc['CESMAR']+'</p>'
	if(crc['BH Digital'] > 0)
		html += '<p><strong>CRC BH Digital</strong> (MG): '+crc['BH Digital']+'</p>'
	
	if(uf_sigla == 'Brasil')
		html += '<p><br/>Clique no mapa para <br/>listar  projetos por UF</p>'
	
	$('#overview').html(html)
	$('#overview').show('fast')		
}

function monta_tr(i){
	tr = '<tr>'
	tr += '<td>'+arr[i][1]+'</td>'
	tr += '<td>'+arr[i][2]+'</td>'
	tr += '<td>'+arr[i][3]+'</td>'
	tr += '<td>'+arr[i][index_crc]+'</td>'
	tr += '<td>'+arr[i][8]+'</td>'
	tr += '</tr>'
	return tr
}
