// In most popular browsers all javascript code is executed in the only thread.
// It freezes browsers completely (Opera is exclusion).
// So this absolutely ugly code is intended to solve the problem.
// When some time is passed control will be returned to browser.
// But this turns function calls into a really odd things like ensure_response(some_function).
// Unfoturnately we have to humble.

// The main problem here is that all these is not too fast.
// Actually call-stack unwinding is very slow. It takes much more time than calculations itself.
// So it would be nice if someone invented another way of doing the things.

var THRESHOLD = 150;

var ret;
var lasttime;
var first;

function init_starting_time()
{
    var d = new Date();

    lasttime = d.getTime();
    first = true;
}

function ensure_response(return_point)
{
    var d = new Date();
    var current_time = d.getTime();

    ret = return_point;

    if ((current_time - lasttime) > THRESHOLD) {
	lasttime = current_time;
	setTimeout("resume()", 0);
	throw 'unwind';
    }

    if (first) {
	first = false;
	resume();

    }
    else {
	throw 'continue';
    }
}

function resume()
{
    while (true) {
	try {
	    ret();
	    return;
	} catch (e) {
	    if (e == 'unwind')
		return;
	}
    }
}
