var const_mapdiv = 'map_result';
var displaymapobj;
var DisplayMap = Class.create();
if ( !GBrowserIsCompatible() ) {
  alert( 'Google Maps が対応していないブラウザのため、この画面は使用できません。ごめんなさい。' );
}
DisplayMap.prototype = {
	initialize: function(){
		this.mapdiv = $( const_mapdiv );
		this.mapdiv.update('<br /><br /><center><u>ここに地図が表示されます。</u></center>');
		this.baseIcon = new GIcon();
		this.baseIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
		this.baseIcon.iconSize = new GSize( 20, 34 );
		this.baseIcon.iconAnchor = new GPoint( 10, 34 );
		this.baseIcon.shadowSize = new GSize( 37, 34 );
	},
	gmapmake: function(){
		if( this.map == null )
			this.map = new GMap2( this.mapdiv );
	},
	hallmap: function( p_l, p_r, name, access, tofofuken, address, uri, ninzu ){
		this.gmapmake();
		
		var point = new GPoint( p_r, p_l );
		this.latlng = new GLatLng(  p_l, p_r );
		this.map.setCenter(this.latlng, 17,G_NORMAL_MAP);
		this.map.addControl(new GLargeMapControl());
		this.map.addControl(new GMapTypeControl());
		if(this.marker != null){
		    this.map.removeOverlay(this.marker);
		}
		this.marker = new GMarker(point);
		this.map.addOverlay(this.marker);
		
		if(this.window_msg != null){
		    this.map.closeInfoWindow();
		}
		var s_address = "";
		if( address != ""){
		    s_address = decodeURIComponent(address) + "<br />";
		}
		if( uri != ""){
		    uri = "<a href=\"" + decodeURIComponent(uri) + "\" target=\"_blank\">ホームページ</a><br />";
		}
		if( ninzu != ""){
		    ninzu = "収容人数：" + decodeURIComponent(ninzu) + "人<br />";
		}
		this.window_msg = "<font size=\"2\"><table border=\"1\" cellpadding=\"0\" cellspacing=\"0\" bordercolor=\"#666666\" "
		           + "width=\"250pt\"><tr><td align=\"center\"><b>[" 
		           + decodeURIComponent(tofofuken) + "]<br />" + decodeURIComponent(name) 
		           + "</b></td></tr></table><table border=\"0\" width=\"250pt\"><tr><td>" + s_address 
		           + uri + ninzu + "<hr />" 
		           + decodeURIComponent(access) + "</td></tr></table></font>";
		this.map.openInfoWindowHtml(this.latlng, this.window_msg);
		
		GEvent.addListener(this.marker, "click", function() {
		    this.map.closeInfoWindow();
		    this.map.openInfoWindowHtml(this.latlng, this.window_msg);
		}.bind( displaymapobj ));
		
		document.getElementById("map_link").innerHTML="<a href=\"print\" onclick=\"window.print();return false;\">このページを印刷</a>&nbsp;<a href=\"http://maps.google.co.jp/?ll=" + p_l + "," + p_r + "&q=" + address + "\" target=\"_blank\">GoogleMapsで大きな地図を表示</a>（実際にお出かけの際は、場所を必ず主催者またはホールにご確認ください。）";
	},
	mapBig: function(){
		this.mapdiv.style.height = ( this.mapdiv.offsetHeight + 150 ) + 'px';
	},
	mapSmall: function(){
		if( this.mapdiv.offsetHeight - 150 >= 300 )
			this.mapdiv.style.height = ( this.mapdiv.offsetHeight - 150 ) + 'px';
	},
	// レストラン検索実行
	callHotpepper: function( startserach, move ){
		var x = "";
		var y = "";
		var genre = document.forms["search"].TPA01_Genre.value;
		var food = document.forms["search"].TPA01_Food.value;
		var word = document.forms["search"].TPH01_FreeWord.value;
		var range = document.forms["search"].TPA01_Range.value;
		if( move == 1 ){
			var center = this.map.getCenter();
			x = encodeURIComponent(Math.round(center.x * 1000000) / 1000000.0);
			y = encodeURIComponent(Math.round(center.y * 1000000) / 1000000.0);
			this.centerX = x;
			this.centerY = y;
		}else if( move == 2 ){
			if( genre == "" && food == "" && word == "" ){
				alert('条件は必ず指定してください');
				$( 'hotpepperresult' ).update( '' ) ;
				$( 'gurunabiresult' ).update( '' ) ;
				return;
			}
			this.centerX = null;
			this.centerY = null;
		}else if( move == 0 ){
			if( this.centerX == null || this.centerY == null ){
				//
			}else{
				x = this.centerX;
				y = this.centerY;
			}
		}
		var parameters = "page=PA01&PA01_Longitude="+x+"&PA01_Latitude="+y+"&PA01_s="+startserach+"&PA01_g="+genre+"&PA01_f="+food+"&PA01_w="+word+"&PA01_r="+range;
		if(this.window_msg != null){
		    this.map.closeInfoWindow();
		}
		new Ajax.Request('./', {
			method: 'post',
			asynchronous: true,
			parameters: parameters,
			onLoading: function() {
				//this.view.innerHTML = "saving...";
			}.bind(this),
			onSuccess: function( originalRequest ) {
				this.displayHotpepper( originalRequest, 1 );
			}.bind(this),
			onFailure: function() {
				alert('レストラン検索が失敗しました。もう一度実行してください。');
			}.bind(this)
		});
		
	},
	// レストラン検索実行
	callGurunabi: function( startserach, move ){
		var x = "";
		var y = "";
		var genre = document.forms["search"].TPA01_Genre2.value;
		var food = document.forms["search"].TPA01_Food2.value;
		var range = document.forms["search"].TPA01_Range2.value;
		if( move == 1 ){
			var center = this.map.getCenter();
			x = encodeURIComponent(Math.round(center.x * 1000000) / 1000000.0);
			y = encodeURIComponent(Math.round(center.y * 1000000) / 1000000.0);
			this.centerX = x;
			this.centerY = y;
		}else if( move == 2 ){
			if( genre == "" && food == "" ){
				alert('条件は必ず指定してください');
				$( 'hotpepperresult' ).update( '' ) ;
				$( 'gurunabiresult' ).update( '' ) ;
				return;
			}
			this.centerX = null;
			this.centerY = null;
		}else if( move == 0 ){
			if( this.centerX == null || this.centerY == null ){
				//
			}else{
				x = this.centerX;
				y = this.centerY;
			}
		}
		var parameters = "page=PA04&PA04_Longitude="+x+"&PA04_Latitude="+y+"&PA04_s="+startserach+"&PA04_g="+genre+"&PA04_f="+food+"&PA04_r="+range;
		if(this.window_msg != null){
		    this.map.closeInfoWindow();
		}
		new Ajax.Request('./', {
			method: 'post',
			asynchronous: true,
			parameters: parameters,
			onLoading: function() {
				//this.view.innerHTML = "saving...";
			}.bind(this),
			onSuccess: function( originalRequest ) {
				this.displayHotpepper( originalRequest, 0 );
			}.bind(this),
			onFailure: function() {
				alert('レストラン検索が失敗しました。もう一度実行してください。');
			}.bind(this)
		});
		
	},
	displayHotpepper: function( originalRequest, p_display ){
		var xotree = new XML.ObjTree();
		var xml = originalRequest.responseText;
		var tree = xotree.parseXML( xml );
		if( tree.Results.NumberOfResults == 0 || tree.Results.NumberOfResults == undefined ){
			alert("周辺にレストランは検索されませんでした。");
			$( 'hotpepperresult' ).update( '' ) ;
			$( 'gurunabiresult' ).update( '' ) ;
			return;
		}
		if( this.hotpeppermarker != null){
			for (i = 0; i < this.hotpeppermarker.length; i++){
			    this.map.removeOverlay(this.hotpeppermarker[i].marker);
			}
		}
		var searchnum = tree.Results.DisplayPerPage;
		var datanum = Number(tree.Results.DisplayFrom) + Number(searchnum) - 1;
		if( datanum > tree.Results.NumberOfResults ){
			datanum = tree.Results.NumberOfResults;
		}
		var listhtml = tree.Results.NumberOfResults+"&nbsp;件中&nbsp;"+tree.Results.DisplayFrom+"&nbsp;-&nbsp;"+datanum+"件目<br />";
		
		var nexthtml = '<table width="100%"><tr><td>';
		// ここから次
		if( tree.Results.Displaybefore != 0 ){
			if( p_display == 0 ){
				nexthtml += '<a href="" onclick="callGurunabi( '+ tree.Results.Displaybefore +', 0 );return false;">前の'+searchnum+'件</a></td>';
			}else{
				nexthtml += '<a href="" onclick="callHotpepper( '+ tree.Results.Displaybefore +', 0 );return false;">前の'+searchnum+'件</a></td>';
			}
		}else{
//			nexthtml += '前の'+searchnum+'件</td>';
			nexthtml += '</td>';
		}
		if( tree.Results.Displayafter != 0 ){
			if( p_display == 0 ){
				nexthtml += '<td align="right"><a href="" onclick="callGurunabi( '+ tree.Results.Displayafter +', 0 );return false;">次の'+searchnum+'件</a></td>';
			}else{
				nexthtml += '<td align="right"><a href="" onclick="callHotpepper( '+ tree.Results.Displayafter +', 0 );return false;">次の'+searchnum+'件</a></td>';
			}
		}else{
//			nexthtml += '<td align="right">次の'+searchnum+'件</td>';
			nexthtml += '<td align="right"></td>';
		}
		nexthtml += "</td></tr></table>";
		
		listhtml += nexthtml+'<table>';
		
		this.hotpeppermarker = new Array();
		if( tree.Results.CountOfResults == 1 ){
			this.createHotpepprIcon(  tree.Results.Shop, 0 );
			listhtml += '<tr><td valign="top"><img src="./common/Concert/img/h1.png" border="0"></td><td><a href="" onclick="displayShopinfo( 0, true);return false;">' +
			            tree.Results.Shop.ShopName + '</a></td></tr>';
		}else{
			
			for (i = 0; i <  tree.Results.CountOfResults; i++) {
				this.createHotpepprIcon(  tree.Results.Shop[i], i );
				listhtml += '<tr><td valign="top"><img src="./common/Concert/img/h' + ( i + 1 ) + '.png" border="0"></td><td>' + 
				            '<a href="" onclick="displayShopinfo( ' + i + ', true);return false;">' +
				            tree.Results.Shop[i].ShopName + '</a><br />('+ tree.Results.Shop[i].FoodName  +')</td></tr>';
			}
		}
		listhtml += '</table>';
		
		listhtml += nexthtml;
		
		if( p_display == 0 ){
			$( 'gurunabiresult' ).update( listhtml ) ;
		}else{
			$( 'hotpepperresult' ).update( listhtml ) ;
		}
	},
	createHotpepprIcon: function( shopinfo, markernumber ){
		var point = new GPoint( shopinfo['Longitude'],shopinfo['Latitude'] );
		var latlng = new GLatLng(  shopinfo['Latitude'], shopinfo['Longitude'] );
		var icon = new GIcon(this.baseIcon);
		icon.image = './common/Concert/img/h'+(markernumber + 1)+'.png';
		
		this.hotpeppermarker[markernumber] = new Array;
		this.hotpeppermarker[markernumber].marker = new GMarker( point, icon );
		this.map.addOverlay( this.hotpeppermarker[markernumber].marker );
		
		var shophref = '<a href="'+ shopinfo.ShopUrl + '" target="_blank">';
		
		this.hotpeppermarker[markernumber].window_msg = shophref + '<b>' + shopinfo.ShopName + '</b></a>(' + shopinfo.FoodName + ')<hr width="400px"/>' +
		'<table width="350px" height="150px"><tr><td valign="top" >' + 
		'<div style="color:red;">' + shopinfo.ShopCatch + '</div><br />' +
		'<table cellSpacing="1" cellPadding="0" bgcolor="#FFFFFF">' +
		'<tr><td align="right" valign="top" nowrap><u>平均予算</u>：</td><td>' + shopinfo.BudgetAverage + '</td></tr>' +
		'<tr><td align="right" valign="top" nowrap><u>営業時間</u>：</td><td>' + shopinfo.Open + '</td></tr>' +
		'<tr><td align="right" valign="top" nowrap><u>定休日</u>：</td><td>' + shopinfo.Close + '</td></tr>' +
		'<tr><td align="right" valign="top" nowrap><u>アクセス</u>：</td><td>' + shopinfo.Access + '</td></tr>' +
		'<tr><td align="right" valign="top" nowrap><u>住所</u>：</td><td>' + shopinfo.ShopAddress + '</td></tr></table>' +
		'</td>';
		if( shopinfo.PictureUrl != null && shopinfo.PictureUrl != "" ){
			this.hotpeppermarker[markernumber].window_msg += shophref + '<td><img src="' + shopinfo.PictureUrl.MbSmallImg + '" border="0" alt="' + shopinfo.ShopName + '"></a></td>'
		}
		this.hotpeppermarker[markernumber].window_msg += '</tr><table>' +
		'';
		this.hotpeppermarker[markernumber].latlng = latlng;
		if( shopinfo.PictureUrl != null && shopinfo.PictureUrl != "" ){
			this.hotpeppermarker[markernumber].PictureUrl = shopinfo.PictureUrl.MbSmallImg;
		}else{
			this.hotpeppermarker[markernumber].PictureUrl = "";
		}
		
		var eventlistener = function() {
		    this.displayShopinfo( markernumber, false );
		};
		eventlistener.markernumber = markernumber;
		GEvent.addListener(this.hotpeppermarker[markernumber].marker, "click", eventlistener.bind( this ));
		
	},
	displayShopinfo: function( markernumber, recenter ){
	    this.map.closeInfoWindow();
	    // 画像の先読み
		if( this.hotpeppermarker[markernumber].PictureUrl != "" ){
			var img = new Image();
			img.src = this.hotpeppermarker[markernumber].PictureUrl;
		}
	    this.map.openInfoWindowHtml(this.hotpeppermarker[markernumber].latlng, this.hotpeppermarker[markernumber].window_msg);
	    if( recenter ){
			this.map.setCenter(this.hotpeppermarker[markernumber].latlng);
//			if(document.all){
//				this.map.centerAtLatLng( this.hotpeppermarker[markernumber].point );
//			}else{
//				this.map.recenterOrPanToLatLng( this.hotpeppermarker[markernumber].point );
//			}
		}
	},
	displayKeyword: function( address ){
		this.gmapmake();
		if( this.geocoder == undefined ){
			this.geocoder = new GClientGeocoder();
		}
		this.geocoder.getLatLng(
			address,
			function( point ) {
				if (!point) {
					alert(address + " は見つかりませんでした。");
				} else {
					this.map.clearOverlays();
					this.map.setCenter( new GLatLng( point.y, point.x ), 17 );
					var marker = new GMarker(point);
					this.map.addOverlay(marker);
					marker.openInfoWindowHtml(address);
				}
			}.bind( this )
			);

		
	}
}
function square_map( p_l, p_r, name, access, tofofuken, address, uri, ninzu ){
	displaymapobj.hallmap( p_l, p_r, name, access, tofofuken, address, uri, ninzu );
}
function displayShopinfo( markernumber ){
	displaymapobj.displayShopinfo( markernumber, true );;
}
function callHotpepper( startserach, move ){
	if( displaymapobj.map == null ){
		alert('地図を表示してから押下してください。');
		return;
	}
	$( 'hotpepperresult' ).update( '<img src="./common/Concert/img/snake_transparent.gif" border="0" align="left">検索中...' ) ;
	$( 'gurunabiresult' ).update( '' ) ;
	$( 'hallsearch' ).hide();
	$( 'gurunabisearch' ).hide();
	$( 'hotpeppersearch' ).show();
	$( 'showhallsearch' ).style.fontWeight = 'normal';
	$( 'showgurusearch' ).style.fontWeight = 'normal';
	$( 'showrestsearch' ).style.fontWeight = 'bold';
	$( 'searchzyoken' ).className = 'search_space2';
	displaymapobj.callHotpepper( startserach, move );
}
function callGurunabi( startserach, move ){
	if( displaymapobj.map == null ){
		alert('地図を表示してから押下してください。');
		return;
	}
	$( 'gurunabiresult' ).update( '<img src="./common/Concert/img/snake_transparent.gif" border="0" align="left">検索中...<br />(時間がかかる場合あり)' ) ;
	$( 'hotpepperresult' ).update( '' ) ;
	$( 'hallsearch' ).hide();
	$( 'hotpeppersearch' ).hide();
	$( 'gurunabisearch' ).show();
	$( 'showhallsearch' ).style.fontWeight = 'normal';
	$( 'showrestsearch' ).style.fontWeight = 'normal';
	$( 'showgurusearch' ).style.fontWeight = 'bold';
	$( 'searchzyoken' ).className = 'search_space3';
	displaymapobj.callGurunabi( startserach, move );
}
function makeHotpepperZyoken(){
	var parameters = "page=PA02";
	new Ajax.Request('./', {
		method: 'post',
		asynchronous: true,
		parameters: parameters,
		onSuccess: function( originalRequest ) {
			$( 'hotpepperzyoken' ).update( originalRequest.responseText );
		}
	});
	parameters = "page=PA05";
	new Ajax.Request('./', {
		method: 'post',
		asynchronous: true,
		parameters: parameters,
		onSuccess: function( originalRequest ) {
			$( 'gurunabzyoken' ).update( originalRequest.responseText );
		}
	});
}
function showAddress( address ){
	displaymapobj.displayKeyword( address );
}
window.onload = function(){
	$( 'showhallsearch' ).style.fontWeight = 'bold';
	
	$( 'mapbig' ).onclick = function(){
		displaymapobj.mapBig();
	}
	$( 'mapsmall' ).onclick = function(){
		displaymapobj.mapSmall();
	}
	$( 'showhallsearch' ).onclick = function(){
		$( 'hotpeppersearch' ).hide();
		$( 'gurunabisearch' ).hide();
		$( 'hallsearch' ).show();
		$( 'searchzyoken' ).className = 'search_space';
		$( 'showhallsearch' ).style.fontWeight = 'bold';
		$( 'showrestsearch' ).style.fontWeight = 'normal';
		$( 'showgurusearch' ).style.fontWeight = 'normal';
	}
	$( 'showrestsearch' ).onclick = function(){
		$( 'hallsearch' ).hide();
		$( 'gurunabisearch' ).hide();
		$( 'hotpeppersearch' ).show();
		$( 'searchzyoken' ).className = 'search_space2';
		$( 'showhallsearch' ).style.fontWeight = 'normal';
		$( 'showgurusearch' ).style.fontWeight = 'normal';
		$( 'showrestsearch' ).style.fontWeight = 'bold';
	}
	$( 'showgurusearch' ).onclick = function(){
		$( 'hallsearch' ).hide();
		$( 'hotpeppersearch' ).hide();
		$( 'gurunabisearch' ).show();
		$( 'searchzyoken' ).className = 'search_space3';
		$( 'showhallsearch' ).style.fontWeight = 'normal';
		$( 'showrestsearch' ).style.fontWeight = 'normal';
		$( 'showgurusearch' ).style.fontWeight = 'bold';
	}
	displaymapobj = new DisplayMap();
//	makeHotpepperZyoken();
}

