Ext.namespace("Zarafa.widgets.shellgame");Zarafa.widgets.shellgame.ShellGameWidget=Ext.extend(Zarafa.core.ui.widget.Widget,{constructor:function(a){a=a||{};Ext.applyIf(a,{name:"shellgamewidget",title:_("Shell Game"),height:350,items:[{xtype:"zarafa.widget.shellgamepanel"}]});Zarafa.widgets.shellgame.ShellGameWidget.superclass.constructor.call(this,a)}});Zarafa.onReady(function(){container.registerWidget(Zarafa.widgets.shellgame.ShellGameWidget,"shellgame",_("Shell Game"),"plugins/shellgame/resources/images/shellgame.png")});
Ext.namespace("Zarafa.widgets.shellgame");Zarafa.widgets.shellgame.ShellGameStates={IDLE:1,SHUFFLE:2,GUESS:3,SHOW:4};Ext.namespace("Zarafa.widgets.shellgame");
Zarafa.widgets.shellgame.ShellGamePlayFieldPanel=Ext.extend(Ext.Container,{moves:20,moveDuration:0.3,moveCupToFrontDuration:0.05,numCups:3,cupContainingBallIndex:1,positions:[],cups:null,ball:null,liftCupDuration:0.75,leftOffset:25,topOffset:50,cupWidth:50,cupMargin:25,shuffleMoveOffset:10,state:null,switches:[],topLeftContainerOffset:null,winSentences:[],loseSentences:[],pickYourCupSentence:_("Select the cup with the ball..."),shufflingSentence:_("Watch the cups..."),cupZIndex:3,cupShuffleZIndexBack:2,
cupShuffleZIndexFront:4,constructor:function(a){a=a||{};Ext.applyIf(a,{height:175,autoEl:{tag:"div",cls:"x-widget-shellgame-playfield"}});this.addEvents("statechange");Zarafa.widgets.shellgame.ShellGamePlayFieldPanel.superclass.constructor.call(this,a)},initComponent:function(){this.cupTemplate='<div class="x-widget-shellgame-cup"><div class="x-widget-shellgame-cup-logo"></div></div>';this.winSentences=[_("Winner, winner, chicken dinner!"),_("You are awesome!"),_("You are a fantastic human specimen!"),
_("Another unbelievable win!"),_("Are you cheating?"),_("I just let you win this one."),_("Well done!"),_("Are you going for a record?"),_("Have you played this game often?"),_("I think you need to get out more."),_("There is something like too much practice."),_("This just in: WOW!"),_("Amazing!"),_("What evil witchcraft is this?"),_("Maybe we should go faster."),_("That took strength, perseverence and a whole lot of brains."),_("Good going!"),_("Hard work pays off."),_("You have a bright career ahead of you."),
_("Buddha would be proud."),_("Kudos!"),_("This is a true example of how even you can accomplish something."),_("I bow before your greatness."),_("I always new you that you had it in you to be a true winner."),_("Oh sorry, I was not paying attention. What happened?"),_("Was that a guess...?"),_("Maybe this was too easy. Try increasing the difficulty."),_("Well on your way to becoming a pro!")];this.loseSentences=[_("That was bad!"),_("You had no idea, did you?"),_("That is probably not good for you ego"),
_("Could you be anymore wrong?"),_("Maybe you should be doing this with transparent cups?"),_("I am not going to be bothered to comment about this one."),_("That was pathetic."),_("Even our testing monkeys got more guesses right than you."),_("Maybe banging your head into the wall will help?"),_("Try to stop sniffing glue."),_("You probably want someone else filling in your tax returns."),_("You have got to be kidding me."),_("Really? That one?"),_("WRONG!"),_("Maybe you are more of a Sudoku-person?"),
_("Did I go too fast for you?"),_("Did the light get in your eye?"),_('Are you suffering from a "sports injury"?'),_("Guessing does not always work."),_("Epic fail!"),_("That was embarrassing!"),_("Hope you did not waste too much blood, sweat and tears for this."),_("You better not tell anyone about this embarressment."),_("BURN!"),_("It is good that you did not play for money."),_("Damn, why did I not ask for money?"),_('Was this another "practice" round?'),_("I did not know anyone could sink this low."),
_("LOSER!"),_("OOOOO, so close! Not really."),_("I pity the fool!")];Zarafa.widgets.shellgame.ShellGamePlayFieldPanel.superclass.initComponent.apply(this,arguments)},onRender:function(){Zarafa.widgets.shellgame.ShellGamePlayFieldPanel.superclass.onRender.apply(this,arguments);this.drawGame()},drawGame:function(){for(var a="",b=0;b<this.numCups;b++)a+=this.cupTemplate;this.masterTpl=new Ext.XTemplate(a,'<div class="x-widget-shellgame-ball"></div>',{disableFormats:true});this.masterTpl.overwrite(this.el);
this.containerElem=Ext.get(this.el.dom);this.cups=[];this.positions=[];this.cupContainingBallIndex=Math.floor(this.numCups/2);for(b=0;b<this.numCups;b++){this.cups[b]=Ext.get(this.containerElem.dom.childNodes[b]);this.positions[b]=[this.leftOffset+(this.cupWidth+this.cupMargin)*b,this.topOffset];this.cups[b].setLeftTop(this.positions[b][0],this.positions[b][1]);this.cups[b].positionIndex=b;this.cups[b].setStyle("z-index",this.cupZIndex);this.mon(this.cups[b],{scope:this,click:this.chooseCup.createDelegate(this,
[b])})}this.ball=Ext.get(this.containerElem.dom.childNodes[this.numCups]);this.ball.setVisible(false);this.changeState(Zarafa.widgets.shellgame.ShellGameStates.IDLE)},calcTopLeftContainerOffset:function(){this.topLeftContainerOffset={x:this.containerElem.getX(),y:this.containerElem.getY()}},setupGame:function(){for(var a=0;a<this.numCups;a++){this.cups[a].setLeftTop(this.positions[a][0],this.positions[a][1]);this.cups[a].positionIndex=a}},startGame:function(){if(this.state==Zarafa.widgets.shellgame.ShellGameStates.IDLE||
this.state==Zarafa.widgets.shellgame.ShellGameStates.GUESS){this.changeState(Zarafa.widgets.shellgame.ShellGameStates.SHOW);this.setupGame();this.showBall(this.shuffle.createDelegate(this))}},setBallToPosition:function(a){var b=this.positions[a][0]+this.cups[this.cupContainingBallIndex].getWidth()/2-this.ball.getWidth()/2;a=this.positions[a][1]+(this.cups[this.cupContainingBallIndex].getHeight()-this.ball.getHeight());this.ball.setLeftTop(b,a)},showBall:function(a){this.calcTopLeftContainerOffset();
this.setBallToPosition(this.cups[this.cupContainingBallIndex].positionIndex);this.ball.setVisible(true);var b=this.topLeftContainerOffset.y+this.positions[this.cups[2].positionIndex][1];this.cups[this.cupContainingBallIndex].shift({y:b-this.ball.getHeight()-10,concurrent:false,duration:this.liftCupDuration});this.cups[this.cupContainingBallIndex].shift({y:b,duration:this.liftCupDuration,concurrent:false,callback:a})},chooseCup:function(a){if(this.state==Zarafa.widgets.shellgame.ShellGameStates.GUESS){a==
this.cupContainingBallIndex?this.ownerCt.instructionLabel.setText(this.winSentences[Math.floor(Math.random()*this.winSentences.length)]):this.ownerCt.instructionLabel.setText(this.loseSentences[Math.floor(Math.random()*this.loseSentences.length)]);this.changeState(Zarafa.widgets.shellgame.ShellGameStates.SHOW);this.showBall(this.onEndGame.createDelegate(this))}},shuffle:function(){this.changeState(Zarafa.widgets.shellgame.ShellGameStates.SHUFFLE);this.calcTopLeftContainerOffset();this.ball.setVisible(false);
var a;this.switches=[];for(var b=0;b<this.moves;b++){for(a=[];a.length<2;){var c=Math.floor(Math.random()*this.cups.length);a.indexOf(c)==-1&&a.push(c)}this.switches.push(a)}this.shuffleStep();this.ownerCt.instructionLabel.setText(this.shufflingSentence)},shuffleStep:function(){if(this.switches.length==0)this.onShuffleEnd();else{var a=this.switches.shift(),b=this.cups[a[0]];a=this.cups[a[1]];var c=this.positions[b.positionIndex],e=this.positions[a.positionIndex];b.setStyle("z-index",this.cupShuffleZIndexBack);
a.setStyle("z-index",this.cupShuffleZIndexFront);b.shift({y:this.topLeftContainerOffset.y+this.topOffset-this.shuffleMoveOffset,concurrent:false,duration:this.moveCupToFrontDuration});a.shift({y:this.topLeftContainerOffset.y+this.topOffset+this.shuffleMoveOffset,concurrent:false,duration:this.moveCupToFrontDuration});b.shift({callback:function(d){d.setStyle("z-index",this.cupZIndex)}.createDelegate(this),x:this.topLeftContainerOffset.x+e[0],concurrent:false,duration:this.moveDuration});a.shift({callback:function(d){d.setStyle("z-index",
this.cupZIndex)}.createDelegate(this),x:this.topLeftContainerOffset.x+c[0],concurrent:false,duration:this.moveDuration});b.shift({y:this.topLeftContainerOffset.y+this.topOffset,concurrent:false,duration:this.moveCupToFrontDuration});a.shift({callback:this.shuffleStep.createDelegate(this),y:this.topLeftContainerOffset.y+this.topOffset,concurrent:false,duration:this.moveCupToFrontDuration});c=b.positionIndex;b.positionIndex=a.positionIndex;a.positionIndex=c}},onShuffleEnd:function(){this.changeState(Zarafa.widgets.shellgame.ShellGameStates.GUESS);
this.ownerCt.instructionLabel.setText(this.pickYourCupSentence)},onEndGame:function(){this.changeState(Zarafa.widgets.shellgame.ShellGameStates.IDLE)},changeState:function(a){this.state=a;this.fireEvent("statechange",this,this.state)}});Ext.reg("zarafa.widget.shellgame.playfieldpanel",Zarafa.widgets.shellgame.ShellGamePlayFieldPanel);Ext.namespace("Zarafa.widgets.shellgame");
Zarafa.widgets.shellgame.ShellGamePanel=Ext.extend(Ext.Container,{instructionLabel:undefined,playfield:undefined,movesSliderfield:undefined,removeCupBtn:undefined,addCupBtn:undefined,constructor:function(a){a=a||{};Ext.applyIf(a,{items:[{height:40,items:[{xtype:"label",text:_("Click shuffle to start the game!"),ref:"../instructionLabel",style:"text-align: center; width: 100%;",handler:function(){this.ownerCt.playfield.startGame()}}]},{xtype:"zarafa.widget.shellgame.playfieldpanel",ref:"playfield"},
{xtype:"button",text:_("Shuffle!"),handler:function(){this.ownerCt.playfield.startGame()}},{xtype:"zarafa.compositefield",hideLabel:true,items:[{xtype:"label",width:50,text:_("Speed:")},{xtype:"sliderfield",width:100,onChange:this.onChangeSpeed.createDelegate(this),listeners:{afterrender:this.onAfterRenderSpeed,scope:this}}]},{xtype:"zarafa.compositefield",hideLabel:true,items:[{xtype:"label",width:50,text:_("Moves:")},{xtype:"sliderfield",width:150,ref:"../movesSliderfield",onChange:this.onChangeMoves.createDelegate(this),
listeners:{afterrender:this.onAfterRenderMoves,scope:this}}]},{xtype:"container",layout:"hbox",align:"stretch",items:[{xtype:"button",text:_("Remove a cup"),ref:"../removeCupBtn",handler:this.onRemoveCup,scope:this},{xtype:"spacer",height:1,width:10},{xtype:"button",text:_("Add a cup"),ref:"../addCupBtn",handler:this.onAddCup,scope:this}]}]});Zarafa.widgets.shellgame.ShellGamePanel.superclass.constructor.call(this,a);this.on("afterlayout",this.onAfterRender,this)},onAfterRenderSpeed:function(a){a.setMinValue(1);
a.setMaxValue(10);a.setValue((this.playfield.moveDuration-0.05)/0.05)},onAfterRenderMoves:function(a){a.setMinValue(1);a.setMaxValue(100);a.setValue(this.playfield.moves)},onChangeSpeed:function(a,b){this.playfield.moveDuration=0.05*(11-b)+0.05},onChangeMoves:function(a,b){this.playfield.moves=b},onRemoveCup:function(){if(this.playfield.numCups>3){this.playfield.numCups--;this.playfield.drawGame()}this.checkStatusButtons()},onAddCup:function(){if(this.playfield.numCups<6){this.playfield.numCups++;
this.playfield.drawGame()}this.checkStatusButtons()},checkStatusButtons:function(){this.playfield.state==Zarafa.widgets.shellgame.ShellGameStates.IDLE&&this.playfield.numCups>3?this.removeCupBtn.enable():this.removeCupBtn.disable();this.playfield.state==Zarafa.widgets.shellgame.ShellGameStates.IDLE&&this.playfield.numCups<6?this.addCupBtn.enable():this.addCupBtn.disable();this.playfield.state==Zarafa.widgets.shellgame.ShellGameStates.IDLE?this.movesSliderfield.enable():this.movesSliderfield.disable()},
onAfterRender:function(){this.checkStatusButtons();this.mon(this.playfield,"statechange",this.onPlayfieldStateChange,this)},onPlayfieldStateChange:function(){this.checkStatusButtons()}});Ext.reg("zarafa.widget.shellgamepanel",Zarafa.widgets.shellgame.ShellGamePanel);
