$(document).ready(function() {
vicisrunning = false;
startanisrunning = true;
$(window).blur(function() {
if(startanisrunning){
clearInterval(introanix);
$(".kachel").removeClass("introanicube");
}
if(vicisrunning){
clearInterval(finalscoreani);
$(".kachel").removeClass("victory");
}
})
var animationend = false;
var level = "medium"
var clickable = false;
var points= 0;
var marathontime = 5
var finalscoreani;
var blinkin;
var actions = 0;
var modus
var hightime
var animod
var rightposdur = false
for(z=0;z<25;z++){
$("#wrap").append('
');
}
for(z=0;z<5;z++){
for(i=0;i<5;i++){
$(".kachel").eq(i+z *5).css("left",90 *(i)).css("top",90 * z);
$(".kachel").eq(i+z *5).data("indez",i+z*5);
}
}
//localStorage.removeItem("actions");
//localStorage.removeItem("adv-time");
//localStorage.removeItem("marathon");
actionar = [];
if(localStorage.getItem("actions") != undefined){
actionartemp = localStorage.getItem("actions");
actionar = actionartemp.split(",");
}
timear = []
if(localStorage.getItem("adv-time") != undefined){
timeartemp = localStorage.getItem("adv-time");
timear = timeartemp.split(",");
}
marathonar = [[],[],[]];
function getarstorage(arrayx) {
toar = arrayx.split("y");
mark = [];
for(i=0;i<3;i++)
mark[i] = toar[i].split(";");
return mark;
}
if(localStorage.getItem("marathon") != undefined){
marathonar = getarstorage(localStorage.getItem("marathon"));
}
function sethighscore(el,el2) {
var tt = el.slice();
tt = tt.concat(el2);
sortedallscore = tt.slice();
thisdouble = false;
sortedallscore.sort(function(a, b){return a-b});
var equalsortedallscore = sortedallscore.slice();
equalsortedallscore.sort(function(a, b){
if(b-a == 0){
thisdouble = a;
}
});
if(thisdouble)
sortedallscore.splice(sortedallscore.indexOf(thisdouble),1);
if(sortedallscore.length > 4 )
sortedallscore.pop();
for(i=0;i 4 )
sortedallscore.pop();
for(i=0;i=9)
StopTimer();
setTimeout(function(){
ShowTime();
}, 500);
}
function gameover() {
clickable = false;
StopTimer();
mSolvedEnd();
}
function randPrev() {
var mixit2= [];
var worksolution2 = solution.slice();
for(i=0;i<25;i++){
var randsel2 = Math.floor(Math.random() * (worksolution2.length) );
mixit2 = mixit2.concat(worksolution2[randsel2]);
worksolution2.splice(randsel2,1);
}
$(".kachel").each(function(indez) {
$(this).removeClass("color1 color2 color3 color4 color5 right top first second");
$(this).html(mixit2[indez]);
$(this).addClass("color"+mixit2[indez]);
});
}
function randFinal() {
mixit= [];
function randomize() {
mixit= [];
var worksolution = solution.slice();
var nn = xn = zn =0;
var no = true;
var po = true;
var rr = 0;
var pp = 0;
var check5 = [];
while (nn < 25 && no) {
var randsel = Math.floor(Math.random() * (worksolution.length) );
mixit = mixit.concat(worksolution[randsel]);
check5 = check5.concat(worksolution[randsel]);
worksolution.splice(randsel,1);
if(check5.length==5){
for(x=1;x<6;x++){
if(check5.indexOf(x) !== -1)
rr++;
}
if(rr == 5){
no = false;
rr=0;
check5=[];
randFinal();
}
rr=0;
check5=[];
}
nn++;
}
while (xn <5 && po) {
rx =0;
check5v =[]
for(x=0;x<21;x+=5){
check5v = check5v.concat(mixit[x+xn]);
}
for(xf=1;xf<6;xf++){
if(check5v.indexOf(xf) !== -1)
rx++;
}
if(rx == 5){
po = false;
check5v =[];
rx=0;
randFinal();
}
check5v =[];
xn++;
}
}
randomize()
for(z=0;z<5;z++){
for(i=0;i<5;i++){
$(".kachel").eq(i+z *5).css("left",90 *(i)).css("top",90 * z);
$(".kachel").eq(i+z *5).data("indez",i+z*5);
}
}
$(".kachel").each(function(indez) {
$(this).removeClass("color1 color2 color3 color4 color5 right top first second");
$(this).html(mixit[indez]);
$(this).addClass("color"+mixit[indez]);
$(this).data("color",mixit[indez]);
});
}
randPrev();
introanix = setInterval(function(){
victoryani("introanicube");
}, 900);
$("#start").click(function() {
$("#overlay").hide();
$("#menu").hide();
//startani(true)
});
function startani() {
if(animod){
points=0;
$("#points").text(0);
}
var scount=0;
randomstart();
blinkin = setInterval(function(){
randPrev();
scount++;
if(scount == 8){
clearInterval(blinkin);
start();
}
}, 140);
}
function start() {
randFinal();
actions = 0;
$("#actions").text(0);
clickable = true;
if(animod){
setTimeout(function(){
StartTimer();
if(modus == "marathon")
$("#timer, #score").css("visibility","visible");
else
$("#timer,#actions").css("visibility","visible");
}, 800);
}
}
var check = false;
function selectdata(el) {
for(x=0;x<25;x++){
if($(".kachel").eq(x).data("indez") == el)
return x;
}
}
function allow(thisel,allowed) {
var naw = [];
var put;
if(level == "hard"){
if( Math.floor((thisel - 10) / 5) == Math.floor((thisel - 10 - 1) / 5) ){
naw=naw.concat(thisel - 10 - 1);
}
if( Math.floor((thisel - 10) / 5) == Math.floor((thisel - 10 + 1) / 5) ){
naw=naw.concat(thisel - 10 + 1);
}
if( Math.floor((thisel + 10) / 5) == Math.floor((thisel + 10 - 1) / 5) ){
naw=naw.concat(thisel + 10 - 1);
}
if( Math.floor((thisel + 10) / 5) == Math.floor((thisel + 10 + 1) / 5) ){
naw=naw.concat(thisel + 10 + 1);
}
if( Math.floor(thisel/5) == Math.floor((thisel - 2)/5)){
naw=naw.concat(thisel - 2 - 5);
naw=naw.concat(thisel - 2 + 5);
}
if( Math.floor(thisel/5) == Math.floor((thisel + 2)/5)){
naw=naw.concat(thisel + 2 - 5);
naw=naw.concat(thisel + 2 + 5);
}
}
if(level == "easy"){
if( Math.floor((thisel) / 5) == Math.floor((thisel - 1) / 5) )
naw=naw.concat(thisel - 1);
if( Math.floor((thisel) / 5) == Math.floor((thisel + 1) / 5) )
naw=naw.concat(thisel + 1);
naw=naw.concat(thisel - 5);
naw=naw.concat(thisel + 5);
}
if(level == "medium"){
wc = 4 - (thisel - Math.floor((thisel) / 5) * 5);
var nn = 0;
while (nn < wc ) {
naw=naw.concat(thisel - 4* (nn +1));
nn++;
}
wc = (thisel - Math.floor((thisel) / 5) * 5);
var nn = 0;
while (nn < wc ) {
naw=naw.concat(thisel + 4* (nn +1));
nn++;
}
wc = (thisel - Math.floor((thisel) / 5) * 5);
var nn = 0;
while (nn < wc ) {
naw=naw.concat(thisel - 6* (nn +1));
nn++;
}
wc = 4 -(thisel - Math.floor((thisel) / 5) * 5);
var nn = 0;
while (nn < wc ) {
naw=naw.concat(thisel + 6* (nn +1));
nn++;
}
}
n = naw.indexOf(allowed);
if(n != -1)
put = true;
else
put = false;
return put;
}
function checkvertical() {
for(pp=0;pp<5;pp++){
var c = 0;
var char = [0,1,2,3,4];
for(i=0+pp;i<20+1 +pp;i=5+i){
for(z=0;z<5;z++){
if($(".kachel").eq(selectdata(i)).html()-1 == char[z]){
c++;
char.splice(z,1);
}
}
}
if(c==5) {
for(i=0+pp;i<20+1 +pp;i=5+i)
$(".kachel").eq(selectdata(i)).addClass("right");
}
else {
for(i=0+pp;i<20+1 +pp;i=5+i)
$(".kachel").eq(selectdata(i)).removeClass("right");
}
}
}
function checkhorizontal() {
$(".kachel").each(function(indez) {
$(this).data("temp",false);
});
for(pp=0;pp<26;pp+=5){
var c = 0;
var char = [0,1,2,3,4]
for(i=0+pp;i<5 + pp;i++){
for(z=0;z<5;z++){
if($(".kachel").eq(selectdata(i)).html()-1 == char[z]){
c++;
char.splice(z,1);
}
}
}
if(c==5) {
for(i=0+pp;i<5 +pp;i++){
$(".kachel").eq(selectdata(i)).addClass("top");
}
}
else {
for(i=0+pp;i<5 +pp;i++){
$(".kachel").eq(selectdata(i)).removeClass("top");
}
}
}
}
xy = 0;
$("body").on( "animationend",".second", function(event) {
$(".first").removeClass("scale");
$(".second").removeClass("scale");
if(!$(".second").hasClass("rightpos"))
actions++;
$("#actions").text(actions);
checkvertical();
checkhorizontal();
clickable = true;
$(".top").each(function() {
if( Math.floor($(this).data("indez")/5) == Math.floor($(".second").data("indez")/5) || Math.floor($(this).data("indez")/5) == Math.floor($(".first").data("indez")/5))
$(this).addClass("rightpos");
});
function checkvertshow(el) {
while (el > 4 ) {
el = el -5;
}
return el;
}
$(".right").each(function() {
if( checkvertshow($(this).data("indez")) == checkvertshow($(".second").data("indez"))|| checkvertshow($(this).data("indez")) == checkvertshow($(".first").data("indez")))
$(this).addClass("rightpos");
});
cc =0;
restart = false;
if(modus == "marathon"){
if($(".top").length == 25){
restart = true;
clickable = false;
$(".kachel").addClass("rightpos");
$(".kachel").removeClass("first second");
}
}
else{
if($(".top").length + $(".right").length== 50){
advVictory();
}
}
});
$("body").on( "animationend",".rightpos:last-child", function(event) {
if(restart){
if(sSec > 1 || sSec == 0 && sMin > 0 || sSec == 1 && sMin > 0) {
mSolved();
animod = false;
startani();
}
restart = false;
}
});
function mSolved(){
$("#bonus").show();
$("#bonus").text("");
function fadebonus(){
$("#bonus").fadeOut();
}
bonus = 0;
yesbonus = false;
if($(".right").length !=0){
bonus =$(".right").length / 5 * 100;
yesbonus = true;
}
ci=0;
scoreani = setInterval(function(){
points = points+10 + bonus/10;
if(yesbonus){
$("#points").text(points);
$("#bonus").text("+ bonus:" + bonus);
}
else{
$("#points").text(points);
}
ci++;
if(ci == 10){
clearInterval(scoreani);
if(yesbonus)
setTimeout(fadebonus, 1000);
}
}, 70);
}
function mSolvedEnd(){
$("#overlay").fadeIn();
points = points + $(".right").length / 5 * 100 / 5 + $(".top").length / 5 * 100 / 5;
setMtScore();
if(level == "easy")
$("#marathon-best").text("Best: "+marathonar[0][0]);
if(level == "medium")
$("#marathon-best").text("Best: "+marathonar[1][0]);
if(level == "hard")
$("#marathon-best").text("Best: "+marathonar[2][0]);
pointsnull = 0;
$("#points").text(0);
var ci=0;
scoreaniend = setInterval(function(){
pointsnull = pointsnull + points/10;
$("#points").text(pointsnull);
$("#finalpoints").text(pointsnull);
ci++;
if(ci == 10){
clearInterval(scoreaniend);
}
}, 70)
}
var firstposleft;
var firstpostop;
var secondposleft;
var secondpostop;
$(".kachel").click(function() {
if(clickable) {
animationend = true;
if($(".first").length + $(".second").length == 2){
$(".kachel").removeClass("first second");
}
if($(".first").length == 0){
$(this).addClass("first");
}
else {
firstdata = $(".first").data("indez");
seconddata = $(this).data("indez");
if(allow(firstdata,seconddata)){
clickable = false;
$(this).addClass("second");
firstposleft = $(".first").position().left;
firstpostop = $(".first").position().top;
secondposleft = $(".second").position().left;
secondpostop = $(".second").position().top;
$(".first").css("left",secondposleft).css("top",secondpostop).data("indez",seconddata).addClass("scale");
$(".second").css("left",firstposleft).css("top",firstpostop).data("indez",firstdata).addClass("scale");
}
else {
$(".first").removeClass("first");
$(this).addClass("first");
}
}
$(".kachel").removeClass("rightpos");
}
});
$("body").on( "animationend",".introani", function(event) {
$("#startintro").hide();
$("#startintro").removeClass("introani");
$(".kachel").removeClass("introanicube");
animod=true;
startani();
});
function startintro() {
$("#startintro").show();
$("#startintro").addClass("introani");
}
$("#modus1").click(function() {
modus = "marathon";
$("#menu").hide();
$("#move1").fadeIn();
});
$("#modus2").click(function() {
modus = "advanced";
$("#score").css("visibility","hidden");
$("#menu").hide();
$("#move2").fadeIn();
});
$("#move1 .movement").click(function() {
clearInterval(introanix);
$("#move1").fadeOut();
if($(this).index() == 1){
level = "easy";
startintro();
}
if($(this).index() == 2){
level = "medium";
startintro();
}
if($(this).index() == 3){
level = "hard";
startintro();
}
});
$("#move2 .movement").click(function() {
clearInterval(introanix);
$("#move2").fadeOut();
if($(this).index() == 1){
level = "hard";
startintro();
}
if($(this).index() == 2){
level = "hard";
startintro();
}
});
shufflec = 0;
function victoryani(el) {
if(shufflec == 0){
shufflec = 1;
}
else {
shufflec =0;
}
for(i=0;i<25;i++){
if(Math.random()>0.5 && !$(".kachel").eq(i).hasClass(el)&& $(".kachel").eq(i).data("timestamp") != shufflec){
$(".kachel").eq(i).addClass(el);
$(".kachel").eq(i).data("timestamp",shufflec);
}
else {
if($(".kachel").eq(i).data("timestamp") == shufflec){
$(".kachel").eq(i).removeClass(el);
$(".kachel").eq(i).data("timestamp",shufflec);
}
}
}
}
function advVictory() {
StopTimer();
clearInterval(introanix);
$(".kachel").removeClass("top right rightpos introanicube first second");
$("#adv-victory").fadeIn();
$("#adv-time").text(time);
$("#adv-actions").text("Actions: " +actions);
setAdvScore();
var tempx = timear[0].substr(1);
tempx = tempx[0]+":"+tempx[1]+tempx[2]+":"+tempx[3]+tempx[4];
$("#adv-bestwrap").text("Best: " + tempx +" | "+ actionar[0]);
finalscoreani = setInterval(function(){
victoryani("victory");
}, 100);
}
$("#minfo,#ainfo").click(function() {
$("#moveinfo").show();
});
$("#closer").click(function() {
$("#moveinfo").hide();
});
function blinkcur(selector,current,array) {
if(array.indexOf(current) != -1)
$(selector).eq(array.indexOf(current)).addClass("hs-blink");
}
$("#adv-highscore,#marathon-highscore").click(function() {
$("#allhighsore").fadeIn();
$("#current-result").addClass("hs-blink");
if(modus == "advanced"){
$("#current-result").text(time + " | Actions: " + actions);
blinkcur("#hs-a-actions li",actions,actionar);
blinkcur("#hs-a-time li",hightime,timear);
}
else{
$("#current-result").text(points);
if(level == "easy")
blinkcur("#hs-m-points-a li",points,marathonar[0]);
if(level == "medium")
blinkcur("#hs-m-points-b li",points,marathonar[1]);
if(level == "hard")
blinkcur("#hs-m-points-c li",points,marathonar[2]);
}
});
$("#menulink").click(function() {
$("#timer, #score,#actions").css("visibility","hidden");
StopTimer();
$("#overlay,#adv-victory,#move1,#move2,#allhighsore,#moveinfo").hide();
$("#allhighsore li,#current-result").removeClass("hs-blink");
$("#menu").fadeIn();
clearInterval(blinkin);
clearInterval(introanix);
clearInterval(finalscoreani);
$(".kachel").removeClass("top right rightpos introanicube first second victory");
introanix = setInterval(function(){
victoryani("introanicube");
}, 900);
startanisrunning = true;
vicisrunning = false;
});
});