//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()}
$("#ardoraAct").mousemove(function (e) {var posX = e.pageX - this.offsetLeft;var posY = e.pageY - this.offsetTop;drawJoin(posX,posY);});
$(".conten").bind("click", function (e) {if (sel1.localeCompare("")==0){sel1=$(this).attr("id");$(this).css({background: colorSele, borderColor: colorButton });
var color1=colorSele;var color2=colorBack;
$(this).css("background","-webkit-gradient(linear, left top, left bottom, from("+color1+"), to("+color2+"))");
$(this).css("background","-webkit-linear-gradient("+color1+", "+color2+")");
$(this).css("background","-moz-linear-gradient(top, "+color1+", "+color2+")");
$(this).css("background","-ms-linear-gradient("+color1+", "+color2+")");
$(this).css("background","-o-linear-gradient("+color1+", "+color2+")");
$(this).css("background","linear-gradient("+color1+", "+color2+")");
$(this).css("filter","progid:DXImageTransform.Microsoft.Alpha(startColorstr='"+color1+"', endColorstr='"+color2+"')");
}else{if (Math.abs(parseInt($(this).attr("id").substr(6,1))-parseInt(sel1.substr(6,1)))>1 || $(this).attr("id").substr(0,7)==sel1.substr(0,7)){
$("#"+sel1).css({ background: colorBack, borderColor: colorText });sel1="";drawJoin(0,0);}else{join1.push(sel1);join2.push($(this).attr("id"));
$("#"+sel1).css({ background: colorBack, borderColor: colorText });sel1="";drawJoin(0,0);drawGame();}}});
$( ".conten" ).dblclick(function(e) {delJoin($(this).attr("id"));});
$(".conten").bind("taphold", function (e) {e.preventDefault();delJoin($(this).attr("id"));});
$(".conten").mouseenter(function () {$(this).css({background: colorSele,borderColor: colorButton});
var color1=colorSele;var color2=colorBack;
$(this).css("background","-webkit-gradient(linear, left top, left bottom, from("+color1+"), to("+color2+"))");
$(this).css("background","-webkit-linear-gradient("+color1+", "+color2+")");
$(this).css("background","-moz-linear-gradient(top, "+color1+", "+color2+")");
$(this).css("background","-ms-linear-gradient("+color1+", "+color2+")");
$(this).css("background","-o-linear-gradient("+color1+", "+color2+")");
$(this).css("background","linear-gradient("+color1+", "+color2+")");
$(this).css("filter","progid:DXImageTransform.Microsoft.Alpha(startColorstr='"+color1+"', endColorstr='"+color2+"')");
});$(".conten").mouseleave(function () {if ($(this).attr("id")!=sel1){ $(this).css({ background: colorBack, borderColor: colorText });}});
}
function delJoin(ele){var pos = join1.indexOf(ele);while (pos>-1){join1.splice(pos,1);join2.splice(pos,1);pos = join1.indexOf(ele);}
pos = join2.indexOf(ele);while (pos>-1){join1.splice(pos,1);join2.splice(pos,1);pos = join2.indexOf(ele);}drawGame();}
function drawJoin(posX,posY){var canvas = document.getElementById("joinCanvas");canvas.width=canvas.width;var context = canvas.getContext("2d");
if (sel1.localeCompare("")!=0){var canLeft = document.getElementById("ardoraAct").offsetLeft;var canTop = document.getElementById("ardoraAct").offsetTop;
context.lineWidth=2.5;var posDivX=document.getElementById(sel1).parentNode.parentNode.offsetLeft+$("#"+sel1).width()+10;var posDivY=document.getElementById(sel1).offsetTop+12+5;
context.strokeStyle=colorButton;context.fillStyle=colorSele;context.lineCap = "round";context.beginPath();context.arc(posDivX, posDivY, 5, 0, 2 * Math.PI, false);
context.fill();context.moveTo(posDivX,posDivY);context.quadraticCurveTo(posDivX+50,posDivY,posX,posY);context.arc(posX, posY, 5,0,2*Math.PI, false);context.stroke();}}
function drawGame(){var canvas = document.getElementById("joinCanvasDo");canvas.width=canvas.width;var context = canvas.getContext("2d");
for (i = 0; i < join1.length; i++) {var canLeft = document.getElementById("ardoraAct").offsetLeft;var canTop = document.getElementById("ardoraAct").offsetTop;context.lineWidth=2.5;
if(parseInt(join1[i].substr(6,1))<parseInt(join2[i].substr(6,1))){var posX=document.getElementById(join2[i]).parentNode.parentNode.offsetLeft-5;var posY=document.getElementById(join2[i]).offsetTop+12+5;//12 é o tamaño da fonte;
var posDivX=document.getElementById(join1[i]).parentNode.parentNode.offsetLeft+$("#"+join1[i]).width()+10;var posDivY=document.getElementById(join1[i]).offsetTop+12+5;}
else{var posX=document.getElementById(join1[i]).parentNode.parentNode.offsetLeft-5;var posY=document.getElementById(join1[i]).offsetTop+12+5;
var posDivX=document.getElementById(join2[i]).parentNode.parentNode.offsetLeft+$("#"+join2[i]).width()+10;var posDivY=document.getElementById(join2[i]).offsetTop+12+5;}
context.strokeStyle=colorSele;context.fillStyle=colorSele;context.lineCap = "round";context.beginPath();context.arc(posDivX, posDivY, 5, 0, 2 * Math.PI, false);context.fill();
context.moveTo(posDivX,posDivY);context.quadraticCurveTo(posDivX+50,posDivY,posX,posY);context.stroke();context.beginPath();
context.arc(posX, posY, 5,0,2*Math.PI, false);context.fill();}}
function randomSort(){
for (i = 0; i < con1.length; i++) {var ind = Math.floor(Math.random()*(con1.length-1))+1;var divId="conten1_"+parseInt(ind);
while (document.getElementById(divId).innerHTML.localeCompare("")!=0){ind++;if (ind>con1.length){ind=1;}divId="conten1_"+parseInt(ind);}document.getElementById(divId).innerHTML=con1[i];}
for (i = 0; i < con2.length; i++) {var ind = Math.floor(Math.random()*(con2.length-1))+1;var divId="conten2_"+parseInt(ind);
while (document.getElementById(divId).innerHTML.localeCompare("")!=0){ind++;if (ind>con2.length){ind=1;}divId="conten2_"+parseInt(ind);}document.getElementById(divId).innerHTML=con2[i];}
for (i = 0; i < con3.length; i++) {var ind = Math.floor(Math.random()*(con3.length-1))+1;var divId="conten3_"+parseInt(ind);
while (document.getElementById(divId).innerHTML.localeCompare("")!=0){ind++;if (ind>con3.length){ind=1;}divId="conten3_"+parseInt(ind);}document.getElementById(divId).innerHTML=con3[i];}
for (i = 0; i < con4.length; i++) {var ind = Math.floor(Math.random()*(con4.length-1))+1;var divId="conten4_"+parseInt(ind);
while (document.getElementById(divId).innerHTML.localeCompare("")!=0){ind++;if (ind>con4.length){ind=1;}divId="conten4_"+parseInt(ind);}document.getElementById(divId).innerHTML=con4[i];}
}
function isCorrect(){
var correct=true; successes=0;var joinWords1=[];var joinWords2=[];
for (i = 0; i < join1.length; i++) {joinWords1.push($("#" +join1[i]).text());joinWords2.push($("#" +join2[i]).text());}
for (i=0;i<joinPar.length;i++) {var check = false;for (j = 0; j < joinWords1.length; j++) {if (joinWords1[j].localeCompare(unirWords(joinPar[i][0]).substring(0,c[i][0])) == 0) {
if (joinWords2[j].localeCompare(unirWords(joinPar[i][1]).substring(0,c[i][1])) == 0) {check = true;}}}if (!check) {check = false;
for (j = 0; j < joinWords1.length; j++) {if (joinWords2[j].localeCompare(unirWords(joinPar[i][0]).substring(0,c[i][0])) == 0) {if (joinWords1[j].localeCompare(unirWords(joinPar[i][1]).substring(0,c[i][1])) == 0) {
check = true;}}}}if (!check) {check=false;for (j = 0; j < joinWords1.length; j++) {if (joinWords1[j].localeCompare(unirWords(joinPar[i][1]).substring(0,c[i][1])) == 0) {if (joinWords2[j].localeCompare(unirWords(joinPar[i][0]).substring(0,c[i][0])) == 0) {
check = true;}}}}if (!check) {check = false;for (j = 0; j < joinWords1.length; j++) {if (joinWords2[j].localeCompare(unirWords(joinPar[i][1]).substring(0,c[i][1])) == 0) {
if (joinWords1[j].localeCompare(unirWords(joinPar[i][0]).substring(0,c[i][0])) == 0) {check = true;}}}}if (!check){correct=false}else{successes++}}
for (i = 0; i < joinWords1.length; i++) {check=false;for (j=0;j<joinPar.length;j++) {if (unirWords(joinPar[j][0]).substring(0,c[j][0]).localeCompare(joinWords1[i]) == 0) {
if (unirWords(joinPar[j][1]).substring(0,c[j][1]).localeCompare(joinWords2[i]) == 0) {check = true;}}}
if (!check) {for (j=0;j<joinPar.length;j++) {if (unirWords(joinPar[j][1]).substring(0,c[j][1]).localeCompare(joinWords1[i]) == 0) {if (unirWords(joinPar[j][0]).substring(0,c[j][0]).localeCompare(joinWords2[i]) == 0) {
check = true;}}}}if (!check) {for (j=0;j<joinPar.length;j++) {if (unirWords(joinPar[j][0]).substring(0,c[j][0]).localeCompare(joinWords2[i]) == 0) {
if (unirWords(joinPar[j][1]).substring(0,c[j][1]).localeCompare(joinWords1[i]) == 0) {check = true;}}}}
if (!check) {for (j=0;j<joinPar.length;j++) {if (unirWords(joinPar[j][1]).substring(0,c[j][1]).localeCompare(joinWords2[i]) == 0) {
if (unirWords(joinPar[j][0]).substring(0,c[j][0]).localeCompare(joinWords1[i]) == 0) {check=true;}}}}if (!check) {correct=false}}
if (correct){score = score + scoreInc;timeAct=timeAct+timeBon;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){ 
join1=[];join2=[];var divJoin=document.getElementsByTagName("div");for (i = 0; i < joinPar.length; i++) {
for (j=0;j<divJoin.length;j++){if ($("#"+divJoin[j].id).text().localeCompare(unirWords(joinPar[i][0]))==0){join1.push(divJoin[j].id);
for (k=0;k<divJoin.length;k++){if ($("#"+divJoin[k].id).text().localeCompare(unirWords(joinPar[i][1]))==0){join2.push(divJoin[k].id);}}}}}drawGame();
}
function paintBack(){}
function unirWords(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;}
