//------------------------------------------------
//地図表示オブジェクト
//------------------------------------------------
var map;
var centerIcon;

//------------------------------------------------
//ページ読み込み時処理関数
//------------------------------------------------
function loadFile(){
    reqFile(onLoadedLeft,"","GET","./file/selnavi.txt",true);
    loadMaps();
}

//------------------------------------------------
//地図表示
//------------------------------------------------
function loadMaps(){
    //検索ボックスにフォーカスをセット
    setFocus('searchbox');  
    
    //ページ読み込み時の処理
    
    //MapType定義（「マップ」と「サテライト」のみ表示）
    var mtMap = new GMapType(G_NORMAL_MAP.getTileLayers(), G_NORMAL_MAP.getProjection(),"マップ");
    var mtSatellite = new GMapType(G_SATELLITE_MAP.getTileLayers(), G_SATELLITE_MAP.getProjection(),"サテライト");
    var mtHibrid = new GMapType(G_HYBRID_MAP.getTileLayers(), G_SATELLITE_MAP.getProjection(),"Hybrid");

    //GMap2のインスタンス作成
    map = new GMap2(document.getElementById("map"));

    //
    map.getMapTypes().length = 0;
    map.addMapType(mtMap);
    map.addMapType(mtSatellite);
    map.addMapType(mtHibrid);
    //上記で設定したコントロール追加
    map.addControl(new GMapTypeControl());

    //パン移動とスライダーズームの使える大きなコントロール
    map.addControl(new GLargeMapControl());

    //沖縄県の緯度・経度
    var lat = 26.212415678577454;
    var lng = 127.68088459968567;
    //var point = new GPoint(lat,lng);
    var point = new GLatLng(lat,lng);
    
    //map.centerAndZoom(point,9);
    map.setCenter(point,8,G_NORMAL_MAP);
    map.addControl(new GOverviewMapControl(new GSize(150,120)));
    new GKeyboardHandler(map);

    //センターアイコン作成
    centerIcon = new GIcon();
    centerIcon.image = "../icon/center03.png";
    centerIcon.iconSize = new GSize(36,36);
    centerIcon.iconAnchor = new GPoint(18,18);

    var centerArrow = new GMarker(map.getCenter(),centerIcon);
    map.addOverlay(centerArrow);

    //クレジットアイコン作成
    var creditIcon = new GIcon();
    creditIcon.image = "../icon/tedaco_logo_01_63x14.gif";
    creditIcon.iconSize = new GSize(63,14);
    creditIcon.iconAnchor = new GPoint(0,48);

    var bbLL = map.getBounds();
//  var creditPoint = new GPoint(bbLL.getSouthWest().lat(),bbLL.getSouthWest().lng());
    var creditPoint = new GLatLng(bbLL.getSouthWest().lat(),bbLL.getSouthWest().lng());
    var creditMark = new GMarker(creditPoint,creditIcon);
    map.addOverlay(creditMark);
    
    setTag();
    
/*
    setTimeout(function(){
        //8秒後にクレジットアイコンの削除
        //map.removeOverlay(creditMark);
    },8000);
*/

    //地図移動でセンターアイコンを常に中心に表示
    GEvent.addListener(map, "moveend", function(){
        map.clearOverlays();
        //map.removeOverlay(creditMark);
        centerArrow = new GMarker(map.getCenter(),centerIcon);
        map.addOverlay(centerArrow);
    
        setTag();
    
    });
/*      GEvent.addListener(centerArrow, "click", function(){
        window.open("http://r.tedaco.net/tms/","てだこMAPサービス");
    });*/
}

//------------------------------------------------
//左セレクトボックスの作成
//------------------------------------------------
function onLoadedLeft(Obj){
    //レスポンステキストをでコード
    //var resText = decodeURIComponent(Obj.responseText);
    var resText = get_response_text(Obj);
    //改行コードで分離して配列作成
    resText = resText.split('\r').join('').split('\n');

    //左select作成
    var html = '<select name="id1" onChange="hndlSelectedLeft(this)">\n';
        html += '<option value="0"selected>--Selected Menu--</option>\n';
    for(var i = 1; i < resText.length; i++){
        rows = resText[i].split(',');
        html += '<option value="' + resText[i] + '">';
        html += rows[0];
        html += '</option>\n';
    }
    html += '</select>';
    document.getElementById('selnavi').innerHTML = html;

    //右セレクトボックス作成
    //初期値として「←メニューを選んでください」のみ作成
    setRightSel();
}

//------------------------------------------------
//右セレクトボックス作成
//初期値として「--Selected Menu--」のみ作成
//------------------------------------------------
function setRightSel(){
    //右select作成
    html = '<select name="id2" onChange="hndlSelectedRight(this)">\n';
    html += '<option value="0"selected>←メニューを選んでください</option>\n';
    html += '</select>';
    document.getElementById('selmap').innerHTML = html;
}

//------------------------------------------------
//左セレクトボックス選択時の処理
//------------------------------------------------
function hndlSelectedLeft(oj){
    if(oj.selectedIndex == 0){
        setRightSel();
    }
    else{
        var selRow = oj.options[oj.selectedIndex].value;
        selRow = selRow.split(',');
        reqFile(onLoadedRight,"","GET","./file/" + selRow[1],true);
    }
}

//------------------------------------------------
//右セレクトボックスの作成
//------------------------------------------------
function onLoadedRight(Obj){
    //レスポンステキストをでコード
    //var resText = decodeURIComponent(Obj.responseText);
    var resText = get_response_text(Obj);
    //改行コードで分離して配列作成
    resText = resText.split('\r').join('').split('\n');

    var html = '<select name="id2" onChange="hndlSelectedRight(this)">\n';
        html +=  '<option value="0"selected>--Selected Menu--</option>\n';
    for(var i = 1; i < resText.length; i++){
        rows = resText[i].split(',');
        html +=  '<option value="' + resText[i] + '">';
        html += rows[0];
        html +=  '</option>\n';
    }
    html += '</select>';
    document.getElementById('selmap').innerHTML = html;
}

//------------------------------------------------
//右セレクトボックス選択時の処理
//------------------------------------------------
function hndlSelectedRight(oj){
    var selRow;

    if(oj.selectedIndex != 0){
        selRow = oj.options[oj.selectedIndex].value;
        selRow = selRow.split(',');

        //地図移動
        delinfo();
        moveMaps(selRow[2],selRow[1],16);
    }
}

//------------------------------------------------
//地図移動処理
//------------------------------------------------
function moveMaps(lat,lng,zoom){
    var mapTypes = map.getCurrentMapType();
    var coord = new GLatLng(lat,lng);
    map.setCenter(coord,zoom,mapTypes);

    //マーカーを表示する
    var marker = new GMarker(coord);
    map.addOverlay(marker);
}

//------------------------------------------------
//貼り付けタグ作成
//------------------------------------------------
function setTag(){
    var coordsXY = map.getCenter();
    var mapw = document.getElementById('mapw').value;
    var maph = document.getElementById('maph').value;
    var mapzoom = document.getElementById('mapzoom').value;
    var mtc = document.getElementById('mtc').value;
    var dmt = document.getElementById('dmt').value;
    document.getElementById('sourceTag').value = '<p><iframe src="http://' + location.hostname + '/tms/tms.php?v=2&mtc=' + mtc + '&dmt=' + dmt + '&w=' + mapw + '&h=' + maph + '&s=' + mapzoom + '&x=' + coordsXY.x + '&y=' + coordsXY.y + '" width="' + mapw + '" height="' + maph + '" frameborder="0" marginwidth="0" marginheight="0" scrolling="no"></iframe><br><a href="http://map.tedaco.net/tms/" target="_blank"><font size="-5">住所検索もできる「てだこMAPサービス」</font></a></p>';
    //プレビューリンク作成
    var prevHtml = '&nbsp;<a href="http://' + location.hostname + '/tms/tms.php?v=2&mtc=' + mtc + '&dmt=' + dmt + '&w=' + mapw + '&h=' + maph + '&s=' + mapzoom + '&x=' + coordsXY.x + '&y=' + coordsXY.y + '" target="_blank"><strong>プレビュー</strong></a>';
    document.getElementById('preview').innerHTML = prevHtml;
    document.getElementById('nzoom').innerHTML = '<strong>' + map.getZoom() + '</strong>';
}

//------------------------------------------------
//タグをクリップボードにコピー
//------------------------------------------------
function cb_copy(){
    var tag = document.getElementById('sourceTag');
    if(document.all && navigator.userAgent.match(/windows/i) && tag.value){
        copy_obj = tag.createTextRange();
        copy_obj.execCommand("Copy");
        alert("タグをクリップボードにコピーしました。");
    }
}

//------------------------------------------------
//フォーカスセット
//------------------------------------------------
function setFocus(id){
    objFocus = document.getElementById(id);
    objFocus.focus();
}

