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

	function PUZ_FIXED(){};
	
	// initialize
	// .................................................	
	PUZ_FIXED.PUZZLE_INITIALIZE = function()
	{
		// Shared Resources
		PUZ_COMMON.PUZZLE_BASE_IMAGE = '<!--[#SITE_URL;]-->/p/status/res/puzzle_base_fixed.png';
		PUZ_COMMON.PUZZLE_SUCCESS_IMAGE = '<!--[#SITE_URL;]-->/p/status/res/success.png';
	
		// Shared variables
		PUZ_COMMON.HTML_ELEMENT = null;
		PUZ_COMMON.ROW_MAX = 3;
		PUZ_COMMON.COL_MAX = 3;
		
		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/piece_01.png",
			"<!--[#SITE_URL;]-->/p/status/res/piece_02.png",
			"<!--[#SITE_URL;]-->/p/status/res/piece_03.png",
			"<!--[#SITE_URL;]-->/p/status/res/piece_04.png",
			"<!--[#SITE_URL;]-->/p/status/res/piece_05.png",
			"<!--[#SITE_URL;]-->/p/status/res/piece_06.png",
			"<!--[#SITE_URL;]-->/p/status/res/piece_07.png",
			"<!--[#SITE_URL;]-->/p/status/res/piece_08.png",
			"<!--[#SITE_URL;]-->/p/status/res/piece_09.png",
		];

		PUZ_COMMON.PUZZLE_PIECES_SET =
		[
			0, 0, 0, 0, 0, 
			0, 0, 0, 0
		];
	
		PUZ_COMMON.PUZZLE_PIECE_RES =
		[
			9, 6, 7, 2, 3,
			5, 8, 4, 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_01.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_FIXED.PUZZLE_LOAD = function()
	{
		PUZ_FIXED.PUZZLE_INITIALIZE();
		
		// Fill puzzle set
		// .................................................
		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_FIXED.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_SET";		
				PUZ_COMMON.HTML_ELEMENT.style.top = (PUZ_COMMON.ROW_INDEX * 135) + 74;
				PUZ_COMMON.HTML_ELEMENT.style.left = (PUZ_COMMON.COL_INDEX * 135) + 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";
				
				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.onclick = function(BIND_INDEX)
									{ 
										return function() 
										{
											PUZ_FIXED.PUZZLE_FREE(BIND_INDEX); 
										}											
									}(PUZ_COMMON.ELEMENT_INDEX);

				
				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_SET";		
				PUZ_COMMON.HTML_ELEMENT.style.top = (PUZ_COMMON.ROW_INDEX * 135) + 74;
				PUZ_COMMON.HTML_ELEMENT.style.left = (PUZ_COMMON.COL_INDEX * 135) + 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";
				
				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.COL_INDEX ++;
			}
			PUZ_COMMON.COL_INDEX = 0;
			PUZ_COMMON.ROW_INDEX ++;
		}	
	}
	

	PUZ_FIXED.PUZZLE_SET = function(ITEM_INDEX)
	{
		
		if(PUZ_COMMON.PUZZLE_LOCK_EDIT === true)
		{
			return;
		}
		
		// Remove Piece
		if(PUZ_COMMON.PUZZLE_PIECE_SELECTED_INDEX == 0)
		{
			//alert('remove: ' + ITEM_INDEX + ' : ' + PUZ_COMMON.PUZZLE_PIECE_SELECTED_INDEX);
			// Check if not empty
			if(PUZ_COMMON.PUZZLE_PIECES_SET[ITEM_INDEX - 1] > 0)
			{
				//alert('removing: ' + (PUZ_COMMON.PUZZLE_PIECES_SET[ITEM_INDEX - 1]));
				
				document.getElementById("IMG_PUZZLE_SET_" + ITEM_INDEX).style.display = "none";
				document.getElementById("IMG_PUZZLE_FREE_" + (PUZ_COMMON.PUZZLE_PIECES_SET[ITEM_INDEX - 1])).style.display = "block";
				PUZ_COMMON.PUZZLE_PIECES_SET[ITEM_INDEX - 1] = 0;
				PUZ_COMMON.SE_FREE.play();
			}
			else
			{
				//alert('nothing to remove: ' + (PUZ_COMMON.PUZZLE_PIECES_SET[ITEM_INDEX - 1]));
			}
		}
		
		// Put piece
		else
		{
			//alert('put: ' + ITEM_INDEX + ' : ' + PUZ_COMMON.PUZZLE_PIECE_SELECTED_INDEX);
			// Check if not empty, call remove
			if(PUZ_COMMON.PUZZLE_PIECES_SET[ITEM_INDEX - 1] > 0)
			{
				PUZ_COMMON.PUZZLE_PIECE_SELECTED_INDEX = 0;
				PUZZLE_SET(ITEM_INDEX);
			}
			else
			{
				PUZ_COMMON.PUZZLE_PIECES_SET[ITEM_INDEX - 1] = PUZ_COMMON.PUZZLE_PIECE_SELECTED_INDEX;
				document.getElementById("IMG_PUZZLE_SET_" + ITEM_INDEX).setAttribute('src', PUZ_COMMON.PUZZLE_PIECES[PUZ_COMMON.PUZZLE_PIECE_SELECTED_INDEX - 1]);
				document.getElementById("IMG_PUZZLE_SET_" + ITEM_INDEX).style.display = "block";
				document.getElementById("IMG_PUZZLE_FREE_" + PUZ_COMMON.PUZZLE_PIECE_SELECTED_INDEX).style.display = "none";
				
				PUZ_COMMON.PUZZLE_PIECE_SELECTED_INDEX = 0;
				
				
				// check result
				PUZ_COMMON.ROW_INDEX = 0;
				
				while(PUZ_COMMON.ROW_INDEX < PUZ_COMMON.PUZZLE_PIECE_RES.length)
				{
					// failure
					if(PUZ_COMMON.PUZZLE_PIECES_SET[PUZ_COMMON.ROW_INDEX] !== PUZ_COMMON.PUZZLE_PIECE_RES[PUZ_COMMON.ROW_INDEX])
					{
						PUZ_COMMON.SE_SET.play();
						return;
					}
					PUZ_COMMON.ROW_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_FIXED.PUZZLE_FREE = function(ITEM_INDEX)
	{
		if(PUZ_COMMON.PUZZLE_LOCK_EDIT === true)
		{
			return;
		}
	
		if(document.getElementById("IMG_PUZZLE_FREE_" + ITEM_INDEX).style.display !== "none")
		{
			PUZ_COMMON.PUZZLE_PIECE_SELECTED_INDEX = ITEM_INDEX;
			PUZ_COMMON.SE_SELECT.play();
			//alert('Selected: ' + PUZ_COMMON.PUZZLE_PIECE_SELECTED_INDEX);
		}
	}
