Ext.namespace("Zarafa.widgets");
Zarafa.widgets.ClockWidget=Ext.extend(Zarafa.core.ui.widget.Widget,{updateTask:undefined,centerX:undefined,centerY:undefined,radius:undefined,margins:5,borderWidth:8,lineWidth:1,clockCanvas:undefined,handsCanvas:undefined,constructor:function(a){a=a||{};Ext.applyIf(a,{name:"clockwidget",title:_("Clock"),height:260});Zarafa.widgets.ClockWidget.superclass.constructor.call(this,a)},onRender:function(){Zarafa.widgets.ClockWidget.superclass.onRender.apply(this,arguments);this.clockCanvas=this.body.createChild({tag:"canvas",
style:"position: absolute;"});this.handsCanvas=this.body.createChild({tag:"canvas",style:"position: absolute;"})},initEvents:function(){Zarafa.widgets.ClockWidget.superclass.initEvents.apply(this,arguments);this.updateTask=Ext.TaskMgr.start({run:this.drawHands,interval:500,scope:this});this.mon(this,"bodyresize",this.redrawClock,this,{buffer:5});this.mon(this,"expand",this.redrawClock,this)},redrawClock:function(){this.calculatePosition();this.drawClock()},calculatePosition:function(){var a=this.body.getWidth(),
b=this.body.getHeight();this.centerX=Math.round(a/2)+this.lineWidth/2;this.centerY=Math.round(b/2)+this.lineWidth/2;this.radius=Math.min(this.centerX,this.centerY)-this.margins},drawClock:function(){this.drawBackground();this.drawLabels();this.drawHands()},drawBackground:function(){if(!(!this.isWidgetVisible()||this.collapsed)){var a=this.clockCanvas;Zarafa.resizeCanvas(a,this.body.getWidth(),this.body.getHeight());var b=a.dom.getContext("2d");b.save();b.circle(this.centerX,this.centerY,this.radius);
b.lineWidth=this.lineWidth;b.strokeStyle="#000000";b.stroke();a=b.createLinearGradient(0,0,0,this.radius*2);a.addColorStop(0,"#9f9f9f");a.addColorStop(0.2,"#afafaf");a.addColorStop(0.8,"#efefef");a.addColorStop(1,"#9f9f9f");b.fillStyle=a;b.fill();b.circle(this.centerX,this.centerY,this.radius-this.borderWidth);b.lineWidth=this.lineWidth;b.strokeStyle="#000000";b.stroke();a=b.createLinearGradient(0,0,0,this.radius*2-this.borderWidth);a.addColorStop(0,"#ffffff");a.addColorStop(0.2,"#e0e0e0");a.addColorStop(0.8,
"#efefef");a.addColorStop(1,"#ffffff");b.fillStyle=a;b.fill();b.beginPath();b.fillStyle="#000000";b.circle(this.centerX,this.centerY,2);b.fill();b.restore()}},drawLabelLine:function(a,b,c,d){b=b/2;c=c/2;a.save();a.translate(this.centerX,this.centerY);a.rotate(Math.PI/180*d);a.moveTo(-b,-this.radius-4);a.lineTo(-b,-this.radius+(4+c));a.lineTo(b,-this.radius+(4+c));a.lineTo(b,-this.radius-4);a.lineTo(-b,-this.radius-4);a.fillStyle="#000000";a.fill();a.restore()},drawLabels:function(){if(!(!this.isWidgetVisible()||
this.collapsed)){var a=this.clockCanvas.dom.getContext("2d");a.save();for(var b=0;b<60;b+=5)b%15===0?this.drawLabelLine(a,2,16,b*360/60):this.drawLabelLine(a,1,10,b*360/60);a.restore()}},drawHand:function(a,b,c,d,e){a.save();a.translate(this.centerX,this.centerY);a.rotate(Math.PI/180*b);a.beginPath();a.moveTo(0,0);a.lineTo(-(c/2),-10);a.lineTo(0,-d);a.lineTo(c/2,-10);a.lineTo(0,0);a.fillStyle=e||"#000000";a.fill();a.lineWidth=this.lineWidth;a.fillStyle="#000000";a.stroke();a.restore()},drawHands:function(){if(!(!this.isWidgetVisible()||
this.collapsed)){var a=this.handsCanvas,b;Zarafa.resizeCanvas(a,this.body.getWidth(),this.body.getHeight());a=a.dom.getContext("2d");a.save();b=new Date;var c=b.getSeconds(),d=b.getMinutes();b=(b.getHours()*60+d)*720/1440;this.drawHand(a,b,8,this.radius/1.8,"#000000");b=d*360/60;this.drawHand(a,b,8,this.radius/1.2,"#000000");b=c*360/60;this.drawHand(a,b,6,this.radius/1.1,"#ff0000");a.restore()}},onDestroy:function(){Zarafa.widgets.ClockWidget.superclass.onDestroy.apply(this,arguments);Ext.TaskMgr.stop(this.updateTask)}});
Zarafa.onReady(function(){Zarafa.supportsCanvas()&&container.registerWidget(Zarafa.widgets.ClockWidget,"clock",_("Clock"),"plugins/clockwidget/resources/images/clock.png")});
