﻿function FunctionVirto()
{
}

FunctionVirto.createDelegate = function FunctionVirto$createDelegate(instance, method) {
    return function() {
        return method.apply(instance, arguments);
    }
}


VirtoClockAndWeather = function()
{
    this.arraySecond = new Array();
    this.arrayMinutes = new Array();
    this.arrayHours = new Array();

    this.radius = 42;
    this.hourRadius = 25;

    this.cX = 52;
    this.cY = 52;

    this.angle = Math.PI*90/180 - Math.PI;
    this.delta = 2*Math.PI / 60;
    this.deltaH = 2*Math.PI / 12;

    this.currentS = null;
    this.currentM = null;
    this.currentH = null;
    this.oldS = null;
    this.oldM = null
    this.oldH = null;
    
    this.containerId = null;
    this.timeZoneOffset = null;
    
    this.digitalClock = null
    this.showSeconds = null;
}

VirtoClockAndWeather.prototype.constructor = VirtoClockAndWeather.prototype;

VirtoClockAndWeather.prototype =
{
    initializeClock: function(containerId, radS, radM, radH, cssClassS, cssClassM, cssClassH, cx, cy, timeZoneOffset, digitalClock, showSeconds)
    {
        VirtoClockAndWeather.call(this);
        
	    if (typeof (cx) !== 'undefined')
		    this.cX = cx;
        if (typeof (cy) !== 'undefined')
		    this.cY = cy;

		this.digitalClock = digitalClock;
		this.showSeconds = showSeconds;
		    
		this.containerId = containerId;
		this.timeZoneOffset = timeZoneOffset;

	    var d = new Date();

	    this.oldS = d.getUTCSeconds();
	    this.oldM = d.getUTCMinutes();
	    this.oldH = d.getUTCHours() + this.timeZoneOffset;

	    if (this.digitalClock !== 'true')
	    {
	        this.createSecPointer(radS, cssClassS);
	        this.createMinutesPointer(radM, cssClassM);
	        this.createHourPointer(radH, cssClassH);
	    
	        this.recountArray(this.arraySecond, this.oldS, this.delta);
	        this.recountArray(this.arrayMinutes, this.oldM, this.delta);
	        this.recountArray(this.arrayHours, this.oldH, this.deltaH);

	        window.setInterval(FunctionVirto.createDelegate(this, this.recountArrays), 1000);
	    }
	    else
	    {
	        if (this.showSeconds === 'true')
	            window.setInterval(FunctionVirto.createDelegate(this, this.recountDigital), 1000);
	        else
	        {
	            this.recountDigital(true);
	            window.setInterval(FunctionVirto.createDelegate(this, this.recountDigital), 10000);
	        }
	    }
    },
    
    makeDouble: function(str)
    {
        if (str.toString().length < 2)
        {
         return "0" + str;
        }
        
        return str;
    },
    
    recountDigital: function(forceShow)
    {
        var d = new Date();

        this.currentS = d.getUTCSeconds();
        this.currentM = d.getUTCMinutes();
        this.currentH = d.getUTCHours() + this.timeZoneOffset;

        if (forceShow || (this.oldM != this.currentM && this.showSeconds === 'false'))
        {
            var _curDate = this.makeDouble(this.currentH) + ' : ' + this.makeDouble(this.currentM);
            var obj = document.getElementById(this.containerId);
            if (obj) {
                obj.innerHTML = _curDate;
                obj.style.left = '70px';
            }

            this.oldM = this.currentM;
        }

	    if (this.oldS != this.currentS && this.showSeconds === 'true')
	    {
            var _curDate = this.makeDouble(this.currentH) + ' : ' + this.makeDouble(this.currentM) + ' : ' + this.makeDouble(this.currentS);
            var obj = document.getElementById(this.containerId);
            if (obj)
            {
                obj.innerHTML = _curDate;
            }

            this.oldS = this.currentS;
        }
    },
    
    createSecPointer: function(rad, css)
    {
        var obj = document.getElementById(this.containerId);
    	for (var i = 0; i < rad; i++)
    	{
		    var newDiv = document.createElement('div');
		    if (css)
			    newDiv.className = css;
		    else
			    newDiv.className = 'secondPointer';
		    this.arraySecond.push(newDiv);
    		
		    if (obj)
			    obj.appendChild(newDiv);
	    }
    },

    createMinutesPointer: function(rad, css)
    {
        var obj = document.getElementById(this.containerId);
	    for (var i = 0; i < rad; i++)
	    {
		    var newDiv = document.createElement('div');
		    if (css)
			    newDiv.className = css;
		    else
			    newDiv.className = 'minutesPointer';

		    this.arrayMinutes.push(newDiv);
    		
		    if (obj)
			    obj.appendChild(newDiv);
	    }
    },

    createHourPointer: function(rad, css)
    {
        var obj = document.getElementById(this.containerId);
	    for (var i = 0; i < rad; i++)
	    {
		    var newDiv = document.createElement('div');
		    if (css)
			    newDiv.className = css;
		    else
			    newDiv.className = 'hourPointer';

		    this.arrayHours.push(newDiv);
    		
		    if (obj)
			    obj.appendChild(newDiv);
	    }
    },


    recountArray: function(arr, counter, dAngle)
    {
	    for (var i = 0; i < arr.length; i++)
	    {
		    arr[i].style.left = this.cX + Math.round(Math.cos(this.angle + dAngle*counter)*i) + 'px';
		    arr[i].style.top = this.cY + Math.round(Math.sin(this.angle + dAngle*counter)*i) + 'px';
	    }
    },

    recountArrays: function()/*arrS, arrM, arrH*/
    {
	    var d = new Date();

	    this.currentS = d.getUTCSeconds();
	    this.currentM = d.getUTCMinutes();
	    this.currentH = d.getUTCHours() + this.timeZoneOffset;

	    if (this.oldS != this.currentS)
	    {
	        this.recountArray(this.arraySecond, this.currentS, this.delta);
	        this.oldS = this.currentS;
	    }

	    if (this.oldM != this.currentM)
	    {
	        this.recountArray(this.arrayMinutes, this.currentM, this.delta);
	        this.oldM = this.currentM;
	    }

	    if (this.oldH != this.currentH)
	    {
	        this.recountArray(this.arrayHours, this.currentH, this.deltaH);
	        this.oldH = this.currentH;
	    }
    }
}