//	 0   1   2
//
//      3   4   5   6
//
//        7   8   9
//      x-------x
//       2 x crad 

	


var crad = 70;      //A forgasi kor sugara
var xoffset3 = 50;   //A 3as pozicioja
var yoffset3 = 150;  //A 3as pozicioja
var irad = 15; //A kep szelessegenek / magassaganak fele
var sin60 = Math.sin(Math.PI/3);
var sin60crad = sin60 * crad;
var cos60 = Math.cos(Math.PI/3);
var cos60crad = cos60 * crad;
var rdegree = 2; //Hany fokkal forgassunk egy idoszelet alatt

var pos = new Array();//Poziciok
var balls = new Array(); //Labdak
var hotspota; //A labda
var hotspotb; //B labda
var rotating = false; //Van-e folyamatban forgatas
var rotateq = new Array(); //A forgatasok varakozasi sora
var scrambling = false;
var scrambled = false;

var secret_state = 0;

function start_secret() {
	initGame();
}
function click_secret() {
	if (secret_state==0) secret_state=1; else if (secret_state==2) secret_state=3; else if (secret_state==3) {secret_state=0;start_secret();} else secret_state=0;
}
function mouseover_secret() {
	if (secret_state==1) secret_state=2; else secret_state=0;
}

function initGame() {
	gamecontainer = document.createElement('DIV');
	gamecontainer.style.position = 'absolute';
	gamecontainer.style.top = '100px';
	gamecontainer.style.left = '0px';
	gamecontainer.style.width = '100%';
	gamecontainer.id = 'gamecontainer';
	gameframe= document.createElement('DIV');
	gameframe.style.width = '750px';
	gameframe.style.marginLeft = 'auto';
	gameframe.style.marginRight = 'auto';
	gameframe.style.backgroundColor = '#eeeeee';
	gameframe.style.borderColor = "#000000";
	gameframe.style.borderWidth = "2px";
	gameframe.style.borderStyle = "solid";
	gameframe.id = "gameframe";
	header= document.createElement('P');
	headertext = document.createTextNode('Egy kis játék...');
	header.appendChild(headertext);
	header.style.textAlign = 'center';
	header.style.fontSize = '16px';
	header.style.fontWeight = 'bold';
	header.style.fontStyle = 'normal';
	header.style.clear = 'both';
	header.style.padding = '5px';
	gd = document.createElement('DIV');
	gd.style.width = '750px';
	gd.style.height = '300px';
	gd.style.position = 'relative';
	game = document.createElement('DIV');
	game.style.position = 'absolute';
	game.style.left = '25px';
	game.style.width = '300px';
	game.style.height = '300px';
	game.style.backgroundColor = '#ffffff';
	game.id = 'game';
	help = document.createElement('DIV');
	help.style.textAlign= 'justify';
	help.style.position= 'absolute';
	help.style.left = '350px';
	help.style.width = '375px';
	helptext = document.createTextNode('Rakja ki a helyes sorrendet a kockák forgatásával. A két középső kocka meletti nyilacskákra kattintva forgathat. Jegyezze meg, milyen helyzetben vannak most a kockák, ez a jó sorrend: felül brillant, középen sárga, alul lila. Kattintson a keverés gombra, figyelje mi történik, és kezdődhet a játék - a jutalom nem marad el.');
	help.appendChild(helptext);
	scramblebutton = document.createElement('INPUT');
	scramblebutton.setAttribute('type','button');
	scramblebutton.setAttribute('value','Keverés');
	scramblebutton.style.marginLeft = 'auto';
	scramblebutton.style.marginRight = 'auto';
	scramblebutton.style.marginTop = '10px';
	scramblebutton.style.borderWidth = '1px';
	scramblebutton.style.borderColor = '#000000';
	scramblebutton.style.backgroundColor = '#ffffff';
	scramblebutton.style.display = 'block';
	scramblebutton.onmousedown = new Function("scramble();return false;");
	help.appendChild(scramblebutton);
	gd.appendChild(game);
	gd.appendChild(help);
	exitlink = document.createElement('P');
	exitlink.style.clear = 'both';
	exitlink.style.cursor = 'pointer';
	exitlink.style.fontWeight = 'bold';
	exitlink.style.textAlign = 'right';
	exitlink.style.padding = '2px';
	exitlink.onclick = new Function("document.body.removeChild(document.getElementById('gamecontainer'));");
	exitlinktext = document.createTextNode('Unom a játékot, inkább rendelek WebLego-t...');
	exitlink.appendChild(exitlinktext);
	gameframe.appendChild(header);
	gameframe.appendChild(gd);
	gameframe.appendChild(exitlink);
	gamecontainer.appendChild(gameframe);
	document.body.appendChild(gamecontainer);
	//Poziciok es kezdoszinek meghatarozasa
	for (i=0; i<=9; i++) pos[i] = new Array();
	pos[3]['x'] = xoffset3 - irad;
	pos[3]['y'] = yoffset3 - irad;
	pos[3]['c'] = 'g';
	pos[4]['x'] = pos[3]['x'] + crad;
	pos[4]['y'] = pos[3]['y'];
	pos[4]['c'] = 'g';
	pos[5]['x'] = pos[3]['x'] + 2*crad;
	pos[5]['y'] = pos[3]['y'];
	pos[5]['c'] = 'g';
	pos[6]['x'] = pos[3]['x'] + 3*crad;
	pos[6]['y'] = pos[3]['y'];
	pos[6]['c'] = 'g';
	pos[0]['x'] = pos[3]['x'] + cos60crad;
	pos[0]['y'] = pos[3]['y'] - sin60crad;
	pos[0]['c'] = 'r';
	pos[1]['x'] = pos[4]['x'] + cos60crad;
	pos[1]['y'] = pos[4]['y'] - sin60crad;
	pos[1]['c'] = 'r';
	pos[2]['x'] = pos[5]['x'] + cos60crad;;
	pos[2]['y'] = pos[5]['y'] - sin60crad;
	pos[2]['c'] = 'r';
	pos[7]['x'] = pos[3]['x'] + cos60crad;;
	pos[7]['y'] = pos[3]['y'] + sin60crad;
	pos[7]['c'] = 'b';
	pos[8]['x'] = pos[4]['x'] + cos60crad;;
	pos[8]['y'] = pos[4]['y'] + sin60crad;
	pos[8]['c'] = 'b';
	pos[9]['x'] = pos[5]['x'] + cos60crad;;
	pos[9]['y'] = pos[5]['y'] + sin60crad;
	pos[9]['c'] = 'b';

	//labdak feltoltese
	for (i=0; i<=9; i++) {
		o = document.createElement('IMG');
		if (pos[i]['c']=='r') {
			o.src = "fileadmin/templates/puzzle_magenta.gif";
		} else if (pos[i]['c']=='b') {
			o.src = "fileadmin/templates/puzzle_purple.gif";
		} else {
			o.src = "fileadmin/templates/puzzle_orange.gif";
		}
		o.style.position = 'absolute';
		o.style.top = pos[i]['y'] + 'px';
		o.style.left = pos[i]['x'] + 'px';
		document.getElementById('game').appendChild(o);
		balls[i] = new Array();
		balls[i]['o'] = o;
		balls[i]['p'] = i;
		balls[i]['c'] = pos[i]['c'];
	}

	//Hotspotok
	hotspota = document.createElement("IMG");
	hotspotb = document.createElement("IMG");
	hotspotc = document.createElement("IMG");
	hotspotd = document.createElement("IMG");
	hotspota.src = "fileadmin/templates/fel.gif";
	hotspotb.src = "fileadmin/templates/fel.gif";
	hotspotc.src = "fileadmin/templates/le.gif";
	hotspotd.src = "fileadmin/templates/le.gif";
	hotspota.style.position = "absolute";
	hotspotb.style.position = "absolute";
	hotspotc.style.position = "absolute";
	hotspotd.style.position = "absolute";
	hotspota.style.left = (pos[4]['x'] - 14) + 'px';
	hotspota.style.top = (pos[4]['y'] - 1) + 'px';
	hotspotb.style.left = (pos[5]['x'] + 27) + 'px';
	hotspotb.style.top = (pos[5]['y'] - 1) + 'px';
	hotspotc.style.left = (pos[4]['x'] - 14) + 'px';
	hotspotc.style.top = (pos[4]['y'] + 14) + 'px';
	hotspotd.style.left = (pos[5]['x'] + 27) + 'px';
	hotspotd.style.top = (pos[5]['y'] + 14) + 'px';
	hotspota.onmousedown = new Function("queue_rotate('a','cw'); return false;");
	hotspotb.onmousedown = new Function("queue_rotate('b','ccw'); return false;");
	hotspotc.onmousedown = new Function("queue_rotate('a','ccw'); return false;");
	hotspotd.onmousedown = new Function("queue_rotate('b','cw'); return false;");
	document.getElementById('game').appendChild(hotspota);
	document.getElementById('game').appendChild(hotspotb);
	document.getElementById('game').appendChild(hotspotc);
	document.getElementById('game').appendChild(hotspotd);
}


function scramble() {
	scrambling = true;
	for (i=1;i<=20;i++) {
		if (Math.random()>0.5) center =  'a'; else center = 'b';
		if (Math.random()>0.5) rot =  'cw'; else rot = 'ccw';
		r = new Array(center,rot);
		rotateq.push(r);
	}
	scrambled = true;
	start_rotate();
}

function queue_rotate(center, rot) {
	if (scrambling) return;
	if (!scrambled) {
		alert('Előbb keverje meg a kockákat a keverés gomb segítségével.');
		return;
	}
	r = new Array(center,rot);
	rotateq.push(r);
	if (rotating==false) start_rotate();
}

function start_rotate() {
	r = rotateq.shift();
	if (r) {
		rotating=true;
		rotate(r[0], r[1]);
	}	
}
function next_rotate() {
	r = rotateq.shift();
	if (r) {
		rotate(r[0], r[1]);
	} else {
		rotating = false;
		scrambling = false;
	}
}
function clear_rotate() {
	rotateq = new Array();
	rotating = false;
}	
function win() {
	scrambled = false;
	alert('Gratulálunk, sikerült megoldani a feladatot. Jutalmul rendeljen magának egy WebLego csomagot! A "Kiraktam a kockákat!" jelmondat megjelölésével most az első havidíjból 10 % kedvezményt adunk.');
}

function rotate(center, rot) {
	rotate_recursive(center, rot, 0);
}

function rotate_recursive(center, rot, degrees) {
	degrees += rdegree;
	setanim(center, rot, degrees);
	if (degrees<60) {
		t = Math.abs(30-degrees);
		t = t*t / 30;
		setTimeout("rotate_recursive('"+center+"','"+rot+"',"+degrees+")",t);
	} else {
		rotate_finish(center,rot);
	}
}
function rotate_finish(center, rot) {
	for (i=0;i<=9;i++) {
		newp = maprotation(center,rot,balls[i]['p']);
		balls[i]['p'] = newp;
		balls[i]['o'].style.left = pos[balls[i]['p']]['x'] + 'px';
		balls[i]['o'].style.top = pos[balls[i]['p']]['y'] + 'px';
	}
	if (check() && !scrambling) {
		clear_rotate();
		win();
	} else {
		next_rotate();
	}
}

function setanim(center, rot, degrees) {
	if (rot=='ccw') degrees *= -1;
	for (i=0;i<=9;i++) {
		cp = getcirclepos(center, balls[i]['p']);
		animx = 0;
		animy = 0;
		switch (cp) {
			case 1: animx = 1 - Math.cos(degrees  * Math.PI/180);
				animy = -1 * Math.sin(degrees * Math.PI/180);
				break;
			case 2: animx = Math.cos(120* Math.PI/180) -  Math.cos((degrees+60) * Math.PI/180) + 1;
				animy = Math.sin(60* Math.PI/180) - Math.sin((degrees+60) * Math.PI/180);
				break;
			case 3: animx = Math.cos(60* Math.PI/180) - Math.cos((degrees + 120) * Math.PI/180) - 1;
				animy = Math.sin(60* Math.PI/180) - Math.sin((degrees + 120) * Math.PI/180);
				break;
			case 4: animx = -1 * (1 - Math.cos(degrees  * Math.PI/180));
				animy = Math.sin(degrees * Math.PI/180);
				break;
			case 5: animx = -1 * (Math.cos(120* Math.PI/180) -  Math.cos((degrees+60) * Math.PI/180) + 1);
				animy = -1 * (Math.sin(60* Math.PI/180) - Math.sin((degrees+60) * Math.PI/180));
				break;
			case 6: animx = -1 * (Math.cos(60* Math.PI/180) - Math.cos((degrees + 120) * Math.PI/180) - 1);
				animy = -1 * (Math.sin(60* Math.PI/180) - Math.sin((degrees + 120) * Math.PI/180));
				break;
			default: animx=0;
				 animy=0;
				 break;
		}	
		animx *= crad;
		animy *= crad;
		balls[i]['o'].style.left = (pos[balls[i]['p']]['x'] + animx) + 'px';
		balls[i]['o'].style.top = (pos[balls[i]['p']]['y'] + animy) + 'px';

	}
}

function getcirclepos(center, pos) {
	//   2  3 
	// 1      4
	//   6  5
	if (center=='a') {
		switch (pos) {
			case 3: return 1; 
			case 0: return 2;
			case 1: return 3;
			case 5: return 4;
			case 8: return 5;
			case 7: return 6;
			default: return 0;
		}

	} else {		
		switch (pos) {
			case 4: return 1; 
			case 1: return 2;
			case 2: return 3;
			case 6: return 4;
			case 9: return 5;
			case 8: return 6;
			default: return 0;
		}

	}
}

function maprotation(center,rot,inp) {
	if (center=='a') {
		if (rot=='cw') {
			switch (inp) {
				case 3: return 0; 
				case 0: return 1;
				case 1: return 5;
				case 5: return 8;
				case 8: return 7;
				case 7: return 3;
				default: return inp;
			}
		} else {
			switch (inp) {
				case 3: return 7; 
				case 0: return 3;
				case 1: return 0;
				case 5: return 1;
				case 8: return 5;
				case 7: return 8;
				default: return inp;
			}
		}
	} else {
		if (rot=='cw') {
			switch (inp) {
				case 4: return 1; 
				case 1: return 2;
				case 2: return 6;
				case 6: return 9;
				case 9: return 8;
				case 8: return 4;
				default: return inp;
			}
		} else {
			switch (inp) {
				case 4: return 8; 
				case 1: return 4;
				case 2: return 1;
				case 6: return 2;
				case 9: return 6;
				case 8: return 9;
				default: return inp;
			}
		}
	}
}

function check() {
	for (i=0;i<=9;i++) {
		p = balls[i]['p'];
		switch (p) {
			case 0:
			case 1:
			case 2: c='r'; break;
			case 3:
			case 4:
			case 5:
			case 6: c='g'; break;
			case 7:
			case 8:
			case 9: c='b'; break;
		}
		if (balls[i]['c'] != c) return false;
	}
	return true;
}
