﻿/*  address JavaScript framework, version 1.3.0
 *  (c)I.S.T Pro 2007-06-07
 *
 *  2007-10-04 setBound を追加
 */
var cLine;
var cAdrr;

function changePrefecture(map) {
    if(!cLine){
        cLine = new ClassXmlList3(prefecture, line, station);
    }
    if(!cAdrr){
        cAdrr = new ClassXmlList3(prefecture, city, area);
    }
    cLine.changeList1("line", map);
    cAdrr.changeList1("addr", map);
}

function changeCity(map) {
    cAdrr.changeList2(map);
}

function changeLine(map) {
    cLine.changeList2(map);
}

function changeArea(map) {
    cAdrr.changeList3(map);
}

function changeStation(map) {
    cLine.changeList3(map);
}

function ClassXmlList3(a1, a2, a3) {
    var dom = new ActiveXObject('microsoft.XMLDOM');
    dom.async = false;
    this.changeList1 = function (path, map) {
        clearOptions(a2);
        clearOptions(a3);
        var url = "../rent/data/map/" + path + "/" + path + "_" + a1.value + ".xml";
        new Ajax.Request(url, {
            method: "get",
            onSuccess: function(objHttp) {
                dom.loadXML(objHttp.responseText);
                setNodes(a2, dom, idp(a1) + idp(a2) + "/name");
                setBound(map, dom, idp(a1));
            },
            onFailure: function(objHttp) {
                dom.loadXML("");
                a2.options[0] = new Option("Error!!", "Error!!");
                window.status = "Error!! Can't read file[" + url + "]";
            }
        });
    };
    this.changeList2 = function (map) {
        clearOptions(a3);
        setNodes(a3, dom, idp(a1) + idv(a2) + idp(a3) + "/name");
        setBound(map, dom, idp(a1) + idv(a2));
    };
    this.changeList3 = function (map) {
        var ndl = dom.selectSingleNode(idp(a1) + idv(a2) + idv(a3));
        setLocate(map, ndl);
    };
}

function setBound(map, dom, path) {
    var nd = dom.selectSingleNode(path);
    var x1 = nd.selectSingleNode("x1");
    if(x1){
        var x2 = nd.selectSingleNode("x2");
        var y1 = nd.selectSingleNode("y1");
        var y2 = nd.selectSingleNode("y2");
        if((x1.text == x2.text) && (y1.text == y2.text))
            map.setCenter(new GLatLng(y1.text, x1.text), 13);
        else {
            var bd = new GLatLngBounds(new GLatLng(y1.text, x1.text), new GLatLng(y2.text, x2.text));
            var zm = map.getBoundsZoomLevel(bd);
            if(zm > 13)
                zm = 13;
            map.setCenter(bd.getCenter(), zm);
        }
    }
}

function idp(sel) {
    return "/" + sel.id;
}

function idv(sel) {
    return idp(sel) + "[name='" + sel.value + "']";
}

function setNodes(sel, xml, path) {
    var ndl = xml.selectNodes(path);
    for ( var i = 0; i < ndl.length; i++ ) {
        var att = ndl[i].getAttribute("value");
        if( att )
            sel.options[i] = new Option(ndl[i].text, att);
        else {
            sel.options[i] = new Option(ndl[i].text, ndl[i].text);
        }
    }
}

function setLocate(map, xml) {
    var y = xml.selectSingleNode("y").text;
    var x = xml.selectSingleNode("x").text;
    map.setCenter(new GLatLng(y, x), 13);
}

function clearOptions(obj) {
    var len = obj.options.length;
    for ( var i = 0; i < len; i++ ) {
        obj.options[0] = null;
    }
}
