/* Copyright © 2009 Whitestone Oy - All rights reserved */

/**
 * a Clock object that updates an analog clock
 *
 * @param el        a DIV element which is used as the clock image (background-image must be a correct clock texture)
 * @param interval  how often is the time updated on screen
 */
function Clock(el, interval)
{
    var self = this;

    /* defaults */
    self.width = false;
    self.height = false;
    self.hour = -1;
    self.min = -1;
    self.interval = 1000;
    self.timer = false;
    self.image = false;
    self.time = -1;
    self.analog = true;
    self.text = true;

    if(typeof interval == 'number')
        self.interval = interval;

    /* initialize analog clock elements */
    try {
        /* kind of unrealible way */
        self.width = el.clientWidth;
        self.height = el.clientHeight;

        if(el.currentStyle) {
            self.image = el.currentStyle.backgroundImage;
        } else {
            self.image = getComputedStyle(el, '').getPropertyValue('background-image');
        }

        self.el_c = el;
        self.el_c.style.backgroundImage = self.image;
        self.el_c.style.backgroundPosition = '0px 0px';
        self.el_c.style.backgroundRepeat = 'no-repeat';
        self.el_h = document.createElement('div');
        self.el_m = document.createElement('div');

        self.el_h.style.width = '100%';
        self.el_h.style.height = '100%';
        self.el_h.style.backgroundImage = self.image;
        self.el_h.style.backgroundPosition = '0px 0px';
        self.el_h.style.backgroundRepeat = 'no-repeat';

        self.el_m.style.width = '100%';
        self.el_m.style.height = '100%';
        self.el_m.style.backgroundImage = self.image;
        self.el_m.style.backgroundPosition = '0px 0px';
        self.el_m.style.backgroundRepeat = 'no-repeat';

        self.el_h.appendChild(self.el_m);
        self.el_c.appendChild(self.el_h);
    } catch(e) {
        dprint("No analog clock. Ok.");
        self.analog = false;
    }

    /* text elements */
    self.el_hour = document.getElementById('serverTime_hour');
    self.el_minute = document.getElementById('serverTime_minute');
    self.el_wday = document.getElementById('serverTime_wday');
    self.el_day = document.getElementById('serverTime_day');
    self.el_month = document.getElementById('serverTime_month');
    self.el_year = document.getElementById('serverTime_year');

    self.run = function() {
        self.stop();
        self.timer = setInterval(self.tick, self.interval);
    }

    self.stop = function() {
        if(self.timer) {
            clearInterval(self.timer);
            self.timer = false;
            self.time = -1;
        }
    }

    self.setTime = function(newTime) {
        if((newTime * 1000) == self.time)
            return; /* no need to update it if it's correct, duh */

        self.time = newTime * 1000;

        /* quick, update our clock! */
        self.date = new Date(self.time);
        self.hour = self.date.getUTCHours();
        self.min = self.date.getUTCMinutes();
        self.update();
    }

    self.tick = function() {
        /* if we don't have a time, don't tick */
        if(self.time < 0)
            return;

        /* tick should happen excatly between intervals */
        self.time+=self.interval;
        self.date = new Date(self.time);

        var hour = false;
        var min = false;

        hour = self.date.getUTCHours();
        min = self.date.getUTCMinutes();

        if(self.min != min) {
            self.hour = hour;
            self.min = min;
            self.update();
        }
    }

    /* javascript doesn't have a number formatting function... */
    self.tenpad = function(val)
    {
        if(typeof val == 'number')
            if(val < 10)
                val = '0'+val;

        return val;
    }

    self.strwday = function(num)
    {
        if(num == 0) return lang("SUNDAY");
        if(num == 1) return lang("MONDAY");
        if(num == 2) return lang("TUESDAY");
        if(num == 3) return lang("WEDNESDAY");
        if(num == 4) return lang("THURSDAY");
        if(num == 5) return lang("FRIDAY");
        if(num == 6) return lang("SATURDAY");
        return 'n/a';
    }

    self.update = function() {
        // local temp variables!
        var hour = self.hour;
        var min = self.min;

        if(self.analog) {
            // change between AM and PM clocks
            if(hour > 11) {
                hour = hour - 12;
                self.el_c.style.backgroundPosition = '0px -'+self.height+'px';
            } else {
                self.el_c.style.backgroundPosition = '0px 0px';
            }

            hour = (hour * 5) + parseInt((5 * (min / 60)));

            hour = (self.width * hour) + self.width;
            min = (self.width * min) + self.width;

            self.el_h.style.backgroundPosition = '-'+hour+'px 0px';
            self.el_m.style.backgroundPosition = '-'+min+'px -'+self.height+'px';
        }

        try { self.el_hour.innerHTML = self.tenpad(self.date.getUTCHours()); } catch(e) { }
        try { self.el_minute.innerHTML = self.tenpad(self.date.getUTCMinutes()); } catch(e) { }
        try { self.el_wday.innerHTML = self.strwday(self.date.getUTCDay()); } catch(e) { }
        try { self.el_day.innerHTML = self.tenpad(self.date.getUTCDate()); } catch(e) { }
        try { self.el_month.innerHTML = self.tenpad(self.date.getUTCMonth()+1); } catch(e) { }
        try { self.el_year.innerHTML = self.date.getUTCFullYear(); } catch(e) { }
    }
}
