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

	function PUZ_PATTERN(){};
	
	
	// initialize
	// .................................................
	PUZ_PATTERN.PUZZLE_PIECE_SET_QUEUE = [];	
	
	PUZ_PATTERN.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/box_01.png",
			"<!--[#SITE_URL;]-->/p/status/res/box_02.png",
			"<!--[#SITE_URL;]-->/p/status/res/box_03.png",
			"<!--[#SITE_URL;]-->/p/status/res/box_04.png"
		];
		PUZ_PATTERN.PUZZLE_BASE_IMAGE = '<!--[#SITE_URL;]-->/p/status/res/puzzle_base_pattern.png';
			
		PUZ_COMMON.PUZZLE_PIECES_SET =
		[
			0, 0, 0, 0, 
			0, 0, 0, 0, 
			0, 0, 0, 0,
			0, 0, 0, 0
		];
		PUZ_PATTERN.PUZZLE_PIECE_SET_QUEUE = [];
		
		PUZ_COMMON.PUZZLE_PIECE_RES =
		[
			2, 2, 2, 2, 
			4, 1, 1, 1, 
			4, 3, 3, 1,
			4, 4, 3, 1
		];
		
		// 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_02.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_PATTERN.PUZZLE_LOAD = function()
	{
		PUZ_PATTERN.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_PATTERN.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.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 = "none";
				
				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.PUZZLE_PIECE_RES[PUZ_COMMON.ELEMENT_INDEX - 1] - 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.COL_INDEX ++;
			}
			PUZ_COMMON.COL_INDEX = 0;
			PUZ_COMMON.ROW_INDEX ++;
		}	
	}
	

	PUZ_PATTERN.PUZZLE_SET = function(ITEM_INDEX)
	{
		
		if(PUZ_COMMON.PUZZLE_LOCK_EDIT === true)
		{
			return;
		}
		
		// Check if not empty, do nothing
		if(PUZ_COMMON.PUZZLE_PIECES_SET[ITEM_INDEX - 1] > 0)
		{
			return;
		}
		
		// Fill pattern fill indexes
		else
		{
			// Compute current grid location
			var FILL_X = 0;
			var FILL_Y = 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;
			}
			
			
			// fill X
			while(FILL_X < PUZ_COMMON.COL_MAX)
			{
				PUZ_COMMON.ELEMENT_INDEX = (FILL_Y * PUZ_COMMON.COL_MAX) + FILL_X + 1;
				if(PUZ_COMMON.PUZZLE_PIECES_SET[PUZ_COMMON.ELEMENT_INDEX - 1] > 0)
				{
					break;
				}
				
				PUZ_PATTERN.PUZZLE_PIECE_SET_QUEUE.push(PUZ_COMMON.ELEMENT_INDEX);
				FILL_X ++;
			}
			FILL_X --; // Exclude last increment before exiting loop
			
			
			// fill Y
			FILL_Y ++; // Should exclude current Row since already exists
			while(FILL_Y < PUZ_COMMON.ROW_MAX)
			{
				PUZ_COMMON.ELEMENT_INDEX = (FILL_Y * PUZ_COMMON.COL_MAX) + FILL_X + 1;
				if(PUZ_COMMON.PUZZLE_PIECES_SET[PUZ_COMMON.ELEMENT_INDEX - 1] > 0)
				{
					break;
				}
				
				PUZ_PATTERN.PUZZLE_PIECE_SET_QUEUE.push(PUZ_COMMON.ELEMENT_INDEX);
				FILL_Y ++;
			}
			
			// call fill pattern
			PUZ_PATTERN.FILL_PATTERN();
		}
	}
	
	PUZ_PATTERN.FILL_PATTERN = function()
	{
		// if nothing to fill
		if(PUZ_PATTERN.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_PATTERN.CHECK_RESULT();
			return;
		}
		
		// Assign image
		var CURRENT_INDEX = PUZ_PATTERN.PUZZLE_PIECE_SET_QUEUE[0];
		var CURRENT_IMAGE = document.getElementById("IMG_PUZZLE_SET_" + CURRENT_INDEX);
		CURRENT_IMAGE.setAttribute('src', PUZ_COMMON.PUZZLE_PIECES[PUZ_COMMON.PUZZLE_PIECE_SELECTED_INDEX]);
		PUZ_COMMON.PUZZLE_PIECES_SET[CURRENT_INDEX - 1] = PUZ_COMMON.PUZZLE_PIECE_SELECTED_INDEX + 1;
		PUZ_PATTERN.PUZZLE_PIECE_SET_QUEUE.shift();
		//alert(CURRENT_IMAGE.id);
		
		// FadeIn Animation
		/*
		$('#' + CURRENT_IMAGE.id).fadeIn('slow');
		$('#' + CURRENT_IMAGE.id).promise().done
		(function()
		{
		    PUZ_PATTERN.FILL_PATTERN();
		});
		*/
		
		CURRENT_IMAGE.style.display = "block";
		PUZ_COMMON.SE_SET.play();
		PUZ_PATTERN.FILL_PATTERN();
	}
	
	
	PUZ_PATTERN.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_PATTERN.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_PATTERN.CHECK_CLEAR = function()
	{
		// check result
		var PIECE_INDEX = 0;

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