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

	function PUZ_FLIP(){};
	
	
	// initialize
	// .................................................
	PUZ_FLIP.PUZZLE_PIECE_SET_QUEUE = [];	
	PUZ_FLIP.PUZZLE_PIECES_MODEL = [];
	PUZ_FLIP.PUZZLE_PIECES_TYPE = [];
	
	PUZ_FLIP.PUZZLE_INITIALIZE = function(PUZZLE_ID)
	{
		// 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 = 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/box_01.png",
			"<!--[#SITE_URL;]-->/p/status/res/box_01.png",
			"<!--[#SITE_URL;]-->/p/status/res/box_01.png",
			
			"<!--[#SITE_URL;]-->/p/status/res/box_01.png",
			"<!--[#SITE_URL;]-->/p/status/res/box_01.png",
			"<!--[#SITE_URL;]-->/p/status/res/box_01.png",
			
			"<!--[#SITE_URL;]-->/p/status/res/box_01.png",
			"<!--[#SITE_URL;]-->/p/status/res/box_01.png",
			"<!--[#SITE_URL;]-->/p/status/res/box_01.png"
		];

		PUZ_COMMON.PUZZLE_PATTERN = 
		[
			"<!--[#SITE_URL;]-->/p/status/res/box_01.png",
			"<!--[#SITE_URL;]-->/p/status/res/box_02.png",
			"<!--[#SITE_URL;]-->/p/status/res/box_04.png"
		];		
		
		
		
		PUZ_FLIP.PUZZLE_BASE_IMAGE = '<!--[#SITE_URL;]-->/p/status/res/puzzle_base_pattern.png';
			
		PUZ_COMMON.PUZZLE_PIECES_SET =
		[
			1, 1, 1, 
			1, 1, 1, 
			1, 1, 1 
		];
		PUZ_FLIP.PUZZLE_PIECE_SET_QUEUE = [];
		PUZ_COMMON.PUZZLE_PIECE_ATTEMPT = 0;
		PUZ_COMMON.PUZZLE_PIECE_MAX_ATTEMPT = 5;
		
		if(PUZZLE_ID == 1)
		{
			PUZ_COMMON.PUZZLE_PIECE_RES =
			[
				3, 1, 3, 
				1, 1, 1, 
				3, 1, 3
			];
		}
		else if(PUZZLE_ID == 2)
		{
			PUZ_COMMON.PUZZLE_PIECE_RES =
			[
				3, 3, 2, 
				3, 1, 2, 
				3, 1, 1
			];		
			PUZ_COMMON.PUZZLE_PIECE_MAX_ATTEMPT = 4;
		}
		else if(PUZZLE_ID == -1)
		{
			PUZ_COMMON.PUZZLE_PIECE_RES =
			[
				0, 0, 0, 
				0, 0, 0, 
				0, 0, 0
			];
			PUZ_COMMON.PUZZLE_PIECE_MAX_ATTEMPT = 99999;
		}
		else
		{
			PUZ_COMMON.PUZZLE_PIECE_RES =
			[
				3, 1, 3, 
				1, 3, 1, 
				3, 1, 3
			];	
		}
	
		// 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_04.mp3');
		PUZ_COMMON.BGM_COMPLETE = new Audio('<!--[#SITE_URL;]-->/p/status/res/complete.mp3');	

		// Plays BGM
		PUZ_COMMON.PLAY_BGM();		
	}
	
	PUZ_FLIP.PUZZLE_LOAD = function(PUZZLE_ID)
	{
		PUZ_FLIP.PUZZLE_INITIALIZE(PUZZLE_ID);
					
		
		// 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_FLIP.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.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_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_PATTERN[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_FLIP.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 ++;
			JSOC_ELEMENT.VAR_VALUE("HTTP_RESULT", 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;
			}
			
			// collect pops for X grid (Except selected item)
			FILL_INDEX = FILL_X - 1;
			while
			(
				FILL_INDEX <= FILL_X + 1 &&
				FILL_INDEX < PUZ_COMMON.COL_MAX
			)
			{
				PUZ_COMMON.ELEMENT_INDEX = (FILL_Y * PUZ_COMMON.COL_MAX) + FILL_INDEX + 1;
				if
				(
					FILL_INDEX >= 0 &&
					ITEM_INDEX != PUZ_COMMON.ELEMENT_INDEX
				)
				{
					PUZ_FLIP.PUZZLE_PIECE_SET_QUEUE.push(PUZ_COMMON.ELEMENT_INDEX);
				}
				FILL_INDEX ++;
			}
			
			
			// fill Y
			FILL_INDEX = FILL_Y - 1;; // Should exclude current Row since already exists
			while
			(
				FILL_INDEX <= FILL_Y + 1 &&
				FILL_INDEX < PUZ_COMMON.ROW_MAX
			)
			{
				PUZ_COMMON.ELEMENT_INDEX = (FILL_INDEX * PUZ_COMMON.COL_MAX) + FILL_X + 1;
				if
				(
					FILL_INDEX >= 0 &&
					ITEM_INDEX != PUZ_COMMON.ELEMENT_INDEX
				)
				{
					PUZ_FLIP.PUZZLE_PIECE_SET_QUEUE.push(PUZ_COMMON.ELEMENT_INDEX);
				}
				
				FILL_INDEX ++;
			}
			
			// flip self
			PUZ_FLIP.PUZZLE_PIECE_SET_QUEUE.push(ITEM_INDEX);
			
			// call fill pattern
			PUZ_FLIP.FLIP_PATTERN();
		}
	}
	
	PUZ_FLIP.FLIP_PATTERN = function()
	{
		// if nothing to fill
		if(PUZ_FLIP.PUZZLE_PIECE_SET_QUEUE.length <= 0)
		{			
			// Check result
			PUZ_FLIP.CHECK_RESULT();
			return;
		}
		
		
		// Assign image
		var CURRENT_INDEX = PUZ_FLIP.PUZZLE_PIECE_SET_QUEUE[0];
		var CURRENT_IMAGE = document.getElementById("IMG_PUZZLE_SET_" + CURRENT_INDEX);
		PUZ_FLIP.PUZZLE_PIECE_SET_QUEUE.shift();
		
		/*
		alert
		(
			"BEFORE: \n" +
			"INDEX: " + CURRENT_INDEX + "\n" +
			"IMAGE ID: " + PUZ_COMMON.PUZZLE_PIECES_SET[CURRENT_INDEX - 1] + "\n" +
			"IMAGE: " + PUZ_COMMON.PUZZLE_PATTERN[PUZ_COMMON.PUZZLE_PIECES_SET[CURRENT_INDEX - 1] - 1]
		);
		*/
		
		// if LAST ITEM
		if(PUZ_FLIP.PUZZLE_PIECE_SET_QUEUE.length <= 0)
		{
			PUZ_COMMON.PUZZLE_PIECES_SET[CURRENT_INDEX - 1] --;
			if(PUZ_COMMON.PUZZLE_PIECES_SET[CURRENT_INDEX - 1] <= 0)
			{
				PUZ_COMMON.PUZZLE_PIECES_SET[CURRENT_INDEX - 1] = PUZ_COMMON.PUZZLE_PATTERN.length;
			}
		}
		
		// if sides
		else
		{
			PUZ_COMMON.PUZZLE_PIECES_SET[CURRENT_INDEX - 1] ++;
			if(PUZ_COMMON.PUZZLE_PIECES_SET[CURRENT_INDEX - 1] > PUZ_COMMON.PUZZLE_PATTERN.length)
			{
				PUZ_COMMON.PUZZLE_PIECES_SET[CURRENT_INDEX - 1] = 1;
			}	
		}	
		CURRENT_IMAGE.setAttribute('src', PUZ_COMMON.PUZZLE_PATTERN[PUZ_COMMON.PUZZLE_PIECES_SET[CURRENT_INDEX - 1] - 1]);
		
		/*
		alert
		(
			"AFTER: \n" +
			"ELEM: " + PUZ_COMMON.PIECE_ELEMENT.id + "\n" +
			"INDEX: " + CURRENT_INDEX + "\n" +
			"IMAGE ID: " + PUZ_COMMON.PUZZLE_PIECES_SET[CURRENT_INDEX - 1] + "\n" +
			"IMAGE: " + PUZ_COMMON.PUZZLE_PATTERN[PUZ_COMMON.PUZZLE_PIECES_SET[CURRENT_INDEX - 1] - 1]
		);
		*/
		
		// FadeOut Animation
		/*
		$('#' + CURRENT_IMAGE.id).fadeOut('slow');
		$('#' + CURRENT_IMAGE.id).promise().done
		(function()
		{
		    PUZ_FLIP.FLIP_PATTERN();
		});
		*/
	
		PUZ_COMMON.SE_SET.play();
		PUZ_FLIP.FLIP_PATTERN();
	}
	
	
	PUZ_FLIP.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_FLIP.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_FLIP.CHECK_CLEAR = function()
	{	
		// check result
		var PIECE_INDEX = 0;

		// Clear if fully filled
		if(PUZ_COMMON.PUZZLE_PIECE_ATTEMPT < PUZ_COMMON.PUZZLE_PIECE_MAX_ATTEMPT)
		{
			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)).setAttribute('src', PUZ_COMMON.PUZZLE_PATTERN[PUZ_COMMON.PUZZLE_PIECES_SET[PIECE_INDEX] - 1]);
			PIECE_INDEX ++;
		}
		PUZ_COMMON.SE_FREE.play();
	}
	
