﻿// JavaScript Document
var times_c;
function Clock(id, size)
{ }
Clock.prototype.init = function(times) { }
function Clock1(id, size) {
    this.ver = "[Free Code] Clock v1.2 by MK in MKing's Kindom";
    this.container = mk.$(id);

    this.canvases = this.container.getElementsByTagName("canvas");


    try {
        if ($.browser.msie) {
            // IE9 supports canvas therefore G_vmlCanvasManager is not defined
            if (G_vmlCanvasManager) {
                for (var i = 0; i < this.canvases.length; i++) {
                    G_vmlCanvasManager.initElement(this.canvases[i]);
                }
            }
        }

        this.ctxes = [this.canvases[0].getContext("2d")
        , this.canvases[1].getContext("2d")
        , this.canvases[2].getContext("2d")
        , this.canvases[3].getContext("2d")
    ];
    } catch (eee) {
        //alert(eee);

        return;
    }

    this.size = size || 65;
    this.canvases[0].width = this.canvases[0].height
		= this.canvases[1].width = this.canvases[1].height
		= this.canvases[2].width = this.canvases[2].height
		= this.canvases[3].width = this.canvases[3].height
		= this.size;
    this.canvases[0].style.width = this.canvases[0].style.height
		= this.canvases[1].style.width = this.canvases[1].style.height
		= this.canvases[2].style.width = this.canvases[2].style.height
		= this.canvases[3].style.width = this.canvases[3].style.height
		= this.size + "px";
    this.canvases[0].style.position
		= this.canvases[1].style.position
		= this.canvases[2].style.position
		= this.canvases[3].style.position
		= "absolute";

    //this.secondStep = 6;/*秒针移动的速度1为1秒移动一下，6仿机械表*/

    this.__radio__ = this.size / 2;
    this.__secondPoint__ = { x: 0, y: 0, angel: 0, _x: 0, _y: 0 }
    this.__hourSecond__ = 60 * 60;

    this.__secondHandLengthOffset__ = 10;
    this.__secondHandThick__ = 1;
    this.__secondHandTailSplit__ = 3;

    this.__minuteHandTailSplit__ = 5;

    this.__hourHandTailSplit__ = 6;

    //this.__drawBack__(this.ctxes[0]);
}
Clock1.prototype.init = function(times) {
    times_c = times;
    if (this.ctxes == undefined || this.ctxes == null) return;
    if (this.ctxes.length < 3) return;
    var _this = this;
    //var t = 0;
    var thour = times[3];
    //var am_pm = "am";
    //if (typeof (TimeAbb_am) != "undefined")
    //    am_pm = TimeAbb_am;
    setInterval(function() {
        times_c[1] += 1;
        if (times_c[1] >= 60) { times_c[1] = 0; times_c[2]++; }
        if (times_c[2] >= 60) { times_c[2] = 0; times_c[3]++; }
        if (times_c[3] > 23) { times_c[3] = 0; }
//        if (times_c[3] >= 12) {
//            am_pm = TimeAbb_pm;
//        }
//        else {
//            am_pm = TimeAbb_am;
//        }

//        thour = times_c[3];
//        if (thour > 12)
//            thour = thour - 12;

        // if (t == 0) {
//        mk.$("h").innerHTML = thour < 10 ? "0" + thour : thour;
//        mk.$("m").innerHTML = times_c[2] < 10 ? "0" + times_c[2] : times_c[2];
//        mk.$("pm_am").innerHTML = am_pm;
//        mk.$("colon").innerHTML = ":";
        //  t++;
        //        } else {
        //            mk.$("h").innerHTML = thour < 10 ? "0" + thour : thour;
        //            mk.$("m").innerHTML = times[2] < 10 ? "0" + times[2] : times[2];
        //            mk.$("pm_am").innerHTML = am_pm;
        //            mk.$("colon").innerHTML = "";
        //            t--;
        //        }

        var angels = _this.__getSecondAngel__(times_c[0], times_c[1], times_c[2], times_c[3]);
        _this.__handTool__(_this.ctxes[1]
        //, _this.__radio__ - _this.__secondHandLengthOffset__ * 5
						   , _this.__radio__ * .7 - _this.__secondHandLengthOffset__
						   , angels[2]
						   , _this.__secondHandThick__ * 3
						   , _this.__radio__
						   , _this.__radio__
						   , _this.__hourHandTailSplit__
						   , "#000"
						   , "#000"
						   );
        _this.__handTool__(_this.ctxes[2]
        //, _this.__radio__ - _this.__secondHandLengthOffset__ * 3.5
						   , _this.__radio__ * .85 - _this.__secondHandLengthOffset__
						   , angels[1]
						   , _this.__secondHandThick__ * 2
						   , _this.__radio__
						   , _this.__radio__
						   , _this.__minuteHandTailSplit__
						    , "#000"
						    , "#000"
						   );
        _this.__handTool__(_this.ctxes[3]
						   , _this.__radio__ - _this.__secondHandLengthOffset__
						   , angels[0]
						   , _this.__secondHandThick__
						   , _this.__radio__
						   , _this.__radio__
						   , _this.__secondHandTailSplit__
						    , "#f38385"
						    , "#f38385"
						   );
        if ($.browser.msie) {
            var cdiv = $("canvas div"); //this.container.getElementsByTagName("canvas");
            cdiv.css("width", "65px");
            cdiv.css("height", "65px");
        }

    }, 1000);
}



Clock1.prototype.__handTool__ = function(ctx, len, angel, thick, offsetX, offsetY, tailSplit, color, color1) {
    var point = mk.getRadioPosition(len, angel, offsetX, offsetY);
    var point0 = mk.getRadioPosition(thick, angel - Math.PI / 2, offsetX, offsetY);
    var point1 = mk.getRadioPosition(thick, angel + Math.PI / 2, offsetX, offsetY);
    var pointTail = mk.getRadioPosition(len / tailSplit, angel + Math.PI, offsetX, offsetY);
    ctx.clearRect(0, 0, this.size, this.size);
    ctx.strokeStyle = color1; //"#f38385";
    ctx.fillStyle = color; //"#000";
    ctx.beginPath();
    ctx.moveTo(pointTail.x, pointTail.y);
    ctx.lineTo(point0.x, point0.y);
    ctx.lineTo(point.x, point.y);
    ctx.lineTo(point1.x, point1.y);

    ctx.closePath();
    ctx.fill();
    ctx.stroke();
    ctx.moveTo(0, 0);
}
Clock1.prototype.__getSecondAngel__ = function(ms, s, m, h) {
    s = s * 1000 + ms;
    m = m + s / 60000;
    h = h + m / 60;
    var angelSecond = Math.PI * s / 30000 - Math.PI / 2;
    var angelMinute = Math.PI * m / 30 - Math.PI / 2;
    var angelHour = Math.PI * h / 6 - Math.PI / 2;
    return [angelSecond, angelMinute, angelHour];
}




/*
Clock.prototype.__drawBack__ = function(ctx){
var subMinuteAngel = Math.PI / 120;
var minuteAngel = Math.PI / 30;
var hourAngel = Math.PI / 6;
ctx.strokeStyle = "#999";
ctx.fillStyle = "#fff";
	
ctx.lineWidth = .5;
for(var i=0; i<240; i++){
var p = mk.getRadioPosition(this.__radio__, i * subMinuteAngel, this.__radio__, this.__radio__);
ctx.beginPath();
ctx.moveTo(this.__radio__, this.__radio__);
		
ctx.lineTo(p.x, p.y);
ctx.closePath();
ctx.stroke();
}
ctx.beginPath();
ctx.moveTo(this.__radio__, this.__radio__);
ctx.arc(this.__radio__, this.__radio__, this.__radio__ * .95, 0, Math.PI * 2, false);
ctx.closePath();
ctx.fill();
	
ctx.lineWidth = 1;
for(var i=0; i<60; i++){
var p = mk.getRadioPosition(this.__radio__, i * minuteAngel, this.__radio__, this.__radio__);
ctx.beginPath();
ctx.moveTo(this.__radio__, this.__radio__);
		
ctx.lineTo(p.x, p.y);
ctx.closePath();
ctx.stroke();
}
ctx.beginPath();
ctx.moveTo(this.__radio__, this.__radio__);
ctx.arc(this.__radio__, this.__radio__, this.__radio__ * .9, 0, Math.PI * 2, false);
ctx.closePath();
ctx.fill();
	
ctx.lineWidth = 2;
for(var i=0; i<12; i++){
var p = mk.getRadioPosition(this.__radio__, i * hourAngel, this.__radio__, this.__radio__);
ctx.beginPath();
ctx.moveTo(this.__radio__, this.__radio__);
ctx.lineTo(p.x, p.y);
ctx.closePath();
ctx.stroke();
}
ctx.beginPath();
ctx.moveTo(this.__radio__, this.__radio__);
ctx.arc(this.__radio__, this.__radio__, this.__radio__ * .85, 0, Math.PI * 2, false);
ctx.closePath();
ctx.fill();
}*/
