var map;
var bound;

// Area Change
function DefaultView(i) {
    var mode;
    if (i.match(/^[A-Za-z]{1}[0-9]+$/)) {
        mode = "id";
    } else if (i.match(/^[0-9]+$/) && i != 1) {
        mode = "num";
    } else {
        mode = "cities";
    }
    sendRequest(
        load,
        '&' + mode + '=' + i,
        'GET',
        './interface.cgi',
        true,
        true
    );
}

// SELECT Options
function AreaChange(i, theForm) {
    MessageDel();
    map.clearOverlays(); // INSERT 2006-09-11 TAK.
    sendRequest(
        markerInit0a,
        '&cities=' + i + '&area=' + theForm.area.value
        + '&nl=' + theForm.nl.value + '&el=' + theForm.el.value + '&limit=' + theForm.limit.value,
        'GET',
        './interface.cgi',
        true,
        true
    );
}

function SetOption(i, theForm, func) {
    if (!func) {
        MessageDel();
    }
    map.clearOverlays();
    sendRequest(
        markerInit0,
        '&area=' + theForm.area.value
        + '&nl=' + theForm.nl.value + '&el=' + theForm.el.value +  '&sw=' + theForm.getSW.value
        + '&ne=' + theForm.getNE.value + '&limit=' + theForm.limit.value,
        'GET',
        './interface.cgi',
        true,
        true
    );
}


// Loading Google Map
function load(oj) {
    var xmlData = oj.responseXML;

    if (GBrowserIsCompatible()) {
        // 地図の表示（＋前処理）
        var res = xmlData.documentElement;
        var nl  = res.getElementsByTagName("nl");
        var el  = res.getElementsByTagName("el");
        var no  = res.getElementsByTagName("no");
        map     = new GMap2(document.getElementById("map"), {
            size     : new GSize(794, 396),
            mapTypes : [G_SATELLITE_MAP, G_NORMAL_MAP, G_HYBRID_MAP]
        });
        map.addControl(new GLargeMapControl());
        map.addControl(new GMapTypeControl());
        map.addControl(new GScaleControl());
        map.enableContinuousZoom();
        map.setCenter (
            new GLatLng(nl[0].firstChild.nodeValue, el[0].firstChild.nodeValue), 17
        );
        // マーカーセット
        markerInit(xmlData);
        // 座標、エリア等の初期値取得
        dataInit(xmlData);
    }

    // 物件情報エリアの表示処理
    listingInfo(xmlData);

        GEvent.addListener(map, "moveend", function() {
            var IDstr = document.getElementById("id").getAttribute("value");
    var nodeX = document.getElementById('el');
    nodeX.setAttribute("value", map.getCenter().x);
    var nodeY = document.getElementById('nl');
    nodeY.setAttribute("value", map.getCenter().y);
            if (!bound.contains(map.getCenter()) && IDstr.match(/^\d+/)) {
                dataInit(xmlData);
                SetOption(0, document.getElementById('controler'), 1);
            }
        });
        GEvent.addListener(map, "zoomend", function(oldLv, newLv) {
            var IDstr = document.getElementById("id").getAttribute("value");
            if (IDstr.match(/^\d+/)) {
                dataInit(xmlData);
                SetOption(0, document.getElementById('controler'), 1);
            } else {
                map.clearOverlays();
                markerInit(xmlData);
            }
        });
}


// マーカー・セットの準備
function markerInit0 (oj) {
    var xmlData = oj.responseXML;
    var res     = xmlData.documentElement;
    var nl      = res.getElementsByTagName("nl");
    var el      = res.getElementsByTagName("el");
    var no      = res.getElementsByTagName("no");
//    map.clearOverlays();
    markerInit(xmlData);
    dataInit(xmlData);
}

function markerInit0a (oj) {
    var xmlData = oj.responseXML;
    var res     = xmlData.documentElement;
    var nl      = res.getElementsByTagName("nl");
    var el      = res.getElementsByTagName("el");
    var no      = res.getElementsByTagName("no");
//    map.clearOverlays();
    map.setCenter(new GLatLng(nl[0].firstChild.nodeValue, el[0].firstChild.nodeValue), 13);
    markerInit(xmlData);
    dataInit(xmlData);
}

function markerInit (xmlData) {
    var doc  = xmlData.documentElement;
    for (var j = 0; j < doc.getElementsByTagName('marker').length; j++) {
	var m_nl	= parseFloat(doc.getElementsByTagName('m_nl')[j].firstChild.nodeValue);
	var m_el	= parseFloat(doc.getElementsByTagName('m_el')[j].firstChild.nodeValue);
	var m_name	= "<img src='" + doc.getElementsByTagName('m_image')[j].firstChild.nodeValue
			+ "' width='80' height='80' align=\"left\" />"
			+ "<div style=\"color: #6e7678\"><b>" + doc.getElementsByTagName('m_name')[j].firstChild.nodeValue
			+ "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</b><br /><br />"
			+ doc.getElementsByTagName('m_address')[j].firstChild.nodeValue + "<br />"
			+ doc.getElementsByTagName('m_date')[j].firstChild.nodeValue + "<br />"
//			+ doc.getElementsByTagName('m_genre')[j].firstChild.nodeValue
//			+ "竣工：" + doc.getElementsByTagName('m_date')[j].firstChild.nodeValue + "<br />";
            ;

        if (doc.getElementsByTagName('m_url')[j].firstChild.nodeValue != "NODATA") {
            m_name = m_name + "<a href=\""
                   + doc.getElementsByTagName('m_url')[j].firstChild.nodeValue
                   + "\" target=\"_blank\">"
                   + doc.getElementsByTagName('m_url')[j].firstChild.nodeValue
                   + "</a><br /></div>\n";
        }
	//----------------------------------------------------------------------------------キャンペーン用吹き出し
        if (doc.getElementsByTagName('m_genre')[j].firstChild.nodeValue == "8") {
		m_name	= "<a href='https://campaign.31sumai.com/em/enq/enq_form.php?qIds=lCdaPvX5OkiC' target='_blank'><img src='/images/31gmap_amazon_a.png' width='318' height='106' align=\"left\" /></a>"
        }
	//-------------------------------------------------------------------------------------
        var icon   = iconInit(doc.getElementsByTagName('m_genre')[j].firstChild.nodeValue);
        var marker = createMarker(new GLatLng(m_nl, m_el), icon, m_name);
        map.addOverlay(marker);
    }
    // 物件情報エリアの表示処理
    listingInfo(xmlData);
}

function createMarker(point, icon, html) {
    var marker  = new GMarker(point, { icon : icon });
    GEvent.addListener(marker, 'click', function() {
        marker.openInfoWindowHtml(html, { maxWidth : 550 });
    });
    return marker;
}

function iconInit (area) {
    var zoomLv = map.getZoom();
	var icon = new GIcon();
	//-----------------------------------------------------
	var image;
	if (zoomLv > 16) {			//14
	image = new Array(
		"",
		"./images/park_mansion.png",
		"./images/park_court.png",
		"./images/park_homes.png",
		"./images/park_tower.png",
		"./images/park_city.png",
		"./images/fine_court.png",
		"./images/park_sample.png",
		"./images/31gmap_amazon.png",
		"./images/park_luxe.png",
		"./images/park_seasons.png"
	);
        icon.image            = image[parseInt(area)];
	//-----------------------------------------------------
	switch (parseInt(area)) {
	case 8:
		icon.iconSize = new GSize(190, 40);
		break;
	default:
		icon.iconSize = new GSize(54, 42);
		break;
	}
	//-----------------------------------------------------
	shadow = new Array(
		"",
		"./images/shadow50.png",
		"./images/shadow50.png",
		"./images/shadow50.png",
		"./images/shadow50.png",
		"./images/shadow50.png",
		"./images/shadow50.png",
		"./images/shadow50.png",
		"./images/shadowA.png",
		"./images/shadow50.png",
		"./images/shadow50.png"
        );
	icon.shadow         = shadow[parseInt(area)];
	//-----------------------------------------------------
	switch (parseInt(area)) {
	case 8:
		icon.shadowSize = new GSize(210, 40);
		break;
	default:
		icon.shadowSize = new GSize(70, 42);
		break;
	}
	//-----------------------------------------------------
	switch (parseInt(area)) {
	case 8:
		icon.iconAnchor       = new GPoint(105, 20);
		break;
	default:
		icon.iconAnchor       = new GPoint(16, 16);
		break;
	}
	//-----------------------------------------------------
	icon.infoWindowAnchor = new GPoint(27, 21);

} else {
	image = new Array("","./images/a.png","./images/b.png",
            "./images/c.png",
            "./images/d.png",
            "./images/e.png",
            "./images/f.png",
            "./images/g.png",
            "./images/f.png"		//amazonキャンペーン
        );
        icon.image            = image[parseInt(area)];
        icon.shadow           = "./images/shadow2_.png";
        icon.iconSize         = new GSize(21, 21);
        icon.shadowSize       = new GSize(0, 0);
        icon.iconAnchor       = new GPoint(10, 10);		34
        icon.infoWindowAnchor = new GPoint(12, 15);
    }
    return icon;
}


function dataInit (xmlData) {
    var res   = xmlData.documentElement;
    var no    = res.getElementsByTagName("no");
    var node  = document.getElementById('area');
    node.setAttribute("value", no[0].firstChild.nodeValue);
    var nodeX = document.getElementById('el');
    nodeX.setAttribute("value", map.getCenter().x);
    var nodeY = document.getElementById('nl');
    nodeY.setAttribute("value", map.getCenter().y);

    bound     = map.getBounds();
    var pSW   = document.getElementById('getSW');
    pSW.setAttribute("value", bound.getSouthWest());
    var pNE   = document.getElementById('getNE');
    pNE.setAttribute("value", bound.getNorthEast());
}


function listingInfo (doc) {
    var data = "";
    for (var k = 0; k < doc.getElementsByTagName('marker').length; k++) {
        data = data + "<dt><a href=\"javascript:Centering("
             + doc.getElementsByTagName('m_nl')[k].firstChild.nodeValue + ", "
             + doc.getElementsByTagName('m_el')[k].firstChild.nodeValue + ");\">"
             + "<span style=\"color: #a3b1b2\">■ </span>" + doc.getElementsByTagName('m_name')[k].firstChild.nodeValue + "</a></dt>\n";
        data = data+"<dd style=\"margin-left: 0em\">"+doc.getElementsByTagName('m_address')[k].firstChild.nodeValue+"</dd>\n";
    }
    document.getElementById("infolist").innerHTML=data;
}

function Centering(nl, el) {
    map.setZoom(17);
    map.panTo(new GLatLng(nl, el));
}

function MessageDel() {
    if (document.getElementById("check").firstChild.nodeValue != "&nbsp;") {
        document.getElementById("check").innerHTML = "&nbsp;";
        document.getElementById("id").setAttribute("value", "1");
    }
}

