var maxsteps=8;
var sd=3;
function fn(x,y){
	var r=0;
	with (Math){
		r=f1*sin(f2*x*y+f9)+f3*sin(f4*x-f5*y+f10)+f6*sin(f7*x+f8*y+f11);
	}
	if(!r)return 0;
	if(r>0)return 1;
	return -1;
}

function _1(){
	return Math.random()*2.0-1.0;
}

var f1=_1();
var f2=_1();
var f3=_1();
var f4=_1();
var f5=_1();
var f6=_1();
var f7=_1();
var f8=_1();
var f9=_1();
var f10=_1();
var f11=_1();
var gc='#fff';
var cnb=document.getElementsByTagName('body')[0];
function setp(){
	if(!cnb){
		cnb=document.getElementsByTagName('body')[0];
	}
	cnb.innerHTML='<div id="x0_0y0_0"></div>';
	f1=_1();
	f2=_1();
	f3=_1();
	f4=_1();
	f5=_1();
	f6=_1();
	f7=_1();
	f8=_1();
	iteration=0;
	cnb.style.backgroundColor='#'
		+Math.floor(Math.random()*9)
		+Math.floor(Math.random()*9)
		+Math.floor(Math.random()*9);
	gc='#'
		+String.fromCharCode(65+Math.floor(Math.random()*5))
		+String.fromCharCode(65+Math.floor(Math.random()*5))
		+String.fromCharCode(65+Math.floor(Math.random()*5));
	windowSize();
	while(ww>20){
		wh/=1.1;
		ww/=1.1;
	}
	minX=-ww/2;
	maxX=ww/2;
	minY=-wh/2;
	maxY=wh/2;
	work();
};

function windowSize() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  ww=myWidth;
  wh=myHeight;
}
var wh=0;
var ww=0;

function ftest(l,r,t,b){
	var v=0;
	var mx=(l+r)/2.0;
	var my=(t+b)/2.0;
	v=Math.abs(
		fn(l,t)
		+fn(l,my)
		+fn(l,b)
		+fn(mx,t)
		+fn(mx,my)
		+fn(mx,b)
		+fn(r,t)
		+fn(r,my)
		+fn(r,b)
		+fn(l+(r-l)*Math.random(),b+(t-b)*Math.random())
	);
	if(v<10){
		return true;
	}
	return false;
}

var iteration=0;
function work(){
	var starttime=new Date();
	var cn=cnb.childNodes;
	var toAppend=[];
	var toRemove=[];
	for(var di in cn){
		d=cn[di];	
		if(d&&d.id){
			var rx=d.id.match(/x([0-9]+)_([0-9]+)y[0-9]+_([0-9]+)/);
			iteration=parseInt(rx[1])+1;
			var ss=Math.pow(sd,iteration);
			var side=100.0/ss+'%';
			var i=parseInt(rx[2]);
			var j=parseInt(rx[3]);						
			toRemove.push(d);	
			for(var ii=0;ii<sd;ii++){
				for(var jj=0;jj<sd;jj++){
					var iii=i*sd+ii;
					var jjj=j*sd+jj;
					if(ftest(
						minX+(iii)*((maxX-minX)/ss),
						minX+(iii+1)*((maxX-minX)/ss),
						minY+(jjj)*((maxY-minY)/ss),
						minY+(jjj+1)*((maxY-minY)/ss)
					)){
						var nd=document.createElement('div');
						nd.style.height=side;
						nd.style.width=side;
						nd.style.position="absolute";
						nd.style.top=(ss-jjj-1)*(100.0/ss)+'%';
						nd.style.left=iii*(100.0/ss)+'%';
						nd.style.backgroundColor=gc;
						nd.style.border="1px solid "+gc;
						nd.innerHTML='&nbsp;';
						nd.setAttribute('id','x'+iteration+'_'+iii+'y'+iteration+'_'+jjj);
						toAppend.push(nd);
					}
				}
			}
		}
	}
	for(var i in toRemove){
		if(toRemove[i].parentNode == cnb) // fuck you, Opera
		cnb.removeChild(toRemove[i]);
	}
	for(var i in toAppend){
		cnb.appendChild(toAppend[i]);
	}
	if(new Date() - starttime < 1000){
		setTimeout(work,100);
	}else{
		setTimeout(setp,2000);
	}
}
