	// FIXED PUZZLE SCRIPT
	// ===============================================================

	function PUZ_POP(){};
	
	
	// initialize
	// .................................................
	PUZ_POP.PUZZLE_PIECE_SET_QUEUE = [];	
	PUZ_POP.PUZZLE_PIECES_MODEL = [];
	PUZ_POP.PUZZLE_PIECES_TYPE = [];
	
	PUZ_POP.PUZZLE_INITIALIZE = function()
	{
		// Shared Resources
		PUZ_COMMON.PUZZLE_BASE_IMAGE = '<!--[#SITE_URL;]-->/p/status/res/puzzle_base_pattern.png';
		PUZ_COMMON.PUZZLE_SUCCESS_IMAGE = '<!--[#SITE_URL;]-->/p/status/res/success.png';
	
		// Shared variables
		PUZ_COMMON.HTML_ELEMENT = null;
		PUZ_COMMON.ROW_MAX = 4;
		PUZ_COMMON.COL_MAX = 4;
		
		PUZ_COMMON.ROW_INDEX = 0;
		PUZ_COMMON.COL_INDEX = 0;
		
		PUZ_COMMON.LINK_ELEMENT = null;
		PUZ_COMMON.PIECE_ELEMENT = null;
		PUZ_COMMON.ELEMENT_INDEX = 0;
		
		// Puzzle resources
		PUZ_COMMON.PUZZLE_PIECES = 
		[
			"<!--[#SITE_URL;]-->/p/status/res/block_01a.png",
			"<!--[#SITE_URL;]-->/p/status/res/block_02a.png",
			"<!--[#SITE_URL;]-->/p/status/res/block_03a.png",
			"<!--[#SITE_URL;]-->/p/status/res/block_04a.png",
			
			"<!--[#SITE_URL;]-->/p/status/res/block_04b.png",
			"<!--[#SITE_URL;]-->/p/status/res/block_03a.png",
			"<!--[#SITE_URL;]-->/p/status/res/block_02b.png",
			"<!--[#SITE_URL;]-->/p/status/res/block_01a.png",
			
			"<!--[#SITE_URL;]-->/p/status/res/block_02b.png",
			"<!--[#SITE_URL;]-->/p/status/res/block_01b.png",
			"<!--[#SITE_URL;]-->/p/status/res/block_04a.png",
			"<!--[#SITE_URL;]-->/p/status/res/block_03b.png",			
			
			"<!--[#SITE_URL;]-->/p/status/res/block_03b.png",
			"<!--[#SITE_URL;]-->/p/status/res/block_04b.png",
			"<!--[#SITE_URL;]-->/p/status/res/block_01b.png",
			"<!--[#SITE_URL;]-->/p/status/res/block_02a.png"
		];
		PUZ_POP.PUZZLE_PIECES_MODEL = 
		[
			1, 2, 3, 4,
			4, 3, 2, 1,
			2, 1, 4, 3,			
			3, 4, 1, 2
		];
		PUZ_POP.PUZZLE_PIECES_TYPE = 
		[
			1, 1, 1, 1,
			2, 1, 2, 1,
			2, 2, 1, 2,
			2, 2, 2, 1
		];
		PUZ_POP.PUZZLE_BASE_IMAGE = 'sandbox_files/puzzle_base_pattern.png';
			
		PUZ_COMMON.PUZZLE_PIECES_SET =
		[
			1, 1, 1, 1, 
			1, 1, 1, 1, 
			1, 1, 1, 1,
			1, 1, 1, 1
		];
		PUZ_POP.PUZZLE_PIECE_SET_QUEUE = [];
		
		PUZ_COMMON.PUZZLE_PIECE_RES =
		[
			0, 0, 0, 0, 
			0, 1, 0, 0, 
			0, 0, 1, 0,
			0, 0, 0, 0
		];
		
		// Puzzle Status
		PUZ_COMMON.PUZZLE_PIECE_SELECTED_INDEX = 0;
		PUZ_COMMON.PUZZLE_LOCK_EDIT = false;
		PUZ_COMMON.PUZZLE_COMPLETED = false;
		
		// Reset puzzle
		PUZ_COMMON.CLEAR();
		
		// Loads BGM
		PUZ_COMMON.SE_SELECT = new Audio('<!--[#SITE_URL;]-->/p/status/res/select.mp3'); 
		PUZ_COMMON.SE_SET = new Audio('<!--[#SITE_URL;]-->/p/status/res/set.mp3'); 
		PUZ_COMMON.SE_FREE = new Audio('<!--[#SITE_URL;]-->/p/status/res/free.mp3'); 
	
		PUZ_COMMON.BGM_ONGAME = new Audio('<!--[#SITE_URL;]-->/p/status/res/ongame_03.mp3');
		PUZ_COMMON.BGM_COMPLETE = new Audio('<!--[#SITE_URL;]-->/p/status/res/complete.mp3');	

		// Plays BGM
		PUZ_COMMON.PLAY_BGM(PUZ_COMMON.PUZZLE_COMPLETED);		
	}
	
	
	PUZ_POP.PUZZLE_LOAD = function()
	{
		PUZ_POP.PUZZLE_INITIALIZE();
					
		
		// Fill puzzle set
		// .................................................
		PUZ_COMMON.ROW_INDEX = 0;
		PUZ_COMMON.COL_INDEX = 0;
		
		while(PUZ_COMMON.ROW_INDEX < PUZ_COMMON.ROW_MAX)
		{
			while(PUZ_COMMON.COL_INDEX < PUZ_COMMON.COL_MAX)
			{
				PUZ_COMMON.ELEMENT_INDEX = ((PUZ_COMMON.ROW_INDEX * PUZ_COMMON.COL_MAX) + PUZ_COMMON.COL_INDEX + 1);
				
				// Add Link (onCLick)
				// -------------------------------------
				PUZ_COMMON.LINK_ELEMENT = document.createElement("a");
				PUZ_COMMON.LINK_ELEMENT.setAttribute('href',"javascript:void(0);");
				PUZ_COMMON.LINK_ELEMENT.onclick = function(BIND_INDEX)
									{ 
										return function() 
										{
											PUZ_POP.PUZZLE_SET(BIND_INDEX); 
										}											
									}(PUZ_COMMON.ELEMENT_INDEX);
									
				PUZ_COMMON.LINK_ELEMENT.innerHTML = "<div id=\"DIV_PUZZLE_SET_" + PUZ_COMMON.ELEMENT_INDEX + "\"></div>";
				document.getElementById('main_div').appendChild(PUZ_COMMON.LINK_ELEMENT);
				
				// Add DIV
				// -------------------------------------
				PUZ_COMMON.HTML_ELEMENT = document.getElementById("DIV_PUZZLE_SET_" + PUZ_COMMON.ELEMENT_INDEX);
				PUZ_COMMON.HTML_ELEMENT.className = "DIV_PUZZLE_PATTERN_SET";		
				PUZ_COMMON.HTML_ELEMENT.style.top = (PUZ_COMMON.ROW_INDEX * 100) + 74;
				PUZ_COMMON.HTML_ELEMENT.style.left = (PUZ_COMMON.COL_INDEX * 100) + 24;
				
				// Add Image
				// -------------------------------------
				PUZ_COMMON.PIECE_ELEMENT = document.createElement("img");
				PUZ_COMMON.PIECE_ELEMENT.id = "IMG_PUZZLE_SET_" + PUZ_COMMON.ELEMENT_INDEX;
				document.getElementById('main_div').appendChild(PUZ_COMMON.PIECE_ELEMENT);		
				PUZ_COMMON.PIECE_ELEMENT.className = "IMG_PUZZLE_PATTERN";
				PUZ_COMMON.PUZZLE_PIECES
				
				PUZ_COMMON.PIECE_ELEMENT.setAttribute('src', PUZ_COMMON.PUZZLE_PIECES[PUZ_COMMON.ELEMENT_INDEX - 1]);
				PUZ_COMMON.PIECE_ELEMENT.style.top = PUZ_COMMON.HTML_ELEMENT.style.top;
				PUZ_COMMON.PIECE_ELEMENT.style.left = PUZ_COMMON.HTML_ELEMENT.style.left;
				PUZ_COMMON.PIECE_ELEMENT.style.display = "block";
				
				PUZ_COMMON.COL_INDEX ++;
			}
			PUZ_COMMON.COL_INDEX = 0;
			PUZ_COMMON.ROW_INDEX ++;
		}
		

		// Fill puzzle free
		// .................................................
		PUZ_COMMON.ROW_INDEX = 0;
		while(PUZ_COMMON.ROW_INDEX < PUZ_COMMON.ROW_MAX)
		{
			while(PUZ_COMMON.COL_INDEX < PUZ_COMMON.COL_MAX)
			{
				PUZ_COMMON.ELEMENT_INDEX = ((PUZ_COMMON.ROW_INDEX * PUZ_COMMON.COL_MAX) + PUZ_COMMON.COL_INDEX + 1);
				
				// Add Link (onCLick)
				// -------------------------------------
				PUZ_COMMON.LINK_ELEMENT = document.createElement("a");
				PUZ_COMMON.LINK_ELEMENT.setAttribute('href',"javascript:void(0);");
				PUZ_COMMON.LINK_ELEMENT.innerHTML = "<div id=\"DIV_PUZZLE_FREE_" + PUZ_COMMON.ELEMENT_INDEX + "\"></div>";
				document.getElementById('main_div').appendChild(PUZ_COMMON.LINK_ELEMENT);
				
				// Add DIV
				// -------------------------------------
				PUZ_COMMON.HTML_ELEMENT = document.getElementById("DIV_PUZZLE_FREE_" + PUZ_COMMON.ELEMENT_INDEX);
				PUZ_COMMON.HTML_ELEMENT.className = "DIV_PUZZLE_PATTERN_SET";		
				PUZ_COMMON.HTML_ELEMENT.style.top = (PUZ_COMMON.ROW_INDEX * 100) + 74;
				PUZ_COMMON.HTML_ELEMENT.style.left = (PUZ_COMMON.COL_INDEX * 100) + 524;
				
				
				// Add Image
				// -------------------------------------
				PUZ_COMMON.PIECE_ELEMENT = document.createElement("img");
				PUZ_COMMON.PIECE_ELEMENT.id = "IMG_PUZZLE_FREE_" + PUZ_COMMON.ELEMENT_INDEX;
				document.getElementById('main_div').appendChild(PUZ_COMMON.PIECE_ELEMENT);		
				PUZ_COMMON.PIECE_ELEMENT.className = "IMG_PUZZLE_PATTERN";
				
				PUZ_COMMON.PIECE_ELEMENT.setAttribute('src', PUZ_COMMON.PUZZLE_PIECES[PUZ_COMMON.ELEMENT_INDEX - 1]);
				PUZ_COMMON.PIECE_ELEMENT.style.top = PUZ_COMMON.HTML_ELEMENT.style.top;
				PUZ_COMMON.PIECE_ELEMENT.style.left = PUZ_COMMON.HTML_ELEMENT.style.left;
				if(PUZ_COMMON.PUZZLE_PIECE_RES[PUZ_COMMON.ELEMENT_INDEX - 1] == 1)
				{
					PUZ_COMMON.PIECE_ELEMENT.style.display = "block";
				}
				else
				{
					PUZ_COMMON.PIECE_ELEMENT.style.display = "none";
				}
					
				PUZ_COMMON.COL_INDEX ++;
			}
			PUZ_COMMON.COL_INDEX = 0;
			PUZ_COMMON.ROW_INDEX ++;
		}	
	}
	

	PUZ_POP.PUZZLE_SET = function(ITEM_INDEX)
	{
		
		if(PUZ_COMMON.PUZZLE_LOCK_EDIT === true)
		{
			return;
		}
		
		// Check if empty, do nothing
		if(PUZ_COMMON.PUZZLE_PIECES_SET[ITEM_INDEX - 1] <= 0)
		{
			return;
		}
		
		// Fill pattern fill indexes
		else
		{
			// Increment attempts
			PUZ_COMMON.PUZZLE_PIECE_ATTEMPT ++;
			
			// Compute current grid location
			var FILL_X = 0;
			var FILL_Y = 0;
			var FILL_INDEX = 0;
			if(ITEM_INDEX > 1)
			{
				FILL_X = (ITEM_INDEX - 1) % PUZ_COMMON.COL_MAX;
				FILL_Y = ((ITEM_INDEX - 1) - FILL_X) / PUZ_COMMON.COL_MAX;
			}
			
			// Get Grid Properties
			var PIECE_MODEL = PUZ_POP.PUZZLE_PIECES_MODEL[ITEM_INDEX - 1];
			var PIECE_TYPE = PUZ_POP.PUZZLE_PIECES_TYPE[ITEM_INDEX - 1];
	
			
			// collect pops for X grid
			FILL_INDEX = 0;
			while(FILL_INDEX < PUZ_COMMON.COL_MAX)
			{
				PUZ_COMMON.ELEMENT_INDEX = (FILL_Y * PUZ_COMMON.COL_MAX) + FILL_INDEX + 1;
				if
				(
					PUZ_COMMON.PUZZLE_PIECES_SET[PUZ_COMMON.ELEMENT_INDEX - 1] > 0 &&
					(
						PUZ_POP.PUZZLE_PIECES_MODEL[PUZ_COMMON.ELEMENT_INDEX - 1] == PIECE_MODEL ||
						PUZ_POP.PUZZLE_PIECES_TYPE[PUZ_COMMON.ELEMENT_INDEX - 1] == PIECE_TYPE	
					)
				)
				{
					PUZ_POP.PUZZLE_PIECE_SET_QUEUE.push(PUZ_COMMON.ELEMENT_INDEX);
				}
				FILL_INDEX ++;
			}
			
			
			// fill Y
			FILL_INDEX = 0; // Should exclude current Row since already exists
			while(FILL_INDEX < PUZ_COMMON.ROW_MAX)
			{
				PUZ_COMMON.ELEMENT_INDEX = (FILL_INDEX * PUZ_COMMON.COL_MAX) + FILL_X + 1;
				if
				(
					PUZ_COMMON.PUZZLE_PIECES_SET[PUZ_COMMON.ELEMENT_INDEX - 1] > 0 &&
					(
						PUZ_POP.PUZZLE_PIECES_MODEL[PUZ_COMMON.ELEMENT_INDEX - 1] == PIECE_MODEL ||
						PUZ_POP.PUZZLE_PIECES_TYPE[PUZ_COMMON.ELEMENT_INDEX - 1] == PIECE_TYPE	
					)
				)
				{
					PUZ_POP.PUZZLE_PIECE_SET_QUEUE.push(PUZ_COMMON.ELEMENT_INDEX);
				}
				
				FILL_INDEX ++;
			}
			
			// call fill pattern
			PUZ_POP.POP_PATTERN();
		}
	}
	
	PUZ_POP.POP_PATTERN = function()
	{
		// if nothing to fill
		if(PUZ_POP.PUZZLE_PIECE_SET_QUEUE.length <= 0)
		{
			PUZ_COMMON.PUZZLE_PIECE_SELECTED_INDEX ++;
			if(PUZ_COMMON.PUZZLE_PIECE_SELECTED_INDEX >= PUZ_COMMON.PUZZLE_PIECES.length)
			{
				PUZ_COMMON.PUZZLE_PIECE_SELECTED_INDEX = 0;
			}
			
			// Check result
			PUZ_POP.CHECK_RESULT();
			return;
		}
		
		
		
		// Assign image
		var CURRENT_INDEX = PUZ_POP.PUZZLE_PIECE_SET_QUEUE[0];
		var CURRENT_IMAGE = document.getElementById("IMG_PUZZLE_SET_" + CURRENT_INDEX);
		PUZ_POP.PUZZLE_PIECE_SET_QUEUE.shift();
		
		// Skip if already removed
		if(PUZ_COMMON.PUZZLE_PIECES_SET[CURRENT_INDEX - 1] <= 0)
		{
			PUZ_POP.POP_PATTERN();
			return;
		}		
		PUZ_COMMON.PUZZLE_PIECES_SET[CURRENT_INDEX - 1] = 0;
		//alert(CURRENT_IMAGE.id);

		// FadeOut Animation
		/*
		$('#' + CURRENT_IMAGE.id).fadeOut('slow');
		$('#' + CURRENT_IMAGE.id).promise().done
		(function()
		{
		    PUZ_POP.POP_PATTERN();
		});
		*/
		
		CURRENT_IMAGE.style.display = "none";
		PUZ_COMMON.SE_SET.play();
		PUZ_POP.POP_PATTERN();
	}
	
	
	PUZ_POP.CHECK_RESULT = function()
	{
		// check result
		var PIECE_INDEX = 0;
		while(PIECE_INDEX < PUZ_COMMON.PUZZLE_PIECES_SET.length)
		{
			// failure
			if(PUZ_COMMON.PUZZLE_PIECES_SET[PIECE_INDEX] !== PUZ_COMMON.PUZZLE_PIECE_RES[PIECE_INDEX])
			{
				PUZ_POP.CHECK_CLEAR(); // Clear if wrong pattern
				return;
			}
			PIECE_INDEX ++;
		}
		
		// Success
		PUZ_COMMON.PUZZLE_LOCK_EDIT = true;
		PUZ_COMMON.PUZZLE_COMPLETED = true;
		document.getElementById("IMG_PUZZLE_SUCCESS").style.display = "block";
		PUZ_COMMON.PLAY_BGM();
	}
	
	PUZ_POP.CHECK_CLEAR = function()
	{
		// check result
		var PIECE_INDEX = 0;

		// Clear if fully filled
		if(PUZ_COMMON.PUZZLE_PIECE_ATTEMPT < 3)
		{
			return;
		}
		
		// Clear
		PUZ_COMMON.PUZZLE_PIECE_ATTEMPT = 0;
		PIECE_INDEX = 0;
		while(PIECE_INDEX < PUZ_COMMON.PUZZLE_PIECES_SET.length)
		{
			PUZ_COMMON.PUZZLE_PIECES_SET[PIECE_INDEX] = 1;
			document.getElementById("IMG_PUZZLE_SET_" + (PIECE_INDEX + 1)).style.display = "block";
			PIECE_INDEX ++;
		}
		PUZ_COMMON.SE_FREE.play();
	}
