// $Revision: 1.4 $
function ECI_melimelo(instanceName, container, monitor, answer)
{
	this.instanceName = instanceName;
	window.ECI_melimelo_instanceName = instanceName;
	this.container = container;
	this.monitor = monitor;
	//this.answer = this.getAnswer();
	this.answer = answer;
	this.asciiAnswer = function()
	{
		var a = "";
		for (var i = 0; i < this.answer.length; i++)
		{
			a += String.fromCharCode(this.answer[i]);
		}
		return a;
	}
	this.init();
}
ECI_melimelo.prototype.splitAnswer = function(str, serialized)
{ // deserialize Sortable.serialize
	// puzzle[]=82&puzzle[]=73&puzzle[]=77&puzzle[]=66&puzzle[]=65&puzzle[]=85&puzzle[]=68
	// 82&73&77&66&65&85&68
	// 82, 73, 77, 66, 65, 85, 68
	return serialized.replace(new RegExp(str+"\\[\\]=","g"),'').split("&");
}
ECI_melimelo.prototype.init = function()
{
	//this.shuffleLetters($(this.container));
	$(this.monitor).style.display = (window.ActiveXObject ? 'inline-block' : 'inline');
	$(this.container).style.display = 'block';
	Sortable.destroy(this.container);
	Sortable.create(
		this.container,
		{
			tag: 'img',
			overlap: 'horizontal',
			constraint: false,
			onUpdate: function(){eval(window.ECI_melimelo_instanceName).updateMonitor()}
		}
	);
}
ECI_melimelo.prototype.updateMonitor = function(obj)
{
	var p = this.container;
	var now = this.splitAnswer(p, Sortable.serialize(p));
	var message = "Tu as ";
	var inplace = 0;
	for (var i = 0; i < now.length; i++)
	{
		if (Number(this.answer[i]) == Number(now[i]))
		{
			inplace++;
		}
	}
	// si le mot est complété
	if (String(now) == String(this.answer))
	{
		$(this.monitor).innerHTML = "Tu as trouvé la solution !";
		//$(this.monitor).addClassName('congrats');
		var sendAnswer = document.createElement('input');
		sendAnswer.setAttribute('type','hidden');
		sendAnswer.setAttribute('name','reponse');
		sendAnswer.setAttribute('value',this.asciiAnswer());
		$('ctl-chercher-indice').form.appendChild(sendAnswer);
		$('ctl-chercher-indice').style.visibility = 'visible';
		Sortable.destroy($(p));
	}
	else
	{
		message += (inplace + " lettre" + (inplace>1?"s":"") + " sur " + now.length + " de bien placée" + (inplace>1?"s":"") + ".");
		$(this.monitor).innerHTML = message;
	}
}
ECI_melimelo.prototype.displayObject = function(obj)
{ // (JS) utility method to display JS object in a human readable form
	var message = "";
	for (var a in obj)
	{
		message += (a + " => " + obj[a] + "\n");
		if (typeof obj[a] != 'object') continue;
		for (var c in obj[a])
		{
			message += ("\t" + c + " => " + obj[a][c] + "\n");
			if (typeof obj[a][c] != 'object') continue;
			for (var d in obj[a][c])
			{
				message += ("\t\t" + d + " => " + obj[a][c][d] + "\n");
				if (typeof obj[a][c][d] != 'object') continue;
				for (var e in obj[a][c][d])
				{
					message += ("\t\t\t" + e + " => " + obj[a][c][d][e] + "\n");
				}
			}
		}
	}
	var b = window.open();
	b.document.open();
	b.document.write('<textarea wrap="off" style="width:100%;height:600px">'+message+'</textarea>');
	b.document.close();
}
/*
ECI_melimelo.prototype.getAnswer = function()
{ // return array of asccii codes extracted from ids in original order
	var answer = [];
	var imgs = $(this.container).getElementsByTagName('img');
	for (var i = 0; i < imgs.length; i++)
	{
		answer[answer.length] = imgs[i].id.match(/_([0-9]+)$/)[1];
	}
	return answer;
}
ECI_melimelo.prototype.shuffleLetters = function(o)
{ // TODO: don't return original word!
	var a,b=[],i,r=function(f,g){return f.splice(g,1)[0]}
	for(i=0;a=o.getElementsByTagName('img')[i];i++){b[i]=a}
	while(o.hasChildNodes()){o.removeChild(o.firstChild)}
	while(b.length>0){o.appendChild(r(b,Math.floor(Math.random()*b.length)))}
	o.style.visibility = 'visible';
}
ECI_melimelo.prototype.formatAnswer = function(str,c)
{ // DEPRECATED: return a string formatted like Sortable.serialize
	var out = str + '[]';
	for (var i = 0; i < c.length; i++)
	{
		out += ("=" + (c[i]) + (i < c.length-1 ? "&" + str + "[]" : ""));
	}
	return out;
}
*/