/*
 * Clicking Bricks (alpha2)
 * Pattern 003 (the default pattern)
 * Copyright 2001, 2002, 2008 Michael Farrell
 * http://micolous.id.au/
 */
 
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 

var block1, block2, block3, block4, block5, lblMoveCount, lblTitle, moveCount=0, resetButton;
// this is the default game pattern.  the solution to this one is 5, 4, 1
var pattern = new Array(15, 24, 29, 21, 5);

function reset() {
	moveCount = 0;
	lblMoveCount.setLabel('You have not yet made a move.');
	block1.setVisible(true);
	block2.setVisible(true);
	block3.setVisible(true);
	block4.setVisible(true);
	block5.setVisible(true);
}

function init() {
	lblTitle = new SFLabel(10,35,'Clicking Bricks SVGForm App');
	lblTitle.setFont("Calibri", "40px");
	(new SFLabel(10, 550, "Copyright 2001, 2002, 2008 Michael Farrell.")).setFont("Calibri", "20px");
	btnWebsite = new SFButton(400,530,200,30,"Visit my website");
	btnWebsite.setFont("Calibri", "20px");
	btnWebsite.onPress = function() { document.location.href="http://micolous.id.au/"; };

	// get pattern from query url
	if (document.location.search.length < 2) {
		// no pattern, put in default
		// (15, 24, 29, 21, 5)... best solution 5, 4, 1
		document.location.search = "16545445";
		return;
	} else {
		// decode pattern
		pati = parseInt(document.location.search.substr(1));
		if (pati < 1082401) {
			// less than minimum game number having one on each button flippable
			(new SFLabel(10,100,"Sorry, the game number selected is less than the minimum game number.")).setFont("Calibri", "20px");
			(new SFLabel(10,150,"You must assign at least one brick to flip for each brick.")).setFont("Calibri", "20px");
			(new SFLabel(10,200,"Therefore, the absolute minimum game number is 1082401 (which is unsolvable).")).setFont("Calibri", "20px");
			return;
		}
		pattern = new Array();
		
		for (x = 0; x < 5; x++) {
			pattern.push(pati % 32);
			pati = pati >> 5;
		}
		
		pattern = pattern.reverse();
	}

	block1 = new SFButton(10, 50, 200,100,'1');
	block2 = new SFButton(210,50, 100,200,'2');
	block4 = new SFButton(10, 150,100,200,'4');
	block5 = new SFButton(110,250,200,100,'5');
	block3 = new SFButton(110,150,100,100,'3');
	resetButton = new SFButton(10,420,100,30,'Reset');
	lblMoveCount = new SFLabel(10,400,'You have not yet made a move.');

	
	block1.onPress = function() { handleClick(0); };
	block2.onPress = function() { handleClick(1); };
	block3.onPress = function() { handleClick(2); };
	block4.onPress = function() { handleClick(3); };
	block5.onPress = function() { handleClick(4); };
	
	block1.setFont("Calibri", "40px");
	block2.setFont("Calibri", "40px");
	block3.setFont("Calibri", "40px");
	block4.setFont("Calibri", "40px");
	block5.setFont("Calibri", "40px");
	resetButton.setFont("Calibri", "20px");
	lblMoveCount.setFont("Calibri", "20px");
	
	resetButton.onPress = reset;
}   		

function handleClick(b) {
	// note, b is 0-indexed.
	patternForBlock = pattern[b];
	if (patternForBlock%2==1)
		block1.toggleVisible();

	if ((patternForBlock>>1)%2==1)
		block2.toggleVisible();

	if ((patternForBlock>>2)%2==1)
		block3.toggleVisible();

	if ((patternForBlock>>3)%2==1)
		block4.toggleVisible();

	if ((patternForBlock>>4)%2==1)
		block5.toggleVisible();
	
	moveCount++;
	if (moveCount == 1) {
		lblMoveCount.setLabel("You have made one move so far.");
	} else {
   		lblMoveCount.setLabel("You have made " + moveCount + " moves so far.");
   	}

	if (!block1.getVisible() && !block2.getVisible() && !block3.getVisible() && !block4.getVisible() && !block5.getVisible()) {
		// winner is you!!!
		lblMoveCount.setLabel("Congratulations, you won in " + moveCount + " moves.");
	}
}

init();

