var w,h,ratio,el,el2,g,g2,my={}
function clockanadigMain(w){this.version='0.62';w=typeof w!=='undefined'?w:250;h=w;radius=w*0.4;var shadow=w*0.00;digWd=w*0.8;digHt=80;var s='';s+='
';s+='
';s+='
';s+='
';s+=' ';s+='
';s+='
Now ';s+='
';document.write(s);el=document.getElementById('clock1');ratio=3;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);el2=document.getElementById('clock2');ratio=3;el2.width=w*ratio;el2.height=h*ratio;el2.style.width=w+"px";el2.style.height=h+"px";g2=el2.getContext("2d");g2.setTransform(ratio,0,0,ratio,0,0);el3=document.getElementById('canavasdig');ratio=3;el3.width=digWd*ratio;el3.height=digHt*ratio;el3.style.width=digWd+"px";el3.style.height=digHt+"px";g3=el3.getContext("2d");g3.setTransform(ratio,0,0,ratio,0,0);dragType='';modes=['now','fix','hr','mn'];modeNo=0;mode=modes[modeNo];clock=new Clock(el,clockRadius());clock.drawFace();clock.drawMinuteMarkers();dig=new Digital(g3,digWd,digHt);dig.update();render();el2.addEventListener("mousedown",onmouseDown,false);el2.addEventListener('touchstart',ontouchstart,false);el2.addEventListener("mousemove",onmousemovePointer,false);}
function setNow(){dragType='';dig.mode='now';}
function digMode(){modeNo=(++modeNo)%modes.length;mode=modes[modeNo];console.log("digMode",modeNo,mode);dragType='';dig.setMode(mode);}
function digSet(){console.log("digSet");}
function ontouchstart(evt){var touch=evt.targetTouches[0];evt.clientX=touch.clientX;evt.clientY=touch.clientY;evt.touchQ=true;onmouseDown(evt)}
function ontouchmove(evt){var touch=evt.targetTouches[0];evt.clientX=touch.clientX;evt.clientY=touch.clientY;evt.touchQ=true;onmouseMove(evt);evt.preventDefault();}
function ontouchend(){el2.addEventListener('touchstart',ontouchstart,false);window.removeEventListener("touchend",ontouchend,false);window.removeEventListener("touchmove",ontouchmove,false);}
function onmousemovePointer(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 angle=Math.atan2(mouseY-clock.center.y,clock.center.x-mouseX);angle-=3*Math.PI/2;if(angle<-2*Math.PI)angle+=2*Math.PI;var draggableQ=false;if(isNear(angle,clock.getHourAngle(),0.1)||isNear(angle,clock.getMinuteAngle(),0.1)){if(dist(mouseY-clock.center.y,clock.center.x-mouseX)>0))*60;clock.redraw();dig.setHH(clock.hh);dig.setMM(clock.mm);break;case 'min':angle1=Math.atan2(clock.center.y-mouseY,mouseX-clock.center.x);var mn=15-30*angle1/Math.PI;if(mn<0)mn+=60;clock.mm=mn;var oldHrMin=(clock.hh-(clock.hh>>0))*60;var diff=mn-oldHrMin;if(diff<-30)diff+=60;if(diff>30)diff-=60;clock.hh+=diff/60;if(clock.hh>12)clock.hh-=12
if(clock.hh<0)clock.hh+=12
clock.redraw();dig.setMM(mn);dig.setHH(clock.hh);dig.setMM(clock.mm);break;default:}}
function clockRadius(){return radius*1.03;}
function render(){if(dragType=='')clock.update();requestAnimationFrame(render);}
var Digital=function(canvas,w,h){this.g=canvas;this.ssQ=false;this.amQ=false;this.hh=0;this.mm=0;this.ss=0;if(this.ssQ){this.numHt=w*0.17;this.numWd=this.numHt*0.45;this.numGap=this.numHt*0.2;this.midX=w/2;this.midY=h/2;}else{this.numHt=w*0.27;this.numWd=this.numHt*0.45;this.numGap=this.numHt*0.2;this.midX=w/2;this.midY=h/2;}
this.typ='led';switch(this.typ){case 'lcd':this.bgClr='rgb(240,240,240)';this.edgeClr=this.bgClr;this.onClr='rgb(40,40,40)';this.offClr='rgb(230,230,230)';this.offClr=this.bgClr;break;case 'led':this.bgClr='rgb(50, 50, 0)';this.edgeClr='rgb(100, 255, 0)';this.onClr='rgb(100, 255, 0)';this.offClr='rgb(80, 80, 0)';break;default:}
this.numbers={n0:[1,1,1,0,1,1,1],n1:[0,0,1,0,0,1,0],n2:[1,0,1,1,1,0,1],n3:[1,0,1,1,0,1,1],n4:[0,1,1,1,0,1,0],n5:[1,1,0,1,0,1,1],n6:[0,1,0,1,1,1,1],n7:[1,0,1,0,0,1,0],n8:[1,1,1,1,1,1,1],n9:[1,1,1,1,0,1,1]};this.mode='now';};Digital.prototype.update=function(){switch(this.mode){case 'now':this.setTimeNow();this.redraw();break;default:}
requestAnimationFrame(this.update.bind(this));};Digital.prototype.redraw=function(){var g=this.g;g.fillStyle=this.bgClr;g.fillRect(0,0,innerWidth,innerHeight);var timeStr
if(this.ssQ){timeStr=this.hh+this.mm+this.ss;}else{timeStr=this.hh+this.mm;}
for(var i=0;i12)this.hh-=12;this.hh=this.hh.toString();if(this.hh==0){this.hh='12'}else if(this.hh.length==1){this.hh='0'+this.hh}
this.mm=time.getMinutes().toString();if(this.mm.length==1){this.mm='0'+this.mm}
this.ss=time.getSeconds().toString();if(this.ss.length==1){this.ss='0'+this.ss}};Digital.prototype.setMode=function(mode){this.mode=mode;console.log("Digital setMode",this.mode);};Digital.prototype.drawNum=function(pos,x,y,numArray){var xShift;if(this.ssQ){switch(pos){case 0:xShift=((this.numWd*3)+(this.numGap*2))*-1-20;break;case 1:xShift=((this.numWd*2)+(this.numGap*1))*-1-20;break;case 2:xShift=(this.numWd)*-1;break;case 3:xShift=this.numGap;break;case 4:xShift=(this.numWd)+(this.numGap*2)+20;break;case 5:xShift=(this.numWd*2)+(this.numGap*3)+20;break;}
xShift-=5;}else{xShift=(pos-3)*this.numWd+this.numGap*pos;xShift+=Math.floor(pos/2)*this.numGap*2;}
for(var i=0;i=12)this.hh-=12;this.mm=this.dt.getMinutes()+this.dt.getSeconds()/60;this.ss=this.dt.getSeconds()+this.dt.getMilliseconds()/1000;this.redraw();};Clock.prototype.redraw=function(){g2.clearRect(0,0,this.canvas.width,this.canvas.height);this.drawHourHand();this.drawMinuteHand();this.drawSecondHand();this.drawPin();};Clock.prototype.drawFace=function(){var faceGradient=g.createRadialGradient(this.center.x,this.center.y,0,this.center.x,this.center.y,this.radius);faceGradient.addColorStop(0,"rgba(255, 231, 180, 0)");faceGradient.addColorStop(1,"rgba(182, 157, 100, 0.3)");g.fillStyle=faceGradient;g.beginPath();g.arc(this.center.x,this.center.y,this.radius,0,2*Math.PI);g.fillStyle='#fff';g.fill();g.fillStyle=faceGradient;g.fill();var edgeGradient=g.createLinearGradient(0,this.center.y-this.radius,0,this.center.y+this.radius);edgeGradient.addColorStop(0,"#999");edgeGradient.addColorStop(0.5,"#eef");edgeGradient.addColorStop(1,"#999");g.fillStyle=edgeGradient;g.beginPath();g.arc(this.center.x,this.center.y,this.radius,0,2*Math.PI);g.closePath();g.arc(this.center.x,this.center.y,this.radius+(this.radius*0.075),0,2*Math.PI,true);g.shadowColor='rgba(0,0,0,0.7)';g.shadowBlur=this.radius*0.3;g.shadowOffsetY=1;g.fill();g.shadowBlur=0;g.shadowOffsetY=0;};Clock.prototype.drawPin=function(){g2.fillStyle='#888';g2.beginPath();g2.arc(this.center.x,this.center.y,4,0,2*Math.PI);g2.fill();};Clock.prototype.drawMinuteMarkers=function(){g.font=' '+((this.radius*this.faceOpt.numSizeRatio)>>0)+'px Arial';g.textAlign="center";g.fillStyle='#444';var i=1,dotSize=2;while(i<=60){var angle=(Math.PI*2)*(-i/60);var dotRad=this.radius*this.faceOpt.dotDistRatio;var dotX=Math.sin(angle)*dotRad+this.center.x;var dotY=Math.cos(angle)*dotRad+this.center.y;if(i%this.faceOpt.keyMarkers){dotSize=1;}else{dotSize=3;g.fillStyle="#004";g.strokeStyle='#fff';g.lineWidth=1;var numRad=this.radius*(1-this.faceOpt.numDistRatio);var numX=Math.sin(angle)*numRad+this.center.x;var numY=Math.cos(angle)*numRad+this.center.y+this.radius*0.09;var h=Math.floor(i/5)+6;if(h>12)h-=12;g.font=' '+((this.radius*this.faceOpt.numSizeRatio)>>0)+'px Arial';g.fillText(h.toString(),numX,numY);numX=Math.sin(angle)*numRad*0.7+this.center.x;numY=Math.cos(angle)*numRad*0.7+this.center.y+this.radius*0.05;if(0){h+=12;if(h==24)h='00';g.font=' '+((0.5*this.radius*this.faceOpt.numSizeRatio)>>0)+'px Arial';g.fillStyle='black';g.fillText(h.toString(),numX,numY);}}
g.fillStyle='#444';g.beginPath();g.arc(dotX,dotY,dotSize,0,Math.PI*2);g.fill();i++;}};Clock.prototype.getSecondAngle=function(){return Math.PI*2*(-this.ss/60);};Clock.prototype.drawSecondHand=function(){this.drawHand(this.getSecondAngle(),this.secondOpt);};Clock.prototype.getMinuteAngle=function(){return(Math.PI*2)*(-this.mm/60);};Clock.prototype.drawMinuteHand=function(){this.drawHand(this.getMinuteAngle(),this.minuteOpt);};Clock.prototype.getHourAngle=function(){return(Math.PI*2)*(-this.hh/12);};Clock.prototype.drawHourHand=function(){this.drawHand(this.getHourAngle(),this.hourOpt);};Clock.prototype.drawHand=function(angle,handOptions){var back=this.radius*handOptions.backwardRadiusRatio;var fore=this.radius*handOptions.forwardRadiusRatio;var thk=handOptions.thickRatio*this.radius/2;var sina=Math.sin(angle);var cosa=Math.cos(angle);var xys=[[-thk,back],[thk,back],[thk,-fore+thk*2],[0,-fore-thk*2],[-thk,-fore+thk*2]];for(var i=0,len=xys.length;i