﻿/*--------------------------------------------------------------------------
 * Copyright (c) 2006, drk<drk7jp@gmail.com>(http://www.drk7.jp/)
 *
 * License :
 *   Articstic License 2.0
 *
 * Drk7jpWeather.js :
 *   This library is a utility for Japan Weather Forecast XML (JWFX) (http://www.drk7.jp/weather/)
 *
 *--------------------------------------------------------------------------*/
var JWFX_NODEID       = 'jwfx';
var JWFX_MENU_NODEID  = 'jwfx_menu';
var JWFX_JWFX_DEFAULT_ID    = '46';
var JWFX_DEFAULT_XML   = '13';
var JWFX_DEFAULT_AREA  = '東京地方';
var JWFX_LOCATION_XML  = '13';
var JWFX_LOCATION_AREA = '東京地方';
var JWFX_COOKIE_NAME   = 'jwfx_locationid';
var JWFX_LOCATION_IDS = 
[
    {'id':46 ,'pref':'東京都' ,   'xml':'13',  'area':'東京地方'},
];


/*--------------------------------------------------------------------------
 * JWFX_Selecter
 *--------------------------------------------------------------------------*/
var JWFX_Selecter = Class.create();
JWFX_Selecter.prototype={

    initialize: function(){
        this.container=$(JWFX_MENU_NODEID);
        this.t_select = document.createElement('select');

        var selected_value = getCookie(JWFX_COOKIE_NAME);
/*        if(!selected_value) {
            selected_value     = JWFX_JWFX_DEFAULT_ID;
            JWFX_LOCATION_XML  = JWFX_DEFAULT_XML;
            JWFX_LOCATION_AREA = JWFX_DEFAULT_AREA;
        } else {
            JWFX_LOCATION_XML  = JWFX_LOCATION_IDS[selected_value-1].xml;
            JWFX_LOCATION_AREA = JWFX_LOCATION_IDS[selected_value-1].area;
        }*/

        new XML2JSON('http://www.drk7.jp/weather/xml/' + JWFX_LOCATION_XML + '.xml' , {container:JWFX_NODEID, template:this.template, id:0});

/*        for(var i=0;i<JWFX_LOCATION_IDS.length;i++){
            this.t_select.options[i]
                = new Option(JWFX_LOCATION_IDS[i].pref + '(' + JWFX_LOCATION_IDS[i].area + ')'
                            ,JWFX_LOCATION_IDS[i].id
                            ,false,false);
        }*/

/*        this.t_select.options[selected_value-1].selected=true;
        this.container.appendChild(this.t_select);
        Event.observe(this.t_select,'change', this.t_onchange.bindAsEventListener(this), false);
        Event.observe(this.t_select,'keydown',this.t_onchange.bindAsEventListener(this), false);
        Event.observe(this.t_select,'keyup',  this.t_onchange.bindAsEventListener(this), false);*/
    },
    template: function(context) {
        var html = '<table summary="" border="0" cellspacing="2" cellpadding="0">'
        + '<tbody>'
        + '<tr>'
        + '<td width="30" align="right">　</td>'
        + '<td width="50" align="center">天気</td>'
        + '<td width="65" align="center">気温</td>'
        + '<td width="60" align="center">降水</td>'
        + '<tr><td colspan="4" bgcolor="#808080"></td></tr>';

        var min = new Array(7);
        var max = new Array(7);
        var day = new Array(7);
        var p_max = new Array(7);
        var p_min = new Array(7);
        var elem = context.pref.area[JWFX_LOCATION_AREA];
        if(!elem) elem = context.pref.area;
        for(var i=0, length=elem.info.length; i<length; i++) {
            day[i]=elem.info[i].date.substr(8,10) + '日';
            min[i]='?'; if(elem.info[i].temperature.range[1].content!='[object Object]') {min[i]=elem.info[i].temperature.range[1].content;}
            max[i]='?'; if(elem.info[i].temperature.range[0].content!='[object Object]') {max[i]=elem.info[i].temperature.range[0].content;}

            if(String(min[i])=='undefined') {min[i]='?';}
            if(String(max[i])=='undefined') {max[i]='?';}

            p_max[i]='?';
            p_min[i]='?';
            if(i<2) {
                for(var j=0; j<4; j++) {
                    if(elem.info[i].rainfallchance.period[j].content!='[object Object]' && elem.info[i].rainfallchance.period[j].content!='--') {
                        if(p_max[i]=='?' || p_max[i]<elem.info[i].rainfallchance.period[j].content) {
                            p_max[i] = elem.info[i].rainfallchance.period[j].content;
                        }
                        if(p_min[i]=='?' || p_min[i]>elem.info[i].rainfallchance.period[j].content) {
                            p_min[i] = elem.info[i].rainfallchance.period[j].content;
                        }
                    }
                }
                if(String(p_max[i])=='undefined') {p_max[i]='?';}
                if(String(p_min[i])=='undefined') {p_min[i]='?';}
            }
        }

        html += '<tr><td width="30">' + day[0] + '</td>'
        + '<td width="50" align="center"><img src="' + elem.info[0].img + '" width="50" title="' + elem.info[0].weather_detail + '" /></td>'
        + '<td width="65" align="center"><font color="#0050F0">' + min[0] + '</font>～<font color="#FF0055">' + max[0] + '</font>℃</td>'
        + '<td width="60" align="center"><font color="#0050F0">' + p_min[0] + '～' + p_max[0] + '%</font></td></tr>'
        + '<tr><td width="30">' + day[1] + '</td>'
        + '<td width="50" align="center"><img src="' + elem.info[1].img + '" width="50" title="' + elem.info[1].weather_detail + '" /></td>'
        + '<td width="65" align="center"><font color="#0050F0">' + min[1] + '</font>～<font color="#FF0055">' + max[1] + '</font>℃</td>'
        + '<td width="60" align="center"><font color="#0050F0">' + p_min[1] + '～' + p_max[1] + '%</font></td></tr>'
        + '</table>';

        return html;
    },
    t_onchange :function() {
        var selected_value = $F(this.t_select);

        if(JWFX_LOCATION_XML == selected_value) { return; }
        JWFX_LOCATION_XML  = JWFX_LOCATION_IDS[selected_value-1].xml;
        JWFX_LOCATION_AREA = JWFX_LOCATION_IDS[selected_value-1].area;

        var expire = new Date('2010','01', '01');
        setCookie(JWFX_COOKIE_NAME, selected_value, expire);
        new XML2JSON('http://www.drk7.jp/weather/xml/' + JWFX_LOCATION_XML+ '.xml' , {container:JWFX_NODEID, template:this.template, id:0});
    }
};


/*--------------------------------------------------------------------------
 * Event
 *--------------------------------------------------------------------------*/
Event.observe(window, "load", function(){
    var d = $(JWFX_NODEID);
    $(JWFX_NODEID).innerHTML = 'now loading...';

    new JWFX_Selecter();
});




/*--------------------------------------------------------------------------
 * Cookie
 *--------------------------------------------------------------------------*/
function setCookie(name,value,expire){
    document.cookie = name + '=' + escape(value) + ((expire==null)?'':('; expires='+expire.toGMTString()));
}

function getCookie(name){
    var search = name + '=';
    if(document.cookie.length>0) {
        offset = document.cookie.indexOf(search);
        if(offset != -1) {
            offset += search.length;
            end     = document.cookie.indexOf(';',offset);
            if(end == -1) {
                end = document.cookie.length;
            }
            return unescape(document.cookie.substring(offset,end));
        }
    }
    return null;
}

