var PingPongBase={events:{},inx:0,createDelegate:function(B,A){return function(){return B.apply(A,arguments)}},isObject:function(A){return !!A&&Object.prototype.toString.call(A)==="[object Object]"},apply:function(A){if(!this.isObject(A)){return }for(var B in A){this[B]=A[B]}},applyIf:function(A){if(!this.isObject(A)){return }for(var B in A){if(this[B]==undefined){this[B]=A[B]}}},random:function(B,A){return B+(Math.random()*(A-B))},initEvents:function(){if(!this.listeners){return }var C=this.listeners;delete this.listeners;var F=C.scope;for(var E in C){if(E=="scope"){continue}if($.isArray(C[E])){var B=C[E];for(var D=0,A=B.length;D<A;D++){this.on(E,B[D],F)}}else{this.on(E,C[E],F)}}},on:function(D,C,B,A){if(!this.events[D]){this.events[D]=[]}else{if(this.hasListener(D,C,B)!==false){return }}C.oid=++this.inx;this.events[D].push({fn:C,scope:B||this,opt:A})},un:function(G,F,E){var A=this.events[G];if(!A){return false}var H=this.hasListener(G,F,E);if(H===false){return false}var B=[];for(var D=0,C=A.length;D<C;D++){if(D==H){continue}B.push(A[D])}this.events[G]=null;if(B.length==0){delete this.events[G]}else{this.events[G]=B}return true},hasListener:function(F,E,D){var A=this.events[F];if(!A){return false}if(!E){return true}if(!D&&!E.oid){return false}for(var C=0,B=A.length;C<B;C++){if(!D&&A[C].fn.oid==E.oid){return C}if(D&&A[C].fn==E&&A[C].scope==D){return C}}return false},trigger:function(){var H=arguments[0],B=this.events[H],C=[],F=null;if(!B){return }for(var E=1,A=arguments.length;E<A;E++){C.push(arguments[E])}for(var E=0,D=B.length;E<D;E++){var G=B[E];F=G.fn.apply(B[E].scope,C);if(G.opt&&G.opt.single){this.un(H,G.fn,G.scope)}if(F===false){return F}}return F}};var PingPongBouncable=function(A,B){this.manager=A;this.el=B;this.id=A.getBncId();this.cachePosition()};PingPongBouncable.prototype=$.extend({},PingPongBase,{manager:null,el:null,left:0,top:0,right:0,bottom:0,width:0,height:0,id:null,allowInside:false,getEl:function(){return this.el},getWidth:function(){return this.width},getHeight:function(){return this.height},getTop:function(){return this.top},getLeft:function(){return this.left},getRight:function(){return this.right},getBottom:function(){return this.bottom},getId:function(){return this.id},cachePosition:function(){var C=this.el.offset(),A=this.el.width(),B=this.el.height();this.left=C.left;this.top=C.top;this.width=A;this.height=B;this.right=C.left+A;this.bottom=C.top+B},intersect:function(G,B){var C=this.left,J=this.top,H=this.bottom,A=this.right,I=B.getWidth(),E=B.getHeight(),D=B.getHDirection(),F=B.getVDirection();if(G.x1<C||G.x>A||G.y>H||G.y1<J){return false}if(Math.abs(G.x1-C)<I&&D=="r"){return"l"}if(Math.abs(G.x-A)<I&&D=="l"){return"r"}if(Math.abs(G.y1-J)<E&&F=="b"){return"t"}if(Math.abs(G.y-H)<E&&F=="t"){return"b"}return false}});var PingPongMoveable={axis:null,lastKeyCode:0,keyboardInterval:null,keyboardSpeed:30,keyboardStep:10,movingInProcess:false,initMouse:function(){this.onMousemoveDelegate=this.createDelegate(this.onMouseMove,this);this.el.bind("mousedown",this.createDelegate(this.onMousemoveStart,this));$("body").bind("mouseup",this.createDelegate(this.onMousemoveEnd,this))},onMousemoveStart:function(A){$("body").bind("mousemove",this.onMousemoveDelegate);this.movingInProcess=true;A.preventDefault();A.stopPropagation();this.onMoveStart()},onMoveStart:function(){},onMousemoveEnd:function(A){$("body").unbind("mousemove",this.onMousemoveDelegate);this.movingInProcess=false;A.preventDefault();A.stopPropagation();this.onMoveEnd()},onMoveEnd:function(){},onMouseMove:function(A){if(this.axis!="x"){this.setTop(A.pageY-this.height/2)}if(this.axis!="y"){this.setLeft(A.pageX-this.width/2)}},initKeyboard:function(){var A=this.manager.getField().getFocusEl();this.onKeydownDelegate=this.createDelegate(this.onKeydown,this);this.onKeyupDelegate=this.createDelegate(this.onKeyup,this);this.onKeyboardDelegate=this.createDelegate(this.onKeyboard,this);A.bind("keydown",this.onKeydownDelegate);A.bind("keyup",this.onKeyupDelegate)},onKeydown:function(A){var B=A.keyCode;if(B<37||B>40){return }this.lastKeyCode=B;if(!this.keyboardInterval){this.keyboardInterval=window.setInterval(this.onKeyboardDelegate,this.keyboardSpeed);this.onKeyboard()}A.preventDefault();A.stopPropagation();return false},onKeyup:function(A){if(this.keyboardInterval){window.clearInterval(this.keyboardInterval);this.keyboardInterval=null}A.preventDefault();A.stopPropagation();return false},setTop:function(C){var B=this.manager.getField().getOffset(),A=this.height;if(C<B.top){C=B.top}else{if(C+A>B.bottom){C=B.bottom-A}}this.top=C;this.bottom=C+this.height;this.el.css("top",C);this.onSetTop(C)},onSetTop:function(A){},setLeft:function(A){var C=this.manager.getField().getOffset(),B=this.width;if(A<C.left){A=C.left}else{if(A+B>C.right){A=C.right-B}}this.left=A;this.right=A+this.width;this.el.css("left",A);this.onSetLeft(A)},onSetLeft:function(A){},onKeyboard:function(){var A=this.lastKeyCode;switch(A){case 38:case 40:if(this.axis=="x"){return }this.setTop(this.top+(this.keyboardStep*(A==38?-1:1)));break;case 37:case 39:if(this.axis=="y"){return }this.setLeft(this.left+(this.keyboardStep*(A==37?-1:1)));break}}};var PingPong=function(B,A){if(A){this.originalConfig=A;if(A.game){this.apply(A.game)}}this.onKeyDelegate=this.createDelegate(this.onKey,this);this.intelligenceChangeDelegate=this.createDelegate(this.intelligenceChange,this);this.initialize(B);this.initEvents();this.trigger("init",this);this.balls[0].setStartPosition()};PingPong.prototype=$.extend({},PingPongBase,{originalConfig:{},field:null,score:null,l:null,r:null,t:null,b:null,playable:true,players:0,balls:[],distracting:0,bouncables:[],bncId:0,running:false,paused:false,blocked:false,showScore:true,lastLooser:"",lastWinner:"",intelligenceChangeTime:0,intelligenceChangeItv:null,autoResetGame:true,showLastScore:true,getConfig:function(){return this.originalConfig},getField:function(){return this.field},getScore:function(){return this.score},getLeft:function(){return this.l},getRight:function(){return this.r},getTop:function(){return this.t},getBottom:function(){return this.b},getPlayer:function(A){return this[A]},getRandomBot:function(){var C=[],E=["t","r","b","l"];for(var B in E){var D=E[B];if(this[D]&&this[D].getMode()=="a"){C.push(D)}}if(C.length==0){return null}if(C.length==1){return C[0]}var A=Math.round((Math.floor(Math.random()*11)*C.length)/10)-1;return C[A]?C[A]:C[0]},getRandomPlayer:function(){var C=[],E=["t","r","b","l"];for(var B in E){var D=E[B];if(this[D]&&this[D].getMode&&this[D].getMode()=="m"){C.push(D)}}if(C.length==0){return null}if(C.length==1){return C[0]}var A=Math.round((Math.floor(Math.random()*11)*C.length)/10)-1;return C[A]?C[A]:C[0]},getLastWinner:function(){return this.lastWinner},getLastLooser:function(){return this.lastLooser},getBall:function(){return this.balls[0]},isRunning:function(){return this.running},isPaused:function(){return this.paused},positionShort2Long:function(A){if(A=="l"){return"left"}if(A=="r"){return"right"}if(A=="t"){return"top"}if(A=="b"){return"bottom"}},oppositePosition:function(A){if(A=="l"){return"r"}if(A=="r"){return"l"}if(A=="t"){return"b"}if(A=="b"){return"t"}},getBouncables:function(){return this.bouncables},getBalls:function(){return this.balls},getPlayers:function(){return{l:this.l,r:this.r,t:this.t,b:this.b}},getBncId:function(){return ++this.bncId},getBallsNum:function(){return this.balls.length},getPlayersNum:function(){return this.players},initialize:function(D){this.field=new PingPongField(this,D);if(this.playable){this.balls.push(new PingPongBall(this,true))}if(this.distracting>0){for(var B=0;B<this.distracting;B++){this.balls.push(new PingPongBall(this,false))}}if(this.l==null&&this.r==null&&this.t==null&&this.b==null){this.l=new PingPongPlayer(this,"l","a");this.r=new PingPongPlayer(this,"r","m");this.players=2}else{if(this.l){this.l=new PingPongPlayer(this,"l","a")}if(this.r){this.r=new PingPongPlayer(this,"r","a")}if(this.t){this.t=new PingPongPlayer(this,"t","a")}if(this.b){this.b=new PingPongPlayer(this,"b","a")}this.players=!!this.l+!!this.r+!!this.t+!!this.b}if(this.showScore){this.score=new PingPongScore(this);this.score.on("hidden",this.createDelegate(this.onScoreHide,this))}var A=this.bouncables,C=this;$(".pp-bouncable").each(function(){A.push(new PingPongBouncable(C,$(this)))});this.field.getFocusEl().bind("keyup",this.onKeyDelegate);this.balls[0].on("wall",this.onBallWall,this)},onScoreHide:function(){this.balls[0].autostart()},block:function(){this.blocked=true},unblock:function(){this.blocked=false},start:function(C){if(this.blocked){return }if(this.running){return }this.running=true;for(var B=0,A=this.balls.length;B<A;B++){if(C&&B==0){this.balls[B].start(true)}else{this.balls[B].start()}}this.trigger("start",this)},stop:function(C){if(this.blocked){return }if(!this.running){return }this.running=false;for(var B=0,A=this.balls.length;B<A;B++){this.balls[B].stop();if(!C){this.balls[B].returnToStartPosition()}}this.trigger("stop",this)},reset:function(){if(this.blocked){return }if(this.isRunning()){this.stop(true)}this.trigger("reset",this)},pause:function(){if(this.blocked){return }if(!this.running){return }if(this.paused){return }for(var B=0,A=this.balls.length;B<A;B++){this.balls[B].pause()}this.paused=true;this.trigger("pause",this)},resume:function(){if(this.blocked){return }if(!this.running){return }if(!this.paused){return }this.paused=false;for(var B=0,A=this.balls.length;B<A;B++){this.balls[B].resume()}this.trigger("resume",this)},resetGame:function(){if(this.blocked){return }this.trigger("resetgame",this);this.reset()},onKey:function(A){if(this.blocked){return }switch(A.keyCode){case 32:A.preventDefault();A.stopPropagation();if(!this.isRunning()){this.start()}else{this[this.isPaused()?"resume":"pause"]()}return false}},onBallWall:function(F,A,B){var E=this.getPlayer(A),C=E.getStatus(),D=this.getPlayer(B);this.lastLooser=E.getPosition();C.takeLife();if(A==B&&this.getPlayersNum()==2){D=false;B=this.oppositePosition(A)}if(A!=B){if(!D&&this.getPlayersNum()==2){D=this.getPlayer(this.oppositePosition(E.getPosition()))}if(D){D.getStatus().addScorePoint()}}if(D){this.lastWinner=D.getPosition()}if(C.getLives()<=0){this.trigger("score",this,E,D,this.showLastScore);this.trigger("game",this,E,D);if(this.autoResetGame){this.resetGame()}}else{this.reset();this.trigger("score",this,E,D,true)}}});var PingPongField=function(B,C){this.manager=B;var A=B.getConfig();if(A.field){this.apply(A.field)}this.updatePositionDelegate=this.createDelegate(this.updatePosition,this);this.setFocusDelegate=this.createDelegate(this.setFocus,this);this.looseFocusDelegate=this.createDelegate(this.looseFocus,this);this.initialize(C);this.initEvents()};PingPongField.prototype=$.extend({},PingPongBase,{el:null,focus:null,width:0,height:0,ofsTop:0,ofsLeft:0,ofsBottom:0,ofsRight:0,minZIndex:100,getWidth:function(){return this.width},getHeight:function(){return this.height},getOffset:function(){return{top:this.ofsTop,left:this.ofsLeft,bottom:this.ofsBottom,right:this.ofsRight}},getOffsetLeft:function(){return this.ofsLeft},getOffsetTop:function(){return this.ofsTop},getOffsetBottom:function(){return this.ofsBottom},getOffsetRight:function(){return this.ofsRight},getFocusEl:function(){return this.focus},getEl:function(){return this.el},getMinZIndex:function(){return this.minZIndex},initialize:function(A){this.el=A||$(".ping-pong");this.el.css("z-index",this.minZIndex);$(window).bind("resize",this.updatePositionDelegate);this.createFocusEl();this.el.bind("click",this.setFocusDelegate);$("body").bind("click",this.looseFocusDelegate);this.manager.on("init",this.updatePosition,this,{single:true})},updatePosition:function(){var G=this.el,E=parseInt(G.css("paddingTop"),10),B=parseInt(G.css("borderTopWidth"),10),C=parseInt(G.css("paddingLeft"),10),F=parseInt(G.css("borderLeftWidth"),10),D=G.offset();if(isNaN(E)){E=0}if(isNaN(B)){B=0}if(isNaN(C)){C=0}if(isNaN(F)){F=0}this.width=G.innerWidth();this.height=G.innerHeight();this.ofsTop=D.top+E+B;this.ofsBottom=this.ofsTop+this.height;this.ofsLeft=D.left+C+F;this.ofsRight=this.ofsLeft+this.width;var A=this.manager;this.focus.css("top",this.ofsTop)},createFocusEl:function(){this.focus=$('<a href="#" class="pp-focus"></a>');this.el.append(this.focus)},setFocus:function(A){this.focus.focus();if(!this.el.hasClass("pp-field-focus")){this.el.addClass("pp-field-focus")}if(A){A.preventDefault();A.stopPropagation()}return false},looseFocus:function(){this.el.removeClass("pp-field-focus")}});var PingPongPlayer=function(C,A,D){this.manager=C;this.mode=D;this.position=A;this.axis=A=="l"||A=="r"?"y":"x";var B=C.getConfig();if(B[A]){this.apply(B[A])}this.originalErrorProbability=this.errorProbability;this.onKeyDownDelegate=this.createDelegate(this.onKeyDown,this);this.onKeyUpDelegate=this.createDelegate(this.onKeyUp,this);this.setBouncePositionDelegate=this.createDelegate(this.setBouncePosition,this);this.manualMoveDelegate=this.createDelegate(this.manualMove,this);this.onUpdateSettingsDelegate=this.createDelegate(this.onUpdateSettings,this);this.id=C.getBncId();this.initialize();this.initEvents()};PingPongPlayer.prototype=$.extend({},PingPongBouncable.prototype,PingPongMoveable,{mode:null,position:null,status:null,margin:5,step:8,autoStep:true,aiItv:null,shift:0,originalErrorProbability:0.5,errorProbability:0.5,errorProbabilityChange:0,changeTime:false,reactionDistance:0.5,speed:40,changeItv:null,showStatus:true,statusPosition:"outside",useMouse:false,startOnClick:false,getMode:function(){return this.mode},getAxis:function(){return this.axis},getPosition:function(){return this.position},getInsideCoord:function(){if(this.axis=="x"){return this.position=="t"?this.top+this.height:this.top}else{return this.position=="l"?this.left+this.width:this.left}},getStatus:function(){return this.status},isStatusVisible:function(){return this.showStatus},getStatusPosition:function(){return this.statusPosition},initialize:function(){this.render();this.status=new PingPongStatus(this.manager,this);this.onResize();var B=this.manager;if(this.mode=="m"){if(this.autoStep){this.keyboardStep=Math.round(this[this.axis=="x"?"width":"height"]/3)}if(this.useMouse){this.initMouse();if(this.useKeyboard){this.initKeyboard()}}else{this.initKeyboard()}}if(this.mode=="a"){if(this.autoStep){this.step=Math.round(this.height/10);if(this.step<3){this.step=3}}var A=this.manager.getBall();if(A.isPlayable()){A.on("start",this.start,this);A.on("stop",this.stop,this)}if(this.changeTime){B.on("start",this.onStartChanging,this);B.on("resume",this.onStartChanging,this);B.on("stop",this.onStopChanging,this);B.on("pause",this.onStopChanging,this)}}$(window).resize(this.createDelegate(this.onResize,this));B.on("init",this.onResize,this,{single:true});B.on("resetgame",this.onResetGame,this);B.on("reset",this.onReset,this)},render:function(){var A=["pp-racket"],B=this.manager.getField();if(this.axis=="x"){A.push(this.position=="t"?"pp-racket-top":"pp-racket-bottom")}else{A.push(this.position=="l"?"pp-racket-left":"pp-racket-right")}A.push(this.mode=="a"?"pp-racket-auto":"pp-racket-manual");this.el=$('<div class="'+(A.join(" "))+'"></div>');this.el.css({left:"-10000px",zIndex:B.getMinZIndex()+5});B.getEl().append(this.el)},onResize:function(){this.adjustPosition();this.setStartPosition();this.cachePosition()},adjustPosition:function(){var E=this.manager.getField(),A=this.axis,C=E[A=="x"?"getHeight":"getWidth"](),D=parseInt(E[A=="x"?"getOffsetTop":"getOffsetLeft"](),10),F=0,B=this[A=="x"?"height":"width"];if(this.position=="l"||this.position=="t"){F=D+this.margin}else{F=D+C-this.margin-B}this[A=="x"?"top":"left"]=F;this[A=="x"?"bottom":"right"]=F+B;this.el.css(A=="x"?"top":"left",F)},setStartPosition:function(){if(this.movingInProcess){return }var E=this.manager.getField(),A=this.axis,C=E[A=="x"?"getWidth":"getHeight"](),D=E[A=="x"?"getOffsetLeft":"getOffsetTop"](),B=this[A=="x"?"width":"height"],F=Math.round(D+(C/2)-(B/2));this[A=="x"?"left":"top"]=F;this[A=="x"?"right":"bottom"]=F+B;this.el.css(A=="x"?"left":"top",F);this.onSetTop()},onSetTop:function(B){var A=this.manager;if(!A.isRunning()&&(this.useMouse||this.useKeyboard)){A.getBall().setStartPosition(false)}},onMoveStart:function(){if(this.startOnClick){this.manager.start(true)}},start:function(A){if(this.mode!="a"||this.aiItv){return }this.aiItv=window.setInterval(this.setBouncePositionDelegate,this.speed)},stop:function(A){if(this.mode!="a"||this.aiItv==null){return }window.clearInterval(this.aiItv);this.aiItv=null;this.shift=0},setBouncePosition:function(){var O=this.manager.getBall(),P=this.manager.getField(),B=O.getHDirection(),A=O.getVDirection(),K=this.position,S=this.reactionDistance,I=this.axis,L=Math.abs(O.getFrontCoord(K)-this.getInsideCoord())/P[I=="x"?"getHeight":"getWidth"]();if(!O.isRunning()||O.isPaused()){return }if(K!=B&&K!=A){this.shift=0;return }if(L>1-S){return }var M=this[I=="x"?"width":"height"];if(!this.shift){var F=Math.random(),R=this.errorProbability;if(F<=R){this.shift=((M/1.3)*R)*(F>=0.5?-1:1)}}var N=O.getCenter(),H=N.left+this.shift,G=N.top+this.shift,J=I=="x"?H:G,C=this[I=="x"?"left":"top"],D=this.step;var Q=C+(M/2),E=C;if(J<Q-D||J>Q+D){E=C+D*(J<Q?-1:1)}this[I=="x"?"setLeft":"setTop"](E)},bounceAngle:function(B){var E=this.manager.getPlayersNum(),J=B.getAngle(),H=B.getDirection(),D=B.getHDirection(),G=B.getVDirection(),A=this.position,C=120,F=180,I=240;if(H=="rb"||H=="lt"){J=this.random(C,F)}if(H=="rt"||H=="lb"){J=this.random(F,I)}if(E<3){if(A=="l"){if(G=="b"&&J>230){J=this.random(180,220)}if(G=="t"&&J<110){J=this.random(120,180)}}if(A=="r"){if(G=="b"&&J>230){J=this.random(120,180)}if(G=="t"&&J<110){J=this.random(180,220)}}if(A=="t"){if(D=="l"&&J>160){J=this.random(90,150)}if(D=="r"&&J<200){J=this.random(210,250)}}if(A=="b"){if(D=="l"&&J<200){J=this.random(210,250)}if(D=="r"&&J>160){J=this.random(90,150)}}}return J},onReset:function(){this.setStartPosition()},onResetGame:function(){this.status.resetLives();this.status.resetScorePoints();if(this.errorProbabilityChange){this.errorProbability=this.originalErrorProbability}},onStartChanging:function(){if(this.changeItv){return }this.changeItv=window.setInterval(this.onUpdateSettingsDelegate,this.changeTime)},onStopChanging:function(){window.clearInterval(this.changeItv);this.changeItv=null},onUpdateSettings:function(){if(this.mode=="a"&&this.errorProbabilityChange&&this.errorProbability<1&&this.errorProbability>0){this.errorProbability+=this.errorProbabilityChange}}});var PingPongStatus=function(B,C){this.manager=B;this.player=C;this.visible=C.isStatusVisible();var A=B.getConfig();if(A.status){this.apply(A.status)}if(this.visible){this.initialize()}};PingPongStatus.prototype=$.extend({},PingPongBase,{el:null,autoAlign:false,score:0,lives:3,maxLives:3,width:0,height:0,visible:true,outsideMargin:10,insideMargin:0,insideHMargin:0,insideVMargin:0,getEl:function(){return this.el},getScore:function(){return this.score},getLives:function(){return this.lives},getMaxLives:function(){return this.maxLives},addScorePoint:function(){this.score+=1},resetScorePoints:function(){this.score=0},takeLife:function(){this.lives-=1;this.drawLives()},resetLives:function(){this.lives=this.maxLives;this.drawLives()},initialize:function(){var C=this.player.getPosition(),C=this.manager.positionShort2Long(C),A="pp-"+C+"-life-container",B=$("."+A);if(B.length>0){this.autoAlign=false;this.width=parseInt(B.width(),10);this.height=parseInt(B.height(),10);this.drawLives()}else{this.autoAlign=true;this.render();this.manager.on("init",this.onResize,this,{single:true});$(window).resize(this.createDelegate(this.onResize,this))}},render:function(){var A=this.player.getPosition(),A=this.manager.positionShort2Long(A),E=this.manager.getField(),B=this.player.getMode(),G=B=="a"?"bot":"player";var I="pp-"+A+"-life-container",F="pp-"+G+"-life";this.el=$('<div class="'+I+' pp-bouncable"></div>');this.el.css("position","absolute");this.el.css("left","-10000px");this.el.css("z-index",E.getMinZIndex()+10);E.getEl().append(this.el);this.drawLives();var C=$("."+F,this.el.get(0)).eq(0),H=parseInt(C.width(),10)*this.getLives(),D=parseInt(C.height(),10);this.el.css({width:H,height:D});this.width=H;this.height=D},drawLives:function(){var A=this.player.getMode(),F=A=="a"?"bot":"player",C="pp-"+F+"-life";$("."+C,this.el.get(0)).remove();for(var E=0;E<this.maxLives;E++){var F=E+1,G=E>=this.lives,D=[C,C+"-"+F];if(G){D.push(C+"-fired");D.push(C+"-"+F+"-fired")}var B=$('<div class="'+(D.join(" "))+'"></div>');this.el.append(B)}},onResize:function(){if(!this.autoAlign){return }var I=this.manager.getField(),C=this.player.getPosition(),D=this.player.getStatusPosition(),K=I.getOffset(),N=I.getWidth(),H=I.getHeight(),G=this.width,B=this.height,E=0,M=0,J=this.outsideMargin,L=this.insideMargin,O=this.insideHMargin||L,A=this.insideVMargin||L,F=this.manager.getPlayersNum();if(F>2){D="outside"}if(D=="outside"){if(C=="t"||C=="b"){E=Math.round(K.left+N/2-G/2)}if(C=="t"){M=K.top-B-J}if(C=="b"){M=K.bottom+J}if(C=="l"||C=="r"){M=Math.round(K.top+H/2-B/2)}if(C=="l"){E=K.left-G-J}if(C=="r"){E=K.right+J}}else{if(C=="l"){E=K.left+O;M=K.bottom-B-A}if(C=="r"){E=K.right-G-O;M=K.bottom-B-A}}this.el.css({left:E,top:M})}});var PingPongBall=function(B,C){this.manager=B;this.playable=C;var A=B.getConfig();if(A.ball){this.apply(A.ball)}this.originalStep=this.step;this.originalSpeed=this.speed;this.moveDelegate=this.createDelegate(this.move,this);this.onUpdateSettingsDelegate=this.createDelegate(this.onUpdateSettings,this);this.initialize();this.initEvents()};PingPongBall.prototype=$.extend({},PingPongBase,{manager:null,el:null,enableAutostart:true,width:0,height:0,top:0,left:0,direction:0,angle:0,originalStep:8,step:8,stepChange:0,originalSpeed:20,speed:20,speedChange:0,changeTime:false,changeItv:null,playable:false,moveItv:null,running:false,paused:false,nextPoint:null,prevBounce:null,prevWall:null,prevPlayer:null,runIndex:0,startFrom:null,firstStartFrom:null,playerMargin:10,startDirection:null,getWidth:function(){return this.width},getHeight:function(){return this.height},getLeft:function(){return this.left},getTop:function(){return this.top},getRight:function(){return this.left+this.width},getBottom:function(){return this.top+this.height},getHFront:function(){return this.direction<0?this.left:this.left+this.width},getVFront:function(){return this.angle<180?this.top:this.bottom},getEl:function(){return this.el},getFrontCoord:function(A){if(A=="l"){return this.left}if(A=="r"){return this.left+this.width}if(A=="t"){return this.top}if(A=="b"){return this.top+this.height}},getDirection:function(){return this.getHDirection()+this.getVDirection()},getHDirection:function(){return this.direction<0?"l":"r"},getVDirection:function(){if(this.direction<0){return this.angle<180?"t":"b"}if(this.direction>0){return this.angle<180?"b":"t"}},getAngle:function(){return this.angle},getStep:function(){return this.step},getSpeed:function(){return this.speed},isRunning:function(){return this.running},isPaused:function(){return this.paused},isPlayable:function(){return this.playable},initialize:function(){this.render();this.width=this.el.width();this.height=this.el.height();$(window).resize(this.createDelegate(this.onResize,this));this.manager.on("init",this.onGameInit,this,{single:true});this.manager.on("init",this.onResize,this,{single:true});if(this.startOnClick){this.el.bind("mouseup",this.createDelegate(this.onStartOnClick,this))}},render:function(){this.el=$('<div class="pp-ball"></div>');this.el.css("left","-10000px");this.el.css("z-index",this.manager.getField().getMinZIndex()+15);if(!this.playable){var A=this.manager.getBallsNum();this.el.addClass("pp-ball-distracting");this.el.addClass("pp-ball-"+A)}this.manager.getField().getEl().append(this.el)},onGameInit:function(){var A=this.manager;A.on("reset",this.onReset,this);A.on("resetgame",this.onResetGame,this);A.on("game",this.onGameEnd,this);if(this.stepChange||this.speedChange){A.on("start",this.onStartChanging,this);A.on("resume",this.onStartChanging,this);A.on("stop",this.onStopChanging,this);A.on("pause",this.onStopChanging,this)}},onReset:function(){this.el.show();this.speed=this.originalSpeed;this.step=this.originalStep;this.setStartPosition(false)},onResetGame:function(){this.el.show();this.runIndex=0},onGameEnd:function(){this.el.hide()},onStartChanging:function(){if(this.changeItv){return }this.changeItv=window.setInterval(this.onUpdateSettingsDelegate,this.changeTime)},onStopChanging:function(){window.clearInterval(this.changeItv);this.changeItv=null},onUpdateSettings:function(){if(this.stepChange){if(this.stepChange<0){if(this.stepMin==undefined||(this.stepMin&&this.step+this.stepChange>this.stepMin)){this.step+=this.stepChange}}if(this.stepChange>0){if(this.stepMax==undefined||(this.stepMax&&this.step+this.stepChange<this.stepMax)){this.step+=this.stepChange}}}if(this.speedChange){if(this.speedChange<0){if(this.speedMin==undefined||(this.speedMin&&this.speed+this.speedChange>this.speedMin)){this.speed+=this.speedChange}}if(this.speedChange>0){if(this.speedMax==undefined||(this.speedMax&&this.speed+this.speedChange<this.speedMax)){this.speed+=this.speedChange}}}this.pause();this.resume()},onResize:function(){this.setStartPosition()},returnToStartPosition:function(){this.setStartPosition(false)},setStartPosition:function(A){var C=this.manager,E=C.getField(),F=E.getOffset(),I=E.getWidth(),D=E.getHeight(),H=Math.round(F.top+(D/2)-(this.height/2)),B=Math.round(F.left+(I/2)-(this.width/2)),J=null;this.startDirection=null;if(this.playable&&this.runIndex==0&&this.firstStartFrom){var G=C[this.firstStartFrom=="player"?"getRandomPlayer":"getRandomBot"](),J=C.getPlayer(G)}else{if(this.playable&&this.startFrom){var G=C[this.startFrom=="winner"?"getLastWinner":"getLastLooser"](),J=C.getPlayer(G)}}if(J){if(G=="t"){H=J.getTop()+this.playerMargin+(this.height/2);B=J.getLeft()+(J.getWidth()/2)-(this.width/2)}else{if(G=="b"){H=J.getTop()-this.playerMargin-(this.height/2);B=J.getLeft()+(J.getWidth()/2)-(this.width/2)}else{if(G=="l"){B=J.getLeft()+this.playerMargin+(this.width/2);H=J.getTop()+(J.getHeight()/2)-(this.height/2)}else{if(G=="r"){B=J.getLeft()-this.playerMargin-(this.width/2);H=J.getTop()+(J.getHeight()/2)-(this.height/2)}}}}this.startDirection=C.oppositePosition(G);this.setMovingClass(J.getMode())}this.setPosition(B,H,A)},setPosition:function(C,B,A){if(A){this.el.animate({left:this.left=C,top:this.top=B},"fast")}else{this.el.css({left:this.left=C,top:this.top=B})}},setMovingClass:function(A){if(!this.playable){return }if(A=="a"){this.el.removeClass("pp-ball-fromplayer");this.el.addClass("pp-ball-frombot")}else{this.el.removeClass("pp-ball-frombot");this.el.addClass("pp-ball-fromplayer")}},onStartOnClick:function(A){A.preventDefault();A.stopPropagation();this.manager.start(true)},autostart:function(){if(!this.enableAutostart||!this.startFrom){return }var A=this.startFrom;if(A=="looser"){var B=this.manager.lastLooser}else{var B=this.manager.lastWinner}if(!B){return }B=this.manager.getPlayer(B);if(B.getMode()=="a"){this.manager.start()}},start:function(E){if(this.running){return }this.running=true;this.prevBounce=null;this.prevWall=null;this.prevPlayer=null;var B=this.manager,A=B.getPlayer("t")||B.getPlayer("b"),G=B.getPlayer("l")||B.getPlayer("r");if(this.startDirection){var F=this.startDirection;if(F=="l"){this.direction=-1}else{if(F=="r"){this.direction=1}else{this.direction=Math.random()<0.5?-1:1}}this.prevPlayer=this.manager.oppositePosition(F)}else{if(E===true){if(B.getPlayer("l").getMode()=="a"){this.direction=-1}else{if(B.getPlayer("r").getMode()=="a"){this.direction=1}else{this.direction=Math.random()<0.5?-1:1}}}else{this.direction=Math.random()<0.5?-1:1}}if(A&&G){this.angle=this.random(90,270)}else{if(A&&!G){var D=this.random(90,135),C=this.random(225,270);this.angle=Math.random()<0.5?D:C}else{this.angle=this.random(135,225)}}this.moveItv=window.setInterval(this.moveDelegate,this.speed);this.el.addClass("pp-ball-moving");this.trigger("start",this);this.runIndex++},pause:function(){if(this.paused){return }window.clearInterval(this.moveItv);this.moveItv=null;this.paused=true},resume:function(){if(!this.paused){return }this.moveItv=window.setInterval(this.moveDelegate,this.speed);this.paused=false},stop:function(){if(!this.running){return }window.clearInterval(this.moveItv);this.moveItv=null;this.running=false;this.el.removeClass("pp-ball-moving");this.el.removeClass("pp-ball-frombot");this.el.removeClass("pp-ball-fromplayer");this.trigger("stop",this);this.direction=0},getNextPoint:function(C,B){var A=this.angle,F=this.direction,C=C*F*-1,E=this.left+(C*Math.cos(A*(Math.PI/180))),D=this.top-(C*Math.sin(A*(Math.PI/180)));if(B){E+=this.width/2;D+=this.height/2}return{left:Math.round(E),top:Math.round(D)}},getCenter:function(){return{top:Math.round(this.top+this.height/2),left:Math.round(this.left+this.width/2)}},getPlayersByDirection:function(){var C=this.getHDirection(),A=this.getVDirection(),E=[];var D=this.manager.getPlayer(C);if(D){E.push(D)}var B=this.manager.getPlayer(A);if(B){E.push(B)}return E},move:function(){var Q=this.angle,I=this.manager,A=I.getField().getOffset(),B=this.getPlayersByDirection(),K=I.getBouncables(),D=I.getPlayersNum(),P=this.getNextPoint(this.step),C=P.left,G=P.top,M={x:C,x1:C+this.width,y:G,y1:G+this.height},H;this.setPosition(C,G);if(this.playable){for(var N=0,O=B.length;N<O;N++){var L=B[N];if((H=L.intersect(M,this))!==false){this.prevBounce=null;this.prevWall=null;this.trigger("bounce",this,L);Q=L.bounceAngle(this);this.angle=360-Q;if(H=="l"||H=="r"){this.direction*=-1}this.setMovingClass(L.getMode());this.prevPlayer=L.getPosition();return }}}for(var N=0,O=K.length;N<O;N++){var F=K[N];if(F.getId()==this.prevBounce){continue}if((H=F.intersect(M,this))!==false){this.angle=360-Q;if(H=="l"||H=="r"){this.direction*=-1}this.prevBounce=F.getId();this.prevWall=null;return }}var J=I.getPlayers(),R=[{id:"l",hit:C<=A.left},{id:"r",hit:C+this.width>=A.right},{id:"t",hit:G<=A.top},{id:"b",hit:G+this.height>=A.bottom}];for(var N=0;N<4;N++){if(!R[N].hit||R[N].id==this.prevWall){continue}var E=R[N];if(this.playable&&J[E.id]){this.trigger("wall",this,E.id,this.prevPlayer);this.stop();return }this.prevWall=E.id;this.prevBounce=null;this.angle=360-Q;if(E.id=="l"||E.id=="r"){this.direction*=-1}}}});var PingPongScore=function(B){this.manager=B;var A=B.getConfig();if(A.score){this.apply(A.score)}this.onTimeoutDelegate=this.createDelegate(this.onTimeout,this);this.initialize()};PingPongScore.prototype=$.extend({},PingPongBase,{visible:false,manager:null,speed:200,lifeTime:3000,useText:true,timeout:null,el:null,t:null,b:null,l:null,r:null,isVisible:function(){return this.visible},initialize:function(){this.render();var A=this.manager;A.on("start",this.onGameStart,this);A.on("score",this.onNewScore,this);this.manager.on("init",this.onResize,this,{single:true});$(window).resize(this.createDelegate(this.onResize,this))},render:function(){this.el=$('<div class="pp-score"></div>');this.t=$('<div class="pp-score-player pp-score-top"></div>');this.b=$('<div class="pp-score-player pp-score-bottom"></div>');this.l=$('<div class="pp-score-player pp-score-left"></div>');this.r=$('<div class="pp-score-player pp-score-right"></div>');this.el.append(this.t);this.el.append(this.l);this.el.append(this.r);this.el.append(this.b);this.el.css({left:"-10000px"});this.manager.getField().getEl().append(this.el);var D=this.manager,E=D.getLeft(),A=D.getRight(),I=D.getTop(),G=D.getBottom(),B=!!(I)+!!(E||A)+!!(G),C=!!(E)+!!(A),H=parseInt(this.l.outerWidth(),10)*C,F=parseInt(this.t.outerHeight(),10)*B;this.el.css({width:H,height:F});this.el.hide();if(!E&&!A){this.l.hide();this.r.hide()}if(!I){this.t.hide()}if(!G){this.b.hide()}},onResize:function(){var C=this.manager.getField(),A=this.el.width(),B=this.el.height();this.el.css({top:Math.round(C.getOffsetTop()+(C.getHeight()/2)-(B/2)),left:Math.round(C.getOffsetLeft()+(C.getWidth()/2)-(A/2))})},updateScore:function(){var A=this.manager,F=this.useText,E=[{el:this.l,p:A.getLeft()},{el:this.r,p:A.getRight()},{el:this.t,p:A.getTop()},{el:this.b,p:A.getTop()}];for(var C=0;C<4;C++){var D=E[C];if(!D.p){continue}if(F){D.el.text(D.p.getStatus().getScore())}for(var B=0;B<10;B++){D.el.removeClass("pp-score-"+B)}D.el.addClass("pp-score-"+D.p.getStatus().getScore())}},onGameStart:function(){this.updateScore();if(this.visible){this.hide(0);window.clearTimeout(this.timeout);this.timeout=null}},onNewScore:function(A,D,C,B){this.updateScore();if(B){this.show(this.speed);this.timeout=window.setTimeout(this.onTimeoutDelegate,this.lifeTime)}},onTimeout:function(){this.hide(this.speed);this.trigger("hidden")},show:function(A){this.visible=true;if(A){this.el.fadeIn(A)}else{this.el.show()}},hide:function(A){this.visible=false;if(A){this.el.fadeOut(A)}else{this.el.hide()}}});
