/*
==================================================================================================
	Lowland - JavaScript low level functions
	Copyright (C) 2012 Sebatian Fastner
==================================================================================================
*/

/**
 * #require(ext.sugar.Object)
 */

(function(window) {
	var hasPostMessage = !!window.postMessage;
	var slice = Array.prototype.slice;
	var timeouts = [];
	var messageName = "$$lowland-zero-timeout-message";
	var id = 0;
	
	var handleMessage = function(event) {
		if (event.source == window && event.data == messageName) {
			lowland.bom.Events.stopPropagation(event);
			if (timeouts.length > 0) {
				var timeout = timeouts.shift();

				timeout[0].apply(timeout[1], timeout[2]);
			}
		}
	};
	lowland.bom.Events.set(window, "message", handleMessage, true);
	
	core.Module("lowland.ext.Function", {
		/**
		 * Delays function for @time {Integer} milliseconds and after that time executes function
		 * in @context {var}.
		 */
		delay : function(time, context) {
			var func = this;
			return setTimeout(function(context, args) {
				func.apply(context||this, args||[]);
			}, time, context, slice.call(arguments, 2));
		},
		
		/**
		 * {Number} Based upon work of http://dbaron.org/log/20100309-faster-timeouts
		 * Calls function lazy in @context {var}.
		 */
		lazy : function(context) {
			context = context || this;
			//timeouts.push([func, slice.call(arguments,1)]);
			var lazyId = id++;
			timeouts.push([this, context, slice.call(arguments,1), lazyId]);
			postMessage(messageName, "*");
			return lazyId;
		},
		
		/**
		 * Cancel lazy request with given @id {Number}.
		 */
		cancelLazy : function(id) {
			for (var i=0; i<timeouts.length; i++) {
				if (timeouts[i][3] === id) {
					timeouts.splice(i, 1);
					return true;
				}
			}
			return false;
		}
	});
	
	core.Main.addMembers("Function", {
		lowDelay : lowland.ext.Function.delay,
		lazy : lowland.ext.Function.lazy
	});

})(window);

@@ -1046,31 +1046,17 @@
 text
-) %7B%0A%09%09%09var func = this;
+, func) %7B
 %0A%09%09%09
@@ -1090,16 +1090,22 @@
 context,
+ func,
  args) %7B
@@ -1158,32 +1158,38 @@
 , time, context,
+ func,
  slice.call(argu
@@ -1195,17 +1195,17 @@
 uments, 
-2
+3
 ));%0A%09%09%7D,
@@ -1369,16 +1369,21 @@
 (context
+, fnt
 ) %7B%0A%09%09%09c
@@ -1501,20 +1501,19 @@
 s.push(%5B
-this
+fnt
 , contex
@@ -1536,17 +1536,17 @@
 guments,
-1
+2
 ), lazyI
@@ -1916,70 +1916,399 @@
 y : 
-lowland.ext.Function.delay,%0A%09%09lazy : lowland.ext.Function.lazy
+function(time, context) %7B%0A%09%09%09var func = this;%0A%09%09%09return setTimeout(function(context, args) %7B%0A%09%09%09%09func.apply(context%7C%7Cthis, args%7C%7C%5B%5D);%0A%09%09%09%7D, time, context, slice.call(arguments, 2));%0A%09%09%7D,%0A%09%09lazy : function(context) %7B%0A%09%09%09context = context %7C%7C this;%0A%09%09%09var lazyId = id++;%0A%09%09%09timeouts.push(%5Bthis, context, slice.call(arguments,1), lazyId%5D);%0A%09%09%09postMessage(messageName, %22*%22);%0A%09%09%09return lazyId;%0A%09%09%7D
 %0A%09%7D)

Fix delay