// Protospace Puzzle Widget
// (C) 2007 Glan Thomas, all rights reserved

Puzzle =
{
	setUpGame: function(l)
	{
		this.level = l;
		document.getElementById('puzzle_level').value = l;	
		CookieMan.setCookie('puzzle_level',l,'/');
		
		this.movDist = 4;
		this.movDelay = 40;
		this.maxX = 180;
		this.maxY = 180;
		
		this.xPos = new Array();
		this.yPos = new Array();
		this.layerList = new Array();
		
		this.moveCount = 0;
		
		this.sizeX = this.maxX/this.level;
		this.sizeY = this.maxY/this.level;
		
		this.movDistX = this.sizeX/this.movDist;
		this.movDistY =this.sizeY/this.movDist;
	
		document.getElementById("puzzleframe").innerHTML = '<div id="doneLayer" style="display:none;" onclick="Puzzle.setUpGame('+l+');"><h3>Well Done!</h3>Play Again?</div>';
		if (browser.isIE)
			sizefix = 0;
		else
			sizefix = 2;
		
		for (i=0;i<(this.level*this.level-1); i++)
		{
			this.xPos[i] = (i%this.level);
			this.yPos[i] = Math.floor(i/this.level);
			
			var element = document.createElement('div'); 
			element.className = 'square';
			element.style.position = 'absolute';
			element.style.left = this.xPos[i]*this.sizeX;
			element.style.top = this.yPos[i]*this.sizeY;
			element.style.width = this.sizeX-sizefix;
			element.style.height = this.sizeY-sizefix;
			element.style.overflow="hidden"; 
			element.id=i; 

			element.innerHTML = '<table class="number"><tr><td>'+(i+1)+'</td></tr></table><img style="width:'+(this.sizeX*this.level)+'; height:'+(this.sizeY*this.level)+'; position:absolute; left:' + (-1-((i%this.level)*this.sizeX)) + '; top:'+ (0-(Math.floor(i/this.level)*this.sizeY+1)) +';"/>';		
			
			if (element.addEventListener)
				element.addEventListener('mousedown',function() { Puzzle.doMove(this.id); return false;},true);
			else if (element.attachEvent)
				element.onmousedown = function() {Puzzle.doMove(this.id); return false;};
			
			document.getElementById("puzzleframe").appendChild(element);
			
			this.layerList[i] = element.style;	
		}			
		this.xPos[(this.level*this.level)] = this.level-1;
		this.yPos[(this.level*this.level)] = this.level-1;
		
		var pict = CookieMan.getCookie('puzzle_picture');
		if (!pict)
			pict = 'meena.jpg';
		this.setPicture(pict);
		
		this.randomize();
		this.locked = false;
	},
	

	dynamicMove : function(movingLayerList)
	{		
		this.moveCount++
		
		if (this.moveCount <= this.movDist)
		{
			for (var i=0;i<movingLayerList.length;i++)
			{
				theLayer = this.layerList[movingLayerList[i]];
				if (this.xPos[(this.level*this.level)] < this.xPos[movingLayerList[i]])
					theLayer.left=(((this.xPos[movingLayerList[i]])%this.level)*this.sizeX)-this.moveCount*(this.movDistX);
				else if (this.xPos[(this.level*this.level)] > this.xPos[movingLayerList[i]])
					theLayer.left=(((this.xPos[movingLayerList[i]])%this.level)*this.sizeX)+this.moveCount*(this.movDistX);
				else if (this.yPos[(this.level*this.level)] < this.yPos[movingLayerList[i]])
					theLayer.top=(((this.yPos[movingLayerList[i]])%this.level)*this.sizeY)-this.moveCount*(this.movDistY);
				else if (this.yPos[(this.level*this.level)] > this.yPos[movingLayerList[i]])
					theLayer.top=(((this.yPos[movingLayerList[i]])%this.level)*this.sizeY)+this.moveCount*(this.movDistY);
			}
		}
		else
		{
			for (var i=0;i<movingLayerList.length;i++)
			{
				if (this.xPos[(this.level*this.level)] < this.xPos[movingLayerList[i]])
					this.xPos[movingLayerList[i]]--
				else if (this.xPos[(this.level*this.level)] > this.xPos[movingLayerList[i]])
					this.xPos[movingLayerList[i]]++
				else if (this.yPos[(this.level*this.level)] < this.yPos[movingLayerList[i]])
					this.yPos[movingLayerList[i]]--
				else if (this.yPos[(this.level*this.level)] > this.yPos[movingLayerList[i]])
					this.yPos[movingLayerList[i]]++
			}
					
			this.xPos[(this.level*this.level)] = this.xPozOfclickedOnSquare;
			this.yPos[(this.level*this.level)] = this.yPozOfclickedOnSquare;
				
			clearInterval(this.move)
			this.moveCount=0;
			this.locked = false;
			
			var complete = true;
			for (var n=0; n<(this.level*this.level-1); n++)
			{
				if ((this.xPos[n] != n % this.level) || (this.yPos[n] != Math.floor(n / this.level)))
					complete = false;
				//alert("("+(n+1)+")  ("+this.xPos[n]+" = "+ n % this.level+")x : (" +this.yPos[n]+" = "+Math.floor(n / this.level)+")y");
			}

			if (!complete)
				this.locked = false
			else
			{	
				var pict = CookieMan.getCookie('puzzle_picture');
				
				if (pict != 'none')
					document.getElementById("puzzleframe").innerHTML = '<img src="images/puzzle/'+pict+'" style="margin: 2px; width:178px; height:178px;"/><div id="doneLayer" onclick="Puzzle.setUpGame('+this.level+');"><h3>Well Done!</h3>Play Again?</div>';
				
				this.locked = true;
				document.getElementById("doneLayer").style.display = 'block';
				
			}	
			
		}
	},
	
	doMove : function(i)
	{
		if ((this.xPos[i] == this.xPos[(this.level*this.level)]) || (this.yPos[i] == this.yPos[(this.level*this.level)]))
		{
			if (this.locked == false)
			{
				this.locked = true;
				var movingLayerList = new Array();
				
				this.xPozOfclickedOnSquare = this.xPos[i];
				this.yPozOfclickedOnSquare = this.yPos[i];
				
				for (var n=0;n<(this.level*this.level-1);n++)
				{
					if (
						   ((this.xPos[i] == this.xPos[n]) && (this.xPos[n] == this.xPos[(this.level*this.level)])) 
						|| ((this.yPos[i] == this.yPos[n]) && (this.yPos[n] == this.yPos[(this.level*this.level)]))
					)	
						if (
							   ((this.xPos[n] <= this.xPos[i] ) && (this.xPos[n]>this.xPos[(this.level*this.level)])) 
							|| ((this.xPos[n] >= this.xPos[i]) && (this.xPos[n]<this.xPos[(this.level*this.level)]))
							|| ((this.yPos[n] <= this.yPos[i]) && (this.yPos[n]>this.yPos[(this.level*this.level)])) 
							|| ((this.yPos[n] >= this.yPos[i]) && (this.yPos[n]<this.yPos[(this.level*this.level)]))
						)
						movingLayerList.push(n);
				}			
				this.move = setInterval(function() {Puzzle.dynamicMove(movingLayerList);},this.movDelay);
			}
		}
		
	},
	
	
	setPicture : function(pict)
	{
		document.getElementById('puzzle_pict').value = pict;
	
		CookieMan.setCookie('puzzle_picture',pict,'/');
		if (pict == 'none')
			document.getElementById('puzzleframe').className = "withoutImages";
		else
		{
			document.getElementById('puzzleframe').className = "withimages";
			var images = document.getElementById('puzzleframe').getElementsByTagName('img');	
			for(var i=0; i<images.length; i++)
				images[i].src='images/puzzle/'+pict;
		}
	},
	
	randomize : function()
	{
		var theList = new Array();
		var theRandomList = new Array();
		
		do
		{
			//	Generate exclusive number list
			for (var n=0; n<(this.level*this.level-1); n++)
				theList[n] = n;
			
			//	Randomise number list
			for (var n=(this.level*this.level-2); n>=0; n--)
			{
				randNum = Math.floor(Math.random()*(n+1));
				theRandomList[n]=theList[randNum];
				
				x = 0
				for (i=0; i<=n; i++)
				{
					if (i != randNum)
					{
						theList[x] = theList[i];
						x++
					}
				}
			}
			
			//	Perform solvent check
			var count = 0
			for (var n=0; n<(this.level*this.level-1); n++)
			{
				for (var i=n+1; i<(this.level*this.level-1); i++)
				{
					if (theRandomList[i] < theRandomList[n])
						count++
				}
			}
			//document.writeln("<br><br> Number of smaller subsequent numbers : " + count)
		
			if (count%2 == 0)
				ok = true
			else
				ok = false
		} 
		while (ok == false)
		
		for (var lay=0; lay<(this.level*this.level)-1; lay++)
		{
			this.xPos[theRandomList[lay]] = (lay%this.level);
			this.yPos[theRandomList[lay]] = Math.floor(lay/this.level);
			
			//this.xPictPos[theRandomList[lay]] = (lay%this.level)-(theRandomList[lay]%this.level);
			//this.yPictPos[theRandomList[lay]] = Math.floor(lay/this.level)-Math.floor(theRandomList[lay]/this.level);
				
			this.layerList[theRandomList[lay]].left=((this.xPos[theRandomList[lay]])*this.sizeX);
			this.layerList[theRandomList[lay]].top=((this.yPos[theRandomList[lay]])*this.sizeY);
		}
		
		this.xPos[(this.level*this.level)] = this.level-1;
		this.yPos[(this.level*this.level)] = this.level-1;		
	}
}