var wordsArray = [];
var answerArray = [];
var answerSubmit;
var endTime;
var timeTaken;
var status;
var level = 0;
var words;
//$(document).ready(function(){
// $('body').data('startTime',new Date());
// startTime = new Date();
// status = "started";
// $.getJSON('../report.php',{user:wgUserName,page:wgPageName,what:status,start:startTime});
//
//});
$(document).ready(function ()
{
try
{
words = $.parseJSON($('#words').text());
}
catch (e)
{
alert(e);
}
setUpArrays();
display(answer, answerArray, matchArray, notMatchArray);
});
function setUpArrays()
{
matchArray = words.stage[level].data.match;
notMatchArray = words.stage[level].data.notMatch;
answer = words.stage[level].anagram;
for (var i = 0; i < answer.length; i++)
{
answerArray.push(answer.charAt(i));
}
for (var i = 0; i < matchArray.length; i++)
{
$("#match").append("
" + matchArray[i] + "\t\t ?
");
}
for (var i = 0; i < notMatchArray.length; i++)
{
$("#notmatch").append("" + notMatchArray[i] + "\t\t ?
");
}
}
function display(answer, answerArray, matchArray, notMatchArray)
{
$("#answer").append("");
for (var i = 0; i < answer.length; i++)
{
$('' + answerArray[i] + '
')
.data('answerArray', answerArray[i])
.attr('id', answerArray[i])
.attr('value', answerArray[i])
.appendTo('.rearrange')
.draggable(
{
containment: '#content',
stack: '.rearrange div',
cursor: 'move',
helper: 'clone',
connectToSortable: '#answerBox',
snap: true,
snapTolerance: 'fit'
});
}
$("#answerBox").droppable().sortable(
{
tolerance: 'pointer',
connectWith: ".droppable , .sortable",
receive: function (event, ui)
{
ui.item.toggle();
},
update: function (event, ui)
{
status = 'attempted';
answerSubmit = $("#answerBox").children(".anagram").text();
// $.getJSON('../report.php',{user:wgUserName,page:wgPageName,what:status,start:$('body').data('startTime'),
//params:answerSubmit, duration:((new Date()).getTime() - startTime.getTime())/1000});
$("#answerSubmit").text(answerSubmit);
if ($("#pageTitle").text() === "How To Play")
{
answerSubmit = '.'+answerSubmit+'.';
}
for (var i = 0; i < matchArray.length; i++)
{
checkMatchPosition(matchArray[i], notMatchArray[i], answerSubmit, i);
}
}
});
$("#deleteBox").droppable(
{
accept: '#answerBox > div',
drop: function (event, ui)
{
var deletedLetter = (ui.draggable).attr('value');
deletedLetter = deletedLetter.replace(/(\||\(|\)|:|\.|\[|\]|\?|\*|\+|\^)/g, "\\$1");
$(ui.draggable).remove();
$("#" + deletedLetter).toggle();
answerSubmit = $("#answerBox").children(".anagram").text();
for (var i = 0; i < matchArray.length; i++)
{
checkMatchPosition(matchArray[i], notMatchArray[i], answerSubmit, i);
}
}
});
}
function checkMatchPosition(matchWord, notMatchWord, answerSubmit, line)
{
var result = matchWord.replace(re, function (str)
{
return '' + str + '';
});
$("#matchWord" + line + "").html(result);
$("#matchResult" + line + "").html("?").css('color', 'black');
var result = notMatchWord.replace(re, function (str)
{
return '' + str + '';
});
$("#notMatchWord" + line + "").html(result);
$("#notMatchResult" + line + "").html("?").css('color', 'black');
var re = new RegExp('voldemort');
try
{
re = new RegExp(answerSubmit);
} catch (e)
{
}
var reMatch = re.exec(matchWord);
var reNotMatch = re.exec(notMatchWord);
var matchPattern = matchWord.search(re); //search if there is a match
var notMatchPattern = notMatchWord.search(re);
var matchWord = $("#matchWord" + line + "").attr('value');
//how to play page
// if ($("#pageTitle").text() === "How To Play")
// {
// if (matchWord.match(re))
// {
// $("#theMessage").toggle();
// }
// }
//match array
if (matchPattern !== -1) //-1 if pattern not found
{
var result = matchWord.replace(re, function (str)
{
return '' + str + '';
});
$("#matchWord" + line + "").html(result);
$("#matchResult" + line + "").html("☹ ").css('color', '#FF0000');
if (reMatch[0] === matchWord)
{
$("#matchResult" + line + "").html("☺ ").css('color', '#00FF00');
}
else
{
}
}
//not match array
if (notMatchPattern !== -1)
{
var result = notMatchWord.replace(re, function (str)
{
return '' + str + '';
});
$("#notMatchWord" + line + "").html(result);
$("#notMatchResult" + line + "").html("✘ ").css('color', '#FF0000'); //red cross
}
else
{
$("#notMatchResult" + line + "").html("✔ ").css('color', '#00FF00'); //green tick
}
if ($("#match span:contains('☺')").length === matchArray.length && $("#notmatch span:contains('✔')").length === notMatchArray.length)
{
// endTime = new Date();
// status = "success";
// timeTaken = (endTime.getTime() - startTime.getTime())/1000; //duration in seconds
//
// $.getJSON('../report.php',{user:wgUserName,page:wgPageName,what:status,start:$('body').data('startTime'),
//end:endTime, duration:timeTaken});
$("#theMessage").toggle();
}
}