/* Value Labels Plugin for flot.
 * Homepage:
 * http://sites.google.com/site/petrsstuff/projects/flotvallab
 *
 * Released under the MIT license by Petr Blahos, December 2009.
 *
 */
(function ($) {
    var options = {
        valueLabels: {
	    show: false,
	    escala: "",
	    imagenes: null,
	    doble: false
        }
    };
    
    function init(plot) {
        plot.hooks.draw.push(function (plot, ctx) {
	    if (!plot.getOptions().valueLabels.show) {
	        return
	    }
	    $.each(plot.getData(), function(ii, series) {
		    plot.getPlaceholder().find("#valueLabels"+ii).remove();
		    var html = '<div id="valueLabels' + series.seriesIndex + '" class="valueLabels">';
		    var last_val = null;
		    var last_x = -1000;
		    var last_y = -1000;
		    img = plot.getOptions().valueLabels.imagenes;
		    for (var i = 0; i < series.data.length; ++i) {
			if (series.data[i] == null)
			    continue;
			  
			var x = series.data[i][0], y = series.data[i][1];
			if (x < series.xaxis.min || x > series.xaxis.max || y < series.yaxis.min || y > series.yaxis.max)
			    continue;
			var val = y;
			if (series.valueLabelFunc) {
				val = series.valueLabelFunc({ series: series, seriesIndex: ii, index: i });
			}
			val = ""+val;
					var xx = series.xaxis.p2c(x)+plot.getPlotOffset().left;
					var yy = series.yaxis.p2c(y)-12+plot.getPlotOffset().top;
					if (Math.abs(yy-last_y)>20 || last_x<xx) {
						last_val = val;
						last_x = xx + val.length*8;
						last_y = yy;

						var imagen = "";
						if (img != null)
						{
							if (i == 0) 
							{
								xx = (xx + 6) - 5;
							}
							else if(i == (series.data.length - 1))
							{
								xx = (xx - 27) - 5;
							}
							else
							{
								xx = xx - 12;
							}
							if (yy < 35) 
							{
								yy = yy + 30;
							}
							else
							{						
								yy = yy - 37;
							}
							
							if (img[x] != null)
							{
								if(img[x][0] != "" && img[x][1] != "")
								{
									imagen = "<li><img class='flotvaluelabels' src='" + img[x][0] + "'/><img class='marginleft1px flotvaluelabels' src='" + img[x][1] + "'/></li>";
								}
								else if (img[x][0] != "" && img[x][1] == "")
								{
									imagen = "<li><img class='marginright2px flotvaluelabels' src='" + img[x][0]+ "'/></li>";
								}
								else
								{
									imagen = "<li><img class='marginright2px flotvaluelabels' src='" + img[x][1]+ "'/></li>";
								}	
							}
					    }
						else
						{
							if (i == 0) 
							{
								xx = (xx + 6) - 5;
							}
							else if(i == (series.data.length - 1))
							{
								xx = (xx - 10) - 5;
							}
							else
							{
								xx = xx - 12;
							}
							if (yy < 26) 
							{
								yy = yy + 17;
							}
							else
							{						
								yy = yy - 8;
							}
						}
						var head = '<div style="text-align:center;left:' + xx + 'px;top:' + yy + 'px;" class="valueLabel';
						var tail = '"><ul class="lista_sin_estilo">' + imagen + '<li>' + val + plot.getOptions().valueLabels.escala + '</li></ul></div>';
						html+= head + tail;
					}

		    }
		    html+= "</div>";
		    plot.getPlaceholder().append(html);
		});
        });
    }
    
    $.plot.plugins.push({
        init: init,
        options: options,
        name: 'valueLabels',
        version: '1.0'
    });
})(jQuery);

