// The code to generate the sparklines is
// Copyright 2005 Jesse Andrews
// Licensed under GPL v2 or later

function sparkline( data ) {

  // --- compute the total, average, max, min --- //

  var total = 0;
  var min = data[0];
  var max = data[0]; 
  
  for (var i=0; i<data.length; i++) { 
    total = total + data[i];
    if (data[i] > max) max = data[i];
    if (data[i] < min) min = data[i];
  }

  var avg = total/data.length;
  
  // ---  scale data for chart --- //

  // our y axis will be scaled to a value between 0..4 and then translated/flipped to be 5..1 (0..1 and 5..6 are the gutters)

  var scale = 4;
  var translate = 1;

  var chart = [];   
  var chartavg = (scale-scale*((avg-min)/max)) + translate;

  for (var i=0; i<data.length; i++) { 
    chart[i] = (scale-scale*((data[i] - min)/max)) + translate;
  }

  // --- start of SVG output: header & average --- //

  svgHeader='<?xml version="1.0" encoding="utf-8"?><svg:svg xmlns:svg="http://www.w3.org/2000/svg" viewBox="0 0 '+2*(chart.length+6)+' 12"><svg:g>';
  svgFooter='</svg:g></svg:svg>';

  var svgSrc = svgHeader;
  svgSrc = svgSrc + '<svg:line x1="' + 0 + '" y1="'+ chartavg +'" x2="' + 2*(chart.length-1) + '" y2="' + 2*chartavg + '" style="stroke-width: 0.1; stroke-opacity: 0;"/>';   

  // --- output all the chart --- //

  var polyPoints = "";
  for (var i=0; i<chart.length; i++) {   
    polyPoints = polyPoints + ' ' + 2*i + ',' + 2*chart[i];
  }                                                      

  svgSrc = svgSrc + '<svg:polyline points="'+polyPoints+'" style="fill:none; stroke:black; stroke-width:.15"/>';

  // --- place final value and spark at end --- //
                                                          
  svgSrc = svgSrc + '<svg:circle cx="'+2*(chart.length-1)+'" cy="'+2*(chart[chart.length-1])+'" r=".5" stroke="0" fill="red"/>';
  svgSrc = svgSrc + '<svg:text x="'+2*(chart.length)+'" y="'+2*(chart[chart.length-1]+1)+'" style="text-anchor: start; font-weight: normal; font-size: 0.1em">'+data[data.length-1]+'</svg:text>';

  svgSrc = svgSrc + svgFooter;
                                 
  return svgSrc;
}            

function embedSparkline( chartData ) {
  document.write('<object type="image/svg+xml" data="data:image/svg+xml;base64,'+ encodeBase64(sparkline(chartData)) +'" width="'+(chartData.length*3+4)+'" height="18"></object>')
}

///// the following is used to encode to base64 and included here without change 

/* Mirage CMS
 * lib/base64.js - Decoding base64 input, used for masking e-mailadresses (anti-spam)
 * (c) 2005 Dennis Kaarsemaker <dennis@kaarsemaker.net>
 */

var END_OF_INPUT = -1;

var base64Chars = new Array(
    'A','B','C','D','E','F','G','H',
    'I','J','K','L','M','N','O','P',
    'Q','R','S','T','U','V','W','X',
    'Y','Z','a','b','c','d','e','f',
    'g','h','i','j','k','l','m','n',
    'o','p','q','r','s','t','u','v',
    'w','x','y','z','0','1','2','3',
    '4','5','6','7','8','9','+','/'
);


var base64Str;
var base64Count;
function setBase64Str(str){
    base64Str = str;
    base64Count = 0;
}
function readBase64(){    
    if (!base64Str) return END_OF_INPUT;
    if (base64Count >= base64Str.length) return END_OF_INPUT;
    var c = base64Str.charCodeAt(base64Count) & 0xff;
    base64Count++;
    return c;
}
function encodeBase64(str){
    setBase64Str(str);
    var result = '';
    var inBuffer = new Array(3);
    var lineCount = 0;
    var done = false;
    while (!done && (inBuffer[0] = readBase64()) != END_OF_INPUT){
        inBuffer[1] = readBase64();
        inBuffer[2] = readBase64();
        result += (base64Chars[ inBuffer[0] >> 2 ]);
        if (inBuffer[1] != END_OF_INPUT){
            result += (base64Chars [(( inBuffer[0] << 4 ) & 0x30) | (inBuffer[1] >> 4) ]);
            if (inBuffer[2] != END_OF_INPUT){
                result += (base64Chars [((inBuffer[1] << 2) & 0x3c) | (inBuffer[2] >> 6) ]);
                result += (base64Chars [inBuffer[2] & 0x3F]);
            } else {
                result += (base64Chars [((inBuffer[1] << 2) & 0x3c)]);
                result += ('=');
                done = true;
            }
        } else {
            result += (base64Chars [(( inBuffer[0] << 4 ) & 0x30)]);
            result += ('=');
            result += ('=');
            done = true;
        }
        lineCount += 4;
        if (lineCount >= 76){
            result += ('\n');
            lineCount = 0;
        }
    }
    return result;
}                       


