/*
 *
 * Gogopop v2.7.1 
 * @2007-2010 trevor cowley. all rights reserved. 
 * http://gogomakeplay.com/
 *
 */
G.cookieId='gogopop';G.F.mainAI=function(){var i;for(i=0;i<84;i++)G.O['exp'+i].AI();G.O.gameOver.AI();if(G.O.viewport.tagContainsMouse()){G.O.newGame.AI('unhover');G.O.sameGame.AI('unhover');G.O.help.AI();if(G.S.skipTurn){G.S.skipTurn=0;return}var currX=Math.floor((G.M.x-G.O.viewport.docx-2)/34),currY=Math.floor((G.M.y-G.O.viewport.docy-2)/34),currI=currY*12+currX;if(currX>11||currY>6||currX<0||currY<0){currX=currY=currI=-1;G.F.resetChecked()}else if(currX>=0&&currY>=0&&G.S.grid[currI]){G.S.grid[currI].AI()}else{G.F.resetChecked()}if(G.S.shiftBlocksDown){G.S.shiftBlocksDown=0;G.F.shiftBlocksDown();if(G.S.shiftBlocksLeft){G.S.shiftBlocksLeft=0;G.F.shiftBlocksLeft()}G.F.checkGameOver()}G.S.resetBlocks=1}else if(G.O.game.tagContainsMouse()){if(G.S.resetBlocks)G.F.resetChecked();G.O.newGame.AI();G.O.sameGame.AI();G.O.help.AI()}else{if(G.S.resetBlocks)G.F.resetChecked();if(G.O.newGame.S.hover)G.O.newGame.AI();if(G.O.sameGame.S.hover)G.O.sameGame.AI();if(G.O.help.S.hover)G.O.help.AI()}};G.F.helpAI=function(cmd){if(!this.on)return;var t=this;if(G.O.helpText.on&&G.O.game.tagContainsMouseClick()){G.O.helpText.turnOff();G.S.skipTurn=1}else if(t.tagContainsMouse()){if(t.tagContainsMouseClick()){if(G.O.helpText.on)G.O.helpText.turnOff();else G.O.helpText.turnOn()}if(!t.S.hover){t.removeClass('buttonUp').addClass('buttonDown').draw();t.S.hover=1}}else if(t.S.hover){t.removeClass('buttonDown').addClass('buttonUp').draw();t.S.hover=0}};G.F.startButtonAI=function(cmd){if(!this.on)return;var t=this;if(!cmd&&t.tagContainsMouse()){if(t.tagContainsMouseClick()){if(t.id=='newGame')G.S.sameGame=0;else G.S.sameGame=1;G.loadBlock('main')}if(!t.S.hover){t.removeClass('buttonUp').addClass('buttonDown').draw();t.S.hover=1}}else if(t.S.hover){t.removeClass('buttonDown').addClass('buttonUp').draw();t.S.hover=0}};G.F.blockAI=function(cmd){var i,arr,t=this,hasNeighbours,Exp;arr=[t.S.y*12+t.S.x-1,t.S.y*12+t.S.x+1,t.S.y*12+t.S.x-12,t.S.y*12+t.S.x+12];if(!cmd){if(t.tagContainsMouseClick()){G.F.resetChecked();t.AI('delete')}else{if(t.S.checked)return t;G.F.resetChecked();t.AI('highlight')}}else if(cmd=='reset'){t.setState({checked:0}).setStyle({backgroundColor:G.S.colors[G.S.colorScheme][t.S.color][1]}).draw();return t}else if(cmd=='highlight'){if(t.S.checked)return t;t.S.checked=1;hasNeighbours=0;for(i=0;i<4;i++){if(i==0&&t.S.x>0||i==1&&t.S.x<11||i==2&&t.S.y>0||i==3&&t.S.y<6){if(G.S.grid[arr[i]]&&G.S.grid[arr[i]].S.color==t.S.color){G.S.grid[arr[i]].AI('highlight');if(G.S.grid[arr[i]].S.checked)hasNeighbours=1}}}if(hasNeighbours)t.setStyle({backgroundColor:G.S.colors[G.S.colorScheme][t.S.color][0]}).draw();return t}else if(cmd=='delete'){if(t.S.checked)return t;t.S.checked=1;hasNeighbours=0;for(i=0;i<4;i++){if(i==0&&t.S.x>0||i==1&&t.S.x<11||i==2&&t.S.y>0||i==3&&t.S.y<6){if(G.S.grid[arr[i]]&&G.S.grid[arr[i]].S.color==t.S.color){G.S.grid[arr[i]].AI('delete');if(G.S.grid[arr[i]].S.checked)hasNeighbours=1}}}if(hasNeighbours){t.S.deleted=1;G.S.shiftBlocksDown=1;Exp=G.F.getNextExplosionSprite();Exp.setVar({x:t.x+17,y:t.y+17}).setState({color:t.S.color}).AI('reset').turnOn()}return t}else if(cmd=='hasNeighbours'){for(i=0;i<4;i++){if(i==0&&t.S.x>0||i==1&&t.S.x<11||i==2&&t.S.y>0||i==3&&t.S.y<6){if(G.S.grid[arr[i]]&&G.S.grid[arr[i]].S.color==t.S.color)return 1}}return 0}};G.F.explosionAI=function(cmd){var t=this,F=t.S.frame;if(cmd=='reset'){t.setVar({x:t.x,y:t.y}).setState({frame:0}).setVar({tx:0,ty:0,tw:0,th:0}).draw().turnOff()}else{if(!t.on)return t;if(F==0)t.setStyle({borderColor:G.S.colors[G.S.colorScheme][t.S.color][0]}).draw();if(F<9)t.setVar({tx:-(F*F*4+25),ty:-(F*F*4+25),w:F*F*8+50,h:F*F*8+50}).draw();else t.turnOff();t.S.frame++}return t};G.F.resetChecked=function(){for(var i=0;i<84;i++)G.O['block'+i].AI('reset');G.S.resetBlocks=0};G.F.checkGameOver=function(){var i,x;G.S.gameOver=1;for(i=0;i<84;i++){if(G.S.grid[i]&&G.S.grid[i].AI('hasNeighbours')){G.S.gameOver=0;break}}if(G.S.gameOver){x=0;for(i=0;i<84;i++)if(G.S.grid[i])x++;x=Math.pow(x,3);if(x<10000)G.F.addScore(10000-x);G.O.gameOver.setState({i:60}).setSrc('Game Over').turnOn()}};G.F.gameOverAI=function(){if(!this.on)return;var t=this;if(t.S.i==30)t.setSrc('Try Again!').draw();else if(t.S.i==0){G.S.sameGame=1;G.loadBlock('main')}t.S.i--};G.F.shiftBlocksDown=function(){var i,j,arr,arrIsEmpty=0,countDeleted=0;for(i=0;i<12;i++){arr=[];for(j=0;j<7;j++){if(G.S.grid[j*12+i]){if(G.S.grid[j*12+i].S.deleted){G.S.grid[j*12+i].turnOff();countDeleted++}else arr.push(G.S.grid[j*12+i]);G.S.grid[j*12+i]=null}}if(arr.length==0)arrIsEmpty=1;else if(arrIsEmpty&&arr.length>0)G.S.shiftBlocksLeft=1;for(j=7-arr.length;j<7;j++){G.S.grid[j*12+i]=arr[j-(7-arr.length)];G.S.grid[j*12+i].setState({x:i,y:j}).setVar({x:i*34+2,y:j*34+2}).draw()}}if(countDeleted)G.F.addScore(Math.pow(countDeleted,3)-4*countDeleted+5)};G.F.shiftBlocksLeft=function(){var i,j,arr,arr2=[],i2=0;for(var i=0;i<12;i++){arr=[];for(j=0;j<7;j++){if(G.S.grid[j*12+i])arr.push(G.S.grid[j*12+i]);G.S.grid[j*12+i]=null}if(arr.length>0){for(j=7-arr.length;j<7;j++){arr2[j*12+i2]=arr[j-(7-arr.length)];arr2[j*12+i2].setState({x:i2,y:j}).setVar({x:i2*34+2,y:j*34+2}).draw()}i2++}}for(i=0;i<=i2;i++)for(j=0;j<7;j++)if(arr2[j*12+i])G.S.grid[j*12+i]=arr2[j*12+i]};G.F.addScore=function(amount){var i,tmpScore;G.S.score=G.S.score+amount;tmpScore=''+G.S.score;for(i=tmpScore.length;i<5;i++){tmpScore='0'+tmpScore}G.O.score.setSrc(tmpScore).draw();if(G.S.score>G.S.gridHiScore){G.S.gridHiScore=G.S.score;G.O.gridHiScore.setSrc(tmpScore).draw()}if(G.S.score>G.S.hiScore){G.cookies.hiScore=G.S.hiScore=G.S.score;G.saveCookies();G.O.hiScore.setSrc(tmpScore).draw()}};G.F.getNextExplosionSprite=function(){G.S.currentExplosion++;return G.O['exp'+G.S.currentExplosion%84]};G.F.loaderAI=function(){if(!this.init){G.M.deselectGob=G.makeGob('game',G,'div',document.getElementById('shell')).setVar({w:428,h:334,nextStyle:{position:'relative'}}).turnOn();this.init=1;G.setState({sameGame:0,hiScore:0,currentExplosion:0,grid:[],colors:[[['#F90','#c50'],['#dc0','#990'],['#f44','#933'],['#9d9','#5a5']],[['#EFB','#9F0'],['#FBE','#C09'],['#FDB','#F60'],['#BFF','#099'],],[['#F8F','#606'],['#EF8','#8B0'],['#8FC','#074'],['#F98','#B20'],],[['#FC8','#B50'],['#8CF','#047'],['#B8F','#309'],['#FF8','#BA0'],],[['#3f3','#2a2'],['#ff3','#aa2'],['#f33','#b22'],['#16f','#13a']],[['#98f','#50d'],['#3dd','#088'],['#9f4','#6b3'],['#fb2','#b80']],]});G.loadCookies()}else if(this.init<2){var V,X=G.O.game;this.init=2;this.AI=G.F.mainAI;G.makeGob('viewportborder',X).setVar({x:8,y:43,z:100,w:412,h:242}).turnOn();V=G.makeGob('viewport',X).setVar({x:9,y:44,z:110,w:410,h:240,addClass:['cursor','pointer']}).turnOn();G.makeGob('score',X).setVar({x:13,y:6,z:1020,w:90,h:34,nextSrc:'00000',nextStyle:{color:'#399'}}).turnOn();G.makeGob('gridHiScore',X).setVar({x:172,y:6,z:1020,w:90,h:34,nextSrc:'00000',nextStyle:{color:'#939'}}).turnOn();G.makeGob('hiScore',X).setVar({x:330,y:6,z:1020,w:90,h:34,nextSrc:'00000',nextStyle:{color:'#993'}}).turnOn();G.makeGob('gameOver',V).setVar({x:93,y:75,z:2020,w:226,h:88,AI:G.F.gameOverAI,nextSrc:'Game Over'}).turnOn();G.makeGob('sameGame',X).setVar({x:275,y:290,z:1020,w:65,h:32,AI:G.F.startButtonAI,nextSrc:'same',addClass:'buttonUp'}).turnOn();G.makeGob('newGame',X).setVar({x:362,y:290,z:1020,w:55,h:32,AI:G.F.startButtonAI,nextSrc:'new',addClass:'buttonUp'}).turnOn();G.makeGob('help',X).setVar({x:10,y:290,z:1020,w:60,h:32,AI:G.F.helpAI,nextSrc:'help',addClass:'buttonUp'}).turnOn();G.makeGob('helpText',V).setVar({z:3020,w:410,h:240,nextSrc:'<div>'+'<h1>How To Play GogoPop</h1>'+'<ol>'+'<li>Click the blocks and clear the screen'+'<li>The <b style="color:#399">first score</b> is the current score'+'<li>The <b style="color:#939">second score</b> is the current grid hi score'+'<li>The <b style="color:#993">third score</b> is the all-time hi score'+'<li>Click <b style="color:#fff">same</b> to replay the same grid'+'<li>Click <b style="color:#fff">new</b> to play a new grid'+'</ol>'+'<div>&copy; 2007 - '+new Date().getFullYear()+' <a href="mailto:trevorcowley@gogomakeplay.com">trevor cowley</a></div>'+'</div>'});for(var i=0;i<84;i++){G.makeGob('block'+i,V).setVar({z:100,w:32,h:32,addClass:'block'});G.makeGob('exp'+i,V).setVar({x:-1000,z:2000,AI:G.F.explosionAI,addClass:'explosion'}).turnOn()}if(G.cookies.hiScore){G.S.hiScore=parseInt(G.cookies.hiScore);var i,tmpHiScore=''+G.S.hiScore;for(i=tmpHiScore.length;i<5;i++)tmpHiScore='0'+tmpHiScore;G.O.hiScore.setSrc(tmpHiScore).draw()}G.loadBlock('main')}};G.makeBlock('main',function(){if(!this.init){this.AI=G.F.loaderAI}else{var i,O;G.O.gameOver.turnOff();G.setState({shiftBlocksDown:0,shiftBlocksLeft:0,score:0});G.O.score.setSrc('00000').draw();if(!G.S.sameGame){G.setState({colorScheme:Math.floor(Math.random()*6),gridHiScore:0});G.O.gridHiScore.setSrc('00000').draw()}for(i=0;i<84;i++){O=G.O['block'+i];O.setVar({x:(i%12)*34+2,y:Math.floor(i/12)*34+2,AI:G.F.blockAI}).setState({x:i%12,y:Math.floor(i/12),checked:0,deleted:0}).setStyle({backgroundColor:'#bbb'});if(G.S.sameGame)O.S.color=O.S.sameColor;else O.S.sameColor=O.S.color=Math.floor(Math.random()*4);O.AI('reset').turnOn();G.S.grid[i]=O}}}).loadBlock('main');

