/*jslint white: true*/
/*
	HTML5 Mandelbrot set & Julia sets by Zoltán Bacskó (http://falcosoft.hu) is licensed under a
	Creative Commons Attribution-ShareAlike 4.0 International License.
	
	Warning! In order this script to load on local machine file: protocol under Google Chrome/Opera Next you have to set --allow-file-access-from-files
*/

	"use strict";
	
	function iter(a, b, x, y, ba, mi) {
		var n = 0;
		var x2 = x * x;
		var y2 = y * y;
	
		do {
			y = 2 * x * y + b;
			x = x2 - y2 + a;
			x2 = x * x;
			y2 = y * y;
			n++;
		}
		while (((x2 + y2) < ba) && (n < mi));
	
		return n;
	}
	
	
	self.onmessage = function(event) {
		var data = event.data;
		var a = data.a;
		var b = data.b;
		var re = data.re;
		var im = data.im;
		var lp1 = data.lp1;
		var maxiter = data.maxiter;
		var bailoutvalue = data.bailoutvalue;
		var surfacewidth = data.surfacewidth;
		var julia = data.julia;
		var n = 0;
		var j;
		data.values = [];
	
		for (j = 0; j < surfacewidth; j++) {
			a = a + lp1;
			if (!julia)
			n = iter(a, b, re, im, bailoutvalue, maxiter);
			else
			n = iter(re, im, a, b, bailoutvalue, maxiter);
			data.values.push(n);
		}
	
		self.postMessage(data);
	};
