// variable globale
var g_color = new Array(26);

// pento : f i l n p t u v w x y z
var pento_str = "filnptuvwxyz"

// liste des images pour chaque position de pentomino
var pento_f = new Array(2,"f","f1","f2","f3","f_","f_1","f_2","f_3")
var pento_i = new Array(2,"i","i1")
var pento_l = new Array(2,"l","l1","l2","l3","l_","l_1","l_2","l_3")
var pento_n = new Array(2,"n","n1","n2","n3","n_","n_1","n_2","n_3")
var pento_p = new Array(2,"p","p1","p2","p3","p_","p_1","p_2","p_3")
var pento_t = new Array(2,"t","t1","t2","t3")
var pento_u = new Array(2,"u","u1","u2","u3")
var pento_v = new Array(2,"v","v1","v2","v3")
var pento_w = new Array(2,"w","w1","w2","w3")
var pento_x = new Array(1,"x")
var pento_y = new Array(2,"y","y1","y2","y3","y_","y_1","y_2","y_3")
var pento_z = new Array(2,"z","z1","z_","z_1")

var pento = new Array(pento_f, pento_i, pento_l,pento_n,pento_p,pento_t,pento_u,pento_v,pento_w,pento_x,pento_y,pento_z)

//----------------------------------------------------------------
// definition obligatoire avec l'utilisation de la bibliothèque
//----------------------------------------------------------------
function	doActionOnRightClick(curElement)
{
	var id = curElement.id.charAt(1);	// identifiant du pento
	var ind = pento_str.indexOf(id);
	if (ind < 0)
		return;

	++pento[ind][0];
	if (pento[ind][0] == pento[ind].length)
		pento[ind][0] = 1;

	set_display(curElement, false);
	curElement.src = "img/" + pento[ind][pento[ind][0]] + ".gif"
	set_display(curElement, true);
}



function dessine_solution(sol,tableau)
{

var len = sol.length;
var col =1 , lig = 1;
var index = 0;
var car;
var obj;
var cel;
	while (index < len)
	{
		car = sol.charAt(index)
		if (car == '/')	// ligne suivante
		{
			col = 1;
			++lig;
		}
		else // affiche couleur de la case
		{
			//if (car != '.')
			{
				obj = document.getElementById(tableau+(col) + (lig))
				obj.style.background = g_color[sol.charCodeAt(index)];
			}
			++col;
		}
		++index
	}

}

// Exemple solution 5 12
// UUUVVVWYYYYZ
// UXUVFFWWYZZZ
// XXXVTFFWWZPP
// LXTTTFNNNPPP
// LLLLTNNIIIII

// les douzes pentominos : F I L N P T U V W X Y Z
g_lettre   = "FILNPTUVWXYZ"

g_pen_f    = "I/I/I/I/I"
g_pen_i    = "I/I/I/I/I"
g_pen_l    = "L /L /L /LL"
g_pen_n    = "I/I/I/I/I"
g_pen_p    = "I/I/I/I/I"
g_pen_t    = "I/I/I/I/I"
g_pen_u    = "I/I/I/I/I"
g_pen_v    = "I/I/I/I/I"
g_pen_w    = "I/I/I/I/I"
g_pen_x    = "I/I/I/I/I"
g_pen_y    = "I/I/I/I/I"
g_pen_z    = "I/I/I/I/I"

g_sol_6x10 = "UUULPPYYYY/UXULPPPWYN/XXXLZZWWNN/TXLLZWWFNV/TTTZZFFFNV/TIIIIIFVVV"

g_sol_5x12 = "UUUVVVWYYYYZ/UXUVFFWWYZZZ/XXXVTFFWWZPP/LXTTTFNNNPPP/LLLLTNNIIIII"

g_sol_4x15 = "UUUPPPLLLLNNNZZ/UYUPPXLTWNNFFZV/YYYYXXXTWWFFZZV/IIIIIXTTTWWFVVV"

g_sol_3x20 = "UUXIIIIINNNFTWYYYYZV/UXXXPPLNNFFFTWWYZZZV/UUXPPPLLLLFTTTWWZVVV"

g_sol_5x6x2 ="PPPFFNYLLLLW/PPFFNNYLZZWW/UUXFNVYYZWWT/UXXXNVYZZTTT/UUXVVVIIIIIT"

g_sol_8x8M = "UUUTLLLL/UXUTTTFL/XXXTFFFW/IXN__FWW/IYN__WWZ/IYNNVZZZ/IYYNVZPP/IYVVVPPP"

g_sol_8x8C = "_XTTTFF_/XXXTZZFF/UXUTZLFN/UUUZZLNN/IIIIILNV/WPPPLLNV/WWPPYVVV/_WWYYYY_"

g_sol_8x8N = "UUUNZVVV/UYUNZZZV/IY_NN_ZV/IYYXNPPP/IYXXXPPW/IT_XF_WW/ITFFFWWL/TTTFLLLL"

g_sol_8x8T = "UU____NL/UTTTWWNL/UUTWWNNL/IYTWXNLL/IYYXXXFF/IYZZXFFV/IYZPPPFV/IZZPPVVV"

//---------------------------------------------------------------
//-- Fonctions utilitaires
//---------------------------------------------------------------
function split(colour, which_pair) {
  // take a hex string and a string position and return the decimal
  // value of that pair (0xFF= 255 etc..)
  return HexToDec(colour.substring(0+ (which_pair* 2),
                                   2+ (which_pair* 2)));
}

function getHex(r,g,b) {
  // convert 3 dec integers to hex strings and use as a colour value
  return '#'+DecToHex(r)+DecToHex(g)+DecToHex(b);
}

var hexbase="0123456789ABCDEF";
function DecToHex(number) {
  // take a decimal integer, return 2-digit hex string
  return hexbase.charAt((number>> 4)& 0xf)+ hexbase.charAt(number& 0xf);
}

function HexToDec(number) {
  // take a hex string, return decimal integer
  return parseInt(number.toUpperCase(), 16);
}

function init()
{
var pento="FILNPTUVWXYZ"
// les 12 pentominos

// initialise couleur
g_color[pento.charCodeAt(0)] = getHex(0,0,0)
g_color[pento.charCodeAt(1)] = "yellow"
g_color[pento.charCodeAt(2)] = "white"
g_color[pento.charCodeAt(3)] = "black"
g_color[pento.charCodeAt(4)] = "gray"
g_color[pento.charCodeAt(5)] = "blue"
g_color[pento.charCodeAt(6)] = "lightgray"
g_color[pento.charCodeAt(7)] = "lightblue"
g_color[pento.charCodeAt(8)] = "cyan"
g_color[pento.charCodeAt(9)] = "magenta"
g_color[pento.charCodeAt(10)] = "green"
g_color[pento.charCodeAt(11)] = "orange"
/*
g_color[pento.charCodeAt(0)] = getHex(255,222,155)
g_color[pento.charCodeAt(1)] = getHex(255,217,136)
g_color[pento.charCodeAt(2)] = getHex(254,207,112)
g_color[pento.charCodeAt(3)] = getHex(254,198,80)
g_color[pento.charCodeAt(4)] = getHex(254,190,56)
g_color[pento.charCodeAt(5)] = getHex(239,162,1)
g_color[pento.charCodeAt(6)] = getHex(211,143,1)
g_color[pento.charCodeAt(7)] = getHex(190,129,1)
g_color[pento.charCodeAt(8)] = getHex(158,107,1)
g_color[pento.charCodeAt(9)] = getHex(131,88,1)
g_color[pento.charCodeAt(10)] = getHex(117,79,0)
g_color[pento.charCodeAt(11)] = getHex(89,61,0)
*/

g_color[pento.charCodeAt(0)] = '#800000'
g_color[pento.charCodeAt(1)] = '#B22222'
g_color[pento.charCodeAt(2)] = '#CD5C5C'
g_color[pento.charCodeAt(3)] = '#FFA07A'
g_color[pento.charCodeAt(4)] = '#FF69B4'
g_color[pento.charCodeAt(5)] = '#DB7093'
g_color[pento.charCodeAt(6)] = '#FF6347'
g_color[pento.charCodeAt(7)] = '#FFC0CB'
g_color[pento.charCodeAt(8)] = '#FF00FF'
g_color[pento.charCodeAt(9)] = '#FF7F50'
g_color[pento.charCodeAt(10)] ='#FFDAB9'
g_color[pento.charCodeAt(11)] ='#C71585'

/*
for (i=0; i<12; ++i)
{
	g_color[pento.charCodeAt(i)] = getHex(250-i*10,0+i*15,0+i*15)
}
*/

// dessine les douzes pentominoes
//dessine_solution(g_pen_i,"pi")
//dessine_solution(g_pen_l,"pl")


// 5 lignes / 12 colonnes
//dessine_solution(g_sol_6x10,"b")
/*
dessine_solution(g_sol_5x12,"c")
dessine_solution(g_sol_4x15,"d")
dessine_solution(g_sol_3x20,"e")
dessine_solution(g_sol_8x8M,"f")
dessine_solution(g_sol_8x8C,"g")
dessine_solution(g_sol_8x8N,"h")
dessine_solution(g_sol_8x8T,"i")
*/

	initDragDrop()

}
//var pento="FILNPTUVWXYZ"


function lire_pos_init() { lire_solution("0+0+f1+72+0+i1+96+0+l_3+120+0+n1+168+0+p_3+192+24+t3+240+0+u2+264+24+v1+336+24+w+360+0+x+408+24+y1+432+0+z1"); }

function initOnLoad()
{
preload("f","f1","f2","f3","f_","f_1","f_2","f_3")
preload("i","i1")
preload("l","l1","l2","l3","l_","l_1","l_2","l_3")
preload("n","n1","n2","n3","n_","n_1","n_2","n_3")
preload("p","p1","p2","p3","p_","p_1","p_2","p_3")
preload("t","t1","t2","t3")
preload("u","u1","u2","u3")
preload("v","v1","v2","v3")
preload("w","w1","w2","w3")
preload("x")
preload("y","y1","y2","y3","y_","y_1","y_2","y_3")
preload("z","z1","z_","z_1")

initGlobal()

lire_pos_init()
//test cookie
if (document.cookie)
{
//alert("Cookie = " + document.cookie.length);
//var c = document.cookie.split(';');
//alert("Cookie = " + document.cookie);
//alert("c = " + c[0]);
}


}


function sauver_solution()
{
var p
var str=""
for (i=0; i<12; ++i)
{
	var p=getObj("_"+pento_str.charAt(i))

	var img = p.src.substring(p.src.lastIndexOf("/")+1,p.src.lastIndexOf("."))
	//alert(img)
	//alert(pento[i][pento[i][0]])
	//alert(p.src)

	str += get_left(p) + "+" + get_top(p) + "+" + img + "+"
}
var dat_fin = new Date(2008,1,1);
var solution = "s"+getObj('numsol').value;
document.cookie = solution+"=" + str +";expires="+dat_fin.toGMTString();
}

function charger_solution()
{
	var num = getObj('numsol').value;
	var solution = "s"+getObj('numsol').value;
	if (document.cookie=="")
	{
		alert("Pas de sauvegarde numéro : "+num)
		return
	}

	var coo = document.cookie.split('; ') // split les cookies
	// recherche la solution numero num
	var bFound = false;
	var str;
	for (i=0; i<coo.length; ++i)
	{
		str = coo[i]
		res = str.split('=')
		if (res[0]==solution)
		{
			str = res[1];
			bFound = true;
			break;
		}
	}
	if (!bFound)
	{
		alert("Pas de sauvegarde numéro : "+num)
		return
	}

	// charge la position
	lire_solution(str)
}

function lire_solution(str)
{
	var c = str.split('+');
	var ind =0;
	for (i=0; i<12; ++i)
	{
		var p=getObj("_"+pento_str.charAt(i))
		if (p)
		{
		 	set_left(p,c[ind])
			set_top (p,c[ind+1])
			p.src ="img/" + c[ind+2] + ".gif"
			ind+=3;
		}
		if (ind > c.length)
			break;
	}
}


function go() {}
