var map	   = null;
var zoom_f = false;
var map_x  = 0;
var map_y  = 0;
var map_v   = 0;
var marker = new Array();
var text   = new Array();
var mx	   = new Array();
var my	   = new Array();

var hed = "";
var fot = "";

function addMarker( id, lat, lng, html, icon, isOpen)
{
	if( lat == null || lng == null || marker[id] ) return;

	point = new GPoint(lat, lng);
	mx[id]	  = lat;
	my[id]	  = lng;
	text[id]  = hed+html+fot;
	if( icon != null ){
		marker[id] = new GMarker(point,icon);
	}else{
		marker[id] = new GMarker(point);
	}
	map.addOverlay(marker[id]);
	if(isOpen == 1){
		marker[id].openInfoWindow(text[id]);
	}
	GEvent.addListener(marker[id], "click", function() {
		marker[id].openInfoWindow(text[id]);
	});
}

function getIcon( url, w, h )
{
	icon = new GIcon();
	icon.image = url;
	icon.transparent = "/img/markerTransparent.png";
	icon.iconSize = new GSize(w, h);
	icon.iconAnchor = new GPoint(w/2, h/2);
	icon.infoWindowAnchor = new GPoint(w/2,0);
	return icon;
}

function getIconWithShadow( url, w, h, sUrl, sw, sh )
{
	icon = new GIcon();
	icon.image = url;
	icon.shadow = sUrl;
	icon.transparent = "/img/markerTransparent.png";
	icon.iconSize = new GSize(w, h);
	icon.shadowSize = new GSize(sw, sh);
	icon.iconAnchor = new GPoint(w/2, h/2);
	icon.infoWindowAnchor = new GPoint(w/2,0);
	return icon;
}

function initDetailsMap( ido, keido, view, text )
{
	hed = "<div style='font-size:10pt;font-weight:bold;'>";
	fot = "</div>";
	createMap( ido, keido, view, hed+text+fot, false );
}


function initHotelMap(ido,keido, view, text)
{
    if(GBrowserIsCompatible()){
		if(!ido || !parseFloat(ido)){
			ido = 39.000000
		}
		if(!keido || !parseFloat(keido)){
			keido = 140.000000
		}
		if(!view){
			view = 4;
		}
		if(!text){
			text = ""
			var icon = false;
			zoom_f = true;
		}else{
			hed = "<div style='font-size:10pt;font-weight:bold;'>";
			fot = "</div>";
			text = hed + text + fot;
			var icon = getIconWithShadow("/img/centerMarker.png", 32, 32, "/img/shadow45.png", 37, 34);
			zoom_f = false;
		}
	
	    hed = "<div style='font-size:10pt;font-weight:bold;'>";
		fot = "</div>";
		createMap( ido, keido, view,text , false, icon );
		crateMarkerFromXml( 0, 0, '_call_session_hotel', null, view );
	}
}

function initAddressMap( ido, keido, view, text )
{
	if(GBrowserIsCompatible()){
        var i_point1 = getIcon("/img/arrow_cebter.gif",23,34);
        var i_point2 = getIcon("/img/pointer_hot.gif",24,32);
		createMap( ido, keido, view, text, true, i_point1 );
		GEvent.addListener(map,'moveend',function() {
			var xy = map.getCenter();
			crateMarkerFromXml(xy.lat(),xy.lng(),"_call_hotel_search");
			crateMarkerFromXml(xy.lat(),xy.lng(),"_call_rest_search",i_point2);
		});
		crateMarkerFromXml( ido, keido, "_call_hotel_search" );
		crateMarkerFromXml( ido, keido, "_call_rest_search", i_point2 );
	}
}

function initRestSearchMap(ido,keido, view, text)
{
    if(GBrowserIsCompatible()){
		if(!ido || !parseFloat(ido)){
			ido = 39.000000;
		}
		if(!keido || !parseFloat(keido)){
			keido = 140.000000;
		}
		if(!view){
			view = 4;
		}
		if(!text){
			text = ""
			var icon = false;
			zoom_f = true;
		}else{
			hed = "<div style='font-size:10pt;font-weight:bold;'>";
			fot = "</div>";
			text = hed + text + fot;
			var icon = getIconWithShadow("/img/centerMarker.png", 32, 32, "/img/shadow45.png", 37, 34);
			zoom_f = false;
		}
		createMap( ido, keido, view,text , false, icon );
        crateMarkerFromXml( 0, 0, '_call_session_rest', null );
	}
}

function initRestDetailMap( ido, keido, view, text, id )
{
	icon = getIconWithShadow("/img/centerMarker.png", 32, 32, "/img/shadow45.png", 37, 34);
    if(GBrowserIsCompatible()){
        hed = "<div style='font-size:10pt;font-weight:bold;'> ";
        fot = "</div>";
		text = "<a href='#top'>" + text + "</a>";
		createMap( ido, keido, view, hed+text+fot, false, icon, id );
        crateMarkerFromXml( 0, 0, '_call_session_rest_detail', null );
	}
}

function initHotelSearchMap()
{
    if(GBrowserIsCompatible()){
        zoom_f = true;
        hed = "<div style='font-size:10pt;font-weight:bold;'>";
        fot = "</div>";
		createMap( 39.000000, 140.000000, 4, null, false );
        crateMarkerFromXml( 0, 0, '_call_session_hotel', null );
	}
}
function initHotelDetailMap( ido, keido, view, text, id )
{
	icon = getIconWithShadow("/img/centerMarker.png", 32, 32, "/img/shadow45.png", 37, 34);
    if(GBrowserIsCompatible()){
        hed = "<div style='font-size:10pt;font-weight:bold;'>";
        fot = "</div>";
		text = "<a href='#top'>" + text + "</a>";
        createMap( ido, keido, view, hed+text+fot, false, icon, id );
        crateMarkerFromXml( 0, 0, '_call_session_hotel_detail', null );
	}
}

function initCafeSearchMap(ido,keido, view, text)
{
	icon = getIconWithShadow("/img/centerMarker.png", 32, 32, "/img/shadow45.png", 37, 34);
    if(GBrowserIsCompatible()){
		if(!ido || !parseFloat(ido)){
			ido = 39.000000
		}
		if(!keido || !parseFloat(keido)){
			keido = 140.000000
		}
		if(!view){
			view = 4;
		}
		if(!text){
			text = ""
			var icon = false;
			zoom_f = true;
		}else{
			hed = "<div style='font-size:10pt;font-weight:bold;'>";
			fot = "</div>";
			text = hed + text + fot;
			var icon = getIconWithShadow("/img/centerMarker.png", 32, 32, "/img/shadow45.png", 37, 34);
			zoom_f = false;
		}
        hed = "<div style='font-size:10pt;font-weight:bold;'>";
        fot = "</div>";
		createMap( ido, keido, view,text , false, icon );
        crateMarkerFromXml( 0, 0, '_call_session_cafe', null );
	}
}

function initCafeDetailMap( ido, keido, view, text, id )
{
	icon = getIconWithShadow("/img/centerMarker.png", 32, 32, "/img/shadow45.png", 37, 34);
    if(GBrowserIsCompatible()){
        hed = "<div style='font-size:10pt;font-weight:bold;'>";
        fot = "</div>";
		text = "<a href='#top'>" + text + "</a>";
        createMap( ido, keido, view, hed+text+fot, false,icon, id );
        crateMarkerFromXml( 0, 0, '_call_session_cafe_detail', null );
	}
}

function initDetailsCafe( ido, keido, view, text )
{
    hed = "<div style='font-size:10pt;font-weight:bold;'>";
    fot = "</div>";
	createMap( ido, keido, view, text, false, null );
}

function initSearchCafeMap()
{
    hed = "<div style='font-size:10pt;font-weight:bold;'>";
    fot = "</div>";
	createMap( 39.000000, 140.000000, 4, false, null );
}

function initAddressMap(ido, keido){
	var view = 12;
	var noIcon = false;
	if(ido == 0 || keido == 0){
		ido = 39.000000;
		keido = 140.000000
		view = 3;
		noIcon = true;
	}
	createSmallMap( ido, keido, view, null, null, false, noIcon);
}

function createMap( ido, keido, view, text, over_f, icon, id )
{
	map_x  = ido;
	map_y  = keido;
	map_v  = view;
    map = new GMap2(document.getElementById("gmap"));
	map.addControl(new GMapTypeControl());
	map.addControl(new GLargeMapControl());
	map.addControl(new GScaleControl());
    if( over_f )map.addControl(new GOverviewMapControl());
	map.setCenter(new GLatLng(ido,keido), view);
	if( text ){
		if( id ){
			addMarker( id, keido, ido, text, icon, true );
		}else{
			mk = new GMarker(map.getCenter(),icon);
    		map.addOverlay(mk);
	    	mk.openInfoWindowHtml(text);
   			GEvent.addListener(mk,'click',function() {
        	mk.openInfoWindowHtml(text);
   			});
		}
	}
}
function createSmallMap( ido, keido, view, text, icon, id, noIcon )
{
	map_x  = ido;
	map_y  = keido;
	map_v  = view;
    map = new GMap2(document.getElementById("gmap"));
	map.addControl(new GSmallZoomControl());
	map.setCenter(new GLatLng(ido,keido), view);	
	if( id ){
		addMarker( id, keido, ido, text, icon, true );
	}else if(!noIcon){
		mk = new GMarker(map.getCenter(),icon);
   		map.addOverlay(mk);
   		if(text){
    		mk.openInfoWindowHtml(text);
			GEvent.addListener(mk,'click',function() {
   	    		mk.openInfoWindowHtml(text);
   			});
		}
	}
}

function crateMarkerFromXml( ido, keido, mode, icon, zoom )
{
	if(idList==undefined){
		idList = "";
	}
	var request = GXmlHttp.create();
	request.open("GET", "/search/SamuraiApi.php?mode="+mode+"&y="+ido+"&x="+keido+"&ids="+idList, true);
	request.setRequestHeader("If-Modified-Since","Wed, 15 Nov 1995 00:00:00 GMT");
	request.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
	request.onreadystatechange = function() {
	var doAdd;
		if (request.readyState == 4) {
//			alert(request.responseText);
			var xmlDoc = request.responseXML;
			var data = xmlDoc.getElementsByTagName("entry");
			for( i = 0; i < data.length; i++ ){
				var id = data[i].getAttributeNode("id").nodeValue;
				var text = data[i].getElementsByTagName('desc')[0].firstChild.nodeValue;
				try{
					doAdd = 1;
       	        	var x  = data[i].getElementsByTagName('lon')[0].firstChild.nodeValue;
					var y  = data[i].getElementsByTagName('lat')[0].firstChild.nodeValue;
				}catch(err){
					doAdd = 0;
				//	alert(request.responseText);
				}
				if(doAdd == 1){
					addMarker( id, x, y, text, icon );
				}
			} 
			if( zoom_f ){
				initZoom();
			}
		}
    }
    request.send(null);
}


function initZoom()
{
	var gb = null;
	var first = 1;
	if( !map ) return ( false );
	for( var index in marker ){
		var mk = marker[index];
		if( mk && mk.getPoint ){
			if( first ){
				gb = new GLatLngBounds( mk.getPoint(), mk.getPoint() );
				first = 0;
			}else{
				var point = mk.getPoint();
				gb.extend( point );
			}
		}
	}
	if( gb != null ){
		map.setCenter( gb.getCenter(), map.getBoundsZoomLevel( gb ) );
	}
	return ( true );
}

function clickCenter()
{
	map.setCenter(new GLatLng(map_x,map_y),map_v);
}

function hotelClicked( i , zoom)
{
	if(zoom == undefined){
		zoom = 13;
	}
	if( !my[i] || !mx[i] ) return;
	map.panTo(new GLatLng(my[i],mx[i]));
    map.setZoom(zoom);
    marker[i].openInfoWindow(text[i]);
}

function moveTo( x, y , zoom)
{
	if(zoom == undefined){
		zoom = 13;
	}
	if( !y || !x ){ return;}
	map.panTo(new GLatLng(y,x));
    map.setZoom(zoom);
	mk = new GMarker(map.getCenter(),null);
   	map.addOverlay(mk);
}

function hotelMkClicked( id )
{
    location.href="#hotel" + id;
//	location.hash = "hotel"+id;
}

function tabelogClicked( i )
{
	map.panTo(new GLatLng(my[i],mx[i]) );
	map.setZoom(17);
	marker[i].openInfoWindow(text[i]);
}

function tabeMkClick( yado_id, id, offset, nowset )
{
	if( offset != nowset ){
		document.location = "/search/hotel_detail.php?yado_id="+yado_id+"&sort=&offset="+offset+"#"+id;
	}else{
		location.hash = id;
	}
}

function hotClicked( i )
{
    map.panTo(new GLatLng(my[i],mx[i]) );
	map.setZoom(17);
    marker[i].openInfoWindow(text[i]);
}

function hotMkClick( yado_id, id, offset, nowset )
{
    if( offset != nowset ){
        document.location = "/search/hotel_coupon.php?yado_id="+yado_id+"&sort=&offset="+offset+"#"+id;
    }else{
        location.hash = id;
    }
}

function tClick( obj, color )
{
    if ((tobj = document.getElementById(obj))) {
	    tobj.style.backgroundColor = color;
	}
}

