var okKeys='ABCDEFGHIJKLMNOPQRSTUVWXYZ ';var typedCrossword=new Array();var currentRow=-1;var currentColumn=-1;var axis=-1;var unHighlight=new Array();var lastHighlightedClue='';var crosswordSolvedHandler=null;var showSolvedMessage=true;function isBlankCell(row,column){var t=crossword[row][column]=="#";if(row<0||column<0||row>=numRows||column>=numColumns){return true}else{return crossword[row][column]=="#"}}function isEmptyCell(row,column){var cellId="#ct"+row+"x"+column;return $(cellId).text().charCodeAt(0)==160}function doUnhighlight(){for(var i=0;i<unHighlight.length;i++){setState(unHighlight[i][0],unHighlight[i][1],0)}}function canChangeAxis(row,column){if(axis==1&&((row>0&&!isBlankCell(row-1,column))||(row+1<numRows&&!isBlankCell(row+1,column)))){return true}else if(axis==-1&&((column>0&&!isBlankCell(row,column-1))||(column+1<numColumns&&!isBlankCell(row,column+1)))){return true}return false}function setState(row,column,state){if(state==0){SetBackground("c"+row+"x"+column,"#FFFFFF")}else{if(state==1){SetBackground("c"+row+"x"+column,"#BEE0FA")}else if(state==2){SetBackground("c"+row+"x"+column,"#7AC4FA");currentRow=row;currentColumn=column}else if(state==3){SetBackground("c"+row+"x"+column,"#FFC1C1")}else if(state==4){SetBackground("c"+row+"x"+column,"#CAF7CF")}unHighlight[unHighlight.length]=new Array(row,column)}}function highlightCurrentSelection(){setState(currentRow,currentColumn,2);if(axis==1){firstCellCol=0;if(currentColumn==0){highlightClue(currentRow,0,'a')}for(var i=currentColumn-1;i>=0;i--){if(isBlankCell(currentRow,i)){highlightClue(currentRow,i+1,'a');firstCellCol=i+1;break}if(i==0){highlightClue(currentRow,0,'a')}setState(currentRow,i,1)}if(isEmptyCell(currentRow,firstCellCol)){setState(currentRow,currentColumn,1);setState(currentRow,firstCellCol,2)}for(var j=currentColumn+1;j<numColumns;j++){if(isBlankCell(currentRow,j)){break}setState(currentRow,j,1)}}else{firstCellRow=0;if(currentRow==0){highlightClue(0,currentColumn,'d')}for(var i=currentRow-1;i>=0;i--){if(isBlankCell(i,currentColumn)){highlightClue(i+1,currentColumn,'d');firstCellRow=i+1;break}if(i==0){highlightClue(0,currentColumn,'d')}setState(i,currentColumn,1)}if(isEmptyCell(firstCellRow,currentColumn)){setState(currentRow,currentColumn,1);setState(firstCellRow,currentColumn,2)}for(var j=currentRow+1;j<numRows;j++){if(isBlankCell(j,currentColumn)){break}setState(j,currentColumn,1)}}}function highlightClue(i,j,pos){if(lastHighlightedClue!=''){SetBackground(lastHighlightedClue,'#FFFFFF')}lastHighlightedClue='clue'+i+'x'+j+pos;SetBackground(lastHighlightedClue,'#D7F1FF')}function selectCell(row,column,direction){if(direction!=undefined&&direction!=0){axis=direction}else{if(canChangeAxis(row,column)){axis=-axis}}currentRow=row;currentColumn=column;doUnhighlight();highlightCurrentSelection()}function getText(row,column){return GetElement("ct"+row+"x"+column).innerHTML}function setText(row,column,text){if(text==" "){text="&nbsp;"}SetInnerHTML("ct"+row+"x"+column,text);checkSolution()}function cursorBack(){if(axis==1&&currentColumn>0&&!isBlankCell(currentRow,currentColumn-1)){setState(currentRow,currentColumn,1);currentColumn--;setState(currentRow,currentColumn,2)}else if(axis==-1&&currentRow>0&&!isBlankCell(currentRow-1,currentColumn)){setState(currentRow,currentColumn,1);currentRow--;setState(currentRow,currentColumn,2)}}function cursorForward(){if(axis==1&&currentColumn<numColumns-1&&!isBlankCell(currentRow,currentColumn+1)){setState(currentRow,currentColumn,1);currentColumn++;setState(currentRow,currentColumn,2)}else if(axis==-1&&currentRow<numRows-1&&!isBlankCell(currentRow+1,currentColumn)){setState(currentRow,currentColumn,1);currentRow++;setState(currentRow,currentColumn,2)}}function columnBack(){}function columnForward(){}function showHint(){if(currentRow==-1||currentColumn==-1){alert(_t("Opcja Podpowiedź pozwala na wyświetlenie litery, która powinna być\nwpisana w aktualnie zaznaczonym polu krzyżówki.")+"\n\n"+_t("Zaznacz pole, dla którego chcesz uzyskać podpowiedź."))}else{setText(currentRow,currentColumn,crossword[currentRow][currentColumn]);var i=currentRow,j=currentColumn;cursorForward();setState(i,j,4)}}function checkForMistakes(){doUnhighlight();for(var i=0;i<numRows;i++){for(var j=0;j<numColumns;j++){if(!isBlankCell(i,j)){if(getText(i,j)!=crossword[i][j]){setText(i,j," ");setState(i,j,3)}}}}checkSolution()}function checkSolution(){if(!showSolvedMessage){return}var valid=true;for(var i=0;i<numRows;i++){for(var j=0;j<numColumns;j++){if(!isBlankCell(i,j)){if(getText(i,j)!=crossword[i][j]){valid=false;break}}}}if(valid){SoundApi.playRecording('/images/sounds/winner.mp3');$.get('/cwiczenia/punkty.php?gra=krzyzowki&zmianapkt=1&reqid='+RandomString());if(crosswordSolvedHandler!=null){crosswordSolvedHandler()}alert('Gratulacje! Prawidłowo rozwiązałeś krzyżówkę.');showSolvedMessage=false}}function showSolution(){showSolvedMessage=false;doUnhighlight();for(var i=0;i<numRows;i++){for(var j=0;j<numColumns;j++){if(!isBlankCell(i,j)){setText(i,j,crossword[i][j])}}}}function setCrosswordSolvedHandler(handler){crosswordSolvedHandler=handler}function handlekeydown(keyCode){if(currentRow==-1||currentColumn==-1){return true}switch(keyCode){case Keys.BACKSPACE:cursorBack();setText(currentRow,currentColumn," ");return false;case Keys.DELETE:setText(currentRow,currentColumn," ");return false;case Keys.LEFT:if(axis==1){cursorBack()}else if(!isBlankCell(currentRow,currentColumn-1)){selectCell(currentRow,currentColumn-1)}else{columnBack()}return false;case Keys.UP:if(axis==-1){cursorBack()}else if(!isBlankCell(currentRow-1,currentColumn)){selectCell(currentRow-1,currentColumn)}else{columnBack()}return false;case Keys.RIGHT:if(axis==1){cursorForward()}else if(!isBlankCell(currentRow,currentColumn+1)){selectCell(currentRow,currentColumn+1)}else{columnForward()}return false;case Keys.DOWN:if(axis==-1){cursorForward()}else if(!isBlankCell(currentRow+1,currentColumn)){selectCell(currentRow+1,currentColumn)}else{columnForward()}return false;default:var keychar=String.fromCharCode(keyCode);if(okKeys.indexOf(keychar)>=0){setText(currentRow,currentColumn,keychar);cursorForward();return false}return true}}KeyboardApiInstance.startCapture();KeyboardApiInstance.addKeyDownListener(handlekeydown);

