
function availableSnowFlakeAngles(string)
	{
	myArray = [];
	count = 0;

		// BUGGY, returning a few invalid patterns, and at least 3 times slower than PHP

	// offset represents fraction of form   k/(2^n)
	offset = 0;

	for(a = 1; a < 180; a++)
		{
		
		
		myR =  buildSnowFlakeInfo(string,a);

		
		//alert(a + " :: " + dump(myR));
		if(myR != false)
			{
			myArray[count] = a;
			count++;
			
			}
		}

	return myArray;
	}


function initializeSnowFlake()
	{
	_min	= {"x":0,"y":0, "when" : {"x" : "","y":""}};
	_max	=	{"x":0,"y":0, "when" : {"x" : "","y":""}};
	_iterations	= 13;	// 6 is normally more than plenty to see pattern
	_up		= 270;  // 0 degrees starts at 3 p.m. and rotates downward to 6 pm. (clockwise)
	_moves	= 0;
	_currentX	=	0;
	_currentY	=	0;
	_currentAngle	= null;
	_previousX	= null;
	_previousY	= null;
	_previousAngle	= null;
	_currentResult = [];
	_rotation	= 1;	/* -1 = counter, 1 = clockwise */
	_precision	= 2;
	}

function rotateMySnowFlake(number,i,j,angle)
		{
		var nx = 0;
		var ny = 0;

		var cx = Number(_currentX);
		var cy = Number(_currentY);		

		_currentAngle = (_previousAngle == null) ? (_up) : (_previousAngle + -1 * _rotation * angle);
		
		nx = (1*cx + -1*_rotation * number * Math.cos(deg2rad(_currentAngle))).toFixed(_precision);
		ny = (1*cy + -1*_rotation * number * Math.sin(deg2rad(_currentAngle))).toFixed(_precision);

		// let's stop
			if(cx == 0 && cy == 0  && i > 0  && j == 0)
				{
				return false;				
				}

		// minMax
			if(nx < _min.x){_min.x = nx; _min.when.x=_moves;}
			if(nx > _max.x){_max.x = nx; _max.when.x=_moves;}
			if(ny < _min.y){_min.y = ny; _min.when.y=_moves;}			
			if(ny > _max.y){_max.y = ny; _max.when.y=_moves;}

		_currentResult[_moves] = {"to" : {"x":nx,"y":ny} };

		_moves++;
		_previousX = _currentX;
		_previousY = _currentY;
		_previousAngle = _currentAngle;

		_currentX = nx;
		_currentY = ny;		

		return true;		
		}
function buildSnowFlakeInfo(string,a)
	{
		myTime = new Date().getTime();
		document.write("angle : " + a + " time :: " +  (myTime - _end) + "<BR /><BR />");


	numberArray = trim(string).split("-");

	initializeSnowFlake();
	
		_short=false;
	for(i = 0; i < _iterations;i++)
		{
		j = 0;
		for (k=0;k<numberArray.length ;k++ )
			{
			myNum = parseFloat(trim(numberArray[k]));
			_return = rotateMySnowFlake(myNum,i,j,a);
			if(_return == false)
				{
				_short=true;
				break;  // php is break 2; , js is break;break;
				break;
				}
			}		
		}


			if(_short == true)
				{
				width = _max.x -  _min.x;
				height = _max.y -  _min.y;
				myResults = {"width" :width, "height" :height,	"min" : {"x":_min.x,"y":_min.y},"max" : {"x":_max.x,"y":_max.y},"iterations" : i,"moves" : _currentResult};				
				return myResults;
				}
	return false;
	}
