function leer_tabla(){
    //First search the table
    filas = new Array()
    values = new Array()
    labels = new Array()
    chart_series = new Array();
    colores = new Array("DE41D1", "4147B9", "F38963","F4F438", "669FF5", "F38536", "BE3E55", "F38BA5", "00FF30");
    colores_dict = {
        hogar:'F4F437',
        dinero:'4147B9',
        salud:'F38763',
        ocio:'D0009C',
        crecimiento:'669FF5',
        familia:'F38435',
        amor:'BE3E55',
        amigos:'F38BA5',
        trabajo:'00FF2E'
    }
    removed_filas = new Array();
    removed_chart_series = new Array();
    $('table#tablaEvolucion').find("th").each(function(i){
        if ($(this).attr('scope') == 'row'){
            //Afecta a columnas
            fila = $(this).text();
			if (fila == 'Crecimiento Personal'){
				fila = 'Crecimiento'
			}
            if (jQuery.inArray(fila.toLowerCase(), quesitos_a_excluir) == -1){
                valores = new Array()
                hermanos = $(this).siblings('td');
                $(this).siblings('td').each(function(i){
                    valores[valores.length] = $(this).text();
                });
                filas[filas.length] = fila;
                values[values.length] = valores;
            }
        }else if($(this).attr('scope') == 'col'){
            label = $(this).text();
            labels[labels.length] = label;
        }
    });
//     Borro el primer Label
    labels.shift();
	    
   labels_to_render = Array(labels[0], labels[labels.length-1])
    for (i in filas){
        chart_series[chart_series.length] = $.gchart.series(filas[i], values[i],colores_dict[filas[i].toLowerCase()])
    }
}

function generar_grafico_tendencia(){
    //chart_series_filtradas = chart_series;
    //console.log(quesitos_a_excluir);
    /*var chart_series_filtradas = new Array();
    for (t in chart_series){
        chart_series_filtradas[t] = chart_series[t];
    }*/
    leer_tabla();
    /*for (icsf in chart_series){
        var serie = chart_series[icsf];
        index = jQuery.inArray(serie.label.toLowerCase(), quesitos_a_excluir)
        if (index>-1){
            console.log('La serie ' + serie.label + ' se tiene que excluir');
            var a = chart_series.splice(icsf,1);
        }
    }*/
    $('div#evolucion').gchart('destroy').gchart({type: 'line',
        maxValue:10,
        axes: [$.gchart.axis('left', 0, 10, 'black')],
        //dataLabels: labels_to_render, legend: 'right',
        series:chart_series
    });
}

$(document).ready( function(){
    reload_rueda();
    $('dd.guardar').hide();
	
    $('form.rueda select').change(function(){
        mostrar_guardar = true;
        $('form.rueda select').each(function(i){
            val = $(this).children("option:selected").val();
            if (val == ''){
                mostrar_guardar = false;
            }            
        });
        if (mostrar_guardar){
            $('dd.guardar').fadeIn();
        }else{
            $('dd.guardar').hide();
        }
    });
    

    $('form.rueda select').change(function(){
        reload_rueda();
    });
	
    $('.media').media({caption:false, width:122, height:90,params:{ wmode: 'transparent' }, bgColor:'transparent'  });
	

    quesitos_a_excluir = new Array();
    leer_tabla();
    generar_grafico_tendencia();
	

//  Filtro del gráfico de evolucion
    $('dd.filtro input:checkbox').change(function(){
        quesitos_a_excluir = new Array();
        $('dd.filtro input:not(:checked)').each(function(i){
            quesito = $(this).attr('name');
            quesitos_a_excluir[quesitos_a_excluir.length]=quesito;
        });
		//console.log(quesitos_a_excluir);
        generar_grafico_tendencia();

    });

//     Comparativa
    $('dd.selects select').change(function(){
        nombre = $(this).attr('name');
        valor = $(this).children('option:selected').attr('value');
        $.getJSON("ajax/puntuaciones-rueda-" + valor + ".html",
            function(data){
                url = '/ruedas/rueda-hogar' + data.hogar + 'profesional' + data.profesional + 'salud' + data.salud + 'dinero' + data.dinero + 'crecimiento' + data.crecimiento + 'amigos' + data.amigos + 'familia' + data.familia + 'amor' + data.amor  + 'ocio' + data.ocio + '.png?w=450&h=350';
                $('dd.'+nombre+' img').attr('src', url);
            });

        $('dd.'+nombre+' img').attr('src');
    });
	
	/*$('area').qtip({
	   content: $(this).attr('href'),
	   position: { target: 'mouse' },
	   show: 'mouseover',
	   hide: 'mouseout'
	})*/
	
	$('area').click(function(){
		var href= $(this).attr('href');
		var info = /(.*)_(\d+)/.exec(href);
		if (info){
			var quesito = info[1];
			var puntuacion = info[2];
			$('select#id_puntuacion_'+quesito).val(puntuacion).change();		
		}
		var info_titulo = /(.*)_titulo/.exec(href);
		if (info_titulo){
			var quesito = info_titulo[1];
		}
		return false;
	});
});