previousPoint = null;

function showTooltip(x, y, contents) {
    jQuery('<div id="tooltip">' + contents + '</div>').css( {
        position: 'absolute',
        display: 'none',
        top: y + 5,
        left: x + 5,
        border: '1px solid #fdd',
        padding: '2px',
        'background-color': '#fee',
        opacity: 0.80
    }).appendTo("body").fadeIn(200);
}

function go(obj) {
    field = document.getElementById(obj);
    location.href = field.value;
}

function showpoint_t(event, pos, item) {
    if (item) {
        if (previousPoint != item.datapoint) {
            previousPoint = item.datapoint;
            
            jQuery("#tooltip").remove();
            var y = item.datapoint[1].toFixed(1);
            showTooltip(item.pageX, item.pageY, item.series.label + ": " + y + "%");
        }
    }
    else {
        jQuery("#tooltip").remove();
        previousPoint = null;            
    }
}

function plot_t(holder, series, ticks) {
    var i = 0;
    jQuery.each(series, function(key, val) {
        val.color = i;
        ++i;
    });

    var data = [];

    jQuery("#choices").find("input:checked").each(function () {
        var key = jQuery(this).attr("name");
        if (key && series[key])
            data.push(series[key]);
    });

    var options = {
        points: { show: true },
        lines: { show: true },
        legend: {
            noColumns: 5,
            backgroundOpacity: 0,
        },
        grid: {
            borderWidth: 0,
            hoverable: true,
        },
        xaxis: {
            ticks: ticks
        },      
        yaxis: {
            max: 35,
        },                          
    }
    
    jQuery.plot(jQuery("#" + holder), data, options);
    jQuery("#" + holder).bind("plothover", showpoint_t);
}

function trends() {
    jQuery("#choices").before('<input type="checkbox" id="toggle" checked="checked" /> Toggle All<br /><br />');
    
    jQuery.each(alldata['Overall'], function(key, val) {
        jQuery("#choices").append(' <input class="slisti" type="checkbox" id="' + key + '" name="' + key + '" checked="checked" /><label for="'+ key +'" class="slistl">' + val.label + '</label>');
    });
    
    jQuery("#choices").find("input").click(do_plot_trends);
    
    var tog = true;
    jQuery("#toggle").click(function() {
        jQuery("#choices").find("input").attr("checked",!tog);
        tog = !tog;
        do_plot_trends()
    }); 
    
    do_plot_trends()
}

function do_plot_trends() {
    jQuery.each(alldata, function(key, val) {
        plot_t(key,val,ticks)
    });
}

function plot_s (holder,series,tot,ticks) {            
    var options = {
        bars: { show: true },
        xaxis: {
            ticks: ticks
        },
        grid: {
            borderWidth: 0,
            hoverable: true,
        },
        
    };
    jQuery.plot(jQuery("#" + holder), [{data: series}], options);
    jQuery("#" + holder).bind("plothover", function(event,pos,item) {showpoint_s(event, pos, item, tot)});
};

function showpoint_s(event, pos, item, tot) {
    if (item) {
        if (previousPoint != item.datapoint) {
            previousPoint = item.datapoint;
            
            jQuery("#tooltip").remove();
            var y = item.datapoint[1].toFixed(1);
            showTooltip(item.pageX, item.pageY, Math.round((y/100)*tot) + ": " + y + "%");
        }
    }
    else {
        jQuery("#tooltip").remove();
        previousPoint = null;            
    }
};

function selections() {
    jQuery.each(alldata, function(key, val) {
        plot_s(key,val[1],val[0],ticks)
    });
}
