
var Namespace = new function() {

    function PFCountdown() {
        this.init_widget();
        this.title = "PF 2012";
        this.last_date = new Date(2000, 0, 1);
        this.timezone_offset = 0;
        this.timezone_name = "Bratislava";
        this.timezone_tzname = "Europe/Bratislava";
    }

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

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

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

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

    PFCountdown.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);
        }
    }

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

    PFCountdown.prototype.set_settings = function(data) {
        var parts = data.split("__", 2);
        if (parts.length < 2) return;
        if (parts[0] != "PFCountdown") 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);
    }

    PFCountdown.prototype.build = function() {
        this.core_build();
        this.spolocne = document.createElement("div");
        set_class(this.spolocne, "spolocne");
        this.spolocne.setAttribute("style", "display: none; width: 180px;");
        this.cas = document.createElement("div");
        this.cas.setAttribute("id", "cas");
        this.hodina = document.createElement("span");
        set_class(this.hodina, "hodina");
        this.cas.appendChild(this.hodina);
        var dvojbodka1 = document.createElement("span");
        set_class(dvojbodka1, "dvojbodka");
        dvojbodka1.innerHTML = ":";
        this.cas.appendChild(dvojbodka1);
        this.minuta = document.createElement("span");
        set_class(this.minuta, "minuta");
        this.cas.appendChild(this.minuta);
        var dvojbodka2 = document.createElement("span");
        set_class(dvojbodka2, "dvojbodka");
        dvojbodka2.innerHTML = ":";
        this.cas.appendChild(dvojbodka2);
        this.sekunda = document.createElement("span");
        set_class(this.sekunda, "sekunda");
        this.cas.appendChild(this.sekunda);
        var stobodka = document.createElement("span");
        set_class(stobodka, "stobodka");
        stobodka.innerHTML = " . ";
        this.cas.appendChild(stobodka);
        this.milisek = document.createElement("span");
        set_class(this.milisek, "milisek");
        this.cas.appendChild(this.milisek);
        this.spolocne.appendChild(this.cas);

        this.new_year_text_before = document.createElement("span");
        set_class(this.new_year_text_before, "hidden");
        this.new_year_text_before.innerHTML = "Do 1. januára 2012 zostáva:";
        this.spolocne.appendChild(this.new_year_text_before);

        this.new_year_text_after = document.createElement("span");
        set_class(this.new_year_text_after, "hidden");
        this.new_year_text_after.innerHTML = "Štastný nový rok 2012 !";
        this.spolocne.appendChild(this.new_year_text_after);

        //this.timezonebox = document.createElement("a");
        //set_class(this.timezonebox, "timezonebox clickable");
        //this.timezonebox.onclick = callback(this.timezone_click, {bind: this})
        //this.timezonebox.innerHTML = this.timezone_name;
        //this.spolocne.appendChild(this.timezonebox);

        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.spolocne.appendChild(this.timezonebox);

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

    function zeroize(number, places) {
        var text = number + '';
        while (text.length < places) {
            text = '0' + text;
        }
        return text;
    }

    PFCountdown.prototype.timeservice_callback = function(ts) {
        var date = ts.ticked_date;
        this.update_values(date);
        this.last_date = date;
        if (this.next_trans) {
            if (this.next_trans_date < date) {
                this.next_trans = null;
                this.next_trans_date = null;
                this.timezone_offset = this.next_timezone_offset;
                var self = this;
                // in case of time shift, wait randomly 100 - 600 seconds before updating timezone data from server
                setTimeout(function() { self.refresh_timezone(self.timezone_tzname); }, (100 + Math.random(500)) * 1000);
            }
        }
    }

    PFCountdown.prototype.update_values = function(normdate) {
        var curtime = normdate.getTime();
        var date = new Date();
        date.setTime(curtime + this.timezone_offset * 1000);
        var pfdate = new Date(2012, 0, 1);
        pfdate.setTime(pfdate.getTime() + 3600 * 1000);
        var diff = pfdate - date;

        this.odhal();
        if (diff > 0) {
            set_class(this.hodina, 'hodina');
            set_class(this.minuta, 'minuta');
            set_class(this.sekunda, 'sekunda');
            set_class(this.new_year_text_before, 'new_year_text_before');
            set_class(this.new_year_text_after, 'hidden');
            //diff += 1000;
        } else {
            diff = date - pfdate;
            set_class(this.hodina, 'new_year_hodina');
            set_class(this.minuta, 'new_year_minuta');
            set_class(this.sekunda, 'new_year_sekunda');
            set_class(this.new_year_text_before, 'hidden');
            set_class(this.new_year_text_after, 'new_year_text_after');
        }
        this.hodina.innerHTML = zeroize(Math.floor(diff/1000/60/60), 2);
        this.minuta.innerHTML = zeroize(Math.floor((diff/1000/60) % 60), 2);
        this.sekunda.innerHTML = zeroize(Math.floor((diff/1000) % 60), 2);
        this.milisek.innerHTML = Math.floor((diff % 1000) / 100.0);
    }

/*
    PFCountdown.prototype.set_timezone = function(timezone_offset, timezone_name) {
        this.timezone_offset = timezone_offset - 3600;
        this.timezone_name = timezone_name;
        //this.timezonebox.innerHTML = timezone_name;
        this.set_timezone_box(timezone_name);
    }
*/

    PFCountdown.prototype.process_timezone_data = function(timezone_data) {
        var data = eval(timezone_data);
        this.timezone_offset = data.offset;
        this.next_trans = data.next_trans;
        this.next_trans_date = new Date(this.next_trans * 1000);
        this.next_timezone_offset = data.next_offset;
        this.set_timezone_box(this.timezone_name);
        var date = this.last_date;
        this.update_values(date);
        this.emit_settings_changed();
    }

    PFCountdown.prototype.refresh_timezone = function(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');
            }
        });
    }

    PFCountdown.prototype.change_timezone = function(timezone_name, timezone_tzname) {
        this.refresh_timezone(timezone_tzname);
        this.timezone_name = timezone_name;
        this.timezone_tzname = timezone_tzname;
        this.set_timezone_box('Načítavam...');
    }

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

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

    PFCountdown.prototype.odhal = function() {
        this.spolocne.style.display = "";
    }

    PFCountdown.prototype.set_timezone_box = function(name) {
        this.timezonebox_span.innerHTML = name + ' ▼';
    }

    return {
        PFCountdown: PFCountdown
    }
}

register_widget(Namespace.PFCountdown);

