<?xml version="1.0" encoding="UTF-8"?>
<Module>
<ModulePrefs
	title="Flickr Memory using tag '__UP_tag__'"
	directory_title="Flickr Memory"
	description="A memory game using Flickr images of your choice! Find the image pairs in as few clicks as possible!"
	author="Sven Charleer"
	author_email="svensgadgets@5dollarshake.com"
	author_affiliation="http://5dollarshake.com"
	author_location="Belgium"
	author_photo="http://5dollarshake.com/files/svencharleer.png"
	author_aboutme="A Software Developer with a bit of spare time on his hands..."
	author_link="http://5dollarshake.com"
	author_quote="Meh!"
	thumbnail="http://5dollarshake.com/files/flickrMemory_thumbnail.png"
	screenshot="http://5dollarshake.com/files/flickrMemory_screenshot.png"
	scaling="false"
	width="320"
	height="255"
	scrolling="false">

	<Require feature="grid"/>
	<Require feature="setprefs" />
</ModulePrefs>


<UserPref name="tag" display_name="Tag" datatype="string" required="true" default_value="cats"/>
<UserPref name="size" display_name="Playfield size" datatype="enum" default_value="6">
	<EnumValue value="6" display_value="2x3"/>
	<EnumValue value="12" display_value="3x4"/>
</UserPref>
<UserPref name="hiscoremode1" default_value="0" datatype="hidden" />
<UserPref name="hiscoremode2" default_value="0" datatype="hidden" />


<Content type="html"><![CDATA[

<style type="text/css">
/* CSS PART STARTS HERE 
   --------------------*/
   
 /*background / title / footer */
	#content 
	{
		margin: 0px;
		font-size: 62.5%; /* Resets 1em to 10px */
		color: #A0C115;
		font-family: 'Lucida Grande', Verdana, Arial, Helvetica, Sans-Serif;
		background-color:#222D2D;
		width:320px;
		height:260px
	}
	#title 
	{
		padding:5px;
		font-size:1.20em;
		border-bottom:solid 1px #BBCCD6;
		background-color: #A0C115;
		color:#222D2D;
		text-align:center;
		letter-spacing:2px;
		font-weight:bold;
	}
	#footer 
	{
		border-top:solid 1px #BBCCD6;
		text-align:center;
		padding:2px;
		
	}
	
	/*table layout*/
	#grid_table {
		display:block;
	
	}
	table 
	{
		padding:0px;
		margin:3px;
		border-collapse:separate;
		border-spacing:2px;
		border:0px;
	
	}
	/*table cells with 2 x 3 layout*/
	.option1 td 
	{ 
		width:100px;
		height:100px; 
		border:0px;
		background-color:#3E4F4F;
		padding:1px;
		margin:1px;
	}
	/*table cells with 3 x 4 layout*/
	.option2 td 
	{ 
		width:74px;
		height:64px; 
		border:0px;
		background-color:#3E4F4F;
		padding:1px;
		margin:1px;
	}
	/*image size with 2 x 3 layout, visible*/
	img.option1_visible 
	{
		max-width:98px;
		max-height:98px;
		display:block;
		visibility:visible
	}
	/*image size with 3 x 4 layout, visible*/
	img.option2_visible 
	{
		max-width:74px;
		max-height:64px;
		display:block;
		visibility:visible
	}
	/*image size with 2 x 3 layout, invisible*/
	img.option1_invisible 
	{
		max-width:98px;
		max-height:98px;
		display:block;
		visibility:hidden
	}
	/*image size with 3 * 4 layout, invisible*/
	img.option2_invisible 
	{
		max-width:74px;
		max-height:64px;
		display:block;
		visibility:hidden
	}

	/*popup section*/
	/*show game over screen*/
	.gameover_visible 
	{
		visibility:visible;
		position:absolute;
		height:100%;
		width:100%;
		margin:0px 0px;
	}
	/*hide popup*/
	.popup_hidden 
	{
		visibility:hidden;
		position:absolute
	}
	/*show intro load screen*/
	.popup_intro 
	{
		visibility:visible;
		position:absolute;
		height:100%;
		width:100%;
		margin:0px 0px;
	}
	/*popup text*/
	#popuptext
	{
		font-size:1.36em;
		color:#FFFFFF;
		width:260px;
		margin:22px;
		background-color:#000000;
		text-align: center;
		padding:5px;
		border:solid 1px #BBCCD6;
	}
	#popuptext strong
	{
		color: #A0C115;
		
	}
	a, a:hover, a:visited
	{
		text-decoration:none;
		color:#BBCCD6;
		text-weight:normal;
	}
	a:hover
	{
		border-bottom: dotted 1px #BBCCD6;
	}
/* CSS PART ENDS HERE 
   --------------------*/
</style>

<div id="content">
<div id="title">Flickr Memory</div>
<div id="popup" class="popup_intro" onClick="reinitGame();"></div>
<div id="grid_table"></div>
<div id="footer">Created by <a target="_blank" href="http://5dollarshake.com">Sven Charleer</a>. 
More info at <a target="_blank" href="http://5dollarshake.com">5DollarShake.com</a></div>
</div>

<script type="text/javascript">

// GLOBAL VARIABLES
// ----------------
var prefs;
var images;
var max_images;
var grid;
var bigGrid;
var nrOfClicks;
var coupleTest;
var gameCanReinit;

// Hide load screen once all is loaded
function ShowGame()
{
	_gel("popup").className = "popup_hidden";
}

// data structure to keep track of the two selected pictures
// verifies whether same pictures are clicked
function Couple()
{
	//first clicked pictures's ID
	this.first = -1;
	//second clicked picture's ID
	this.second = -1;
	//first clicked picture's position in grid
	this.firstPos = -1;
	//second clicked picture's position in grid
	this.secondPos = -1;
	
	//called when image is clicked
	//adds data to first or second depending on first or second click
	//return false if first click and first picture info is added.
	//return true if second click and second picture info is added.
	this.addOne =function(id, pos)
	{
		if(this.first == -1){this.first = id; this.firstPos = pos;return false;}
		else {this.second = id;this.secondPos = pos;return true;}
	}
	
	//verifies whether the ID of both pictures is equal
	//requires addOne function to have returned true (thus two pictures were clicked)
	this.equal=function()
	{
		if(this.first == this.second)
		{
			return true;
		}
		else 
		{
			return false;
		}
	}
	
	//reinitialize the data, set all values to -1
	this.init = function()
	{
		this.first = -1;
		this.second = -1;
		this.firstPos = -1;
		this.secondPos = -1;
	}
}

// Initialize global variables
function initGlobalVariables()
{
	prefs = new _IG_Prefs();
	images = new Array();
	//each image exists twice in game
	max_images = prefs.getInt("size")/2;
	//keep track of number of clicks by user
	nrOfClicks = 0;
	//big or small grid
	if(prefs.getString("size") == 6)
	{
		bigGrid = false;
	}
	else
	{
		bigGrid = true;
	}
	//define a test object to compare pictures
	coupleTest = new Couple();
	gameCanReinit = false;
	
}


//tile datastructure
//keeps information such as picture id, image data, visibility and whether both images are found or not
function Tile(id,image,visible)
{
	this.id = id;
	this.image = image;
	this.visible = visible;
	this.found = false;
}

//Fisher Yates shuffle for array
//shuffles all images
function fyShuffle(shuffleArray)
{
	for(var x = shuffleArray.length-1;x >= 0;x--)
	{
		var y = Math.floor( Math.random() * (x));
		var tx = Object(shuffleArray[x]);
		var ty = Object(shuffleArray[y]);
		shuffleArray[x] = Object(ty);
		shuffleArray[y] = Object(tx);
	}
}

//initialize grid
function initGrid()
{
	//create backend object
	var backend = new Object();
	backend.data = new Array();
	
	//load each image twice into data array, using Tile objects
	for(var x = 0;x<images.length;x++)
	{
		backend.data[x*2] = new Tile(x, images[x],false);
		backend.data[(x*2) + 1] = new Tile(x, images[x],false);
	}

	//shuffle images
	fyShuffle(backend.data);

	//default view for grid item
	backend._IGG_getNormalView = function(index)
	{
		var tile = Object(this.data[index]);
		var classN = "";

		//set correct class for img object depending on grid size and visibility
		if(bigGrid){
			classN = "option2";
		}
		else
		{
			classN ="option1";
		}
		
		if(tile.visible)
		{
			classN+="_visible";
		}
		else{
			classN+="_invisible";
		}

		//return image
		return '<img class="'+ classN +'" src="' +tile.image.src + '" alt="" />';
	}

	//is drag source to true, to be clickable
	backend._IGG_isDragSource = function(index)
	{
		return true
	}
	
	//is drag source to false
	backend._IGG_isDragTarget = function(index, sourceIndex)
	{
		return false;
	}

	backend._IGG_handleClick = function(source) 
	{
		//only take action when image is invisibile
		if(!Object(this.data[source]).visible)
		{
			//add click
			nrOfClicks++;

			//set image to visible
			Object(this.data[source]).visible = true;
			//refresh cell and show visible
			this._IGG_refreshCell(source);
			
			//save id and position of image
			var id = Object(this.data[source]).id;
			var pos = source;

			//add clicked image, if two image clicked, continue with test
			if(coupleTest.addOne(id,pos))
			{
				//check whether both images are equal
				var test = coupleTest.equal();
				if(test)
				{
					//set both images to found, reinit coupleTest object
					Object(this.data[coupleTest.firstPos]).found = true;
					Object(this.data[coupleTest.secondPos]).found = true;
					coupleTest.init();
				}
				else
				{
					//set both images to not found, reinit coupleTest object
					Object(this.data[coupleTest.firstPos]).visible = false;
					Object(this.data[coupleTest.secondPos]).visible = false;
					coupleTest.init();
				}
			}

		}
	
		//wait 2 seconds before making selected, unequal images disappear
		setTimeout("grid.refreshAll()",2000);
	
		//check if game is over
		if(this.GameOver())
		{
			
			var previousHiScore = 0;
			//get previous hiscore and set new one, depending on which size of grid
			if(bigGrid)
			{
				previousHiScore = prefs.getInt("hiscoremode2");
				prefs.set("hiscoremode2",nrOfClicks);
			}
			else
			{
				previousHiScore = prefs.getInt("hiscoremode1");
				prefs.set("hiscoremode1",nrOfClicks);
			}
			//show game over screen
			//check if new hiscore
			var message = "";
			var message2 = "";
			if(previousHiScore > nrOfClicks)
			{
				message = "You have beaten your high score <br/>of <strong>" + previousHiScore + "</strong> clicks!";
				message2 = "New high score: <strong>" + nrOfClicks + "</strong> clicks";
			}
			else
			{
				message = "Score: <strong>" + nrOfClicks + "</strong> clicks.";
				if(previousHiScore != 0)
				{
					message2 = "High score: <strong>" + previousHiScore + "</strong> clicks";
				}
			}
		
			_gel("popup").innerHTML = '<div id="popuptext">'+ message +'<br/><br/>'+ message2 +
									'<br/><br/>Click to restart game</div>';
			_gel("popup").className = "gameover_visible";
			
			//allow user to click on game to reinit
			gameCanReinit = true;

		}
	}

	//define handledrag to stop extra refresh
	backend._IGG_handleDrag = function(source, target)
	{
		//nothing
	}

	//define to show nothing when trying to drag
	backend._IGG_getSurrogateView = function(index) {
		return "";
	}

	//check gameover function
	backend.GameOver = function()
	{
		//if any tile is not set to found, return false
		for(var x=0;x<this.data.length;x++)
		{
			if(!this.data[x].found)
			{
				return false;
			}

		}
		//return true, game is over
		return true;
	}

	//set style depending on size of grid
	if(!bigGrid){
		grid = new _IG_Grid(backend, "mygrid", 2, 3);
		grid.getTable().className = "option1";
	}
	else
	{
		grid = new _IG_Grid(backend, "mygrid", 3, 4);
		grid.getTable().className = "option2";
	}

	_gel("grid_table").appendChild(grid.getTable());
	grid.initDragging();
}

//turn xml feed into array of images
function xmlHandler(response)
{
	//check whether there is data in feed
	if(response == null || typeof(response) != "object" ||
							response.firstChild == null)
	{
		alert("There is no data.");
		return;
	}

	var html = "";
	//get all item tags
	var items = response.getElementsByTagName("item");
	for(var x = 0;x < items.length && x < max_images;x++)
	{
		var nodeList = items.item(x).childNodes;
		var img = "";
		//get all media:content nodes, containing the img urls
		for(var y = 0;y<nodeList.length ;y++)
		{
			var node = nodeList.item(y);
			if(node.nodeName == "media:thumbnail")
			{
				//get the img url
				img = _IG_GetImage(node.getAttribute("url"));
			}
		}
		//put image in array
		images.push(img);
	}
	//initialize the grid now that we have all image data
	initGrid();
	
	//leave intro screen for a few seconds
	
	_gel("popup").className = "popup_intro";
	_gel("popup").innerHTML = '<div id="popuptext">Welcome to <strong>Flickr Memory</strong>' +
							'<br/><br/>Edit the <strong>Gadget settings</strong> to change the Flickr tag' +
							' or the playfield size you would like to use!<br/><br>Enjoy! ;)</div>';
	setTimeout("ShowGame()", 6000);

}

function reinitGame()
{
	if(gameCanReinit)
	{
		_gel("grid_table").innerHTML = "";
		//init global vars
		initGlobalVariables();
		// fetch xml and load xml handler
		_IG_FetchXmlContent("http://api.flickr.com/services/feeds/photos_public.gne?tags=" + prefs.getString("tag") + "&format=rss2",
						xmlHandler,
						max_images,
						true);
	}
}

// MAIN START
// ----------

//init global vars
initGlobalVariables();
// fetch xml and load xml handler
_IG_FetchXmlContent("http://api.flickr.com/services/feeds/photos_public.gne?tags=" + prefs.getString("tag") + "&format=rss2",
					xmlHandler,
					max_images,
					true);







</script>
]]></Content>
</Module>