
var memoryGame;var debug=false;function resetLog(){$("#js-console").text('');}
function log(text){if(!debug){return;}
if($("#js-console").text()==''){$("#js-console").html('<input type="button" onclick="javascript:resetLog();" value="clear"/>');}
$("#js-console").append(text+'<br />');}
function flip(element){var card=memoryGame.getCard($(element).attr("id"));if(card.isFound||memoryGame.locked){return;}
if(!card.isFlipped){$(element).rotate3Di(180,500);$(element).attr("src",card.imageUrl);memoryGame.revealed(card);}
card.isFlipped=!card.isFlipped;}
function cardsEqual(carda,cardb){return(carda.cardId.substring(1)==cardb.cardId.substring(1));}
var handleCallback=function(result){log('handleCallback');var dataItems=result.getModelImages();for(var i=0;i<dataItems.length;i++){log('add item to game '+dataItems[i])
log('item i0 '+dataItems[i][0]);log('item i1 '+dataItems[i][1]);log('item i2 '+dataItems[i][2]);memoryGame.addCard(dataItems[i][0],'/tulipa/media/'+dataItems[i][1],dataItems[i][2]);}
showStartButtons();}
function startGame(){$('#board').html('');$('#status').html('<input type="Button" onClick="window.location.reload()" value="Sexy Memory neustarten!"/><br />Punkte: <span id="score">0</span><br />Zeit: <span id="timer">00:00</span><br />Aufgedeckte Models: ');$('#score').html('');$('#timer').html('');memoryGame.createBoard();memoryGame.reset();memoryGame.start();}
function showElements(){$('#board').html('<img src="'+contextPath+'memory/sexyMemory.jpg" onClick="javascript:startGame();" alt="Sexy Memory starten!"/>');}
function startTimer(){if(memoryGame.isEnded){return;}
var running=Math.floor(((new Date())-memoryGame.startTime)/1000);var min=Math.floor(running/60);var sec=Math.floor(running%60);if(sec<10){sec='0'+sec;}
if(min<10){min='0'+min;}
$('#timer').html(min+':'+sec);setTimeout("startTimer()",1000);}
$(function(){memoryGame=new MemoryGame();log('try get the data')
var dataItems=modelData;for(var i=0;i<dataItems.length;i++){log('add item to game '+dataItems[i])
log('item i0 '+dataItems[i][0]);log('item i1 '+dataItems[i][1]);log('item i2 '+dataItems[i][2]);memoryGame.addCard(dataItems[i][0],dataItems[i][1],dataItems[i][2]);}
showElements();});function Card(cardId,cardImage,urlName){this.cardId=jQuery.trim(cardId);this.backImage=contextPath+'memory/memory-karte.png';this.imageUrl=cardImage+'small.jpg';this.urlName=urlName;this.isFlipped=false;this.isFound=false;this.reset=function(){var somevar=$('#'+this.cardId);if(this.isFound){somevar.attr("src",this.imageUrl);}else{somevar.rotate3Di(0,500);somevar.attr("src",card.backImage);}
this.isFlipped=this.isFound;return(!this.isFound);}}
function MemoryGame(){this.cards=new Array();this.flip1;this.locked;this.score;this.startTime;this.isEnded=false;this.start=function(){this.score=0;this.startTime=new Date();$('#score').html(this.score);startTimer();}
this.reset=function(){log('-- reset all cards --');this.flip1=undefined;this.locked=false;var count=0;$('#score').html(this.score);for(var i=0;i<this.cards.length;i++){count+=this.cards[i].reset();}
log('remaining cards: '+count);if(count<=0){this.endGame();}}
this.endGame=function(){this.isEnded=true;var board=$('#board').html();if(window.uid){$('#board').html('<div style="background-color: light-gray; border: 2px solid dark_gray">GEWONNEN!!!<form method="GET" action="http://test.Bordell-Tour.de/highscoreLogger" name="formular"><input type="hidden" name="UID" value='+uid+'><input type="hidden" name="score" value='+$('#timer').html()+'><table border="0"><tbody><tr>    <td>Name:</td>    <td><input name="name"></td>  </tr>  <tr>    <td>E-Mail:</td>    <td><input name="email"></td>  </tr>  <tr></tbody>       </table>       <input name="btnSenden" value="Weiter" type="submit"><br>      </form></div>'+board);}else{alert('Du gewinnst nach '+$('#timer').html()+' mit '+this.score+' Punkten');}}
this.addCard=function(cardId,cardImage,urlName){card=new Card('a'+cardId,cardImage,urlName);this.cards.unshift(card);card=new Card('b'+cardId,cardImage,urlName);this.cards.push(card);}
this.createBoard=function(){this.cards.sort(function(){return 0.5-Math.random();})
this.cards.sort(function(){return 0.5-Math.random();})
this.cards.sort(function(){return 0.5-Math.random();})
this.cards.sort(function(){return 0.5-Math.random();})
this.cards.sort(function(){return 0.5-Math.random();})
this.cards.sort(function(){return 0.5-Math.random();})
this.cards.sort(function(){return 0.5-Math.random();})
this.cards.sort(function(){return 0.5-Math.random();})
for(var i=0;i<this.cards.length;i++){this.cards[i].isFlipped=false;this.cards[i].isFound=false;$("#board").append('<div class="card"><img onClick="javascript:flip(this);" id="'
+this.cards[i].cardId
+'" src="'
+this.cards[i].imageUrl
+'"></div>');}}
this.getCard=function(cardId){for(i=0;i<this.cards.length;i++){if(this.cards[i].cardId==cardId){return this.cards[i];}}}
this.revealed=function(card){log('revealed '+card.cardId);if(!this.flip1){log('is first flip');this.flip1=card;}else if(!cardsEqual(this.flip1,card)){log('card not equal');this.locked=true;this.score--;setTimeout('memoryGame.reset();',1500);}else if(cardsEqual(this.flip1,card)){log('card are equal');this.flip1.isFound=true;this.score+=3;card.isFound=true;$('#status').append('<br /><br /><a href="http://www.bordell-tour.de/modelcard/'+card.urlName+'" target="_blank" >'+card.urlName+'<br /><img src="'+card.imageUrl+'"/></a>');this.reset();}}}