/////////////////////////////////////////////////////////////////////////////////
//  *** Googlemap search for the sex industry ***
//
// +++ ATTENTION +++
// This program uses prototype.js library, so don't separate from this system.                 
// +++ ATTENTIION +++
//
//
/////////////////////////////////////////////////////////////////////////////////

//<![CDATA[
var map;        //Googlemapのこと
var paras;
var customDiv;

//マーカー作成
var icon = new GIcon(); //GIconがオリジナルマーカー宣言
icon.image = "images/icon.png"; //アイコンの位置
icon.iconSize = new GSize(54, 75); //サイズ
icon.iconAnchor = new GPoint(27, 75); //アイコンの始点
icon.infoWindowAnchor = new GPoint(27, 4); //吹き出しの出る位置
var icon_center = new GIcon(icon,"images/icon_center.png");

function customBox() { }
customBox.prototype = new GOverlay();
customBox.prototype.initialize = function(map) {map.getPane(G_MAP_MAP_PANE).appendChild(customDiv);}
customBox.prototype.redraw = function(force){}


function loading(pointx, pointy, pointz, icon_id) {
	$("shopid").value = icon_id;
	//座標設定
	map = new GMap2($("map"));

	point = new GLatLng(pointy, pointx);
	map.setCenter(point, pointz);
	ctrlObj = new GOverviewMapControl(new GSize(150,150));
	map.addControl(ctrlObj);
	map.addControl(new GMapTypeControl(true)); 
	map.addControl(new GLargeMapControl());
	gGeo = new GClientGeocoder();

	map.enableContinuousZoom();


//	enableGoogleBar();

	GEvent.addListener(map, "dragend", getzahyo); //ドラッグされた時のイベント GEvent.addListenerはGooglemapイベント

	new Ajax.Request("mark.php?" +(new Date).getTime(),{ onSuccess:Marking,onFailure: displayError});

	function Marking(mark_obj){ //データゲットが成功したら
		data = mark_obj.responseText.split("[data]");
		
		var vmax = data.length;
		var win_html;
		var area = map.getBounds();

        var gmarkersA = [];
        var mm = new GMarkerManager(map, {borderPadding:1});

		map.clearOverlays(); 

		var datas = new Array('選んでください', '麺類(ラーメン、うどん、蕎麦 etc…)', '肉料理(焼肉、焼鳥 etc…)', '和・洋菓子(ケーキ・お団子 etc…)', 'その他（寿司・鍋・お好み焼き etc…）', '行っちゃダメ！(マズイ・ボッタクリ etc…)');

		for(i=0; i<vmax; i++){
			mark_data = data[i].split("[zan]");
			point = new GLatLng(mark_data[0], mark_data[1]);
			var smax = mark_data.length-1;
			if(area.contains(point)){//マップの範囲内のみ表示
				win_html = "";
				for(k=2; k<smax; k+=4){ 
					win_html += '<div align=left style="color:#ff9900;font-size:12px;">'+ datas[mark_data[k+2]] + '</div>';
					win_html += '<div align=left>' + mark_data[k+1] + '</div>';
	//				if(mark_data[k+3] != 0)win_html += '<div align=left><img src="img_shop/'+mark_data[k+3]+'" /></div>';
					win_html += '<div align=right style="font-size:12px;"><a href="index.php?shopid='+ mark_data[k] +'">詳細情報はコチラ≫</a></div>';

					win_html += '<div align=right style="font-size:12px;"><a href="streetview.php?shopid='+ mark_data[k] +'">StreetViewはコチラ≫</a></div>';

					icon_flg = icon; //アイコンの位置
//					if(icon_id == mark_data[k]) icon_flg = icon_center; //アイコンの位置
					if(icon_id == mark_data[k]){
						cwin_html = win_html;
						var r_marker = createMarker(cwin_html, icon_center);
        				gmarkersA.push(r_marker);
					}else{
						var r_marker = createMarker(win_html, icon_flg);
		           		gmarkersA.push(r_marker);
					}
				}
			}
		}
		mm.addMarkers(gmarkersA, 3);
        mm.refresh();

	}

///////////////////// マーカーとフキダシ（変更不要）///////////////////////
	function createMarker(win_html, icon_flg) {
		var c_marker = new GMarker(point, icon_flg);
		GEvent.addListener(c_marker, "click", function() { c_marker.openInfoWindowHtml(win_html); });
        return c_marker;
	}


}



//////////////////　新規登録　///////////////////////////////////////
function new_place(){
	new Ajax.Request("edit.php?" +(new Date).getTime(),{ onSuccess:new_on, onFailure: displayError});
	
	function new_on(httpobj){
		id = httpobj.responseText;
		document.location = "index2.php?shopid="+id;
	};
}


//////////////////更新ボタンが押されたら///////////////////////////////////////
function Editinfo(uni){

	//入力チェック
	if($F("name") == ""){
		alert('「名前」が入力されていません！');
		return false;
	}
	if($F("type") == 0){
		alert('「タイプ」を選んでください！');
		return false;
	}
	if($F("ad1") == ""){
		alert('「住所」が入力されていません！');
		return false;
	}

//規則違反ワード検索
	var str = new Array('風俗', 'ホテルヘルス', 'デリバリーヘルス', 'ホテヘル', 'デリヘル', 'エッチ', 'スケベ', '本番', 'シックスナイン', 'デートクラブ', '出会い系', 'AV女優', 'ファッションヘルス', '大人のおもちゃ', 'ソープランド', 'キャバクラ', 'セクキャバ', 'ラブホテル', '援助交際', '裏ビデオ', 'SM', 'ローションプレイ', 'ブルセラ', '人妻', 'ホストクラブ', 'キャバレー', 'テレクラ', 'ストリップ', 'アダルト', 'SEX', '性感');

	var name_dat = str_replace(" ", "", $F("name"));
		name_dat = str_replace("　", "", name_dat);
	var info_dat = str_replace(" ", "", $F("info"));
		info_dat = str_replace("　", "", info_dat);
	
	for(i=0; i<str.length; i++){
		var rex = new RegExp(str[i],"i"); 
		if((name_dat.match(rex)) || (info_dat.match(rex))){
			alert('規則違反ワードが検出されました！　登録できません！');
			return false;
		}
	}

	//登録処理
	paras  = 'id=' + uni + '&name=' + $F("name") + '&type=' + $F("type") + '&hp=' + $F("hp")+ '&ad1=' + $F("ad1");
	paras += '&ad2=' + $F("ad2") + '&info=' + $F("info") + '&tell=' + $F("tell");
	paras += '&chx=' + $F("chx");
	paras += '&chy=' + $F("chy");

	new Ajax.Request("up.php?" +(new Date).getTime(),{ method:"post",parameters:paras,onSuccess:success,onFailure: displayError});
	
		function success(httpobj){
			id = httpobj.responseText;
			document.location = "index.php?shopid="+id;
		};

}


/////////////////////////　座標取得　//////////////////////////////////
function savePoint(shopid){
	$("shopid").value = shopid;

	var address = $("ad1").value;
	gGeo.getLatLng(address, function checkGeo(point){
		if(! point){
			$("ad1").value = "";
			alert("住所が間違っています！　もう一度入力してください！");
			return false;
		}else{
			map.setCenter(point, 16);
			point = map.getCenter(); //地図の中心を変数に格納
			$("chy").value = point.y;
			$("chx").value = point.x;
			Editinfo(shopid);
		}
	});
	
}


////////////////////  検索窓  ///////////////////////////////////////////
function search_add(address){ //検索開始
	gGeo.getLatLng(address, function showMap(point){ //geocoderにより住所を座標に変更
		if(point){
			map.setCenter(point, 16); //地図の中心を検索した位置に合わせる。拡大率は16
			getzahyo(); //中心座標取得
		}else{
			alert("該当する住所がありません。キーワードを変えてもう一度お試し下さい"); //エラーメッセージ
		}
	});
}


///////////////////  中心座標取得  /////////////////////////////////////
function getzahyo(){
	point = map.getCenter(); //地図の中心を変数に格納
	$("chx").value = point.x; //x座標を"chX"にぶっこむ
	$("chy").value = point.y; //y座標を"chY"にぶっこむ
	z = map.getZoom();

	icon_id = $("shopid").value;
	loading(point.x, point.y, z, icon_id);
}





///////////////　文字置換　//////////////////////
function str_replace( search, replace, subject ) {
    // Replace all occurrences of the search string with the replacement string
    // 
    // +    discuss at: http://kevin.vanzonneveld.net/techblog/article/javascript_equivalent_for_phps_str_replace/
    // +       version: 803.1715
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Gabriel Paderni
    // *     example 1: str_replace(' ', '.', 'Kevin van Zonneveld');
    // *     returns 1: 'Kevin.van.Zonneveld'

    if(!(replace instanceof Array)){
        replace=new Array(replace);
        if(search instanceof Array){//If search    is an array and replace    is a string, then this replacement string is used for every value of search
            while(search.length>replace.length){
                replace[replace.length]=replace[0];
            }
        }
    }

    if(!(search instanceof Array))search=new Array(search);
    while(search.length>replace.length){//If replace    has fewer values than search , then an empty string is used for the rest of replacement values
        replace[replace.length]='';
    }

    if(subject instanceof Array){//If subject is an array, then the search and replace is performed with every entry of subject , and the return value is an array as well.
        for(k in subject){
            subject[k]=str_replace(search,replace,subject[k]);
        }
        return subject;
    }

    for(var k=0; k<search.length; k++){
        var i = subject.indexOf(search[k]);
        while(i>-1){
            subject = subject.replace(search[k], replace[k]);
            i = subject.indexOf(search[k],i);
        }
    }

    return subject;

}

///////////////　読み込みエラーは以下の処理　//////////////////////
function displayError(){
	alert( "ファイルの取得に失敗しました" );
}


//]]>
