﻿//マップ
var map;
var mapSize;
var rmenu;
var mgr;
var newMarker;
var geocoder;
var markerList = new Array();
var login;
var login_user;

var markerMinZoom = 9;
var markerMaxZoom = 17;
var highRankMinZoom = 5;
var highRankMaxZoom = 8;

//都道府県
var kenList = new Array(
    "その他", "北海道", "青森県", "岩手県", "宮城県", "秋田県",
    "山形県", "福島県", "茨城県", "栃木県", "群馬県",
    "埼玉県", "千葉県", "東京都", "神奈川県", "新潟県",
    "富山県", "石川県", "福井県", "山梨県", "長野県",
    "岐阜県", "静岡県", "愛知県", "三重県", "滋賀県",
    "京都府", "大阪府", "兵庫県", "奈良県", "和歌山県",
    "鳥取県", "島根県", "岡山県", "広島県", "山口県",
    "徳島県", "香川県", "愛媛県", "高知県", "福岡県",
    "佐賀県", "長崎県", "熊本県", "大分県", "宮崎県",
    "鹿児島県", "沖縄県");

//-------------------------------------------------------------------------
//-------------------------------------------------------------------------
function load() {
    if (GBrowserIsCompatible()) {
        
        windowResized();

        //マップ表示
        var opts = {mapTypes : [G_NORMAL_MAP, G_SATELLITE_MAP]};
        map = new GMap2(document.getElementById("map"), opts);
        var tokyo = new GLatLng(35.681631, 139.764982);
        map.setCenter(tokyo, 5);
        
        //マップコントロール表示
        map.addControl(new GLargeMapControl());
        map.addControl(new GScaleControl());
        map.addControl(new GMapTypeControl());
        
        //設定
        map.enableContinuousZoom();
        map.enableDoubleClickZoom();
        map.enableInfoWindow();
        map.enableScrollWheelZoom();

        //geocoder作成
        geocoder = new GClientGeocoder();
        
        //右クリックメニュー作成
        createRMenu();
        
        //クリックイベント
//        GEvent.addListener(map, 'click', mapClicked);
        
        //地図読み込み終了イベント
        GEvent.addListener(map, 'load', updateMap());
        
        //地図移動イベント
        GEvent.addListener(map, 'moveend', mapMoveEnd);
        
        //ログイン情報
        updateLoginStatus();
        
        window.onresize = windowResized;
        
    }
}

//-------------------------------------------------------------------------
// Window Resized
//-------------------------------------------------------------------------
function windowResized() {

//	var map_obj = $("map");
//	var dispsize = getDispSize();

//	map_obj.style.width = (dispsize.width - 200) + "px";
//	map_obj.style.height = (dispsize.height - 40) + "px"; 
}

function getDispSize() {
	if (document.all) {
		if (window.opera)
			return {width:document.body.clientWidth, height:document.body.clientHeight};
		else
			return {width:document.documentElement.clientWidth, height:document.documentElement.clientHeight};
	} else if (document.layers || document.getElementById) {
		return {width:window.innerWidth, height:window.innerHeight};
	}
}

//-------------------------------------------------------------------------
//-------------------------------------------------------------------------
function mapClicked(overlay, point) {

//    if (overlay) {
        //overlay.remove();
//        mgr.removeMarker(overlay);
//    }
    
}

//-------------------------------------------------------------------------
//-------------------------------------------------------------------------
function mapMoveEnd(overlay, point) {
    
    //ランキング
    updateRankingList();
        
}

//-------------------------------------------------------------------------
//右クリックメニュー作成
//-------------------------------------------------------------------------
function createRMenu() {

    rmenu = document.createElement("div");
    rmenu.id = "rmenu";
    rmenu.className = "rmenu";
    rmenu.style.visibility = "hidden";

    mapSize = map.getSize();
    
    //メニューをmap内に
    map.getContainer().appendChild(rmenu);
    
    //イベント
    GEvent.addListener(map, "singlerightclick", function(point) {
        var rmenux = point.x;
        var rmenuy = point.y;
        var rmenuLatLng = map.fromContainerPixelToLatLng(point);

        //メニューの内容
//        rmenu.innerHTML = "<a href='javascript:map.zoomIn();'>ズームインして詳細を表示</a><br />"
//            + "<a href='javascript:map.zoomOut();'>ズームアウトして広域を表示</a><br />"
//            + "<a href='javascript:map.panTo(new GLatLng" + rmenuLatLng + ");'>ここを地図の中心</a>";

	    var zoom = map.getZoom();
        var html = "";
        if (login && zoom >= markerMinZoom) {
            html =
                "<div class='rmenuItem' onmouseover='rmenuMouseOver(this);' onmouseout='rmenuMouseOut(this);' onclick='openRegistClinicWindow(new GLatLng" + rmenuLatLng + ")'>新規登録</div>"
        }
        
        rmenu.innerHTML =
            html
            + "<div class='rmenuItem' onmouseover='rmenuMouseOver(this);' onmouseout='rmenuMouseOut(this);' onclick='rmenuZoomIn()'>ズームインして詳細を表示</div>"
            + "<div class='rmenuItem' onmouseover='rmenuMouseOver(this);' onmouseout='rmenuMouseOut(this);' onclick='rmenuZoomOut()'>ズームアウトして広域を表示</div>"
            + "<div class='rmenuItem' onmouseover='rmenuMouseOver(this);' onmouseout='rmenuMouseOut(this);' onclick='rmenuCenter(new GLatLng" + rmenuLatLng + ")'>ここを地図の中心</div>"

        //一度(0,0)においてメニューのサイズを計算する
        var dummyPos = new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(0, 0));
        dummyPos.apply(rmenu);
        var dim = Element.getDimensions("rmenu");
        if (rmenux > (mapSize.width - dim.width)) {
            rmenux -= dim.width;
        }
        if (rmenuy > (mapSize.height - dim.height)) {
            rmenuy -= dim.height;
        }
        //補正した位置に表示する
        var rmenuPos = new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(rmenux, rmenuy));
        rmenuPos.apply(rmenu);
        rmenu.style.visibility = "visible";

    });
    
    //地図上を左クリックしたらメニューが消える
    GEvent.addListener(map, "click", function() {
        if (rmenu.style.visibility == "visible") {
            rmenu.style.visibility = "hidden";
        }
    });
    
    //マップからフォーカスがはずれたらメニューを非表示
    GEvent.addListener(map, "mouseout", function() {
        if (rmenu.style.visibility == "visible") {
            rmenu.style.visibility = "hidden";
        }
    });
    
}

//-------------------------------------------------------------------------
//新規登録ウィンドウを表示する
//-------------------------------------------------------------------------
function openRegistClinicWindow(latLng) {
    
    var html;
    var html2;
    
    html = ""
        + "<form name='form_regist' action='regist_clinic.php' enctype='multipart/form-data' method='post' target='regist_clinic'>"
        + "<input type='hidden' name='latitude' value='" + latLng.lat() + "'>"
        + "<input type='hidden' name='longitude' value='" + latLng.lng() + "'>"
        + "<input type='hidden' id='edit' name='edit' value='0'>"
        + "<input type='hidden' id='address1_org' name='address1_org' value=''>"
        + "<input type='hidden' id='address2_org' name='address1_org' value=''>"
        + "<input type='hidden' id='address3_org' name='address1_org' value=''>"
        + "<input id='user_id' name='user_id' type='hidden' value='" + login_user.id + "' />"
        + "<input name='evaluation' type='hidden' value='' />"
        + "<input name='evaluation_item1' type='hidden' value='' />"
        + "<input name='evaluation_item2' type='hidden' value='' />"
        + "<input name='evaluation_item3' type='hidden' value='' />"
        + "<input name='evaluation_item4' type='hidden' value='' />"
        + "<input name='evaluation_item5' type='hidden' value='' />"
        + "<input name='evaluation_item6' type='hidden' value='' />"
        + "<input name='evaluation_item7' type='hidden' value='' />"
        + "<input name='evaluation_item8' type='hidden' value='' />"
        + "<input name='evaluation_item9' type='hidden' value='' />"
        + "<input name='evaluation_item10' type='hidden' value='' />"
        + "<div>新規登録<div>"
        + "<table class='infoWindow'>"
        + "<tr><th>名称</th><td><input id='clinic_name' name='clinic_name' type='text' size='40' maxlength='100'></td></tr>"
        + "<tr><th>住所</th>"
        + "<td>〒<input id='zip_code#0' name='zip_code#0' type='text' size='4' maxlength='3' onkeyup='getAddress(\"zip_code#0\", \"zip_code#1\", \"address1\", \"address2\", \"address3\");'>-"
        + "<input id='zip_code#1' name='zip_code#1' type='text' size='5' maxlength='4' onkeyup='getAddress(\"zip_code#0\", \"zip_code#1\", \"address1\", \"address2\", \"address3\");'><br>"
        + getKenSelect("")
        + "<input id='address2' name='address2' type='text' size='40' maxlength='60'><br>"
        + "<input id='address3' name='address3' type='text' size='40' maxlength='60'></td></tr>"
        + "<tr><th>電話番号</th>"
        + "<td><input id='tel#0' name='tel#0' type='text' size='5' maxlength='4'>"
        + "-<input id='tel#1' name='tel#1' type='text' size='5' maxlength='4'>"
        + "-<input id='tel#2' name='tel#2' type='text' size='5' maxlength='4'></td></tr>"
        + "<tr><th>メールアドレス</th><td><input id='email' name='email' type='text' size='40' maxlength='100'></td></tr>"
        + "<tr><th>ホームページ</th><td><input id='url' name='url' type='text' size='40' maxlength='100'></td></tr>"
        + "<tr><th>紹介文</th><td><textarea id='introduction' name='introduction' rows='3' cols='40'></textarea></td></tr>"
        + "<tr><th>診療科目1</th><td>"
        + "<select id='icon_no' name='icon_no' style='width: 250px;'>"
        + "<option value='0'>(選択して下さい)</option><option value='1'>歯科全般</option><option value='2'>審美歯科(ホワイトニング含む)</option><option value='3'>矯正歯科</option><option value='4'>その他(衛生士だけのクリーニングサロン的なものも含む)</option>"
        + "</select>"
        + "</td></tr>"
        + "<tr><th>診療科目2</th><td>"
        + "<select id='icon_no2' name='icon_no2' style='width: 250px;'>"
        + "<option value='0'>(選択して下さい)</option><option value='1'>歯科全般</option><option value='2'>審美歯科(ホワイトニング含む)</option><option value='3'>矯正歯科</option><option value='4'>その他(衛生士だけのクリーニングサロン的なものも含む)</option>"
        + "</select>"
        + "</td></tr>"
        + "<tr><th>診療科目3</th><td>"
        + "<select id='icon_no3' name='icon_no3' style='width: 250px;'>"
        + "<option value='0'>(選択して下さい)</option><option value='1'>歯科全般</option><option value='2'>審美歯科(ホワイトニング含む)</option><option value='3'>矯正歯科</option><option value='4'>その他(衛生士だけのクリーニングサロン的なものも含む)</option>"
        + "</select>"
        + "</td></tr>"
        + "<tr><th>診療科目4</th><td>"
        + "<select id='icon_no4' name='icon_no4' style='width: 250px;'>"
        + "<option value='0'>(選択して下さい)</option><option value='1'>歯科全般</option><option value='2'>審美歯科(ホワイトニング含む)</option><option value='3'>矯正歯科</option><option value='4'>その他(衛生士だけのクリーニングサロン的なものも含む)</option>"
        + "</select>"
        + "</td></tr>"
        + "<tr><th>写真1</th><td><input id='image1' name='image1' type='file' size='30'></td></tr>"
        + "<tr><th>写真2</th><td><input id='image2' name='image2' type='file' size='30'></td></tr>"
        + "</table>"
        + "<br><br>"
        + "<input type='button' value='次へ' onclick='map.getInfoWindow().selectTab(1)'>"
        + "<input type='button' value='キャンセル' onclick='cancelClinic()'>"
        + "</form>";
    
    html2 = ""
        + "<form name='form_regist2'>"
        + "<table class='infoWindow'>"
        + "<tr><th>評価</th><td>"
        + "<label><input type='checkbox' id='evaluation_item1_new' value='1'>歯科医師スタッフの言葉遣い、服装、対応などが良い</label><br>"
        + "<label><input type='checkbox' id='evaluation_item2_new' value='1'>診療室や待合室の雰囲気が良く、清潔</label><br>"
        + "<label><input type='checkbox' id='evaluation_item3_new' value='1'>会計や受付では声を掛けやすく、質問をしやすい</label><br>"
        + "<label><input type='checkbox' id='evaluation_item4_new' value='1'>治療などの説明は十分で、納得できる</label><br>"
        + "<label><input type='checkbox' id='evaluation_item5_new' value='1'>治療は満足できる</label><br>"
        + "<label><input type='checkbox' id='evaluation_item6_new' value='1'>治療費は納得できる</label><br>"
        + "<label><input type='checkbox' id='evaluation_item7_new' value='1'>医療機器や設備は十分</label><br>"
        + "<label><input type='checkbox' id='evaluation_item8_new' value='1'>診療までの待ち時間は我慢できる範囲</label><br>"
        + "<label><input type='checkbox' id='evaluation_item9_new' value='1'>会計の待ち時間は我慢できる範囲</label><br>"
        + "<label><input type='checkbox' id='evaluation_item10_new' value='1'>家族や友人などに是非紹介したい</label><br>"
        + "</td></tr>"
        + "<tr><th>コメント</th><td><textarea id='evaluation_new' name='evaluation' rows='3' cols='40'></textarea></td></tr>"
        + "</table>"
        + "<input type='button' value='戻る' onclick='map.getInfoWindow().selectTab(0)'>"
        + "<input type='button' value='登録する' onclick='registClinic()'>"
        + "<input type='button' value='キャンセル' onclick='cancelClinic()'>"
        + "</form>";
    
    //メニューを隠す
    rmenu.style.visibility = "hidden";

    //マーカー表示
    newMarker = new GMarker(latLng);
    mgr.addMarker(newMarker, markerMinZoom, markerMaxZoom);
    mgr.refresh();
    //map.addOverlay(marker);
    GEvent.addListener(newMarker, "infowindowclose", function() {
        if (newMarker) {
            mgr.removeMarker(newMarker);
		    mgr.refresh();
            newMarker = null;
        }
    });
    var infoTabs = [
        new GInfoWindowTab("情報", "<div class='infoWindow'>" + html + "</div>"),
        new GInfoWindowTab("評価する", "<div class='infoWindow'>" + html2 + "</div>")
    ];
    newMarker.openInfoWindowTabsHtml(infoTabs);
    
    //
    getAddressByLatLng(latLng, "address1", "address2", "address3");

}

//-------------------------------------------------------------------------
//都道府県選択
//-------------------------------------------------------------------------
function getKenSelect(ken) {

    var html;
    
    html = "<select name='address1' id='address1'>";
    
    for (var i = 0; i < kenList.length; ++i) {
    	
    	var selected = "";
    	if (kenList[i] == ken) {
    		selected = " selected";
    	}
    	
        html += "<option value='" + kenList[i] + "'" + selected + ">" + kenList[i] + "</option>";
    }
    html += "</select>";
    
    return html;

}

//-------------------------------------------------------------------------
//クリニック情報ウィンドウを表示する
//-------------------------------------------------------------------------
function openClinicWindow(marker, clinic_id) {

    //情報ウィンドウの中身取得
    var $params = "clinic_id=" + clinic_id;
    
    new Ajax.Request('get_clinic_info.php',
        {
            method: 'post',
            onComplete: getResponse,
            parameters: $params
        });
    function getResponse(req) {
        eval("var result = " + req.responseText);
        
        if (!result.success) {
            return;
        }
        
        var clinic = result.clinic;
        
        var image0 = "";
        if (clinic.image0) {
            //イメージあり
//            image0 = "<img width='80' height='60' src='clinic_image.php?clinic_id=" + clinic.id + "&index=0' onclick='openClinicImage(" + clinic.id + "," + 0 + ")'>";
            image0 = ""
            	+ "<a href='clinic_image.php?clinic_id=" + clinic.id + "&index=0&t=" + (new Date()).getTime() + "' rel='lightbox[a]' title='写真1'>"
            	+ "<img src='clinic_image.php?clinic_id=" + clinic.id + "&index=0&thumbnail=1&t=" + (new Date()).getTime() + "'>"
            	+ "</a>";
        }
        
        var image1 = "";
        if (clinic.image1) {
            //イメージあり
//            image1 = "<img width='80' height='60' src='clinic_image.php?clinic_id=" + clinic.id + "&index=1' onclick='openClinicImage(" + clinic.id + "," + 1 + ")'>";
            image1 = ""
            	+ "<a href='clinic_image.php?clinic_id=" + clinic.id + "&index=1&t=" + (new Date()).getTime() + "' rel='lightbox[a]' title='写真2'>"
            	+ "<img src='clinic_image.php?clinic_id=" + clinic.id + "&index=1&thumbnail=1&t=" + (new Date()).getTime() + "'>"
            	+ "</a>";
        }
        
        var email = "";
        if (clinic.email) {
            email = "<a href='mailto:" + clinic.email + "'>" + clinic.email + "</a>";
        }
        
        var url = "";
        if (clinic.url) {
            url = "<a href='" + clinic.url + "' target='_blank'>" + clinic.url + "</a>";
        }
        
        var htmlIcon = "";
        if (clinic.icon_no > 0) {
        	htmlIcon += "<img src='" + getIcon(clinic.icon_no, clinic.evaluation_rank, 1) + "'>";
        }
        if (clinic.icon_no2 > 0) {
        	htmlIcon += "<img src='" + getIcon(clinic.icon_no2, clinic.evaluation_rank, 1) + "'>";
        }
        if (clinic.icon_no3 > 0) {
        	htmlIcon += "<img src='" + getIcon(clinic.icon_no3, clinic.evaluation_rank, 1) + "'>";
        }
        if (clinic.icon_no4 > 0) {
        	htmlIcon += "<img src='" + getIcon(clinic.icon_no4, clinic.evaluation_rank, 1) + "'>";
        }
        
        var htmlClinicInfo = "";
        if (clinic.approval_flag == 0) {
        	//未承認
        	htmlClinicInfo = "<div>承認されていません。</div>";
        }
        var address = "";
        if (clinic.zip_code != "") {
        	address = "〒" + clinic.zip_code.substr(0, 3) + "-" + clinic.zip_code.substr(3, 4) + " " + clinic.address1 + "<br>" + clinic.address2 + "<br>" + clinic.address3;
        } else {
        	address = clinic.address1 + "<br>" + clinic.address2 + "<br>" + clinic.address3;
        }
        
        var nl2br = function(str){ return str.replace(/\r|\n/,'<br>') };

        htmlClinicInfo += "<table class='infoWindow'>"
            + "<tr><th>総合評価</th><td>"
            + "<img src='" + getStarIcon(clinic.evaluation_rank) + "' onmouseover='return overlib(\"" + getEvaluationTotal(result.evaluation) + "\", CAPTION, \"評価された項目\", WIDTH, 300)' onmouseout='return nd()'>"
            + "</td></tr>"
            + "<tr><th>診療科目</th><td>" + htmlIcon + "</td></tr>"
            + "<tr><th>名称</th><td>" + clinic.clinic_name + "</td></tr>"
            + "<tr><th>住所</th>"
            + "<td>" + address + "</td></tr>"
            + "<tr><th>電話番号</th>"
            + "<td>" + clinic.tel + "</td></tr>"
            + "<tr><th>メールアドレス</th><td>" + email + "</td></tr>"
            + "<tr><th>ホームページ</th><td>" + url + "</td></tr>"
            + "<tr><th>紹介文</th><td>" + nl2br(clinic.introduction) + "</td></tr>"
            + "<tr><td colspan='2'>"
            + image0
            + image1
            + "</td></tr>"
            + "</table>";

        var html1 = "";
        var evaluationList = result.evaluation_list;
        for (var i = 0; i < evaluationList.length; ++i) {
            var evaluation = evaluationList[i];
            var detail = getEvaluationDetail(evaluation);
						if(evaluation.user_name){
	            html1 += ""
 	               + "<table class='infoWindow'>"
 	               + "<tr><th>ユーザーID</th><td>" + evaluation.user_name + "</td></tr>"
 	               + "<tr><th>評価</th><td>"
 	               + "<img src='" + getStarIcon(evaluation.evaluation_rank) + "' onmouseover='return overlib(\"" + detail + "\", CAPTION, \"評価された項目\", WIDTH, 300)' onmouseout='return nd()'>"
 	               + "</td></tr>"
 	               + "<tr><th>コメント</th><td>" + nl2br(evaluation.evaluation) + "</td></tr>"
 	               + "</table><hr>"
 	               ;
						}
						else{
	            html1 += ""
 	               + "<table class='infoWindow'>"
 	               + "<tr><th>ユーザーID</th><td>" + evaluation.user_id + "</td></tr>"
 	               + "<tr><th>評価</th><td>"
 	               + "<img src='" + getStarIcon(evaluation.evaluation_rank) + "' onmouseover='return overlib(\"" + detail + "\", CAPTION, \"評価された項目\", WIDTH, 300)' onmouseout='return nd()'>"
 	               + "</td></tr>"
 	               + "<tr><th>コメント</th><td>" + nl2br(evaluation.evaluation) + "</td></tr>"
 	               + "</table><hr>"
 	               ;
						}
        }

            
        var htmlEvaluation = ""
            + "<div style='width: 400px; height: 300px; overflow-y: scroll; border: solid 1px gray;'>"
            + html1
            + "</div>"
            ;
            
        var htmlEvaluation2;
        if (login) {
        	if (clinic.evaluated > 0) {
        		htmlEvaluation2 = "評価済みです。";
        	} else {
		        htmlEvaluation2 = ""
		            + "<form name='form_regist' action='regist_clinic.php' enctype='multipart/form-data' method='post' target='regist_clinic'>"
		            + "<input type='hidden' name='clinic_id' id='clinic_id' value='" + clinic.id + "' />"
		            + "<input id='user_id' name='user_id' type='hidden' value='" + login_user.id + "' />"
		            + "<table class='infoWindow'>"
		            + "<tr><th>ユーザーID</th><td>" + login_user.user_name + "</td></tr>"
		            + "<tr><th>評価</th><td>"
		            + "<label><input type='checkbox' id='evaluation_item1' value='1'>歯科医師スタッフの言葉遣い、服装、対応などが良い</label><br>"
		            + "<label><input type='checkbox' id='evaluation_item2' value='1'>診療室や待合室の雰囲気が良く、清潔</label><br>"
		            + "<label><input type='checkbox' id='evaluation_item3' value='1'>会計や受付では声を掛けやすく、質問をしやすい</label><br>"
		            + "<label><input type='checkbox' id='evaluation_item4' value='1'>治療などの説明は十分で、納得できる</label><br>"
		            + "<label><input type='checkbox' id='evaluation_item5' value='1'>治療は満足できる</label><br>"
		            + "<label><input type='checkbox' id='evaluation_item6' value='1'>治療費は納得できる</label><br>"
		            + "<label><input type='checkbox' id='evaluation_item7' value='1'>医療機器や設備は十分</label><br>"
		            + "<label><input type='checkbox' id='evaluation_item8' value='1'>診療までの待ち時間は我慢できる範囲</label><br>"
		            + "<label><input type='checkbox' id='evaluation_item9' value='1'>会計の待ち時間は我慢できる範囲</label><br>"
		            + "<label><input type='checkbox' id='evaluation_item10' value='1'>家族や友人などに是非紹介したい</label><br>"
		            + "</td></tr>"
		            + "<tr><th>コメント</th><td><textarea id='evaluation' name='evaluation' rows='3' cols='40'></textarea></td></tr>"
		            + "</table>"
		            + "<input type='button' value='評価する' onclick='evaluateClinic()'>"
		            + "</form>"
		            ;
			}
        } else {
        	htmlEvaluation2 = "ログインが必要です。";
        }

		var tel_parts = clinic.tel.split("-");
		var htmlEdit = "";
		//alert(clinic.user_name + "/" + clinic.user_id + "/" + login_user.id);
		if (login && clinic.user_name == login_user.id) {
		//if (login && clinic.user_id == login_user.id) {
	        htmlEdit = "<form name='form_edit' action='edit_clinic.php' enctype='multipart/form-data' method='post' target='regist_clinic'>"
	        + "<input type='hidden' id='edit' name='edit' value='1'>"
	        + "<input type='hidden' id='clinic_id' name='clinic_id' value='" + clinic.id+ "'>"
	        + "<input type='hidden' id='address1_org' name='address1_org' value=''>"
	        + "<input type='hidden' id='address2_org' name='address1_org' value=''>"
	        + "<input type='hidden' id='address3_org' name='address1_org' value=''>"
	        + "<input id='user_id' name='user_id' type='hidden' value='" + login_user.id + "' />"
	        + "<div>編集<div>"
	        + "<table class='infoWindow'>"
	        + "<tr><th>名称</th><td><input id='clinic_name' name='clinic_name' type='text' size='40' maxlength='100' value='" + clinic.clinic_name + "'></td></tr>"
	        + "<tr><th>住所</th>"
	        + "<td>〒<input id='zip_code#0' name='zip_code#0' type='text' size='4' maxlength='3' value='" + clinic.zip_code.substr(0, 3) + "' onkeyup='getAddress(\"zip_code#0\", \"zip_code#1\", \"address1\", \"address2\", \"address3\");'>-"
	        + "<input id='zip_code#1' name='zip_code#1' type='text' size='5' maxlength='4' value='" + clinic.zip_code.substr(3, 4) + "' onkeyup='getAddress(\"zip_code#0\", \"zip_code#1\", \"address1\", \"address2\", \"address3\");'><br>"
	        + getKenSelect(clinic.address1)
	        + "<input id='address2' name='address2' type='text' size='40' maxlength='60' value='" + clinic.address2 + "'><br>"
	        + "<input id='address3' name='address3' type='text' size='40' maxlength='60' value='" + clinic.address3 + "'></td></tr>"
	        + "<tr><th>電話番号</th>"
	        + "<td><input id='tel#0' name='tel#0' type='text' size='5' maxlength='4' value='" + tel_parts[0] + "'>"
	        + "-<input id='tel#1' name='tel#1' type='text' size='5' maxlength='4' value='" + tel_parts[1] + "'>"
	        + "-<input id='tel#2' name='tel#2' type='text' size='5' maxlength='4' value='" + tel_parts[2] + "'></td></tr>"
	        + "<tr><th>メールアドレス</th><td><input id='email' name='email' type='text' size='40' maxlength='100' value='" + clinic.email + "'></td></tr>"
	        + "<tr><th>ホームページ</th><td><input id='url' name='url' type='text' size='40' maxlength='100' value='" + clinic.url + "'></td></tr>"
	        + "<tr><th>紹介文</th><td><textarea id='introduction' name='introduction' rows='3' cols='40'>" + clinic.introduction + "</textarea></td></tr>"
	        + "<tr><th>診療科目1</th><td>"
	        + "<select id='icon_no' name='icon_no' style='width: 250px;'>"
	        + getIconNoOption(clinic.icon_no)
	        + "</select>"
	        + "</td></tr>"
	        + "<tr><th>診療科目2</th><td>"
	        + "<select id='icon_no2' name='icon_no2' style='width: 250px;'>"
	        + getIconNoOption(clinic.icon_no2)
	        + "</select>"
	        + "</td></tr>"
	        + "<tr><th>診療科目3</th><td>"
	        + "<select id='icon_no3' name='icon_no3' style='width: 250px;'>"
	        + getIconNoOption(clinic.icon_no3)
	        + "</select>"
	        + "</td></tr>"
	        + "<tr><th>診療科目4</th><td>"
	        + "<select id='icon_no4' name='icon_no4' style='width: 250px;'>"
	        + getIconNoOption(clinic.icon_no4)
	        + "</select>"
	        + "</td></tr>"
	        + "<tr><th>写真1</th><td><input id='image1' name='image1' type='file' size='30'></td></tr>"
	        + "<tr><th>写真2</th><td><input id='image2' name='image2' type='file' size='30'></td></tr>"
	        + "</table>"
	        + "<br><br>"
	        + "<input type='button' value='登録する' onclick='editClinic()'>"
	        + "</form>";

	        var infoTabs = [
	            new GInfoWindowTab("情報", "<div class='infoWindow'>" + htmlClinicInfo + "</div>"),
	            new GInfoWindowTab("編集", "<div class='infoWindow'>" + htmlEdit + "</div>"),
	            new GInfoWindowTab("評価を見る", "<div class='infoWindow'>" + htmlEvaluation + "</div>"),
	            new GInfoWindowTab("評価する", "<div class='infoWindow'>" + htmlEvaluation2 + "</div>")
	        ];
        } else {
	        var infoTabs = [
	            new GInfoWindowTab("情報", "<div class='infoWindow'>" + htmlClinicInfo + "</div>"),
	            new GInfoWindowTab("評価を見る", "<div class='infoWindow'>" + htmlEvaluation + "</div>"),
	            new GInfoWindowTab("評価する", "<div class='infoWindow'>" + htmlEvaluation2 + "</div>")
	        ];
        }

//		infoTabs[] = new GInfoWindowTab("情報", "<div class='infoWindow'>" + htmlClinicInfo + "</div>");
//		if (login && clinic.user_id == login_user.id) {
//			infoTabs[] = new GInfoWindowTab("編集", "<div class='infoWindow'>" + htmlEdit + "</div>");
//		}
//		infoTabs[] = new GInfoWindowTab("評価を見る", "<div class='infoWindow'>" + htmlEvaluation + "</div>");
//		if (clinic.evaluated > 0) {
//			infoTabs[] = new GInfoWindowTab("評価する", "<div class='infoWindow'>" + htmlEvaluation2 + "</div>");
//	    };
        
        //情報ウィンドウ
        marker.openInfoWindowTabsHtml(infoTabs);
        
//        initLightbox();
        myLightbox.updateImageList();
    }
    
}

//-------------------------------------------------------------------------
//診療科目Option
//-------------------------------------------------------------------------
function getIconNoOption(icon_no) {
	
	var icons = { 0: "(選択して下さい)", 1: "歯科全般", 2: "審美歯科(ホワイトニング含む)", 3: "矯正歯科", 4: "その他(衛生士だけのクリーニングサロン的なものも含む)" };
	var s = "";

	for (var key in icons) {
		
		var selected = "";
		if (icon_no == key) {
			selected = " selected";
		}
		var opt = "<option value='" + key + "'" + selected + ">" + icons[key] + "</option>";
		s += opt;
	}
	return s;
}

//-------------------------------------------------------------------------
//評価ツールチップ
//-------------------------------------------------------------------------
function getEvaluationDetail(evaluation) {

	var s = "";
	
	if (evaluation.evaluation_item1 == 1) {
		s += "歯科医師スタッフの言葉遣い、服装、対応などが良い<br>";
	}
	if (evaluation.evaluation_item2 == 1) {
		s += "診療室や待合室の雰囲気が良く、清潔<br>";
	}
	if (evaluation.evaluation_item3 == 1) {
		s += "会計や受付では声を掛けやすく、質問をしやすい<br>";
	}
	if (evaluation.evaluation_item4 == 1) {
		s += "治療などの説明は十分で、納得できる<br>";
	}
	if (evaluation.evaluation_item5 == 1) {
		s += "治療は満足できる<br>";
	}
	if (evaluation.evaluation_item6 == 1) {
		s += "治療費は納得できる<br>";
	}
	if (evaluation.evaluation_item7 == 1) {
		s += "医療機器や設備は十分<br>";
	}
	if (evaluation.evaluation_item8 == 1) {
		s += "診療までの待ち時間は我慢できる範囲<br>";
	}
	if (evaluation.evaluation_item9 == 1) {
		s += "会計の待ち時間は我慢できる範囲<br>";
	}
	if (evaluation.evaluation_item10 == 1) {
		s += "家族や友人などに是非紹介したい<br>";
	}
	return s;
}

//-------------------------------------------------------------------------
//評価ツールチップ
//-------------------------------------------------------------------------
function getEvaluationTotal(evaluation) {

	var s = "";
	
	s += "歯科医師スタッフの言葉遣い、服装、対応などが良い(" + evaluation.evaluation_item1 + "人)<br>";
	s += "診療室や待合室の雰囲気が良く、清潔(" + evaluation.evaluation_item2 + "人)<br>";
	s += "会計や受付では声を掛けやすく、質問をしやすい(" + evaluation.evaluation_item3 + "人)<br>";
	s += "治療などの説明は十分で、納得できる(" + evaluation.evaluation_item4 + "人)<br>";
	s += "治療は満足できる(" + evaluation.evaluation_item5 + "人)<br>";
	s += "治療費は納得できる(" + evaluation.evaluation_item6 + "人)<br>";
	s += "医療機器や設備は十分(" + evaluation.evaluation_item7 + "人)<br>";
	s += "診療までの待ち時間は我慢できる範囲(" + evaluation.evaluation_item8 + "人)<br>";
	s += "会計の待ち時間は我慢できる範囲(" + evaluation.evaluation_item9 + "人)<br>";
	s += "家族や友人などに是非紹介したい(" + evaluation.evaluation_item10 + "人)<br>";
	return s;
}

//-------------------------------------------------------------------------
//評価する
//-------------------------------------------------------------------------
function evaluateClinic() {

	var msg = "";
	
	if (!$F("evaluation")) {
		msg += "コメントを入力してください。\n";
	}

	if (msg) {
		alert(msg);
		return;
	}
	
	

    //情報ウィンドウの中身取得
    var $params = "clinic_id=" + $F("clinic_id")
        + "&user_id=" + $F("user_id")
        + "&evaluation_item1=" + $F("evaluation_item1")
        + "&evaluation_item2=" + $F("evaluation_item2")
        + "&evaluation_item3=" + $F("evaluation_item3")
        + "&evaluation_item4=" + $F("evaluation_item4")
        + "&evaluation_item5=" + $F("evaluation_item5")
        + "&evaluation_item6=" + $F("evaluation_item6")
        + "&evaluation_item7=" + $F("evaluation_item7")
        + "&evaluation_item8=" + $F("evaluation_item8")
        + "&evaluation_item9=" + $F("evaluation_item9")
        + "&evaluation_item10=" + $F("evaluation_item10")
        + "&evaluation=" + $F("evaluation")
        ;
        
    new Ajax.Request('evaluate_clinic.php',
        {
            method: 'post',
            onComplete: getResponse,
            parameters: $params
        });
    
    function getResponse(req) {
        //alert(req.responseText);
        eval("var result = " + req.responseText);
        
        if (result.success) {

	        alert("登録しました。");
	        var infoWind = map.getInfoWindow();
	        if (infoWind) {
	            infoWind.hide();
	        }
	        
	        //アイコンの更新
		    var marker = markerList[result.clinic.id];
	        if (marker) {
	            mgr.removeMarker(marker);
	            marker = null;
	        }
	        marker = createMarker(result.clinic);
	        markerList[result.clinic.id] = marker;

		    var zoom = map.getZoom();
		    if (zoom < markerMinZoom) {
		        mgr.addMarker(marker, highRankMinZoom, highRankMaxZoom);
		    } else {
		        mgr.addMarker(marker, markerMinZoom, markerMaxZoom);
		    }
		} else {
	        alert(result.message);
		}
    }

}

//-------------------------------------------------------------------------
//新規クリニック登録追加
//-------------------------------------------------------------------------
function registClinic() {

	//入力チェック
	var msg = "";
	var icon_no = $F("icon_no");
	var icon_no2 = $F("icon_no2");
	var icon_no3 = $F("icon_no3");
	var icon_no4 = $F("icon_no4");
	
	if (!$F("clinic_name")) {
		msg += "名称を入力して下さい。\n";
	}
	
//	if (!$F("zip_code#0") || !$F("zip_code#1")) {
//		msg += "郵便番号を入力して下さい。\n";
//	}

	if (!$F("address1") || !$F("address2")) {
		msg += "住所を入力して下さい。\n";
	}

	var address1 = $F("address1");
	var address2 = $F("address2");
	var address3 = $F("address3");
	var address1_org = $F("address1_org");
	var address2_org = $F("address2_org");
	var address3_org = $F("address3_org");
	if ((address1_org || address2_org) && (address1_org != address1 || address2_org != address2.substr(0, address2_org.length))) {
		msg += "住所が正しくありません。\n";
	}

	if (!$F("tel#0") || !$F("tel#1") || !$F("tel#2")) {
		msg += "電話番号を入力して下さい。\n";
	}

	if (!$F("introduction")) {
		msg += "紹介文を入力して下さい。\n";
	}

	if ($F("icon_no") == 0) {
		msg += "診療科目1を入力して下さい。\n";
	}

	if ((icon_no != 0 && (icon_no == icon_no2 || icon_no == icon_no3 || icon_no == icon_no4))
		|| (icon_no2 != 0 && (icon_no2 == icon_no3 || icon_no2 == icon_no4))
		|| (icon_no3 != 0 && icon_no3 == icon_no4)) {

		msg += "診療科目が重複しています。\n";
	}
	
	if (!$F("evaluation_new")) {
		msg += "コメントを入力してください。\n";
	}
	
	if (msg) {
		alert(msg);
		return;
	}
	
	//評価
	document.form_regist.evaluation_item1.value = $F("evaluation_item1_new");
	document.form_regist.evaluation_item2.value = $F("evaluation_item2_new");
	document.form_regist.evaluation_item3.value = $F("evaluation_item3_new");
	document.form_regist.evaluation_item4.value = $F("evaluation_item4_new");
	document.form_regist.evaluation_item5.value = $F("evaluation_item5_new");
	document.form_regist.evaluation_item6.value = $F("evaluation_item6_new");
	document.form_regist.evaluation_item7.value = $F("evaluation_item7_new");
	document.form_regist.evaluation_item8.value = $F("evaluation_item8_new");
	document.form_regist.evaluation_item9.value = $F("evaluation_item9_new");
	document.form_regist.evaluation_item10.value = $F("evaluation_item10_new");
	document.form_regist.evaluation.value = $F("evaluation_new");
	
    //ファイルがあるのでpostで登録
    var frm = document.form_regist;

    frm.submit();
    
}

//-------------------------------------------------------------------------
//クリニック編集
//-------------------------------------------------------------------------
function editClinic() {

	//入力チェック
	
	var msg = "";
	var icon_no = $F("icon_no");
	var icon_no2 = $F("icon_no2");
	var icon_no3 = $F("icon_no3");
	var icon_no4 = $F("icon_no4");
	
	if (!$F("clinic_name")) {
		msg += "名称を入力して下さい。\n";
	}
	
//	if (!$F("zip_code#0") || !$F("zip_code#1")) {
//		msg += "郵便番号を入力して下さい。\n";
//	}

	if (!$F("address1") || !$F("address2")) {
		msg += "住所を入力して下さい。\n";
	}

	var address1 = $F("address1");
	var address2 = $F("address2");
	var address3 = $F("address3");
	var address1_org = $F("address1_org");
	var address2_org = $F("address2_org");
	var address3_org = $F("address3_org");
	if ((address1_org || address2_org) && (address1_org != address1 || address2_org != address2.substr(0, address2_org.length))) {
		msg += "住所が正しくありません。\n";
	}

//	if (!$F("tel#0") || !$F("tel#1") || !$F("tel#2")) {
//		msg += "電話番号を入力して下さい。\n";
//	}

	if (!$F("introduction")) {
		msg += "紹介文を入力して下さい。\n";
	}

	if ($F("icon_no") == 0) {
		msg += "診療科目1を入力して下さい。\n";
	}

	if ((icon_no != 0 && (icon_no == icon_no2 || icon_no == icon_no3 || icon_no == icon_no4))
		|| (icon_no2 != 0 && (icon_no2 == icon_no3 || icon_no2 == icon_no4))
		|| (icon_no3 != 0 && icon_no3 == icon_no4)) {

		msg += "診療科目が重複しています。\n";
	}
	
	if (msg) {
		alert(msg);
		return;
	}
	
    //ファイルがあるのでpostで登録
    var frm = document.form_edit;

    frm.submit();
    
}

//-------------------------------------------------------------------------
//クリニックキャンセル
//-------------------------------------------------------------------------
function cancelClinic() {
    var infoWind = map.getInfoWindow();
    if (infoWind) {
        infoWind.hide();
    }
    
    if (newMarker) {
        mgr.removeMarker(newMarker);
        //mgr.refresh();
        newMarker = null;
    }
}

//-------------------------------------------------------------------------
//regist_clinic がロードされた
//-------------------------------------------------------------------------
function registClinicFrameLoaded() {
    var doc;
    
    var registClinic = $("regist_clinic");
    var doc = registClinic.contentWindow.document;
    var text = doc.body.innerHTML;
    if (text == "") {
        return;
    }
    eval("var result = " + text);
    if (result.success) {
        alert("登録しました。");
        var infoWind = map.getInfoWindow();
        if (infoWind) {
            infoWind.hide();
        }
        
        if (newMarker) {
            mgr.removeMarker(newMarker);
            mgr.refresh();
            newMarker = null;
        }

        var marker = createMarker(result.clinic);
        mgr.addMarker(marker, markerMinZoom, markerMaxZoom);

        //連想記憶する
        markerList[result.clinic.id] = marker;
        
    } else {
        alert(result.message);
    }

}

//-------------------------------------------------------------------------
//表示更新
//-------------------------------------------------------------------------
function updateMap() {

    //画面の左上、右下の位置
    var size = map.getSize();
    var latLng1 = map.fromContainerPixelToLatLng(new GPoint(0, 0));
    var latLng2 = map.fromContainerPixelToLatLng(new GPoint(size.width, size.height));

//    var $params = "latitude1=" + latLng1.lat()
//        + "&longitude1=" + latLng1.lng()
//        + "&latitude2=" + latLng2.lat()
//        + "&longitude2=" + latLng2.lng();
    
    new Ajax.Request('get_clinic_list.php',
        {
            method: 'post',
            onComplete: getResponse
        });
    
    function getResponse(req) {
        //alert(req.responseText);
				//var result = req.responseText;
				eval("var result = " + req.responseText);
        var clinic_list = result.clinic_list;
        var high_rank_list = result.high_rank_list;
        
//        mgr = new GMarkerManager(map);
        mgr = new MarkerManager(map);
        var i;
        var markers = [];
        for (i = 0; i < clinic_list.length; ++i)  {
            
            var clinic = clinic_list[i];
            var marker = createMarker(clinic);
            markers.push(marker);
            
            //連想記憶する
            markerList[clinic.id] = marker;
            
        }
        mgr.addMarkers(markers, markerMinZoom, markerMaxZoom);
        
        markers = [];
        for (i = 0; i < high_rank_list.length; ++i)  {
            
            var clinic = high_rank_list[i];
            var marker = createMarker(clinic);
            markers.push(marker);
            
            //連想記憶する
            markerList[clinic.id] = marker;
            
        }
        mgr.addMarkers(markers, highRankMinZoom, highRankMaxZoom);
        
        mgr.refresh();

        //新着更新
       updateNewestList();

        //ランキング
        updateRankingList();
        
    }
}

//-------------------------------------------------------------------------
//マーカー作成
//-------------------------------------------------------------------------
function createMarker(clinic) {
    //アイコン
    var icon = new GIcon();
    icon.image = getIcon(clinic.icon_no, clinic.evaluation_rank, clinic.approval_flag);
    icon.iconSize = new GSize(41, 41);
//    icon.shadowSize = new GSize(32, 32);
//    icon.printImage = "/icons/icon" + clinic.icon_no + ".png";
//    icon.mozPrintImage = "/icons/icon" + clinic.icon_no + ".png";
//    icon.printShadow = "/icons/icon" + clinic.icon_no + ".png";
   icon.iconAnchor = new GPoint(16, 32);
   icon.infoWindowAnchor = new GPoint(16, 0);

    var latLng = new GLatLng(clinic.latitude, clinic.longitude);
    var marker = new GMarker(latLng, { title: clinic.clinic_name, icon: icon, draggable: false });

    //ダブルクリックイベント
    GEvent.addListener(marker, "click", function() {
        openClinicWindow(marker, clinic.id);
    });
    
    GEvent.addListener(marker, "dragend", function() {
        
        if (!confirm("移動しますか？")) {
            marker.setPoint(new GLatLng(clinic.latitude, clinic.longitude));
            //map.setCenter(marker.getPoint());
            return;
        }
        
        var latlng = marker.getPoint();
        clinic.latitude = latlng.lat();
        clinic.longitude = latlng.lng();
        
        //変更情報
        var $params = "clinic_id=" + clinic.id
            + "&latitude=" + clinic.latitude
            + "&longitude=" + clinic.longitude
            ;
            
        new Ajax.Request('update_clinic.php',
            {
                method: 'post',
                onComplete: getResponse,
                parameters: $params
            });
        
        function getResponse(req) {
            eval("var result = " + req.responseText);
        }
        
    });
    
    return marker;
}

//-------------------------------------------------------------------------
//ズームイン
//-------------------------------------------------------------------------
function rmenuZoomIn() {
    
    rmenu.style.visibility = "hidden";
    map.zoomIn();
}

//-------------------------------------------------------------------------
//ズームアウト
//-------------------------------------------------------------------------
function rmenuZoomOut() {
    
    rmenu.style.visibility = "hidden";
    map.zoomOut();
}

//-------------------------------------------------------------------------
//センター
//-------------------------------------------------------------------------
function rmenuCenter(latLng) {
    
    rmenu.style.visibility = "hidden";
    map.panTo(latLng);
}

//-------------------------------------------------------------------------
//
//-------------------------------------------------------------------------
function rmenuMouseOver(element) {
    
    element.style.backgroundColor = "#87ceeb";
}

//-------------------------------------------------------------------------
//
//-------------------------------------------------------------------------
function rmenuMouseOut(element) {
    element.style.backgroundColor = "white";
}

//-------------------------------------------------------------------------
//住所検索
//-------------------------------------------------------------------------
function searchAddress() {
    
    var frm = document.frmSearchAddress;
    var address = frm.address.value;
    geocoder.getLocations(address, getResponse);
    return false;
    function getResponse(locations) {
        if (locations && locations.Placemark && locations.Placemark.length) {
						//alert(locations.Status.code+" / "+locations.name + " / " + locations.Placemark.length);
            var divAddressList = $("addressList");
            var html = "";
						var plength = locations.Placemark.length;
						if(plength == 1){
								var placemark = locations.Placemark[0];
								var text = placemark.address;
								var latlng = new GLatLng(placemark.Point.coordinates[1], placemark.Point.coordinates[0]);
								moveMap(latlng.lat(),latlng.lng(),text);
						}
						else{
							for (var i = 0; i < plength; ++i) {
								var placemark = locations.Placemark[i];
//              var text = placemark.AddressDetails.Country.AddressLine[0];
								var text = placemark.address;
								var latlng = new GLatLng(placemark.Point.coordinates[1], placemark.Point.coordinates[0]);
								html += "<div class='addressListItem' onclick='moveMap(" + latlng.lat() + "," + latlng.lng() + ",\"" + text + "\")'>" + text + "</div>";
							}
						}
            divAddressList.innerHTML = html;
        }else{
					if(address){
						alert(address + "は見つかりませんでした。");
					}
					else{
						alert("住所が指定されていません。");
					}
        }
    }
}

//-------------------------------------------------------------------------
//moveMap
//-------------------------------------------------------------------------
function moveMap(lat, lng, address) {

    var latlng = new GLatLng(lat, lng);
    map.setCenter(latlng, 16);
    map.openInfoWindowHtml(latlng, "住所:" + address);

}

//-------------------------------------------------------------------------
//新着表示
//-------------------------------------------------------------------------
function updateNewestList() {

    var $params = "days=14"
        + "&size=10";
    
    new Ajax.Request('get_newest_list.php',
        {
            method: 'post',
            onComplete: getResponse,
            parameters: $params
        });
    
    function getResponse(req) {
        //alert(req.responseText);
        eval("var result = " + req.responseText);


        var html = "<table width='124' border='0' cellspacing='0' cellpadding='3'>";
        for (var i = 0; i < result.length; ++i)  {
            
            var clinic = result[i];
            
//            var text = "<div class='newestListItem' onclick='selectClinic(" + clinic.id + ")'>"+ (i + 1) + ". " + clinic.clinic_name + "</div>";
			var text = "";
			text += "<tr>";
			text += "<td><font color='black'><img src='img/yajirusi_map.gif' alt='' height='8' width='6' align='absmiddle' border='0' hspace='5'><a href='#' onclick='selectClinic(" + clinic.id + ")'>" + clinic.clinic_name + "</a></font></td>";
			text += "</tr>";
			
            html += text;
            
        }
        $("newestList").innerHTML = html;
        
    }
}

//-------------------------------------------------------------------------
//ランキング表示
//-------------------------------------------------------------------------
function updateRankingList() {

    //画面の左上、右下の位置
    var size = map.getSize();
    var latLng1 = map.fromContainerPixelToLatLng(new GPoint(0, 0));
    var latLng2 = map.fromContainerPixelToLatLng(new GPoint(size.width, size.height));

    var $params = "latitude1=" + latLng1.lat()
        + "&longitude1=" + latLng1.lng()
        + "&latitude2=" + latLng2.lat()
        + "&longitude2=" + latLng2.lng()
        + "&size=10";
    
    new Ajax.Request('get_ranking_list.php',
        {
            method: 'post',
            onComplete: getResponse,
            parameters: $params
        });
    
    function getResponse(req) {
        //alert(req.responseText);
        eval("var result = " + req.responseText);

        var html = "<table width='129' border='0' cellspacing='0' cellpadding='1'>";
        for (var i = 0; i < result.length; ++i)  {
            
            var clinic = result[i];
            
//            var text = "<div class='rankingListItem' onclick='selectClinic(" + clinic.id + ")'>"+ (i + 1) + ". " + clinic.clinic_name + "</div>";
			
			var ranking = i + 1;
			var text = "";
			if (i < 3) {
				text += "<tr>";
				text += "<td width='23'>";
				text += "<div align='center'>";
				text += "<font color='black'><img src='img/ranking-_" + ranking + ".gif' alt='" + ranking + "' height='15' width='23' align='baseline' border='0'></font></div>";
				text += "</td>";
				text += "<td id='ranking1'><font color='black'><a href='#' onclick='selectClinic(" + clinic.id + ")'><b>" + clinic.clinic_name + "</b></a></font></td>";
				text += "</tr>";
			} else {
				text += "<tr>";
				text += "<td width='23'>";
				text += "<div align='center'>";
				text += "<font color='#66cc33'><b>" + ranking + "</b></font></div>";
				text += "</td>";
				text += "<td><font color='black'><a href='#' onclick='selectClinic(" + clinic.id + ")'>" + clinic.clinic_name + "</a></font></td>";
				text += "</tr>";
			}

            html += text;
            
        }
        html += "<tr><td colspan='2'>*ランキングは投稿者5人以上の医院が対象</td></tr>";
        html += "</html>";
        $("rankingList").innerHTML = html;
        
    }
    
}

//-------------------------------------------------------------------------
//-------------------------------------------------------------------------
function selectClinic(clinic_id) {

    var marker = markerList[clinic_id];
    
    var zoom = map.getZoom();
//    if (zoom < markerMinZoom) {
        zoom = markerMinZoom;
        map.setCenter(marker.getPoint(), zoom);
//    }
    openClinicWindow(marker, clinic_id);

}

//-------------------------------------------------------------------------
//ログイン
//-------------------------------------------------------------------------
function loginSystem() {
    
    var frm = document.frmLogin;
    
    var $params = "login_id=" + frm.login_id.value
        + "&password=" + frm.password.value;
    
    new Ajax.Request('login.php',
        {
            method: 'post',
            onComplete: getResponse,
            parameters: $params
        });
    
    function getResponse(req) {
        //alert(req.responseText);
        eval("var result = " + req.responseText);
        if (!result.success) {
            alert("ユーザ名または、パスワードが違います。");
        }
        updateLoginStatus();
    }

    return false;
}

//-------------------------------------------------------------------------
//ログアウトシステム
//-------------------------------------------------------------------------
function logoutSystem() {
    
    var frm = document.frmLogin;
    
    new Ajax.Request('logout.php',
        {
            method: 'post',
            onComplete: getResponse
        });
    
    function getResponse(req) {
        eval("var result = " + req.responseText);
        updateLoginStatus();
    }

    return false;
}

//--------------------------------------------------------------------------
//	ログインステータス更新																									
//--------------------------------------------------------------------------

function updateLoginStatus(){
	var html;
	login = false;
	var frm = document.frmLogin;
	new Ajax.Request('get_login_status.php',{ method: 'post', onComplete: getResponse });
	function getResponse(req) {
		eval("var result = " + req.responseText);
		login = result.success;
				//alert(login);
        if(!login) {
            html = "<form name='frmLogin' action='index.php' method='post'>"
             + "ログインID:<br><input type='text' name='login_id'><br>"
             + "パスワード:<br><input type='password' name='password'><br>"
             + "<input type='submit' name='LoginButton' value='ログイン'>"
             + "</form>";
             $("loginStatus").innerHTML = html;
        } else {
            html = "<form name='frmLogin' action='index.php' method='post'>"
             + "ユーザーID: " + result.user.id + "<br>"
             + "<input type='hidden' c='logout' value='logout'>"
             + "<input type='submit' name='LogoutButton' value='ログアウト'>"
             + "</form>";
             $("loginStatus").innerHTML = html;
             
	        login_user = result.user;
        }
    }
}

/*
function updateLoginStatus(){
	var html;
	login = false;
	var frm = document.frmLogin;
	new Ajax.Request('get_login_status.php',{ method: 'post', onComplete: getResponse });
	function getResponse(req) {
		eval("var result = " + req.responseText);
		login = result.success;
alert(login);
        if (!login) {
//            html = "<form name='frmLogin' onsubmit='return loginSystem();'>"
//             + "ログインID:<input type='text' name='login_id'>&nbsp;"
//             + "パスワード:<input type='password' name='password'>&nbsp;"
//             + "<input type='submit' value='ログイン'>"
//             + "</form>";
             html = '';
             $("loginStatus").innerHTML = html;
        } else {
            html = "<form name='frmLogin' onsubmit='return logoutSystem();'>"
        //   + "ユーザーID: " + result.user.user_name + "&nbsp;"
             + "ユーザーID: " + result.user + "&nbsp;"
//             + "<input type='submit' value='ログアウト'>"
             + "</form>";
             
             $("loginStatus").innerHTML = html;
             
	        login_user = result.user;
        }
    }
}
*/
//-------------------------------------------------------------------------
//星アイコン取得
//-------------------------------------------------------------------------
function getStarIcon(rank) {
    
    return "/icons/star" + Math.round(rank) + ".gif";
}

//-------------------------------------------------------------------------
//アイコン取得
//-------------------------------------------------------------------------
function getIcon(icon_no, rank, approval_flag) {
    
    if (approval_flag == 0) {
	    return "/icons/unregistration.png";
    }
    
    if (rank == 0) {
    	rank = 1;
    }
    rank = Math.round(rank);
    
    return "/icons/icon" + icon_no + "" + rank + ".png";
}


//-------------------------------------------------------------------------
//イメージ
//-------------------------------------------------------------------------
function openClinicImage(clinic_id, index) {
    
    var url = "clinic_image.php?clinic_id=" + clinic_id + "&index=" + index;
    window.open(url);
}

var cur_zip_code = "";
var pref;
var addr1;
var addr2;

//-------------------------------------------------------------------------
//住所検索
//-------------------------------------------------------------------------
function getAddress(zip1_name, zip2_name, pref_name, addr1_name, addr2_name) {
    
    var zip_code;
    var zip1 = $(zip1_name);
    var zip2 = $(zip2_name);

    var pref = $(pref_name);
    var addr1 = $(addr1_name);
    var addr2 = $(addr2_name);

    if (zip1.value.length != 3 || zip2.value.length != 4) {
        return;
    }
    
    zip_code = zip1.value + zip2.value;
    if (cur_zip_code == zip_code) {
        return;
    }
    cur_zip_code = zip_code;

    $params = 'zip_code=' + zip_code;
    
    new Ajax.Request('get_address.php',
        {
            method: 'get',
            onComplete: getResponse,
            onFailure: getError,
            parameters: $params
        });

	function getError(req) {
	    alert(req.responseText);
	}

	function getResponse(req) {

        eval("var result = " + req.responseText);

	    if (result.success && addr1.value == "" && addr2.value == "") {
	        pref.value = result.zipcode.address1;
	        addr1.value = result.zipcode.address2 + result.zipcode.address3;
	        addr2.value = "";
	    }
	}
}

//-------------------------------------------------------------------------
//緯度経度から住所を取得
//-------------------------------------------------------------------------
function getAddressByLatLng(latLng, pref_name, addr1_name, addr2_name) {

	var lat = latLng.lat();
	var lng = latLng.lng();
	
    var pref = $(pref_name);
    var addr1 = $(addr1_name);
    var addr2 = $(addr2_name);
    var pref_org = $(pref_name + "_org");
    var addr1_org = $(addr1_name + "_org");
    var addr2_org = $(addr2_name + "_org");

	//測地系を変更する(世界->日本)
	
	lng = lng + lat * 0.000046047 + lng * 0.000083049 - 0.010041;
	lat = lat + lat * 0.00010696 - lng * 0.000017467 - 0.0046020;
	
	$params = 'lat=' + lat + '&lng=' + lng;
	
    new Ajax.Request('get_addressbylatlng.php',
		{
			method: 'get',
			onComplete: getResponse,
			onFailure: getError,
			parameters: $params
		});
	
	function getError(req) {
	    alert(req.responseText);
	}

	function getResponse(req) {

        eval("var result = " + req.responseText);

	    if (result.success) {
	        pref.value = result.address.address1;
	        addr1.value = result.address.address2 + result.address.address3;
	        addr2.value = "";
	        if (pref_org) {
		        pref_org.value = pref.value;
		    }
	        if (addr1_org) {
		        addr1_org.value = addr1.value;
		    }
		    if (addr2_org) {
	        	addr2_org.value = addr2.value;
	        }
	    }
	}
}







// もともと↓だった・・・
///////////////////////////////////////////////////////////////////////////////////////////
// <script src="http://www.apagard.com/contents/js/navi.js" type="text/javascript"></script>
///////////////////////////////////////////////////////////////////////////////////////////
//  ロールオーバースクリプト
//　Standards Compliant Rollover Script
//　Author : Daniel Nolan
//　http://www.bleedingego.co.uk/webdev.php

function initRollovers() {
	if (!document.getElementById) return
	
	var aPreLoad = new Array();
	var sTempSrc;
	var aImages = document.getElementsByTagName('img');

	for (var i = 0; i < aImages.length; i++) {		
		if (aImages[i].className == 'imgover') {
			var src = aImages[i].getAttribute('src');
			var ftype = src.substring(src.lastIndexOf('.'), src.length);
			var hsrc = src.replace(ftype, '_o'+ftype);

			aImages[i].setAttribute('hsrc', hsrc);
			
			aPreLoad[i] = new Image();
			aPreLoad[i].src = hsrc;
			
			aImages[i].onmouseover = function() {
				sTempSrc = this.getAttribute('src');
				this.setAttribute('src', this.getAttribute('hsrc'));
			}	
			
			aImages[i].onmouseout = function() {
				if (!sTempSrc) sTempSrc = this.getAttribute('src').replace('_o'+ftype, ftype);
				this.setAttribute('src', sTempSrc);
			}
		}
	}
}

///////////////////////////////////////////////////////////////////////////////////////////

