// level specification

var numberOfLevels = 20;
var numberOfChallengeLevels = 20;
var numberOfPuzzleLevels = 20;
var gameStarted = false;

var startChallengeYellows=1;
var startChallengeReds=1;
var startChallengeYellowsIncrement=2;
var startChallengeRedsIncrement=2;

var numberOfChallengeYellows = startChallengeYellows; // in a challenge mode game, the number of yellows in a level
var numberOfChallengeReds = startChallengeReds; // in a challenge mode game, the number of reds in a level
var challengeYellowsIncrement=startChallengeYellowsIncrement;
var challengeRedsIncrement=startChallengeRedsIncrement;

// Level Text
var hintText = new Array(20);

var puzzleHintText = new Array ("",
"Use arrow keys to move",
 "Catch all the pollen",
 "Don't catch the red pollen",
 "Explosions change red pollen into yellow pollen",
 "You can hide below the bricks",
 "Explode the box for bonus points",
 "Explode the box to create extra pollen",
 "Powerup lets you catch red pollen",
 "Bouncy bouncy!",
 "Powerup City",
 "Antigravity!",
 "All together now",
 "Flying school",
 "Mexican wave",
 "What goes up must come down",
 "Pachinko!",
 "Catch me if you can",
 "Blocks on the Move",
 "Pollentastic",
 "Chaotic behaviour");
 
var challengeHintText = new Array ("",
 "Challenge level 1",
 "Challenge level 2",
 "Challenge level 3",
 "Challenge level 4",
 "Challenge level 5",
 "Challenge level 6",
 "Challenge level 7",
 "Challenge level 8",
 "Challenge level 9",
 "Challenge level 10",
 "Challenge level 11",
 "Challenge level 12",
 "Challenge level 13",
 "Challenge level 14",
 "Challenge level 15",
 "Challenge level 16",
 "Challenge level 17",
 "Challenge level 18",
 "Challenge level 19",
 "Challenge level 10");

// source locations (easier than typing the same numbers each time)
var source_y = 2; // all sources start at same height

// one or three sources - use source_x_02 for single source
var source_x_01 = 125;
var source_x_02 = 251;
var source_x_03 = 377;

// two sources
var source_x_04 = 167;
var source_x_05 = 335;

// four sources
var source_x_06 = 100;
var source_x_07 = 200;
var source_x_08 = 302;
var source_x_09 = 402;

// five sources - use source_x_02 for middle sources
var source_x_10 = 83;
var source_x_11 = 167;
var source_x_12 = 335;
var source_x_13 = 419;

function initSourceX()
{
	// because the width of the source graphics may vary, this automatically moves the images left to the right point. Runs at page load only.
	source_x_01 -= sourceWidth/2;
	source_x_02 -= sourceWidth/2;
	source_x_03 -= sourceWidth/2;
	source_x_04 -= sourceWidth/2;
	source_x_05 -= sourceWidth/2;
	source_x_06 -= sourceWidth/2;
	source_x_07 -= sourceWidth/2;
	source_x_08 -= sourceWidth/2;
	source_x_09 -= sourceWidth/2;
	source_x_10 -= sourceWidth/2;
	source_x_11 -= sourceWidth/2;
	source_x_12 -= sourceWidth/2;
	source_x_13 -= sourceWidth/2;
}

var numberOfFreefall=0; // we won't want more than 1 Freefall powerup in a level
var numberOfAntigravity=0; // we won't want more than 1 Antigravity powerup in a level

// var source1_x = 4;
//var source1_y = 4;
//var source2_x = 168;
//var source2_y = 4;
//var source3_x = 336;
//var source3_y = 4;

function CreateLevels()
{
	// reference for creating level objects
	// CreateSource(colour, x_pos, y_pos) - colour is just the graphic
	// StartSource(oSource, number, colour, interval, delay) - colour of balls created
	// CreatePowerup(type, x_pos, y_pos, vx, vy, speed) - vx, vy must be unit vector
	
	// mapwidth = 368 px
	// map height = 380 px
	// no objects above 50 px
	if(gameMode==0) // Puzzle Mode
	{
		switch (currentLevel)
		{
			case 1:
				// create source
				var oSource2 = CreateSource("yellow", source_x_02, source_y);
					
				// start the yellow balls
				ballsToCatch = 1;
				StartSource(oSource2, ballsToCatch, "yellow", 4, 0); 
				break;
			case 2:
				// create source
				var oSource2 = CreateSource("yellow", source_x_02, source_y);
					
				// start the yellow balls
				ballsToCatch = 5;
				StartSource(oSource2, ballsToCatch, "yellow", 4, 0); 
				break;
			case 3:
				// create sources
				var oSource1 = CreateSource("red", source_x_04, source_y);
				var oSource2 = CreateSource("yellow", source_x_05, source_y);
				
				// start the yellow balls
				ballsToCatch = 5;
				StartSource(oSource2, ballsToCatch, "yellow", 4, 0);
				
				// start some red balls with a delay
				StartSource(oSource1, 1, "red", 8, 2);
				break;
			case 4:
				// create sources
				var oSource1 = CreateSource("red", source_x_01, source_y);
				var oSource2 = CreateSource("yellow", source_x_02, source_y);
				var oSource3 = CreateSource("red", source_x_03, source_y);
				
				// start the yellow balls
				ballsToCatch = 10;
				StartSource(oSource2, ballsToCatch, "yellow", 4, 0);
				
				// start some red balls with a delay
				StartSource(oSource1, 4, "red", 12, 2);
				StartSource(oSource3, 4, "red", 12, 6);
				break;
			case 5:
				// create sources
				var oSource1 = CreateSource("red", source_x_01, source_y);
				var oSource2 = CreateSource("yellow", source_x_02, source_y);
				var oSource3 = CreateSource("red", source_x_03, source_y);
				
				// start the yellow balls
				ballsToCatch = 10;
				StartSource(oSource2, ballsToCatch, "yellow", 4, 0);
				
				// start some red balls with a delay
				StartSource(oSource1, 4, "red", 12, 2);
				StartSource(oSource3, 4, "red", 12, 6);
				
				// create blocks
				CreatePowerup("block", 1, 254, 1, 0, 0);
				CreatePowerup("block", 437, 254, 1, 0, 0);
				break;
			case 6:
				// create sources
				var oSource1 = CreateSource("red", source_x_04, source_y);
				var oSource2 = CreateSource("yellow", source_x_05, source_y);
				
				// start the yellow balls
				ballsToCatch = 10;
				StartSource(oSource2, ballsToCatch, "yellow", 4, 0);
				
				// start the red balls
				StartSource(oSource1, 5, "red", 8, 2);
				
				// create a powerup
				CreatePowerup("points", 219, 200, 1, 0, 0);
				break;
			case 7:
				// create sources
				var oSource1 = CreateSource("red", source_x_01, source_y);
				var oSource2 = CreateSource("yellow", source_x_02, source_y);
				var oSource3 = CreateSource("red", source_x_03, source_y);
				
				// start the yellow balls
				ballsToCatch = 10;
				StartSource(oSource2, ballsToCatch, "yellow", 4, 0);
				
				// start some red balls with a delay
				StartSource(oSource1, 4, "red", 16, 4);
				StartSource(oSource3, 4, "red", 16, 4);
				
				// create blocks
				CreatePowerup("block", 219, 140, 1, 0, 10);
				CreatePowerup("balls", 219, 200, 1, 0, 0);
				break;
			case 8:
				// create sources
				var oSource1 = CreateSource("red", source_x_01, source_y);
				var oSource2 = CreateSource("yellow", source_x_02, source_y);
				var oSource3 = CreateSource("red", source_x_03, source_y);
				
				// start the yellow balls
				ballsToCatch = 10;
				StartSource(oSource2, ballsToCatch, "yellow", 6, 8);
				
				// start some red balls with a delay
				StartSource(oSource1, 4, "red", 3, 0);
				StartSource(oSource3, 4, "red", 3, 1);
				
				// create blocks
				CreatePowerup("catchreds", 219, 140, 1, 0, 10);
				CreatePowerup("catchreds", 219, 200, -1, 0, 10);
				break;
			case 9:
				// create sources
				var oSource1 = CreateSource("red", source_x_01, source_y);
				var oSource2 = CreateSource("yellow", source_x_02, source_y);
				var oSource3 = CreateSource("red", source_x_03, source_y);
				
				// start the yellow balls
				ballsToCatch = 10;
				StartSource(oSource2, ballsToCatch, "yellow", 8, 0);
				
				// start some red balls with a delay
				StartSource(oSource1, 4, "red", 24, 4);
				StartSource(oSource3, 4, "red", 24, 12);
				
				// create blocks
				CreatePowerup("block", 219, 100, 1, 0, 0);
				CreatePowerup("catchreds", 219, 220, 0, -1, 10);
				CreatePowerup("block", 219, 254, 1, 0, 0);
				
				CreatePowerup("points", 100, 150, 0, -1, 0);
				CreatePowerup("balls", 338, 150, 0, -1, 0);
				break;
			case 10:
				// create sources
				var oSource1 = CreateSource("red", source_x_01, source_y);
				var oSource2 = CreateSource("yellow", source_x_02, source_y);
				var oSource3 = CreateSource("red", source_x_03, source_y);
				
				// start the yellow balls
				ballsToCatch = 10;
				StartSource(oSource2, ballsToCatch, "yellow", 8, 8);
				
				// start some red balls with a delay
				StartSource(oSource1, 5, "red", 10, 0);
				StartSource(oSource3, 5, "red", 10, 4);
				
				// create blocks
				CreatePowerup("catchreds", 20, 170, 1, 0, 10);
				CreatePowerup("balls", 418, 170, 0, -1, 10);
				CreatePowerup("catchreds", 418, 220, -1, 0, 10);
				CreatePowerup("balls", 20, 220, 0, 1, 10);
				
				CreatePowerup("points", 219, 120, 0, -1, 0);
				break;
			case 11:
				// create sources
				var oSource1 = CreateSource("red", source_x_01, source_y);
				var oSource2 = CreateSource("yellow", source_x_02, source_y);
				var oSource3 = CreateSource("red", source_x_03, source_y);
							
				// create a powerup
				CreatePowerup("catchreds", 219, 204, 1, 0, -10);
				CreatePowerup("balls", 219, 158, 1, 0, 10);
				CreatePowerup("antigravity", 219, 112, 1, 0, -10);
				
				ballsToCatch = 30;
				StartSource(oSource2, ballsToCatch, "yellow", 4, 0);
				
				// start some red balls with a delay
				StartSource(oSource1, 10, "red", 16, 3);
				StartSource(oSource3, 10, "red", 16, 5);
				break;
			case 12:
				// create sources
				var oSource1 = CreateSource("yellow", source_x_10, source_y);
				var oSource2 = CreateSource("red", source_x_11, source_y);
				var oSource3 = CreateSource("yellow", source_x_02, source_y);
				var oSource4 = CreateSource("red", source_x_12, source_y);
				var oSource5 = CreateSource("yellow", source_x_13, source_y);
				
				// create a powerup
	//			CreatePowerup("freefall", 152, 249, 0, -1, 0);
				
				ballsToCatch = 30;
				StartSource(oSource1, 10, "yellow", 16, 0);
				StartSource(oSource3, 10, "yellow", 16, 0);
				StartSource(oSource5, 10, "yellow", 16, 0);
				
				// start some red balls with a delay
				StartSource(oSource2, 10, "red", 16, 0);
				StartSource(oSource4, 10, "red", 16, 0);
				break;
			case 13:
				// create sources
				var oSource1 = CreateSource("red", source_x_04, source_y);
				var oSource2 = CreateSource("yellow", source_x_05, source_y);
				
				// start the yellow balls
				ballsToCatch = 20;
				StartSource(oSource2, ballsToCatch, "yellow", 4, 0);
				
				// start the red balls
				StartSource(oSource1, 20, "red", 8, 2);
				
				// create powerups
				CreatePowerup("block", 100, 321, 0, -1, 0);
				CreatePowerup("block", 338, 321, 0, -1, 0);
				
				CreatePowerup("balls", 100, 88, 0, -1, 0);
				CreatePowerup("points", 219, 170, 0, -1, 0);
				CreatePowerup("catchreds", 338, 88, 0, -1, 0);
				break;
			case 14:
				// create sources
				var oSource1 = CreateSource("yellow", source_x_10, source_y);
				var oSource2 = CreateSource("red", source_x_11, source_y);
				var oSource3 = CreateSource("yellow", source_x_02, source_y);
				var oSource4 = CreateSource("red", source_x_12, source_y);
				var oSource5 = CreateSource("yellow", source_x_13, source_y);
				
				// create a powerup
				CreatePowerup("catchreds", 1, 117, 0, -1, 0);
				CreatePowerup("catchreds", 1, 168, 0, -1, 0);
				CreatePowerup("points", 437, 200, 0, -1, 0);
				CreatePowerup("points", 437, 251, 0, -1, 0);
				
				ballsToCatch = 30;
				StartSource(oSource1, 10, "yellow", 10, 0);
				StartSource(oSource3, 10, "yellow", 10, 1);
				StartSource(oSource5, 10, "yellow", 10, 2);
				
				// start some red balls with a delay
				StartSource(oSource2, 10, "red", 10, 0.5);
				StartSource(oSource4, 10, "red", 10, 1.5);
				break;
			case 15:
				// create sources
				var oSource1 = CreateSource("red", source_x_01, source_y);
				var oSource2 = CreateSource("yellow", source_x_02, source_y);
				var oSource3 = CreateSource("red", source_x_03, source_y);
				
				// create powerups
				CreatePowerup("block", 107, 179, 0, -1, 10);
				CreatePowerup("block", 219, 179, 0, -1, 10);
				CreatePowerup("block", 331, 179, 0, -1, 10);
				
				ballsToCatch = 30;
				StartSource(oSource2, ballsToCatch, "yellow", 4, 0);
				
				// start some red balls with a delay
				StartSource(oSource1, 8, "red", 16, 3);
				StartSource(oSource3, 8, "red", 16, 5);
				break;
			case 16:
				// create sources
				var oSource1 = CreateSource("red", source_x_04, source_y);
				var oSource2 = CreateSource("yellow", source_x_05, source_y);
				
				// start the yellow balls
				ballsToCatch = 30;
				StartSource(oSource2, ballsToCatch, "yellow", 4, 0);
				
				// start the red balls
				StartSource(oSource1, 10, "red", 16, 2);
				
				// create powerups
				CreatePowerup("block", 120, 83, 0, -1, 0);
				CreatePowerup("block", 318, 83, 0, -1, 0);
				
				CreatePowerup("block", 20, 185, 0, -1, 0);
				CreatePowerup("block", 219, 185, 0, -1, 0);
				CreatePowerup("block", 418, 185, 170, -1, 0);
				
	
				break;
			case 17:
				// create sources
				var oSource1 = CreateSource("red", source_x_01, source_y);
				var oSource2 = CreateSource("yellow", source_x_02, source_y);
				var oSource3 = CreateSource("red", source_x_03, source_y);
				
				// start the yellow balls
				ballsToCatch = 30;
				StartSource(oSource2, ballsToCatch, "yellow", 8, 0);
				
				// start some red balls with a delay
				StartSource(oSource1, 10, "red", 24, 4);
				StartSource(oSource3, 10, "red", 24, 12);
				
				// create blocks
				CreatePowerup("block", 40, 97, 1, 0, 0);
				CreatePowerup("block", 403, 97, 0, -1, 0);
				
				CreatePowerup("block", 1, 129, -1, 0, 0);
				CreatePowerup("block", 437, 129, 0, 1, 0);
				
				CreatePowerup("block", 1, 161, -1, 0, 0);
				CreatePowerup("block", 437, 161, 0, 1, 0);
				
				CreatePowerup("block", 40, 193, 1, 0, 0);
				CreatePowerup("block", 403, 193, 0, -1, 0);
				
				CreatePowerup("balls", 1, 230, 1, 0, 10);
				CreatePowerup("balls", 437, 230, -1, 0, 10);
				break;
			case 18:
				// create sources
				var oSource1 = CreateSource("red", source_x_04, source_y);
				var oSource2 = CreateSource("yellow", source_x_05, source_y);
				
				// start the yellow balls
				ballsToCatch = 30;
				StartSource(oSource2, ballsToCatch, "yellow", 4, 0);
				
				// start the red balls
				StartSource(oSource1, 10, "red", 16, 2);
				
				// create blocks
				CreatePowerup("block", 20, 168, 0, -1, 10);
				
				CreatePowerup("block", 219, 118, 1, 0, 10);
				CreatePowerup("block", 219, 168, 1, 0, 10);
				CreatePowerup("block", 219, 218, 1, 0, 10);
				
				CreatePowerup("block", 418, 168, 0, -1, 10);
				
				CreatePowerup("balls", 100, 230, 0, -1, 0);
				CreatePowerup("antigravity", 338, 230, 0, -1, 0);
				break;
			case 19:
				// create sources
				var oSource1 = CreateSource("yellow", source_x_10, source_y);
				var oSource2 = CreateSource("red", source_x_11, source_y);
				var oSource3 = CreateSource("yellow", source_x_02, source_y);
				var oSource4 = CreateSource("red", source_x_12, source_y);
				var oSource5 = CreateSource("yellow", source_x_13, source_y);
				
				ballsToCatch = 30;
				StartSource(oSource1, 10, "yellow", 16, 0);
				StartSource(oSource3, 10, "yellow", 16, 0);
				StartSource(oSource5, 10, "yellow", 16, 0);
				
				// start some red balls with a delay
				StartSource(oSource2, 8, "red", 16, 0);
				StartSource(oSource4, 8, "red", 16, 0);
				
				// create a powerup
				CreatePowerup("block", 155, 130, 0, -1, 0);
				CreatePowerup("block", 219, 130, 0, -1, 0);
				CreatePowerup("block", 283, 130, 0, -1, 0);
				
				CreatePowerup("block", 124, 162, 0, -1, 0);
				CreatePowerup("catchreds", 188, 162, 0, -1, 0);
				CreatePowerup("balls", 252, 162, 0, -1, 0);
				CreatePowerup("block", 316, 162, 0, -1, 0);
				
				CreatePowerup("block", 1, 264, 0, -1, 0);
				CreatePowerup("block", 437, 264, 0, -1, 0);
				
				break;
			case 20:
				// create sources
				var oSource1 = CreateSource("red", source_x_01, source_y);
				var oSource2 = CreateSource("yellow", source_x_02, source_y);
				var oSource3 = CreateSource("red", source_x_03, source_y);
				
				ballsToCatch = 30;
				StartSource(oSource2, ballsToCatch, "yellow", 4, 0);
				
				// start some red balls with a delay
				StartSource(oSource1, 10, "red", 12, 3);
				StartSource(oSource3, 10, "red", 12, 5);
				
				// create a powerup
				var oPowerup1 = CreatePowerup("block", 20, 82, 1, 0, 10);
				var oPowerup2 = CreatePowerup("block", 418, 82, 0, 1, 10);
				var oPowerup3 = CreatePowerup("block", 418, 270, -1, 0, 10);
				var oPowerup4 = CreatePowerup("block", 20, 270, 0, -1, 10);
				
				var oPowerup1 = CreatePowerup("catchreds", 219, 128, 1, 0, 0);
				var oPowerup1 = CreatePowerup("balls", 219, 178, 1, 0, 0);
				var oPowerup1 = CreatePowerup("freefall", 219, 228, 1, 0, 0);
				break;
			// CreateSource(colour, x_pos, y_pos)
			// StartSource(oSource, number, colour, interval, delay)
			// CreatePowerup(type, x_pos, y_pos, vx, vy, speed)
		}
		return;
	}
	else
	{
		if(gameMode==1) // Challenge Mode
		{
			numberOfChallengeYellows = startChallengeYellows; // in a challenge mode game, the number of yellows in a level
			numberOfChallengeReds = startChallengeReds; // in a challenge mode game, the number of reds in a level
			challengeYellowsIncrement=startChallengeYellowsIncrement;
			challengeRedsIncrement=startChallengeRedsIncrement;
			for (i = 1; i < (currentLevel); i++)
			{
				numberOfChallengeYellows+=challengeYellowsIncrement;
				numberOfChallengeReds+=challengeRedsIncrement;
				challengeYellowsIncrement+=1;
				challengeRedsIncrement+=1;
			}
		}
		// Challenge mode levels share a common template
		// create sources
		var oSource1 = CreateSource("red", source_x_04, source_y);
		var oSource2 = CreateSource("yellow", source_x_05, source_y);
			
		// start the yellow balls
		ballsToCatch = numberOfChallengeYellows;
		StartSource(oSource2, ballsToCatch, "yellow", 2, 2);
				
		// start some red balls with a delay
		StartSource(oSource1, numberOfChallengeReds, "red", 1, 0);
		
		// create powerups
		// up to 4 random powerups, each at a different (preset) height to avoid clashes
		// on average 2 powerups i.e. 50% chance to create each one
		numberOfFreefall=0; // we won't want more than 1 Freefall powerup
		numberOfAntigravity=0; // we won't want more than 1 Antigravity powerup

		if (RandomInteger(2)==1)
		{
			var newPowerup = createRandomPowerup();
			var oPowerup1 = CreatePowerup(newPowerup._type, newPowerup._x_pos, 80, newPowerup._vx, newPowerup._vy, newPowerup._speed);
		}
		
		if (RandomInteger(2)==1)
		{	
			var newPowerup = createRandomPowerup();
			var oPowerup2 = CreatePowerup(newPowerup._type, newPowerup._x_pos, 130, newPowerup._vx, newPowerup._vy, newPowerup._speed);
		}
		
		if (RandomInteger(2)==1)
		{
			var newPowerup = createRandomPowerup();
			var oPowerup3 = CreatePowerup(newPowerup._type, newPowerup._x_pos, 180, newPowerup._vx, newPowerup._vy, newPowerup._speed);
		}
		
		if (RandomInteger(2)==1)
		{
			var newPowerup = createRandomPowerup();
			var oPowerup4 = CreatePowerup(newPowerup._type, newPowerup._x_pos, 230, newPowerup._vx, newPowerup._vy, newPowerup._speed);
		}
	}
}

function createRandomPowerup()
{
	var my_type;
	var my_x_pos;
	var my_y_pos=80; // not actually using this yet, add it if needed
	var my_vx=1;
	var my_vy=0;
	var my_speed=0;
	
	var randomNumber = RandomInteger(5)-1; // random number between 0 and 4

	switch (randomNumber)
	{
		case 0:
			my_type="block";
			break;
		case 1:
			my_type="catchreds";
			break;
		case 2:
			if(numberOfAntigravity==0)
			{
				my_type="antigravity";
				numberOfAntigravity+=1;
			}
			else
			{
				my_type="balls";
			}
			break;
		case 3:
			if(numberOfFreefall==0)
			{
				my_type="freefall";
				numberOfFreefall+=1;
			}
			else
			{
				my_type="catchreds";
			}
			break;
		case 4:
			my_type="balls";
			break;
	}
	
	my_x_pos=RandomInteger(437); // random horizontal position
	
	// random movement direction
	randomNumber = RandomInteger(4)-1; // random number between 0 and 3
	switch (randomNumber)
	{
		case 0:
			my_vx=0;
			my_vy=1;
			break;
		case 1:
			my_vx=1;
			my_vy=0;
			break;
		case 2:
			my_vx=0;
			my_vy=-1;
			break;
		case 3:
			my_vx=-1;
			my_vy=0;
			break;
	}
	
	// randomly stationary or moving
	if (RandomInteger(2)==1)
	{
		my_speed=0;
	}
	else
	{
		my_speed=10;
	}
	
	return {_type:my_type, _x_pos:my_x_pos, _y_pos:my_y_pos, _vx:my_vx, _vy:my_vy, _speed:my_speed};	
}
