var my={};function moneymastermakesMain(){this.version='1.30';my.relPath='index-2.html';w=540;h=550;rect={x:220,y:65,wd:300,ht:300};startX=50;moneys=[["CA","Canada","$",[5,10,25,100,200],[200,500,1000,2000,5000],100,1,[40,140]],["US","USA","$",[1,5,10,25,50,100,500],[100,500,1000,2000],100,1,[40,180]],["MX","Mexico","$",[20,50,100,200,500,1000,2000],[2000,5000,10000,20000,50000,100000],1000,1,[40,220]],["UK","UK","£",[1,2,5,10,20,50,100,200],[200,500,1000,2000],100,1,[230,130]],["EU","Europe","€",[1,2,5,10,20,50,100,500],[100,200,500,1000,2000,5000],100,1,[220,160]],["ILS","Israel","₪",[10,50,100,200,500,1000],[1000,2000,5000,10000,20000,50000],1000,1,[250,190]],["LBP","Lebanon","L",[250,500,1000,5000,10000,20000,50000],[1000,2000,5000,10000,20000,50000],10000,1,[300,160]],["EGP","Egypt","LE",[50,100,500,1000,2000,5000,10000],[1000,2000,5000,10000,20000,50000],10000,1,[320,190]],["PKR","Pakistan","Rs",[1,2,5,10,20,50,100],[100,500,1000,2000,5000,10000,20000,50000],100,0,[290,215]],["CNY","China","¥",[50,100,200,500,1000,2000,5000],[500,1000,2000,5000,10000,20000],1000,1,[400,160]],["BDT","Bangladesh","Tk",[1,2,5,10,20,50,100],[100,500,1000,2000,5000,10000,20000,50000],100,0,[400,190]],["IN","India","Rs",[1,2,5,10,20,50,100],[100,500,1000,2000,5000,10000,20000,50000],100,0,[380,215]],["MYR","Malaysia","RM",[5,10,20,50,100,200,500],[500,1000,2000,5000,10000,20000,50000],100,1,[380,240]],["HK","Hong Kong","$",[10,20,50,100,200,500,1000],[2000,5000,10000,20000,50000,100000],100,1,[470,240]],["PHP","Philippines","P",[1,5,10,20,50,100,200],[200,500,1000,2000,5000,10000,20000,50000],100,0,[440,265]],["SG","Singapore","$",[5,10,20,50,100,200,500],[1000,2000,5000,10000,20000,50000,100000],100,1,[400,290]],["ZA","South Africa","R",[5,10,20,50,100,200,500],[500,1000,2000,5000,10000],100,1,[260,320]],["AU","Australia","$",[5,10,20,50,100,200,500],[200,500,1000,2000,5000,10000],100,1,[460,315]],["NZD","NZ","$",[10,20,50,100,200,500,1000],[1000,2000,5000,10000,20000,50000,100000],100,1,[530,340]],["GEN","Generic","$",[1,2,5,10,20,50,100],[100,200,500,1000,2000,5000],100,1,[400,360]]];codeAt=0;nameAt=1;symbolAt=2;denomsAt=3;paidAt=4;makeAt=5;decAt=6;coordAt=7;games=[{mode:"make",names:["Make","Echo"],showTot:true,fewCoins:false,giveChg:false,doAdd:false,makeQ:true,score:1},{mode:"simptot",names:["Target","simple"],showTot:true,fewCoins:false,giveChg:false,doAdd:false,score:1},{mode:"simp",names:["Target (no totals)","simple (sin totales)"],showTot:false,fewCoins:false,giveChg:false,doAdd:false,score:2},{mode:"fewtot",names:["Handfull","algunas monedas"],showTot:true,fewCoins:true,giveChg:false,doAdd:false,score:2},{mode:"few",names:["Pocketfull (no totals)","algunas monedas (sin totales)"],showTot:false,fewCoins:true,giveChg:false,doAdd:false,score:3},{mode:"givetot",names:["Give Change","da el cambio"],showTot:true,fewCoins:false,giveChg:true,doAdd:false,score:3},{mode:"give",names:["Give Change (no totals)","da el cambio (sin totales)"],showTot:false,fewCoins:false,giveChg:true,doAdd:false,score:4},{mode:"addtot",names:["Add/Remove",""],showTot:true,fewCoins:false,giveChg:false,doAdd:true,score:1}];game=games[0];this.tgt=0;this.denHTML='';var s='';s+='
';s+=getPopHTML('list',20,400);s+='';s+='';s+='';document.write(s);el=document.getElementById('canvasId');ratio=2;el.width=w*ratio;el.height=h*ratio;el.style.width=w+"px";el.style.height=h+"px";g=el.getContext("2d");g.setTransform(ratio,0,0,ratio,0,0);money=moneys[1];crncy=money[codeAt];denoms=money[denomsAt];newShapes();dragging=false;dragHoldX=0;dragHoldY=0;score=0;document.getElementById('score').innerHTML=score.toString();doneQ=false;newGame();listpop();el.addEventListener("mousedown",mouseDownListener,false);el.addEventListener('touchstart',ontouchstart,false);el.addEventListener("mousemove",dopointer,false);findMakeCount();}
function findMakeCount(){my.justMade='';my.mades=[];my.makes=[];my.makeCount=0;var denoms=money[denomsAt];var tgt=money[makeAt];if(money[decAt]==0){console.log("no dec");}
whatMakes(tgt,0,"",denoms,0);console.log("my.makeCount",money[0],my.makeCount,my.makes);}
function whatMakes(tgt,sofar,madeStr,denoms,pos){if(pos>=denoms.length){return;}
var denom=denoms[pos];do{if(sofar0){madeStr=madeStr+",";}
madeStr=madeStr+denom;}while(sofar<=tgt);}
function setVis(id,onQ){if(onQ){document.getElementById(id).style.visibility='visible';}else{document.getElementById(id).style.visibility='hidden';}}
function newCrncy(){listpop();}
function newGame(){document.getElementById('info').innerHTML='';document.getElementById('success').style.opacity=0;setVis('userLbl',game.showTot);setVis('userVal',game.showTot);setVis('checkBtn',false);setVis('custLbl',game.giveChg);setVis('custVal',game.giveChg);var userLbl="Total:";if(game.giveChg){userLbl="Change:";}
document.getElementById('userLbl').innerHTML=userLbl;if(game.doAdd){this.tgt=newTargetAdd();}else{newShapes();this.tgt=newTarget();document.getElementById('needVal').innerHTML=fmtCurrency(this.tgt);if(game.makeQ){findMakeCount();document.getElementById('makefoundVal').innerHTML=0;document.getElementById('makewaysVal').innerHTML=my.makeCount;}
if(game.giveChg){var tryN=0;var cust=0;do{cust=money[paidAt][getRandomInt(0,money[paidAt].length-1)];tryN++;}while(cust<=this.tgt&&tryN<100);document.getElementById('custVal').innerHTML=fmtCurrency(cust);document.getElementById('needLbl').innerHTML="Items cost:";document.getElementById('needVal').innerHTML=fmtCurrency(cust-this.tgt);}}
doDenHTML();drawShapes();}
function newTarget(){if(game.makeQ){return money[makeAt];}
var ds=money[denomsAt];var sels;var tryCount=0;do{var x=0;var denN=4;sels=[];for(var i=0;i=0;i--){var coinVal=money[denomsAt][i];if(coinVal!=0){numCoins=Math.round(x/coinVal-0.5);x-=numCoins*coinVal;totalCoins+=numCoins;if(x==0)break;}}
if(x!=0)return(0);return(totalCoins);}
function newTargetAdd(){tempArr=[];for(var i=0;i8)addQ=false;if(newCount<3)addQ=true;var denom=0;if(addQ){var choice=Math.randomInt(money[coinsAt].length);denom=money[coinsAt][choice];coinAdd=1;}else{choice=Math.randomInt(currCoinArr.length);denom=currCoinArr[choice].denom;coinAdd=-1;}
newCount+=coinAdd;newTotal+=coinAdd*denom;chgCount++;}while(chgCount<2||newTotal==currTotal||newTotal<=0);return(newTotal);}
function newShapes(){shapes=[];for(var i=0;i';}}
return s;}
function listpop(){var pop=document.getElementById('listpop');pop.style.transitionDuration="0.3s";pop.style.opacity=1;pop.style.zIndex=12;pop.style.left='20px';}
function ontouchstart(evt){var touch=evt.targetTouches[0];evt.clientX=touch.clientX;evt.clientY=touch.clientY;evt.touchQ=true;mouseDownListener(evt)}
function ontouchmove(evt){var touch=evt.targetTouches[0];evt.clientX=touch.clientX;evt.clientY=touch.clientY;evt.touchQ=true;mouseMoveListener(evt);evt.preventDefault();}
function ontouchend(){el.addEventListener('touchstart',ontouchstart,false);window.removeEventListener("touchend",ontouchend,false);if(dragging){dragging=false;moneyDrop();window.removeEventListener("touchmove",ontouchmove,false);}}
function dopointer(e){var bRect=el.getBoundingClientRect();var mouseX=(e.clientX-bRect.left)*(el.width/ratio/bRect.width);var mouseY=(e.clientY-bRect.top)*(el.height/ratio/bRect.height);var overQ=false;for(var i=0;ihighestIndex){dragHoldX=mouseX-shapes[i].x;dragHoldY=mouseY-shapes[i].y;highestIndex=i;dragIndex=i;}}}
if(dragging){shapes[dragIndex].shadowQ=true;if(evt.touchQ){window.addEventListener('touchmove',ontouchmove,false);}else{window.addEventListener("mousemove",mouseMoveListener,false);}}
if(evt.touchQ){el.removeEventListener("touchstart",ontouchstart,false);window.addEventListener("touchend",ontouchend,false);}else{el.removeEventListener("mousedown",mouseDownListener,false);window.addEventListener("mouseup",mouseUpListener,false);}
if(evt.preventDefault){evt.preventDefault();}
else if(evt.returnValue){evt.returnValue=false;}
return false;}
function mouseUpListener(){el.addEventListener("mousedown",mouseDownListener,false);window.removeEventListener("mouseup",mouseUpListener,false);if(dragging){dragging=false;moneyDrop();window.removeEventListener("mousemove",mouseMoveListener,false);}}
function mouseMoveListener(evt){if(dragIndex<0)return;var bRect=el.getBoundingClientRect();var mouseX=(evt.clientX-bRect.left)*(el.width/ratio/bRect.width);var mouseY=(evt.clientY-bRect.top)*(el.height/ratio/bRect.height);var minX=-shapes[dragIndex].wd/2;var maxX=w-shapes[dragIndex].wd/2;var posX=mouseX-dragHoldX;posX=(posXmaxX)?maxX:posX);var minY=-shapes[dragIndex].ht*0.7;var maxY=h-shapes[dragIndex].ht*0.3;var posY=mouseY-dragHoldY;posY=(posYmaxY)?maxY:posY);shapes[dragIndex].x=posX;shapes[dragIndex].y=posY;var div=document.getElementById('denom'+dragIndex);div.style.left=posX+'px';div.style.top=posY+'px';if(shapes[dragIndex].shadowQ){div.style.filter='drop-shadow(5px 5px 5px #222)';div.style.webkitFilter='drop-shadow(5px 5px 5px #222)';}else{div.style.filter='none';div.style.webkitFilter='none';}}
function moneyDrop(){document.getElementById('click').play();var me=shapes[dragIndex];me.shadowQ=false;if(me.typ=='base'){if(hitTest(rect,me.x+me.wd/2,me.y+me.ht/2)){var temp=new Money(me.denom);temp.setxy(me.x,me.y);temp.typ='copy';shapes.push(temp);doDenHTML();}
me.moveStt();}
if(me.typ=='copy'){if(hitTest(rect,me.x+me.wd/2,me.y+me.ht/2)){}else{shapes.splice(dragIndex,1);doDenHTML();}}
drawShapes();}
function getPopHTML(id,yp,ht){var s='';s+='
';s+=getCrncyHTML();s+='
';return s;}
function getCrncyHTML(){s='';s+='
';s+='';for(var i=0;i'+m[nameAt]+'';}
s+='
';return s;}
function doCrncy(n){money=moneys[n];crncy=money[codeAt];denoms=money[denomsAt];newShapes();newGame();var pop=document.getElementById('listpop');pop.style.opacity=0;pop.style.zIndex=-1;pop.style.left='-900px';}
function getGameHTML(){s='';s+='
';return s;}
function chgGame(n){console.log("chgGame",n);game=games[n];newGame();}
function hitTest(shape,mx,my){if(mx(shape.x+shape.wd))return false;if(my>(shape.y+shape.ht))return false;return true;}
function Money(den){this.typ='base';this.shadowQ=false;this.denom=den;this.visQ=true;this.wd=76;this.ht=76;}
Money.prototype.setxy=function(x,y){this.x=x;this.y=y;};Money.prototype.setStt=function(){this.xStt=this.x;this.yStt=this.y;};Money.prototype.moveStt=function(){this.x=this.xStt;this.y=this.yStt;};function drawShapes(){g.clearRect(0,0,el.width,el.height);var tot=0;my.dens=[];for(var i=0;i=my.makeCount){msg("You got them all! Wow!!");}else{msg('Yes! Try another...');setTimeout(msgClear,1500);}}}else{score+=game.score;document.getElementById('score').innerHTML=score;doSuccess();}}}
function msg(s){document.getElementById('info').innerHTML=s;}
function msgClear(){msg('');}
function doSuccess(){document.getElementById('success').style.opacity=1;document.getElementById('snd1').play();setTimeout(newGame,1500);}
function dist(dx,dy){return(Math.sqrt(dx*dx+dy*dy));}
function loop(currNo,minNo,maxNo,incr){currNo+=incr;var range=maxNo-minNo+1;if(currNomaxNo){currNo=minNo+(currNo-minNo)%range;}
return currNo;}
function constrain(min,val,max){return(Math.min(Math.max(min,val),max));}
CanvasRenderingContext2D.prototype.drawBox=function(midX,midY,radius,angle){g.beginPath();var pts=[[0,0],[Math.cos(angle),Math.sin(angle)],[Math.cos(angle)+Math.cos(angle+Math.PI/2),Math.sin(angle)+Math.sin(angle+Math.PI/2)],[Math.cos(angle+Math.PI/2),Math.sin(angle+Math.PI/2)],[0,0]];for(var i=0;i0){s+=" + ";}
if(n==1){s+=this.fmtCurrency(_loc3_);}else{s+=(n+" x "+this.fmtCurrency(_loc3_));}
n=1;_loc3_=den;}
i++;}
if(s.length>0){s+=" + ";}
if(n==1){s+=this.fmtCurrency(_loc3_);}else{s+=(n+" x "+this.fmtCurrency(_loc3_));}
return s;}
function fmtCurrency(cents){if(money[decAt]==1){dollars=Math.floor(cents/100);cents=cents-dollars*100;if(cents<10){x=".0"+cents;}else{x="."+cents;}
x=dollars+x;}else{x=cents;}
return(money[symbolAt]+x);}