
/**
 * PollGadget
 */
NakazawaVillage.PollGadget = new Class({
    load: function(){
        var gadget = this;
        $("pollGadgetViewStatBtn").addEvent("click", function(e){
            new Event(e).stop();
            gadget.viewStat();
        });
        $("pollGadgetVoteBtn").addEvent("click", function(e){
            new Event(e).stop();
            gadget.vote();
        });
        $("pollGadgetBackBtn").addEvent("click", function(e){
            gadget.viewFrm();
        });
        this.stat();
    },
    viewFrm: function(){
        $("pollGadgetStatView").setStyle("display", "none");
        $("pollGadgetFrm").setStyle("display", "");
    },
    viewStat: function(){
        $("pollGadgetStatView").setStyle("display", "");
        $("pollGadgetFrm").setStyle("display", "none");
    },
    setStat: function(data){
        this.pollId = data.pollId;

        $("pollGadgetQuestion").set("html", data.title);

        var opts = $("pollGadgetOptions");
        var tbl = $("pollGadgetStatTbl");
        opts.empty();
        tbl.empty();
        data.stat.each(function(i){
            var label = new Element("label", {
                styles: {display: "block"}
            });
            var radio = new Element("input", {
                name: "vote",
                type: "radio",
                value: i.optionId
            });
            label.grab(radio).appendText(i.optionText);
            opts.adopt(label);

            var tr = new Element("tr");
            var th = new Element("th", {
                text: i.optionText
            });
            var share = new Element("td", {
                text: i.share + " %"
            });
            tr.adopt(th, share);
            tbl.adopt(tr);
        });
    },
    stat: function(){
        var gadget = this;
        new Request.JSON({
            url: "/gadget/",
            data: {
                "class": "poll",
                method: "stat"
            },
            onError: function(){
            },
            onSuccess: function(reply){
                if (reply.status != "OK"){
                    return;
                }
                gadget.setStat(reply.data);
            }
        }).send();
    },
    vote: function(answer){
        var gadget = this;
        var answer = $$("#pollGadgetFrm input[name=vote]:checked")[0];
        if (!answer){
            return;
        }
        answer = answer.value;
        new Request.JSON({
            url: "/gadget/",
            data: {
                "class": "poll",
                method: "vote",
                args: {
                    pollId: this.pollId,
                    optionId: answer
                }
            },
            onError: function(){
            },
            onSuccess: function(reply){
                if (reply.status != "OK"){
                    return;
                }
                gadget.setStat(reply.data);
                gadget.viewStat();
            }
        }).send();
    }
});

/**
 * WeatherGadget
 */
NakazawaVillage.WeatherGadget = new Class({
    load: function(){
        this.get();
    },
    get: function(){
        var gadget = this;
        new Request.JSON({
            url: "/gadget/",
            data: {
                "class": "weather", 
                method: "get"
            },
            onError: function(){
            },
            onSuccess: function(reply){
                if (reply.status != "OK"){
                    return;
                }
                gadget.setWeather(reply.data);
            }
        }).send();
    },
    setWeather: function(weather){
        var transCodes = ["all", "after", "occ"];
        var weatherCodes = ["fair", "cloudy", "rain", "thunder", "snow"];
        var left = -60 * (transCodes.indexOf(weather.code[0]) + 1);
        var top0 = -40 * weatherCodes.indexOf(weather.code[2]);
        var top1 = -40 * weatherCodes.indexOf(weather.code[1]);
        if (weather.high == "") weather.high = "－";
        if (weather.low == "")  weather.low = "－";
        $("weatherGadgetIcon0").style.backgroundPosition = left + "px " + top0 + "px";
        $("weatherGadgetIcon1").style.backgroundPosition = left + "px " + top1 + "px";
        //$("yahooWeatherBase").className = "yahooWeather" + b.code[0].capitalize();
        $('weatherGadgetForecast').set("text", weather.text.replace("後", "のち"));
        $('weatherGadgetHigh').set("text", weather.high);
        $('weatherGadgetLow').set("text", weather.low);
//        $("yahooWeatherIcon0").href = b.link;
    }
});


/**
 * RouteGadget
 */
NakazawaVillage.RouteGadget = new Class({
    load: function(){
        var gadget = this;
        $("routeGadgetDate").value = new Date().increment("day", 1).format("%Y/%m/%d");
        $("routeGadgetFrm").addEvent("submit", function(e){
            new Event(e).stop();
            gadget.route();
        });
        $$("#routeGadgetTransport input").addEvent("click", function(e){
            gadget.transportChanged();
        });
        this.transportChanged();
    },
    getTransport: function(){
        var checked = $$("#routeGadgetTransport input:checked");
        return checked.length? checked[0].value: null;
    },
    transportChanged: function(){
        var v = this.getTransport() == "train"? "visible": "hidden";
        $("routeGadgetTimeGroup").setStyle("visibility", v);
    },
    route: function(){
        var from = $("routeGadgetFrom").value;
        if (from == ""){
            $("routeGadgetFrom").highlight("#ffcc66");
            return;
        }
        var transport = this.getTransport();
        this.log(from, transport);

        var url = "http://maps.google.co.jp/maps?";
        url += "saddr=" + encodeURIComponent(from) + "&";
        url += "daddr=" + encodeURIComponent("群馬県吾妻郡草津町大字草津６１８ (中沢ヴィレッジ)") + "&";
        if (transport == "train"){
            url += "date=" + encodeURIComponent($("routeGadgetDate").value) + "&";
            url += "time=" + encodeURIComponent($("routeGadgetTime").value) + "&";
            url += "ttype=" + $("routeGadgetTimeType").value + "&";
        }
        else {
            url += "dirflg=d&";
        }
        open(url, "_blank");
    },
    log: function(from, transport){
        new Request.JSON({
            url: "/gadget/",
            data: {
                "class": "route",
                method: "log",
                args: {
                    from: from,
                    transport: transport
                }
            }
        }).send();
    }
});

(function(){
    with (NakazawaVillage) {
        new PollGadget().load();
        new WeatherGadget().load();
        new RouteGadget().load();
    }
})();

