/*	16-puzzle-Generator.js : 16-puzzle stuff

	In preparation of 64-puzzle-Generator
*/

//	now for "Porting over JavaScript 1.6 Array methods"
/*
	* Sugar Arrays (c) Creative Commons 2006
	* http://creativecommons.org/licenses/by-sa/2.5/
	* Author: Dustin Diaz | http://www.dustindiaz.com
	* Reference: http://www.dustindiaz.com/basement/sugar-arrays.html
*/
Function.prototype.method=function(name,fn){this.prototype[name]=fn;return this;};if(!Array.prototype.forEach){Array. method('forEach',function(fn,thisObj){var scope=thisObj||window;for(var i=0,j=this.length;i<j;++i){fn.call(scope,this[i],i,this);}});}
//	I've only extracted the forEach-part out of sugar-arrays-min.js


function getJavaScript( url)
{
	document.write('<script type="text/javascript" src="' + url + '"></scr'+'ipt>');
	//	Note that you should split the closing script tag into two parts.
}	//	getJavaScript

getJavaScript( '/javascripts/AjaxConnexion.js');

/*	The cells of the grid will be staggered.
	The size of the orthoGrid is assumed to be controlled
	by an external css.
	The grid cells will hold an image element, 
	whose id equals the coordinate, like '3,2' (col 3, row 2).
	TopLeft = '0,0' 
*/

var sepCoord = ',';

var tileCount = 16;
var freeRow = 'x';

var orthograms_imgroot = '/cms/Joomla15/images/icc-generator/orthograms/backslide/';

var init_img = orthograms_imgroot + 'blanc.gif';
// init_img = '';


/*	writeOrthoGrid constructs a grid within a table like this:
	in 'col,row' coordinates of the orthogonal grid, aka orthogridID
	each cell contains an image with this as its element id.
	
	writeOrthoGrid( 4, 5) gives:
	
 	0,0		1,0		2,0		3,0
 	0,1		1.1		2,1		3,1
 	0,2		1,2		2,2		3,2
 	0,3		1,3		2,3		3,3
 	0,4		1,4		2,4		3,4
 
*/

function writeOrthoGrid( pCols, pRows)
{	var iCol, iRow;
	var orthogridID;

	//	first the style
	document.write( '<style type="text/css">table.orthogrid {padding:0px;margin:4px;} .orthogrid tr {vertical-align:top;padding:0px;margin:0px;border:0px;height:30px;} .orthogrid td {padding:0px;margin:0px;width:30px;height:30px;} .orthogrid img {position:absolute;} div#connexion-name {font-weight:bold;margin-bottom:12px;}</style>');
	
	if (pCols > 0 && pCols > 0)
	{
/*		document.write( '<div id="connexion-name"></div>');

		//	now for some place to show the free pieces
		document.write( '<table class="orthogrid"><tr>');
		for (iCol = 0 ; iCol < tileCount ; iCol += 1)
		{
			orthogridID = iCol + sepCoord + freeRow;
			document.write( '<td><img src="' + init_img + '" id="' + orthogridID + '" title="" alt=""></td>');
		}
		document.write( '</tr></table>');
*/	
		document.write( '<table class="orthogrid">');
	
		for (iRow = 0 ; iRow < pRows ; iRow += 1)
		{
			document.write( '<tr>');
			for (iCol = 0 ; iCol < pCols ; iCol += 1)
			{
				orthogridID = iCol + sepCoord + iRow;
				document.write( '<td><img src="' + init_img + '" id="' + orthogridID + '" title="" alt=""></td>');
			}
			document.write( '</tr>');
		}
		document.write( '</table>');
	}
	
}	//	writeOrthoGrid



function setImage( orthogridID, newurl, newtitle)
{
	var imgObj = document.getElementById( orthogridID);
	var anchorObj;
	if (imgObj != null)
	{	imgObj.src = (newurl == null ? init_img : newurl);
		imgObj.title = (newtitle == null ? '' : newtitle);
	}

}	//	setImage

function removeImage( orthogridID)
//	when used as a foreach function, the parameter list will be: element, index, array
{
	setImage( orthogridID);		//	implicit clearing the position
	
}	//	removeImage

/*	=== object Orthogram ===	
	holds static data on a Orthogram
*/
//	constructor
function Orthogram( nr, ic_nr, name)
{	this.nr = nr;					//	binary
	this.ic_nr = ic_nr;				//	I Ching number, not applicable really
	this.name = name;		//	English name
}

/*	=== end of object Orthogram ===	*/

//	the list of orthograms
var orthograms = new Array();

orthograms[ 0] = new Orthogram( 0, 0, "");
orthograms[ 5] = new Orthogram( 5, 5, "");
orthograms[ 8] = new Orthogram( 8, 8, "");
orthograms[ 4] = new Orthogram( 4, 4, "");
orthograms[ 7] = new Orthogram( 7, 7, "");
orthograms[ 2] = new Orthogram( 2, 2, "");
orthograms[ 15] = new Orthogram( 15, 15, "");
orthograms[ 6] = new Orthogram( 6, 6, "");
orthograms[ 3] = new Orthogram( 3, 3, "");
orthograms[ 10] = new Orthogram( 10, 10, "");
orthograms[ 14] = new Orthogram( 14, 14, "");
orthograms[ 1] = new Orthogram( 1, 1, "");
orthograms[ 11] = new Orthogram( 11, 11, "");
orthograms[ 9] = new Orthogram( 9, 9, "");
orthograms[ 13] = new Orthogram( 13, 13, "");
orthograms[ 12] = new Orthogram( 12, 12, "");

// orthograms[ 16] = new Orthogram( 16, -1, "Dim sum", "demanding");



function setImageByBinary( orthogridID, orthoVal)
//	when used as a foreach function, the parameter list will be: element, index, array
{	var newimgurl, newtitle;
	var orthgram = orthograms[ orthoVal];
	var orthoStr = '00' + orthoVal;
	orthoStr = orthoStr.substring( orthoStr.length - 2);

	newimgurl = orthograms_imgroot + orthoStr + '.gif';
	newtitle = orthgram.ic_nr + '. ' + orthgram.name;
	setImage( orthogridID, newimgurl, newtitle);

}	//	setImageByBinary


/*	=== object Rectangle ===	
	holds top left bottom right coordinates
	defaults to 0 0 0 0
*/
//	constructor
function Rectangle( top, left, bottom, right)
{
	this.top = (top == null ? 0 : top);
	this.left = (left == null ? 0 : left);
	this.bottom = (bottom == null ? 0 : bottom);
	this.right = (right == null ? 0 : right);
	//	methods
	this.Addpt = RectangleAddpt;
}	//	Rectangle

function RectangleAddpt( col, row)
{
	this.top = Math.min( this.top, row);
	this.bottom = Math.max( this.bottom, row);
	this.left = Math.min( this.left, col);
	this.right = Math.max( this.right, col);

}	//	RectangleAddpt

/*	=== end of object Rectangle ===	*/

/*	=== object Connexion ===	
	holds a laid-out Connexion
*/
//	constructor
function Connexion( name)
{	this.name = (name ? name : '');
	this.positions = new Array();
	//	put all tiles on the free row
	for (var tile = 0 ; tile < tileCount ; tile += 1)
	{	orthogridID = tile + sepCoord + freeRow;
		this.positions[ tile] = orthogridID;
	}
	this.bbox = new Rectangle();
	//	methods
	this.Put = ConnexionPut;
	this.Display = ConnexionDisplay;
}	//	Connexion

//	add a Orthogram with 
//	binary nr
//	on orthogridID
function ConnexionPut( nr, orthogridID)
{
//	this.positions[ (nr < 0 ? 16 : nr)] = orthogridID;
	this.positions[ nr] = orthogridID;
	
	var pos = orthogridID.split( sepCoord);	//	yielding [ col, row ] array
	this.bbox.Addpt( pos[0], pos[1]);
	
}	//	ConnexionPut

var _displayConnexion = new Connexion( 'dummy');

function ConnexionDisplay( )
{
	//	first remove current Connexion from the display
	_displayConnexion.positions.forEach( removeImage);
	_displayConnexion = this;
	_displayConnexion.positions.forEach( setImageByBinary);

	var cnxName = document.getElementById( "connexion-name");
	if (cnxName)
	{	cnxName.innerHTML = _displayConnexion.name;
	}

}	//	ConnexionDisplay

/*	=== end of object Connexion ===	*/




