
var Namespace = new function() {

    function AnalogClock() {
        this.init_widget();
        this.title = "Ručičkové";
        this.last_date = new Date(2000, 0, 1);
        this.timezone_offset = 0;
        this.timezone_name = "Bratislava";
        this.timezone_tzname = "Europe/Bratislava";
        this.timezonebox = {}
    }

    AnalogClock.prototype = new Widget;
    AnalogClock.name = "AnalogClock";

    AnalogClock.prototype.init = function(div_name) {
        this.div_name = div_name;
    }

    AnalogClock.prototype.start = function() {
        timeservice.add_listener(this);
    }

    AnalogClock.prototype.stop = function() {
        timeservice.remove_listener(this);
    }

    AnalogClock.prototype.set_initial_values = function(dct) {
        if (dct.timezone_offset != 'undefined') this.timezone_offset = dct.timezone_offset;
        if (dct.timezone_name != 'undefined') {
            this.timezone_name = dct.timezone_name;
            this.set_timezone_box(dct.timezone_name);
        }
        if (dct.timezone_tzname != 'undefined') {
            this.timezone_tzname = dct.timezone_tzname;
            this.change_timezone(this.timezone_name, this.timezone_tzname);
        }
    }

    AnalogClock.prototype.get_settings = function() {
        var data = "AnalogClock__";
        data += this.timezone_offset + ":";
        data += this.timezone_name + ":";
        data += this.timezone_tzname;
        return escape(data);
    }

    AnalogClock.prototype.set_settings = function(data) {
        var parts = data.split("__", 2);
        if (parts.length < 2) return;
        if (parts[0] != "AnalogClock") return;
        parts = unescape(parts[1]).split(":");
        if (parts.length < 3) return;
        this.timezone_offset = parseInt(parts[0]);
        this.timezone_name = parts[1];
        this.timezone_tzname = parts[2];
        this.set_timezone_box(this.timezone_name);
        this.change_timezone(this.timezone_name, this.timezone_tzname);
    }

    AnalogClock.prototype.canvas_init = function(element) {
        if (G_vmlCanvasManager != undefined) { // ie IE
            G_vmlCanvasManager.initElement(element);
        }
    }

    AnalogClock.prototype.build = function() {
        this.core_build();

        if (IE) {
            var notice_div = document.createElement("div");
            notice_div.style.position = "absolute";
            notice_div.style.width = "230px";
            notice_div.style.height = "230px";
            notice_div.style.left = "10px";
            notice_div.style.top = "8px";
            notice_div.style.color = "#555";
            notice_div.style.fontSize = "11px";
            notice_div.style.fontFamily = 'Arial';
            notice_div.style.textAlign = "justify";
            var text = 'S lútosťou Vám oznamujeme, že Váš prehliadač je mierne postihnutý a štandard HTML 5 mu robí vážne problémy. ';
            text += 'Ručičkové hodinky sa preto nemohli zobraziť. Našťastie tento problém sa dá vyliečit tak, že Váš počítač nakŕmite zdravým software. ';
            text += 'Až 80% našich návštevníkov používa prehliadač FireFox. Zdravá výživa pre Váš PC.<br/><br/>';
            text += '<div style="position: relative; left: 25px;">';
            text += '<a href="http://www.mozilla-europe.org/sk/firefox/"><img src="images/FireFox100.jpg" style="border: 0px;"/></a>';
            text += '</div>';
            notice_div.innerHTML = text;
            this.canvas_div.appendChild(notice_div);
            return;
        }

        this.canvas = document.createElement("canvas");
        this.canvas_div.appendChild(this.canvas);

        this.canvas.setAttribute("width", 200);
        this.canvas.setAttribute("height", 200);

        this.canvas.style.width = "200px";
        this.canvas.style.height = "200px";
        this.canvas.style.position = "absolute";
        this.canvas.style.left = "25px";
        this.canvas.style.top = "10px";

        if (IE) {
            this.canvas_init(this.canvas);
        }

        this.ctx = this.canvas.getContext("2d");
        this.ctx.scale(1, 1);

        this.timezonebox = document.createElement("span");
        set_class(this.timezonebox, "timezonebox");
        this.timezonebox_a = document.createElement("a");
        set_class(this.timezonebox_a, "button");
        this.timezonebox_a.onclick = callback(this.timezone_click, {bind: this})
        this.timezonebox_span = document.createElement("span");
        this.set_timezone_box(this.timezone_name);
        this.timezonebox_a.appendChild(this.timezonebox_span);
        this.timezonebox.appendChild(this.timezonebox_a);

        this.canvas_div.appendChild(this.timezonebox);
    }

    AnalogClock.prototype.update_values = function(normdate) {
        if (IE) return;

        var curtime = normdate.getTime();
        var date = new Date(curtime + this.timezone_offset * 1000);

        this.render(date.getUTCHours(), date.getUTCMinutes(), date.getUTCSeconds());
        this.odhal();
        // tu kresli
    }

    AnalogClock.prototype.timeservice_callback = function(ts) {
        var date = ts.ticked_date;
        if (date.getUTCSeconds() == this.last_date.getUTCSeconds()) {
            return;
        }
        this.update_values(date);
        this.last_date = date;
    }

    AnalogClock.prototype.odhal = function() {
        //this.canvas.style.display = "";
    }

    AnalogClock.prototype.process_timezone_data = function(timezone_data) {
        var data = eval(timezone_data);
        this.timezone_offset = data.offset;
        this.next_trans = data.next_trans;
        this.set_timezone_box(this.timezone_name);
        var date = this.last_date;
        this.update_values(date);
        this.emit_settings_changed();
    }

    AnalogClock.prototype.change_timezone = function(timezone_name, timezone_tzname) {
        jQuery.ajax({
            url: '/get_tz?tzname=' + timezone_tzname,
            context: this,
            success: function(data) {
                this.process_timezone_data(data);
            },
            error: function(jqXHR, status, error_thrown) {
                this.set_timezone_box('Chyba');
            }
        });

        this.timezone_name = timezone_name;
        this.set_timezone_box('Načítavam...');
    }

    AnalogClock.prototype.timezone_chosen = function(originator) {
        this.change_timezone(originator.timezone_name, originator.timezone_tzname);
    }

    AnalogClock.prototype.timezone_click = function(evt) {
        new TimeZoneChooser(this.canvas_div, callback(this.timezone_chosen, {bind: this}));
    }

    AnalogClock.prototype.set_timezone_box = function(name) {
        // in case of IE, this element was not created.. there is an IE-bashing text instead :)
        if (! this.timezonebox_span) return;
        this.timezonebox_span.innerHTML = name + ' ▼';
    }

    AnalogClock.prototype.fullCircleAt = function(x, y, radius, fg_color, bg_color) {
        with (this.ctx) {
            save();
            globalAlpha = 1;
            lineWidth = 1;
            beginPath();
            arc(x, y, radius, 0, 2*Math.PI, false);
            if (bg_color) {
                fillStyle = bg_color;
                fill();
            }
            strokeStyle = '#777777';
            stroke();
            restore();
        }
    }

    AnalogClock.prototype.radialLineAtAngle = function(angle_fraction, line_width, start_at, end_at, color) {
        with (this.ctx) {
            save();
            translate(100, 100);
            rotate(Math.PI * (2 * angle_fraction - 0.5));
            globalAlpha = 1;
            strokeStyle = color;
            lineWidth = line_width;
            beginPath();
            moveTo(start_at, 0)
            lineTo(end_at, 0);
            stroke();
            restore();
        }
    }

    AnalogClock.prototype.render = function(hour, min, sec) {
        this.ctx.clearRect(0, 0, 200, 200);

        if ((hour >=6) && (hour <= 20)) {
            var fg_color = '#000000';
            var fg_color2 = '#777777';
            var bg_color = '#f0f0f0';
        } else {
            var fg_color = '#ffffff';
            var fg_color2 = '#444444';
            var bg_color = '#000000';
        }

        this.fullCircleAt(100, 100, 95, fg_color2, bg_color);

        for (var i=0; i<60; i++)
            if (i % 5)
                this.radialLineAtAngle(i/60, 2, 89, 93, fg_color);
            else
                this.radialLineAtAngle(i/60, 4, 80, 93, fg_color);

        this.radialLineAtAngle((hour+min/60)/12, 8, -15, 50, fg_color);
        this.radialLineAtAngle((min+sec/60)/60, 7, -15, 75, fg_color);
        this.radialLineAtAngle(sec/60, 1, -20, 85, fg_color);
    }

    return {
        AnalogClock: AnalogClock
    }
}

register_widget(Namespace.AnalogClock);

