

// Peity jQuery plugin version 0.3.3
// (c) 2010 Ben Pickles
// http://benpickles.github.com/peity/
// Released under MIT license.

(function($, document) {
  var peity = $.fn.peity = function(type, options) {
    if (document.createElement("canvas").getContext) {
      this.each(function() {
        $(this).change(function() {
          var value = $(this).html();
          peity.graphers[type]($(this), $.extend({}, peity.defaults[type], options));
          $(this).trigger("chart:changed", value);
        }).trigger("change");
      });
    }

    return this;
  };

  peity.graphers = {};
  peity.defaults = {};

  peity.add = function(type, defaults, grapher){
    peity.graphers[type] = grapher;
    peity.defaults[type] = defaults;
  };

  function createCanvas(width, height) {
    var canvas = document.createElement("canvas")
    canvas.setAttribute("width", width)
    canvas.setAttribute("height", height)
    return canvas
  }

  peity.add(
    "line",
    {
      colour: "#383838",
      strokeColour: "#c94444",
      strokeWidth: 1,
      delimeter: ",",
      height: 16,
      max: null,
      width: 950
    },
    function($this, opts) {
      var elem = createCanvas(opts.width, opts.height)
      var values = $this.text().split(opts.delimeter)
      var max = Math.max.apply(Math, values.concat([opts.max]));
      var ratio = opts.height / max;
      var width = opts.width / (values.length - 1);
      var coords = [];
      var i;

      var canvas = elem.getContext("2d");
      canvas.beginPath();
      canvas.moveTo(0, opts.height);

      for (i = 0; i < values.length; i++) {
        var height = ratio * values[i];
        var x = i * width;
        var y = opts.height - height;
        coords.push({ x: x, y: y });
        canvas.lineTo(x, y);
      }

      canvas.lineTo(opts.width, opts.height);
      canvas.fillStyle = opts.colour;
      canvas.fill();

      canvas.beginPath();
      canvas.moveTo(0, coords[0].y);
      for (i = 0; i < coords.length; i++) {
        canvas.lineTo(coords[i].x, coords[i].y);
      }
      canvas.lineWidth = opts.strokeWidth;
      canvas.strokeStyle = opts.strokeColour;
      canvas.stroke();

      $this.wrapInner($("<span>").hide()).append(elem)
    }
  );

  peity.add(
    'bar',
    {
      colour: "#4D89F9",
      delimeter: ",",
      height: 16,
      max: null,
      width: 32
    },
    function($this, opts) {
      var elem = createCanvas(opts.width, opts.height)
      var values = $this.text().split(opts.delimeter)
      var max = Math.max.apply(Math, values.concat([opts.max]));
      var ratio = opts.height / max;
      var width = opts.width / values.length;

      var canvas = elem.getContext("2d");
      canvas.fillStyle = opts.colour;

      for (var i = 0; i < values.length; i++) {
        var height = ratio * values[i];
        var x = i * width;
        var y = opts.height - height;

        canvas.fillRect(x, y, width, height);
      }

      $this.wrapInner($("<span>").hide()).append(elem)
    }
  );
})(jQuery, document);


$(document).ready(function(){$("li").fadeTo("slow",0.5);$("li").hover(function(){$(this).fadeTo("slow",1.0)},function(){$(this).fadeTo("slow",0.5)});$("a").fadeTo("slow",0.9);$("a").hover(function(){$(this).fadeTo("slow",1.0)},function(){$(this).fadeTo("slow",0.9)})});
