//Creado con Ardora - www.webardora.net
//bajo licencia Attribution-NonCommercial-NoDerivatives 4.0 International (CC BY-NC-ND 4.0)
//para otros usos contacte con el autor
function initAct(){
if (tiAval){parent.iniciaActividade()}if ((tiTime) && (tiButtonTime)){paintButtonTime();}
cssColors();addElements();drawGuide();upElements();
document.addEventListener("touchstart", touchHandler, true);document.addEventListener("touchmove", touchHandler, true);document.addEventListener("touchend", touchHandler, true);document.addEventListener("touchcancel", touchHandler, true);
$(".figure").mouseup(function(evt) {deselectElement(evt);})
.mousedown(function(evt) {$("#playArea").append($(this));var doRotate=false;var p=$(this).attr("points").split(" ");var disX=5000;var disY=5000;var cRotX=-5;var cRotY=-5;
for (i=0;i<p.length;i++){var parentOffset = $(this).parent().offset();var pI=p[i].split(",");disX=evt.clientX-parentOffset.left-parseInt(pI[0]);disY=evt.clientY-parentOffset.top-parseInt(pI[1]);if (disX*disX+disY*disY<400){doRotate=true;cRotX=parseInt(pI[0]);cRotY=parseInt(pI[1]);}}
if (doRotate){var newPoints="";for (i=0;i<p.length;i++){var pI=p[i].split(",");var nPx=cRotX+(parseInt(pI[0])-cRotX)*0.707-(parseInt(pI[1])-cRotY)*0.707;var nPy=cRotY+(parseInt(pI[0])-cRotX)*0.707+(parseInt(pI[1])-cRotY)*0.707;nPx=Math.round(nPx/10)*10;
nPy=Math.round(nPy/10)*10;newPoints=newPoints+nPx.toString()+","+nPy.toString()+" ";} $(this).attr("points",$.trim(newPoints));} else {selectElement(evt);}});
}
function touchHandler(event) {var touch = event.changedTouches[0];var simulatedEvent = document.createEvent("MouseEvent");simulatedEvent.initMouseEvent({touchstart: "mousedown",touchmove: "mousemove",touchend: "mouseup"}[event.type], true, true, window, 1,
touch.screenX, touch.screenY,touch.clientX, touch.clientY, false,false, false, false, 0, null);touch.target.dispatchEvent(simulatedEvent);event.preventDefault();};
function addElements(){var dis=10;
for (i=0;i<40;i++){for (j=0;j<40;j++){var svg = document.getElementsByTagName("svg")[0];var newElement = document.createElementNS("http://www.w3.org/2000/svg","circle");
newElement.setAttribute("cx",i*dis);newElement.setAttribute("cy",j*dis);newElement.setAttribute("r",1);newElement.style.fill = "#B7B7B7";svg.appendChild(newElement);}}
}
function upElements(){$("#playArea").append($("#gt7"));$("#playArea").append($("#gt6"));$("#playArea").append($("#gt5"));$("#playArea").append($("#gt4"));$("#playArea").append($("#gt3"));$("#playArea").append($("#gt2"));$("#playArea").append($("#gt1"));}
function drawGuide(){var svg = document.getElementsByTagName("svg")[0];var g = document.createElementNS("http://www.w3.org/2000/svg", "g");g.setAttribute("id","guide");svg.appendChild(g);
var newF1=document.createElementNS("http://www.w3.org/2000/svg", "polygon");newF1.setAttribute("points",s1);newF1.style.stroke = "#D4D4D4";newF1.style.strokeWidth = "0px";newF1.style.fill = "#D4D4D4";
var newF2=document.createElementNS("http://www.w3.org/2000/svg", "polygon");newF2.setAttribute("points",s2);newF2.style.stroke = "#D4D4D4";newF2.style.strokeWidth = "0px";newF2.style.fill = "#D4D4D4";
var newF3=document.createElementNS("http://www.w3.org/2000/svg", "polygon");newF3.setAttribute("points",s3);newF3.style.stroke = "#D4D4D4";newF3.style.strokeWidth = "0px";newF3.style.fill = "#D4D4D4";
var newF4=document.createElementNS("http://www.w3.org/2000/svg", "polygon");newF4.setAttribute("points",s4);newF4.style.stroke = "#D4D4D4";newF4.style.strokeWidth = "0px";newF4.style.fill = "#D4D4D4";
var newF5=document.createElementNS("http://www.w3.org/2000/svg", "polygon");newF5.setAttribute("points",s5);newF5.style.stroke = "#D4D4D4";newF5.style.strokeWidth = "0px";newF5.style.fill = "#D4D4D4";
var newF6=document.createElementNS("http://www.w3.org/2000/svg", "polygon");newF6.setAttribute("points",s6);newF6.style.stroke = "#D4D4D4";newF6.style.strokeWidth = "0px";newF6.style.fill = "#D4D4D4";
var newF7=document.createElementNS("http://www.w3.org/2000/svg", "polygon");newF7.setAttribute("points",s7);newF7.style.stroke = "#D4D4D4";newF7.style.strokeWidth = "0px";newF7.style.fill = "#D4D4D4";
var svg_g=document.getElementById("guide");svg_g.appendChild(newF1);svg_g.appendChild(newF2);svg_g.appendChild(newF3);svg_g.appendChild(newF4);svg_g.appendChild(newF5);svg_g.appendChild(newF6);svg_g.appendChild(newF7);svg_g.style.fillOpacity="0.7"; }
function selectElement(evt) {selectedElement = evt.target;currentX = evt.clientX;currentY = evt.clientY;currentMatrix = selectedElement.getAttributeNS(null, "transform").slice(7,-1).split(" ");for(var i=0; i<currentMatrix.length; i++) {currentMatrix[i] = parseFloat(currentMatrix[i]);} selectedElement.setAttributeNS(null, "onmousemove", "moveElement(evt)");}
function moveElement(evt){dx = evt.clientX - currentX;dy = evt.clientY - currentY;currentMatrix[4] += dx;currentMatrix[5] += dy;newMatrix = "matrix(" + currentMatrix.join(" ") + ")";selectedElement.setAttributeNS(null, "transform", newMatrix);currentX = evt.clientX;currentY = evt.clientY;}
function deselectElement(evt){if(selectedElement != 0){selectedElement.removeAttributeNS(null, "onmousemove");selectedElement.removeAttributeNS(null, "onmouseout");selectedElement.removeAttributeNS(null, "onmouseup");actPoints(selectedElement,evt);selectedElement = 0;}}
function actPoints(fig,e){dx=e.clientX;dy=e.clientY;var p=$(fig).attr("points").split(" ");var newPoints="";for (i=0;i<p.length;i++){var pI=p[i].split(",");var nPx=(Math.round((parseInt(pI[0])+currentMatrix[4])/10))*10;var nPy=(Math.round((parseInt(pI[1])+currentMatrix[5])/10))*10;newPoints=newPoints+nPx.toString()+","+nPy.toString()+" ";} $(fig).attr("points",$.trim(newPoints));$(fig).attr("transform","matrix(1 0 0 1 0 0)");}
function randomSort(){
}
function isCorrect(){
var tiF=false;var canvas = document.getElementById("testCanvas");var ima = canvas.getContext("2d");ima.fillStyle = "white";ima.fillRect(0, 0, canvas.width, canvas.height);ima.fillStyle = "black";ima.strokeStyle = "black";ima.lineWidth=3;
for (z = 1; z < 8; z++) {ima.beginPath();var p = $("#gt" + z.toString()).attr("points").split(" ");var pI = p[0].split(",");var nX = parseInt(pI[0]);var nY = parseInt(pI[1]);ima.moveTo(nX, nY);
for (i = 1; i < p.length; i++) {var pI=p[i].split(",");var nX = parseInt(pI[0]);var nY = parseInt(pI[1]);ima.lineTo(nX, nY);} var nX = parseInt(pI[0]);var nY = parseInt(pI[1]);ima.lineTo(nX, nY);ima.closePath();ima.stroke();ima.fill();}
for (x=0;x<41; x++) {testMatrix[x] = new Array(41);}
for (x=0; x<401; x += 10) {for (y = 0; y < 401; y += 10) {var col=Math.round(x/10);var row=Math.round(y/10);var colorR = ima.getImageData(x,y,1,1).data[0];if (parseInt(colorR)==0){testMatrix[row][col]=1;tiF=true;} else {testMatrix[row][col]=0;}}}
if (tiF){var isClean = true;while (isClean) {for (x=0; x<41; x++) {if (testMatrix[x][0]==1) {isClean=false}} if (isClean) {for (x = 0; x < 40; x++) {for (y = 0; y < 40; y++) {testMatrix[x][y] = testMatrix[x][y + 1]}} for (x=0;x<41;x++) {testMatrix[x][40]=0;}}}
isClean = true;while (isClean) {for (x=0;x<41;x++) {if (testMatrix[0][x]==1) {isClean=false}} if (isClean) {for (x=0;x<40;x++) {for (y=0;y<40;y++) {testMatrix[x][y] = testMatrix[x + 1][y]}} for (x=0;x<41;x++) {testMatrix[40][x]=0;}}}
var isClean = true;while (isClean) {for (x=0;x<41;x++) {if (matrix[x][0]==1) {isClean=false}} if (isClean) {for (x = 0; x < 40; x++) {for (y = 0; y < 40; y++) {matrix[x][y] = matrix[x][y + 1]}} for (x = 0; x < 41; x++) {matrix[x][40] = 0;}}}
isClean = true;while (isClean) {for (x=0;x<41;x++) {if (matrix[0][x] == 1) {isClean=false}} if (isClean) {for (x = 0; x < 40; x++) {for (y=0;y<40;y++){matrix[x][y] = matrix[x + 1][y]}}for (x = 0; x < 41; x++) {matrix[40][x] = 0;}}}
}
var correct = true;for (i=0;i<41;i++) {for (j=0;j<41; j++) {if (matrix[i][j]!=testMatrix[i][j]) {correct=false;}}}
if (correct) {score=score+scoreInc;timeAct=timeAct+timeBon;successes++;showMessage("Ok");} else {attempts++;score=score-scoreDec;if (tiAttempts) {if (attempts > attemptsMax) {showMessage("Attempts");} else {showMessage("Error");}} else {showMessage("Error");}}
}
function goTime(){clearInterval(timeInterval);showMessage("Time");}
function showSol(oldTypeGame){ 
}
function paintBack(){}
function TangramWords(input) {var output = ""; var chr1, chr2, chr3 = ""; var enc1, enc2, enc3, enc4 = "";var i = 0;
var btest = /[^A-Za-z0-9\+\/\=]/g; if (btest.exec(input)) { alert("Invalid characters");} input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
do { enc1 = wordsStr.indexOf(input.charAt(i++)); enc2 = wordsStr.indexOf(input.charAt(i++)); enc3 = wordsStr.indexOf(input.charAt(i++)); enc4 = wordsStr.indexOf(input.charAt(i++)); chr1 = (enc1 << 2) | (enc2 >> 4); chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);chr3 = ((enc3 & 3) << 6) | enc4;
output = output + String.fromCharCode(chr1);if (enc3 != 64) {output = output + String.fromCharCode(chr2);} if (enc4 != 64) {output = output + String.fromCharCode(chr3);}
chr1 = chr2 = chr3 = ""; enc1 = enc2 = enc3 = enc4 = "";} while (i < input.length);return unescape(output);}
Array.prototype.in_array=function(){ for(var j in this){ if(this[j]==arguments[0]){return true;}}return false;}
