var processor = {
  timerCallback: function() {
    if (this.video.paused || this.video.ended) {
      return;
    }
    this.computeFrame();
    var self = this;
    setTimeout(function () {
        self.timerCallback();
      }, 200);
  },

  doLoad: function() {
    this.video = document.getElementById("video");
/*
    if ($.browser.safari) {
			video.src = 'http://movies.apple.com/movies/us/apple/ipoditunes/2007/touch/ads/apple_ipodtouch_touch_r640-9cie.mov';
			video.type = 'type="video/mov"';
		}
*/
    this.c1 = document.getElementById("c1");
    this.ctx1 = this.c1.getContext("2d");
    this.c2 = document.getElementById("c2");
    this.ctx2 = this.c2.getContext("2d");
    this.c3 = document.getElementById("c3");
    this.ctx3 = this.c3.getContext("2d");
    var self = this;
    this.video.addEventListener("play", function() {
        self.width = self.video.videoWidth / 2;
        self.height = self.video.videoHeight / 2;
        self.timerCallback();
      }, false);	
  },

  pixparms: {
	  edges: {mono:true,invert:false},
	  posterize: {levels:2},
	  sharpen: {amount: 1.0},
	  mosaic: {blockSize: 5}
  },

  pix: function() {
	  this.params = {canvas: this.c3, options: {rect: {left: 0, top:0, width: this.width, height: this.height}}};
	  var action = $("input[type='radio']:checked").val();
	  $.extend(this.params.options, this.pixparms[action]);
	  Pixastic.Actions[action].process(this.params);
	  //mosaic acts on the passed canvas instead of returning the data
		switch (action) {
			case "mosaic" : 
				return this.ctx3.getImageData(0, 0, this.width, this.height);
				break;
			default:
				return this.params.canvasData;
		}
  },

  computeFrame: function() {
    this.ctx1.drawImage(this.video, 0, 0, this.width, this.height);
    var frame = this.ctx1.getImageData(0, 0, this.width, this.height);
		var l = frame.data.length / 4;

    for (var i = 0; i < l; i++) {
      var r = frame.data[i * 4 + 0];
      var g = frame.data[i * 4 + 1];
      var b = frame.data[i * 4 + 2];
      if (g > 100 && r > 100 && b < 43)
        frame.data[i * 4 + 3] = 0;
    }
    this.ctx3.clearRect(0, 0, this.width, this.height);
    this.ctx3.putImageData(frame, 0, 0);
    this.ctx2.putImageData(this.pix(), 0, 0);
    return;
  }
};
