var map;
var latl = 37.5;
var lngl = 137.8233;
var zml = 5;
var latr;
var lngr;
var zmr;
var pointData = '';
var pointData2 = '';
var htmlm = new Array();
var htmlm2 = new Array();
var html = new Array();
var html2 = new Array();
var markers = new Array();
var markers2 = new Array();
var doneurl = new Array();
var coneurl = new Array();
var parray = new Array();
var pagekind = 'both';
var exflg = true;
var nicknames = new Array();
var d_title = new Array();
var cm_title = new Array();
var c_title = new Array();
var uri_type;
var uri_id;

Event.observe(window, 'resize', extmap2, false);

function zoomMapControl() {}
zoomMapControl.prototype = new GControl();
zoomMapControl.prototype.initialize = function(map) {
    var zoomMap = document.createElement("img");
    zoomMap.setAttribute("src", "./skin/default/img/mapext.gif");
    zoomMap.setAttribute("id", "zmbtn");
    this.setButtonStyle_(zoomMap);
    GEvent.addDomListener(
        zoomMap,
        "click",
        function() {
            extmap();
        }
    );
    map.getContainer().appendChild(zoomMap);
    return zoomMap;
}
zoomMapControl.prototype.getDefaultPosition = function() {
    return new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(0, 265));
}
zoomMapControl.prototype.setButtonStyle_ = function(button) {
    button.style.cursor = "pointer";
    button.style.height = "69px";
}

function areaSearchControl() {}
areaSearchControl.prototype = new GControl();
areaSearchControl.prototype.initialize = function(map) {
    var areaSearch = document.createElement("div");
    this.setButtonStyle_(areaSearch);
    areaSearch.appendChild(document.createTextNode("現在の範囲で絞り込む"));
    GEvent.addDomListener(
        areaSearch,
        "click",
        function() {
            searcharea();
        }
    );
    map.getContainer().appendChild(areaSearch);
    return areaSearch;
}
areaSearchControl.prototype.getDefaultPosition = function() {
    return new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(69, 25));
}
areaSearchControl.prototype.setButtonStyle_ = function(button) {
    button.style.backgroundColor = "white";
    button.style.borderTop = "1px solid";
    button.style.borderLeft = "1px solid";
    button.style.borderRight = "2px ridge";
    button.style.borderBottom = "2px ridge";
    button.style.textAlign = "center";
    button.style.width = "140px";
    button.style.cursor = "pointer";
}

function resetmapControl() {}
resetmapControl.prototype = new GControl();
resetmapControl.prototype.initialize = function(map) {
    var resetmap = document.createElement("div");
    this.setButtonStyle_(resetmap);
    resetmap.appendChild(document.createTextNode("リセット"));
    GEvent.addDomListener(
        resetmap,
        "click",
        function() {
            reset();
        }
    );
    map.getContainer().appendChild(resetmap);
    return resetmap;
}
resetmapControl.prototype.getDefaultPosition = function() {
    return new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(7, 25));
}
resetmapControl.prototype.setButtonStyle_ = function(button) {
    button.style.backgroundColor = "white";
    button.style.borderTop = "1px solid";
    button.style.borderLeft = "1px solid";
    button.style.borderRight = "2px ridge";
    button.style.borderBottom = "2px ridge";
    button.style.textAlign = "center";
    button.style.width = "58px";
    button.style.cursor = "pointer";
}

function loadMap() {
	map = new GMap2(document.getElementById("map"));
	map.enableDoubleClickZoom();
	map.enableContinuousZoom();
	map.addControl(new GLargeMapControl());
	map.addControl(new GMapTypeControl());
	map.addControl(new GScaleControl());
	map.addControl(new GOverviewMapControl());
	map.addControl(new areaSearchControl());
	map.addControl(new resetmapControl());
	map.addControl(new zoomMapControl());
	icon = new GIcon();
	icon.image = "./skin/default/img/markerg.png";
	icon.shadow = "./skin/default/img/shadow50.png";
	icon.iconSize = new GSize(20, 34);
	icon.shadowSize = new GSize(37, 34);
	icon.iconAnchor = new GPoint(10, 34);
	icon.infoWindowAnchor = new GPoint(10, 0);
	GEvent.addListener(
	    map,
	    'zoomend',
	    function(oldZoomLevel,newZoomLevel) {
	        zml = newZoomLevel;
	    }
	);
	GEvent.addListener(
	    map,
	    'moveend',
	    function() {
	        xy = map.getCenter();
	        latl = xy.lat();
	        lngl = xy.lng();
	    }
	);

	map.setCenter(new GLatLng(latl,lngl),zml,G_NORMAL_MAP);
}

function initZoom() {
    var list = [];
    var gb;
    var first = 1;
    list = markers.concat(markers2);
    for(i=0;i<list.length;i++) {
        var marker = list[i];
        if(marker != undefined) {
            if(first){
                gb = new GLatLngBounds(marker.getPoint(),marker.getPoint());
                first = 0;
            }else{
                var point = marker.getPoint();
                gb.extend(point);
            }
        }
    }
    map.setCenter(gb.getCenter(),map.getBoundsZoomLevel(gb),G_NORMAL_MAP);
}

function initMarker() {
    //var manager = new GMarkerManager(map);
    markers = [];
    markers2 = [];
	var i;
	for (i=0;i<pointData.length;i++) {
	    if(!isNaN(pointData[i].zoom) && !isNaN(pointData[i].lon) && !isNaN(pointData[i].lat)) {
	        var marker = plotById(i);
    	    map.addOverlay(marker);
            //manager.addMarker(marker,0);
	    }
	}
	var j;
	for (j=0;j<pointData2.length;j++) {
	    if(!isNaN(pointData2[j].zoom) && !isNaN(pointData2[j].lon) && !isNaN(pointData2[j].lat)) {
	        var marker = plotById2(j);
    	    map.addOverlay(marker);
            //manager.addMarker(marker,0);
	    }
	}
}

function plotById(id) {
    if(pointData[id].oneid == 'top') {
        doneurl[id] = './?m=pc&a=page_h_gmaps_diary&target_c_diary_id='+pointData[id].inid+'&url='+encodeURIComponent(pointData[id].url);
    } else {
        doneurl[id] = './?m=pc&a=page_h_gmaps_diary_comment&target_c_diary_comment_id='+pointData[id].oneid+'&url='+encodeURIComponent(pointData[id].url);
    }
    htmlm[id] = makeInfom(id);
    var marker = new GMarker(new GPoint(pointData[id].lon, pointData[id].lat),{icon:icon,title:pointData[id].info});
    GEvent.addListener(marker, "click", function() {
        this.openInfoWindowHtml(htmlm[id], {maxUrl:doneurl[id]});
        if(pointData[id].zoom == zml) {
            document.getElementById('d'+id).style.display='none';
        }
    document.getElementById('rd'+id).style.display='none';
    });
    markers[id] = marker;
    return marker;
}

function plotById2(id) {
    coneurl[id] = './?m=pc&a=page_h_gmaps_topic&target_c_topic_comment_id='+pointData2[id].oneid+'&url='+encodeURIComponent(pointData2[id].url);
    htmlm2[id] = makeInfo2m(id);
    var marker = new GMarker(new GPoint(pointData2[id].lon, pointData2[id].lat),{title:pointData2[id].info});
    GEvent.addListener( marker, "click", function() {
        this.openInfoWindowHtml(htmlm2[id], {maxUrl:coneurl[id]});
        if(pointData2[id].zoom == zml) {
            document.getElementById('c'+id).style.display='none';
        }
    document.getElementById('rc'+id).style.display='none';
    });
    markers2[id] = marker;
    return marker;
}

function makeInfom(i) {
    var nickname = pointData[i].nickname;
    var mid = pointData[i].mid;
    var info = pointData[i].info;
    var note = pointData[i].note;
    var img = pointData[i].img;
    var date = pointData[i].date;
    var url = pointData[i].url;
    var inid = pointData[i].inid;
    var id = i;
    if(nicknames[mid] == undefined) {
        nicknames[mid] = nickname;
    }
    if(d_title[inid] == undefined) {
        d_title[inid] = pointData[i].title;
    }
    var npoint = '<span id=\'d' + id + '\'>&nbsp;<img src="./skin/default/img/icon_arrow_1.gif" align="absmiddle"><a href=\'javascript:listSelected('+id+');\'>作成時の倍率で表示</a></span>';
    var rpoint = '<span id=\'rd' + id + '\'>&nbsp;<img src="./skin/default/img/icon_arrow_1.gif" align="absmiddle"><a href=\'javascript:rv('+id+');\'>前に戻る</a></span>';
	var html = "<div style='width:280px;line-height:150%;font-size:9pt;font-family:sans-serif;text-align:left;'><div style='border-bottom:#cccccc 1px solid;'><span style='font-weight:bold;color:#60E060;'>■日記</span>"+rpoint+npoint+"</div>";
	if (img != '') {
		html += "<div style='padding:5px 0;'>" + date + "&nbsp;" + nickname + "&nbsp;<img src='./skin/default/img/icon_arrow_1.gif' align='absmiddle'><a href='javascript:mindiv("+mid+");'>メンバーで絞り込む</a></div><div><div style='width:150px;float:left;'><div style='font-weight:bold;'><a href='javascript:void(0);' onClick='map.getInfoWindow().maximize();'>" + info + "</a></div><div>" + note + "<br><img src='./skin/default/img/icon_arrow_1.gif' align='absmiddle'><a href='" + url + "' target='_blank'>コメントをする</a></div></div><div style='width:130px;height:130px;float:right;'><img src='./img.php?filename="+ img +"&w=120&h=120' style='padding:5px;'></div></div>";
	} else {
		html += "<div style='padding:5px 0;'>" + date + "&nbsp;" + nickname + "&nbsp;<img src='./skin/default/img/icon_arrow_1.gif' align='absmiddle'><a href='javascript:mindiv("+mid+");'>メンバーで絞り込む</a></div><div style='font-weight:bold;'><a href='javascript:void(0);' onClick='map.getInfoWindow().maximize();'>" + info + "</a></div><div>" + note + "<br><img src='./skin/default/img/icon_arrow_1.gif' align='absmiddle'><a href='" + url + "' target='_blank'>コメントをする</a></div>";
    }
	html += "<div style='text-align:right;clear:both;'><div style='margin-top:3px;padding-top:3px;border-top:#cccccc 1px solid;'><img src='./skin/default/img/icon_arrow_1.gif' align='absmiddle'><a href='javascript:indiv(\"d_page\","+inid+");'>この日記で絞り込む</a><div style='padding:3px 0;'><img src='./skin/default/img/icon_arrow_1.gif' align='absmiddle'><a href='javascript:void(0);' onClick='map.getInfoWindow().maximize();'>全文を見る</a></div></div></div>";
	return html + "</div>";
}

function makeInfo2m(i) {
    var nickname = pointData2[i].nickname;
    var mid = pointData2[i].mid;
    var info = pointData2[i].info;
    var note = pointData2[i].note;
    var img = pointData2[i].img;
    var date = pointData2[i].date;
    var url = pointData2[i].url;
    var cmid = pointData2[i].cmid;
    var inid = pointData2[i].inid;
    var id = i;
    if(nicknames[mid] == undefined) {
        nicknames[mid] = nickname;
    }
    if(cm_title[cmid] == undefined) {
        cm_title[cmid] = pointData2[i].cmtitle;
    }
    if(c_title[inid] == undefined) {
        c_title[inid] = pointData2[i].title;
    }
    var npoint = '<span id=\'c' + id + '\'>&nbsp;<img src="./skin/default/img/icon_arrow_1.gif" align="absmiddle"><a href=\'javascript:listSelected2('+id+');\'>作成時の倍率で表示</a></span>';
    var rpoint = '<span id=\'rc' + id + '\'>&nbsp;<img src="./skin/default/img/icon_arrow_1.gif" align="absmiddle"><a href=\'javascript:rv2('+id+');\'>前に戻る</a></span>';
    if(url.indexOf("event") != -1) {
        cname = "イベント";
    } else {
        cname = "トピック";
    }
	var html = "<div style='width:280px;line-height:150%;font-size:9pt;font-family:sans-serif;text-align:left;'><div style='border-bottom:#cccccc 1px solid;'><span style='font-weight:bold;color:#FD766A;'>■"+cname+"</span>"+rpoint+npoint+"</div>";
	if (img != '') {
		html += "<div style='padding:5px 0;'>" + date + "&nbsp;" + nickname + "&nbsp;<img src='./skin/default/img/icon_arrow_1.gif' align='absmiddle'><a href='javascript:mindiv("+mid+");'>メンバーで絞り込む</a></div><div><div style='width:150px;float:left;'><div style='font-weight:bold;'><a href='javascript:void(0);' onClick='map.getInfoWindow().maximize();'>" + info + "</a></div><div>" + note + "<br><img src='./skin/default/img/icon_arrow_1.gif' align='absmiddle'><a href='" + url + "' target='_blank'>書き込みをする</a></div></div><div style='width:130px;height:130px;float:right;'><img src='./img.php?filename="+ img +"&w=120&h=120' style='padding:5px;'></div></div>";
	} else {
		html += "<div style='padding:5px 0;'>" + date + "&nbsp;" + nickname + "&nbsp;<img src='./skin/default/img/icon_arrow_1.gif' align='absmiddle'><a href='javascript:mindiv("+mid+");'>メンバーで絞り込む</a></div><div style='font-weight:bold;'><a href='javascript:void(0);' onClick='map.getInfoWindow().maximize();'>" + info + "</a></div><div>" + note + "<br><img src='./skin/default/img/icon_arrow_1.gif' align='absmiddle'><a href='" + url + "' target='_blank'>書き込みをする</a></div>";
    }
	html += "<div style='text-align:right;clear:both;'><div style='margin-top:3px;padding-top:3px;border-top:#cccccc 1px solid;'><img src='./skin/default/img/icon_arrow_1.gif' align='absmiddle'><a href='javascript:indiv(\"c_page\","+inid+");'>この"+cname+"で絞り込む</a>&nbsp;<img src='./skin/default/img/icon_arrow_1.gif' align='absmiddle'><a href='javascript:cmindiv("+cmid+");'>コミュで絞り込む</a><div style='padding:3px 0;'><img src='./skin/default/img/icon_arrow_1.gif' align='absmiddle'><a href='javascript:void(0);' onClick='map.getInfoWindow().maximize();'>全文を見る</a></div></div></div>";
	return html + "</div>";
}

function initList() {
        if(d_total_num == 'トータル数[0]件') {
            document.getElementById("d_page").innerHTML = "";
        } else {
	        var str = "";
	        var i;
	        var pre = "<li style='padding:3px 0;border-bottom:1px solid #cccccc;'><img src='./skin/dummy.gif' style='width:14px;height:14px;' class='icon icon_1'>";
	        var mid1 ="<br><a href='javascript:listSelectedm(";
	        var mid2 =")'>";
	        var tail = "</a>";
	        for (i=0;i<pointData.length;i++) {
		        if (pointData[i].info == undefined) break;
	        	if(!isNaN(pointData[i].zoom) && !isNaN(pointData[i].lon) && !isNaN(pointData[i].lat)) {
		            str += pre + pointData[i].date + mid1 + i + mid2 + pointData[i].info + tail + "（" + pointData[i].nickname + "）</li>";
		        }
	        }
	        str = "<div style='text-align:left;padding:5px;'><ol>" + str + "</ol></div>";
	        document.getElementById("d_page").innerHTML = str;
	    }
	
        if(c_total_num == 'トータル数[0]件') {
            document.getElementById("c_page").innerHTML = "";
        } else {
	        var str = "";
	        var j;
	        var pre = "<li style='padding:3px 0;border-bottom:1px solid #cccccc;'><img src='./skin/dummy.gif' style='width: 14px; height: 14px;' class='icon icon_2'>";
	        var mid1 ="<br><a href='javascript:listSelected2m(";
	        var mid2 =")'>";
	        var tail = "</a>";
	        for (j=0;j<pointData2.length;j++) {
		        if (pointData2[j].info == undefined) break;
	        	if(!isNaN(pointData2[j].zoom) && !isNaN(pointData2[j].lon) && !isNaN(pointData2[j].lat)) {
		            str += pre + pointData2[j].date + mid1 + j + mid2 + pointData2[j].info + tail + "（" + pointData2[j].nickname + "）</li>";
		        }
	        }
	        str = "<div style='text-align:left;padding:5px;'><ol>" + str + "</ol></div>";
	        document.getElementById("c_page").innerHTML = str;
	    }
}

function initPager() {
	    document.getElementById("num1").innerHTML = d_total_num;
	    document.getElementById("pager1").innerHTML = d_page_link;
        document.getElementById("num2").innerHTML = c_total_num;
        document.getElementById("pager2").innerHTML = c_page_link;
}

function listSelected(id) {
		addllz();
		map.setCenter(new GLatLng(pointData[id].lat, pointData[id].lon), parseInt(pointData[id].zoom), G_NORMAL_MAP);
		markers[id].openInfoWindowHtml(htmlm[id], {maxUrl:doneurl[id]});
		document.getElementById('d'+id).style.display='none';
}

function listSelected2(id) {
		addllz();
		map.setCenter(new GLatLng(pointData2[id].lat, pointData2[id].lon), parseInt(pointData2[id].zoom), G_NORMAL_MAP);
		markers2[id].openInfoWindowHtml(htmlm2[id], {maxUrl:coneurl[id]});
		document.getElementById('c'+id).style.display='none';
}

function listSelectedm(id) {
		map.panTo(new GLatLng(pointData[id].lat, pointData[id].lon));
		markers[id].openInfoWindowHtml(htmlm[id], {maxUrl:doneurl[id]});
        if(pointData[id].zoom == zml) {
            document.getElementById('d'+id).style.display='none';
        }
        document.getElementById('rd'+id).style.display='none';
}

function listSelected2m(id) {
		map.panTo(new GLatLng(pointData2[id].lat, pointData2[id].lon));
		markers2[id].openInfoWindowHtml(htmlm2[id], {maxUrl:coneurl[id]});
        if(pointData2[id].zoom == zml) {
            document.getElementById('c'+id).style.display='none';
        }
        document.getElementById('rc'+id).style.display='none';
}

function reSize(relat,relng,rezoom) {
    map.setCenter(new GLatLng(relat,relng),rezoom,G_NORMAL_MAP);
}

function addPoint() {
    pointname = document.getElementById('pname').value;
    if(pointname == '') {
        alert('ブックマーク名を入力してください');
    } else {
        if(parray.length > 9){
            alert('ブックマークは10か所までです');
        } else {
            id = (new Date()).getTime();
            document.getElementById('mapnavi').innerHTML += '<span id="c'+id+'">&nbsp;|&nbsp;<input type="checkbox" class="no_bg" value="c'+id+'" id="'+id+'">&nbsp;<a href="javaScript:void(0)" onClick="reSize('+latl+','+lngl+','+zml+')">'+pointname+'</a></span>';
            encpname = encodeURI(pointname);
            parray.push({"id":id,"lat":latl,"lng":lngl,"zm":zml,"pname":encpname});
            recPoint();
        }
    }
}

function deletePoint() {
    var flg = false;
    var parray2 = new Array();
    for(i=0; i<parray.length; i++) {
        var chkObj = document.getElementById(parray[i].id);
        if (chkObj.checked) {
            document.getElementById(chkObj.value).style.display = "none";
            flg = true;
        } else {
            parray2.push(parray[i]);
        }
    }
    if(flg) {
        parray = parray2;
        recPoint();
    } else {
        alert('削除するブックマークをチェックしてください');
    }
    delete parray2;
}

function recPoint() {
    if(parray.length != 0) {
        var cookiestr = 'parray=['
        for (i=0; i<parray.length; i++) {
            cookiestr += '{"id":"'+parray[i].id+'","lat":"'+parray[i].lat+'","lng":"'+parray[i].lng+'","zm":"'+parray[i].zm+'","pname":"'+parray[i].pname+'"},';
        }
        cookiestr = cookiestr.slice(0,-1);
        cookiestr += '];';
        document.cookie = cookiestr + "; expires=Thu, 1-Jan-2030 00:00:00 GMT";
    } else {
        document.cookie = "parray=; expires=Thu, 1-Jan-1970 00:00:00 GMT";
    }
}

function openPoint() {
    ckary = document.cookie.split("; ");
    ckstr = "";
    i = 0;
    for(i=0; i<ckary.length; i++) {
        if (ckary[i].substr(0,7) == "parray=") {
            eval(ckary[i]);
        }
    }
    buildpoint = "";
    if(parray) {
        for (i=0; i<parray.length; i++) {
            buildpoint += '<span id="c'+parray[i].id+'">&nbsp;|&nbsp;<input type="checkbox" class="no_bg" value="c'+parray[i].id+'" id="'+parray[i].id+'">&nbsp;<a href="javaScript:void(0)" onClick="reSize('+parray[i].lat+','+parray[i].lng+','+parray[i].zm+')">'+decodeURI(parray[i].pname)+'</a></span>';
        }
        document.getElementById('mapnavi').innerHTML += buildpoint;
    }
}

function extmap() {
    if(document.getElementById('zmbtn').src.indexOf('minus',0) == -1) {
        if(!document.all && (document.layers || document.getElementById)) {
            w=window.innerWidth-20;
        } else {
            if(document.getElementById && (document.compatMode=='CSS1Compat')) {
                w=document.documentElement.clientWidth;
            } else {
                if(document.all) {
                    w=document.body.clientWidth;
                }
            }
        }
        document.getElementById('zmbtn').src = './skin/default/img/mapext_minus.gif';
        document.getElementById('mainflm').style.width = w + 'px';
        document.getElementById('mapflm').style.width = w-180 + 'px';
        document.getElementById('map').style.width = w-185 + 'px';
        document.getElementById('mapnavi').style.width = w-213 + 'px';
        map.checkResize();
        map.setCenter(new GLatLng(latl,lngl),zml,G_NORMAL_MAP);
    } else {
        document.getElementById('zmbtn').src = './skin/default/img/mapext.gif';
        document.getElementById('mainflm').style.width = '713px';
        document.getElementById('mapflm').style.width = '533px';
        document.getElementById('map').style.width = '528px';
        document.getElementById('mapnavi').style.width = '500px';
        map.checkResize();
        map.setCenter(new GLatLng(latl,lngl),zml,G_NORMAL_MAP);
    }
}

function extmap2() {
    if(document.getElementById('zmbtn') && document.getElementById('zmbtn').src.indexOf('minus',0) != -1 && exflg) {
        if(!document.all && (document.layers || document.getElementById)) {
            w=window.innerWidth-20;
            if(window.innerWidth-20 < 713) {
                w = 713;
            } else {
                w=window.innerWidth-20;
            }
        } else {
            if(document.getElementById && (document.compatMode=='CSS1Compat')) {
                if(document.documentElement.clientWidth <713) {
                    w = 713;
                } else {
                    w=document.documentElement.clientWidth;
                }
            } else {
                if(document.all) {
                    if(document.body.clientWidth < 713) {
                        w = 713;
                    } else {
                        w=document.body.clientWidth;
                    }
                }
            }
        }
        document.getElementById('zmbtn').src = './skin/default/img/mapext_minus.gif';
        document.getElementById('mainflm').style.width = w + 'px';
        document.getElementById('mapflm').style.width = w-180 + 'px';
        document.getElementById('map').style.width = w-185 + 'px';
        document.getElementById('mapnavi').style.width = w-213 + 'px';
        map.checkResize();
        map.setCenter(new GLatLng(latl,lngl),zml,G_NORMAL_MAP);
    }
}

function addllz() {
	latr=map.getCenter().lat();
	lngr=map.getCenter().lng();
	zmr=map.getZoom();
	}
	
function rv(id) {
	map.setCenter(new GLatLng(latr,lngr),zmr,G_NORMAL_MAP);
    markers[id].openInfoWindowHtml(htmlm[id], {maxUrl:doneurl[id]});
    document.getElementById('rd'+id).style.display='none';
	}
	
function rv2(id) {
	map.setCenter(new GLatLng(latr,lngr),zmr,G_NORMAL_MAP);
	markers2[id].openInfoWindowHtml(htmlm2[id], {maxUrl:coneurl[id]});
    document.getElementById('rc'+id).style.display='none';
	}
	
function search(keyword) {
    url = './?m=pc&a=page_h_gmaps_list_data&keyword=' + encodeURI(keyword);
    map.clearOverlays();
    uri_type = '';
    uri_id = '';
    uriopen();
    pagekind = 'both';
    loadData(url);
}

function pagenetion(keytype,pagetype,page) {
    if(pagetype) {
        pageq = '&' + pagetype + '=' + page;
    } else {
        pageq = '';
    }
    if(document.getElementById(keytype) && document.getElementById(keytype).value) {
        keyq = '&' + document.getElementById(keytype).value;
    } else {
        keyq = '';
    }
    url = './?m=pc&a=page_h_gmaps_list_data'+pageq+keyq;
    map.clearOverlays();
    pagekind = pagetype;
    loadData(url);
}

function searcharea() {
    var area = map.getBounds();
    var n = area.getNorthEast().lat();
    var s = area.getSouthWest().lat();
    var e = area.getNorthEast().lng();
    var w = area.getSouthWest().lng();
    url = './?m=pc&a=page_h_gmaps_list_data&area='+n+"_"+s+"_"+e+"_"+w;
    map.clearOverlays();
    uri_type = '';
    uri_id = '';
    uriopen();
    pagekind = 'both';
    loadData(url);
}

function indiv(pagetype,id,flag) {
    exflg = false;
    page = 1;
    pageq = '&' + pagetype + '=' + page;
    idq = '&' + pagetype + '_id=' + id;
    url = './?m=pc&a=page_h_gmaps_list_data'+pageq+idq;
    if(pagetype == 'd_page') {
        pointData2 = '';
        c_total_num = 'トータル数[0]件';
        c_page_link = '';
        uri_type = 'd';
        uri_id = id;
    } else {
        pointData = '';
        d_total_num = 'トータル数[0]件';
        d_page_link = '';
        uri_type = 'c';
        uri_id = id;
    }
    pagekind = 'both';
    if(flag) {
        return url;
    } else {
        map.clearOverlays();
        loadData(url);
    }
}

function cmindiv(cmid,flag) {
    exflg = false;
    pageq = '&c_page=1';
    cmidq = '&cm_page_id=' + cmid;
    url = './?m=pc&a=page_h_gmaps_list_data'+pageq+cmidq;
    pointData = '';
    d_total_num = 'トータル数[0]件';
    d_page_link = '';
    uri_type = 'cm';
    uri_id = cmid;
    pagekind = 'both';
    if(flag) {
        return url;
    } else {
        map.clearOverlays();
        loadData(url);
    }
}

function mindiv(mid,flag) {
    exflg = false;
    midq = '&m_page_id=' + mid;
    url = './?m=pc&a=page_h_gmaps_list_data'+midq;
    uri_type = 'm';
    uri_id = mid;
    pagekind = 'both';
    if(flag) {
        return url;
    } else {
        map.clearOverlays();
        loadData(url);
    }
}

function reset() {
    map.clearOverlays();
    latl = 37.5;
    lngl = 137.8233;
    zml = 5;
	map.setCenter(new GLatLng(latl,lngl),zml,G_NORMAL_MAP);
    uri_type = '';
    uri_id = '';
	uriopen();
	pagekind = 'both';
    loadData('./?m=pc&a=page_h_gmaps_list_data&');
}

function uriopen(type,id) {
    if(type && id) {
        if(type == 'm') {
            var uri_title = "<div style='font-weight:bold;'>" + nicknames[id] + "さんのマップ</div>" + domain + '?m=pc&a=page_h_gmaps_list_all&type=m_page&id=' + id;
        }
        if(type == 'cm') {
            var uri_title = "<div style='font-weight:bold;'>「" +cm_title[id] + "」のマップ</div>" + domain + '?m=pc&a=page_h_gmaps_list_all&type=cm_page&id=' + id;
        }
        if(type == 'd') {
            var uri_title = "<div style='font-weight:bold;'>「" +d_title[id] + "」のマップ</div>" + domain + '?m=pc&a=page_h_gmaps_list_all&type=d_page&id=' + id;
        }
        if(type == 'c') {
            var uri_title = "<div style='font-weight:bold;'>「" +c_title[id] + "」のマップ</div>" + domain + '?m=pc&a=page_h_gmaps_list_all&type=c_page&id=' + id;
        }
        document.getElementById('uri').innerHTML = uri_title;
        document.getElementById('uribox').style.display = "block";
        new Effect.Highlight('uribox')
    } else {
        document.getElementById('uribox').style.display = "none";
    }
}

function loadData(url) {
    if(type && id) {
        if(type == 'cm_page') {
            url = cmindiv(id,true);
            uri_type = 'cm';
            uri_id = id;
        } else {
            if(type == 'm_page') {
                url = mindiv(id,true);
                uri_type = 'm';
                uri_id = id;
            } else {
                url = indiv(type,id,true);
                if(type == 'd_page') {
                    uri_type = 'd';
                } else {
                    uri_type = 'c';
                }
                uri_id = id;
            }
        }
        type = "";
        id = "";
        var all = true;
    }
    var options = {
        method : 'get',
        parameters : '',
        onFailure: function() { alert("通信に失敗しました"); },
        onComplete: function(res) {
            eval(res.responseText);
            if(d_total_num == 'トータル数[0]件') {
                pointData = '';
            }
            if(c_total_num == 'トータル数[0]件') {
                pointData2 = '';
            }
            if(url == './?m=pc&a=page_h_gmaps_list_data' || all) {
                loadMap();
                openPoint();
            }
            initMarker();
            initList();
            initPager();
            if(url.indexOf('&area=',0) == -1 && url != './?m=pc&a=page_h_gmaps_list_data' && url != './?m=pc&a=page_h_gmaps_list_data&') {
                initZoom();
            }
            uriopen(uri_type,uri_id);
            exflg = true;
        }
    }
    new Ajax.Request(url, options);
}

Ajax.Responders.register({
    onCreate: function() {
        if(pagekind) {
            if(pagekind == 'both') {
                document.getElementById('d_page').innerHTML = '<div style="padding:5px;text-align:center"><img src="./skin/default/img/loading.gif"></div>';
                document.getElementById('c_page').innerHTML = '<div style="padding:5px;text-align:center"><img src="./skin/default/img/loading.gif"></div>';
            } else {
                document.getElementById(pagekind).innerHTML = '<div style="padding:5px;text-align:center"><img src="./skin/default/img/loading.gif"></div>';
            }
        }
    }
});

var Effect = new Object();

Effect.Highlight = Class.create();
Effect.Highlight.prototype = {
  initialize: function(element) {
    this.element = $(element);
    this.start  = 153;
    this.finish = 255;
    this.current = this.start;
    this.fade();
  },
  
  fade: function() {
    if (this.isFinished()) return;
    if (this.timer) clearTimeout(this.timer);
    this.highlight(this.element, this.current);
    this.current += 17;
    this.timer = setTimeout(this.fade.bind(this), 250);
  },
  
  isFinished: function() {
    return this.current > this.finish;
  },
  
  highlight: function(element, current) {
    element.style.backgroundColor = "#ffff" + current.toColorPart();
  }
}
