
/**
 * Module dependencies.
 */

var Canvas = require('../lib/canvas')
  , canvas = new Canvas(600, 300);

$ = jQuery = function(canvas){
  if (global == this) {
    return new jQuery(canvas);
  } else if (canvas instanceof jQuery) {
    return canvas;
  } else {
    this.width = function(){ return canvas.width };
    this.height = function(){ return canvas.height; };
    this.html = function(){};
    this.remove = function(){};
    this.append = function(){};
    this.find = function(){ return this; };
    this.appendTo = function(){ return this; };
    this.css = function(){ return this; };
    this.get = function(){ return canvas; };
  }
};

jQuery.isArray = Array.isArray;

jQuery.isFunction = function(obj){
  return 'function' == typeof obj;
};

jQuery.isPlainObject = function(obj) {
  return '[object Object]' == Object.prototype.toString.call(obj);
};

var n = 0;

document = {
  createElement: function(type){
    if ('canvas' != type) return;
    if (1 == ++n) return canvas;
    return new Canvas;
  }
};

$.browser = {};

jQuery.extend = function() {
	// copy reference to target object
	var target = arguments[0] || {}, i = 1, length = arguments.length, deep = false, options, name, src, copy, copyIsArray;

	// Handle a deep copy situation
	if ( typeof target === "boolean" ) {
		deep = target;
		target = arguments[1] || {};
		// skip the boolean and the target
		i = 2;
	}

	// Handle case when target is a string or something (possible in deep copy)
	if ( typeof target !== "object" && 'function' == typeof target ) {
		target = {};
	}

	// extend jQuery itself if only one argument is passed
	if ( length === i ) {
		target = this;
		--i;
	}

	for ( ; i < length; i++ ) {
		// Only deal with non-null/undefined values
		if ( (options = arguments[ i ]) != null ) {
			// Extend the base object
			for ( name in options ) {
				src = target[ name ];
				copy = options[ name ];

				// Prevent never-ending loop
				if ( target === copy ) {
					continue;
				}

				// Recurse if we're merging plain objects or arrays
				if ( deep && copy && ( jQuery.isPlainObject(copy) || (copyIsArray = jQuery.isArray(copy)) ) ) {
					if ( copyIsArray ) {
						copyIsArray = false;
						clone = src && Array.isArray(src) ? src : [];

					} else {
						clone = src && '[object Object]' == Object.prototype.toString.call(src) ? src : {};
					}

					// Never move original objects, clone them
					target[ name ] = jQuery.extend( deep, clone, copy );

				// Don't bring in undefined values
				} else if ( copy !== undefined ) {
					target[ name ] = copy;
				}
			}
		}
	}

	// Return the modified object
	return target;
};

require('./flot/jquery.flot.js');

var d1 = [];
for (var i = 0; i < 14; i += 0.5)
    d1.push([i, Math.sin(i)]);

var d2 = [[0, 3], [4, 8], [8, 5], [9, 13]];

var d3 = [];
for (var i = 0; i < 14; i += 0.5)
    d3.push([i, Math.cos(i)]);

var d4 = [];
for (var i = 0; i < 14; i += 0.1)
    d4.push([i, Math.sqrt(i * 10)]);

var d5 = [];
for (var i = 0; i < 14; i += 0.5)
    d5.push([i, Math.sqrt(i)]);

var d6 = [];
for (var i = 0; i < 14; i += 0.5 + Math.random())
    d6.push([i, Math.sqrt(2*i + Math.sin(i) + 5)]);

var start = new Date;
                 
$.plot($({ width: canvas.width, height: canvas.height }), [
    {
        data: d1,
        lines: { show: true, fill: true }
    },
    {
        data: d2,
        bars: { show: true }
    },
    {
        data: d3,
        points: { show: true }
    },
    {
        data: d4,
        lines: { show: true }
    },
    {
        data: d5,
        lines: { show: true },
        points: { show: true }
    },
    {
        data: d6,
        lines: { show: true, steps: true }
    }
]);

require('http').createServer(function(req, res){
  var stream = canvas.createPNGStream();
  res.writeHead(200, { 'Content-Type': 'image/png' });
  stream.on('data', function(chunk){
    res.write(chunk);
  });
  stream.on('end', function(){
    res.end();
  });
}).listen(3000);
console.log(new Date - start);
