/*
 * Paper.js
 *
 * This file is part of Paper.js, a JavaScript Vector Graphics Library,
 * based on Scriptographer.org and designed to be largely API compatible.
 * http://paperjs.org/
 * http://scriptographer.org/
 *
 * Distributed under the MIT license. See LICENSE file for details.
 *
 * Copyright (c) 2011, Juerg Lehni & Jonathan Puckey
 * http://lehni.org/ & http://jonathanpuckey.com/
 *
 * All rights reserved.
 */

var Event = {
	add: function(el, events) {
		for (var type in events) {
			var func = events[type];
			if (el.addEventListener) {
				el.addEventListener(type, func, false);
			} else if (el.attachEvent) {
				// Make a bound closure that calls on the right object and
				// passes on the global event object as a parameter.
				el.attachEvent('on' + type, func.bound = function() {
					func.call(el, window.event);
				});
			}
		}
	},

	remove: function(el, events) {
		for (var type in events) {
			var func = events[type];
			if (el.removeEventListener) {
				el.removeEventListener(type, func, false);
			} else if (el.detachEvent) {
				// Remove the bound closure instead of func itself
				el.detachEvent('on' + type, func.bound);
			}
		}
	},

	getPoint: function(event) {
		return Point.create(
			event.pageX || event.clientX + document.documentElement.scrollLeft,
			event.pageY || event.clientY + document.documentElement.scrollTop
		);
	},

	getElement: function(event) {
		return event.target || event.srcElement;
	},

	getOffset: function(event) {
		// Remove target offsets from page coordinates
		return Event.getPoint(event).subtract(
				Element.getOffset(Event.getElement(event)));
	}
};

Event.requestAnimationFrame = new function() {
	var part = 'equestAnimationFrame',
	// Chrome shipped without the time arg in m10
		request = window['r' + part] || window['webkitR' + part]
			|| window['mozR' + part] || window['oR' + part]
			|| window['msR' + part];
	if (request) {
		// Check if time is defined in callbacks, and if not, clear request
		// again so we won't use the faulty method.
		request(function(time) {
			if (time == undefined)
				request = null;
		});
	}

	// So we need to fake it. Define helper functions first:
	var callbacks = [],
		fastRate = 1000 / 60,
		slowRate = 1000,
		timer;

	// Checks all installed callbacks for element visibility and execute if
	// needed.
	function checkCallbacks() {
		if (!focused)
			return;
		for (var i = callbacks.length - 1; i >= 0; i--) {
			var entry = callbacks[i],
				func = entry[0],
				element = entry[1];
			if (!element || Element.isVisible(element)) {
				// Handle callback and remove it from callbacks list.
				callbacks.splice(i, 1);
				func(+new Date);
			}
		}
	}

	// Installs interval timer that checks all callbacks. This results in much
	// faster animations than repeatedly installing timout timers.
	function setTimer(timeout) {
		window.clearInterval(timer);
		timer = window.setInterval(checkCallbacks, timeout);
	}

	var focused = true;
	Event.add(window, {
		focus: function() {
			focused = true;
			// Switch to falst checkCallback calls while window is focused.
			if (timer)
				setTimer(fastRate);
		},
		blur: function() {
			focused = false;
			// Switch to slow checkCallback calls while window is blured.
			if (timer)
				setTimer(slowRate);
		}
	});

	return function(callback, element) {
		if (request)
			return request(callback, element);
		callbacks.push([callback, element]);
		if (!timer)
			setTimer(fastRate);
	};
};

@@ -2246,16 +2246,261 @@
 timer;%0A%0A
+%09// Installs interval timer that checks all callbacks. This results in much%0A%09// faster animations than repeatedly installing timout timers.%0A%09function setTimer(timeout) %7B%0A%09%09window.clearInterval(timer);%0A%09%09timer = window.setInterval(function() %7B%0A%09%09
 %09// Chec
@@ -2564,15 +2564,17 @@
 cute
- if%0A
+%0A%09%09
 %09//
+ if
  nee
@@ -2583,36 +2583,8 @@
 d.%0A%09
-function checkCallbacks() %7B%0A
 %09%09if
@@ -2595,16 +2595,17 @@
 ocused)%0A
+%09
 %09%09%09retur
@@ -2609,16 +2609,17 @@
 turn;%0A%09%09
+%09
 for (var
@@ -2659,24 +2659,25 @@
 ; i--) %7B%0A%09%09%09
+%09
 var entry = 
@@ -2690,24 +2690,25 @@
 cks%5Bi%5D,%0A%09%09%09%09
+%09
 func = entry
@@ -2712,16 +2712,17 @@
 try%5B0%5D,%0A
+%09
 %09%09%09%09elem
@@ -2736,24 +2736,25 @@
 ntry%5B1%5D;%0A%09%09%09
+%09
 if (!element
@@ -2783,24 +2783,25 @@
 element)) %7B%0A
+%09
 %09%09%09%09// Handl
@@ -2846,16 +2846,17 @@
 s list.%0A
+%09
 %09%09%09%09call
@@ -2875,24 +2875,25 @@
 (i, 1);%0A%09%09%09%09
+%09
 func(+new Da
@@ -2903,263 +2903,20 @@
 ;%0A%09%09
-%09%7D%0A
 %09%09%7D%0A%09
-%7D%0A%0A%09// Installs interval timer that checks all callbacks. This results in much%0A%09// faster animations than repeatedly installing timout timers.%0A%09function setTimer(timeout) %7B%0A%09%09window.clearInterval(timer);%0A%09%09timer = window.setInterval(checkCallbacks
+%09%09%7D%0A%09%09%7D
 , ti

Clean up Event.requestAnimationFrame.