﻿// CODE0101/20100623
var ASTmap;
var G_MARKERS = [];
var initLatLng;

// Define Marker Images logo_gmap5-96*96 / logo_gmap4-118*116 / logo_gmap3-137*124
var SCALE_ICON = 0.4;
var WIDTH_ICON = 96;
var ASTIcon=new GIcon(G_DEFAULT_ICON);

	ASTIcon.image = "./image/logo_gmap7r.gif";
	ASTIcon.printImage = "./image/logo_gmap7r.gif";
	// ASTIcon.shadow = "./image/logo_gmap7_s.gif";
	ASTIcon.iconSize   = new GSize(WIDTH_ICON * SCALE_ICON ,WIDTH_ICON * SCALE_ICON);
	ASTIcon.shadowSize = new GSize(WIDTH_ICON * SCALE_ICON ,WIDTH_ICON * SCALE_ICON);
	ASTIcon.iconAnchor = new GPoint((WIDTH_ICON * SCALE_ICON)/3 ,WIDTH_ICON * SCALE_ICON);


function NSetEventListener(elm,ev,func,cap){

	// DOM Event (browser switch)
	if(elm.addEventListener){
		elm.addEventListener(ev,func,cap);

	} else if(elm.attachEvent){
		elm.attachEvent('on'+ev,func,cap);

	} else {
		alert("Your browser is not supported");
	}

}


function initialize(lat,lng) {

 if(GBrowserIsCompatible()){

	var ELM_mapCanvas = document.getElementById("map_canvas");
	ASTmap = new GMap2(ELM_mapCanvas);

	// MapType ========================================
	ASTmap.addMapType(G_PHYSICAL_MAP);
	//=================================================

	// MapTypeControl =================================
	ASTmap.addControl(new GHierarchicalMapTypeControl());
	ASTmap.addControl(new GLargeMapControl());
	ASTmap.addControl(new GOverviewMapControl());
	//=================================================

	// Default Position
	initLatLng=new GLatLng(lat,lng)
	ASTmap.setCenter(initLatLng,5);

	ASTmap.enableContinuousZoom();
	ASTmap.enableScrollWheelZoom();

	NInitMarker();

	// Events
	GEvent.addListener(ASTmap,"moveend",function(){
		document.getElementById("linkgmap").href = NGetGoogleMapUrl();
	});

	// Mouse Over(Upside) =================================
	var elm=document.getElementById("reset");
		if(elm){ NSetCellMouseOver(elm);
		elm.innerHTML = TXT_UPSIDE_ALL;
		}

	elm=document.getElementById("Losaka");
		if(elm){ NSetCellMouseOver(elm);
		elm.innerHTML = TXT_UPSIDE_OSK;
		}

	elm=document.getElementById("Ltokyo");
		if(elm){ NSetCellMouseOver(elm);
		elm.innerHTML = TXT_UPSIDE_TKY;
		}

	elm=document.getElementById("Lmizushima");
		if(elm){ NSetCellMouseOver(elm);
		elm.innerHTML = TXT_UPSIDE_MIZ;
		}
	// ====================================================
 } 
}


function NCreateMarker(i,branch,dom,lat,lng,zoom,zip,address,tel,fax,tab2,tab3,cid){

 var gInfoWindowTabs=[];
 var tabX=0;
 var kmlPoint = new GLatLng(lat,lng);

 G_MARKERS[i]=new GMarker(kmlPoint,{title:branch,icon:ASTIcon});

	// InfoWindow
	var gmap_tag="<div align=right><font size=2><a href='"+NGetGoogleMapUrl(lat,lng,zoom,cid)+"'>"+TXT_OPENGOOGLE+"</a></font></div>";

	if(TXT_COUNTRY=="en"){
	 gInfoWindowTabs[tabX++]=new GInfoWindowTab(TXT_ADDRESS,"<div id='infoWndH'>"+branch+"</div><div align=left class='infoWnd1'>"+address+" "+zip+", Japan<br/><br/><table border=0 style='font-size:9pt;'><tr><td>　</td><td>TEL:"+tel+"</td><td>　</td><td>FAX:"+fax+"</td></tr></table></div>"+gmap_tag);
	} else {
	 gInfoWindowTabs[tabX++]=new GInfoWindowTab(TXT_ADDRESS,"<div id='infoWndH'>"+branch+"</div><div align=left class='infoWnd1'>〒"+zip+"<br/>　"+address+"<table border=0 style='font-size:9pt;'><tr><td>　</td><td>TEL:"+tel+"</td><td>　</td><td>FAX:"+fax+"</td></tr></table></div>"+gmap_tag);
	}
	if(tab2!=null){
	 gInfoWindowTabs[tabX++]=new GInfoWindowTab(TXT_ACCESS,"<div id='infoWndH'>"+branch+"</div><div align=left class='infoWnd2'>"+tab2+"</div>"+gmap_tag);
	}
	if(tab3!=null){
	 gInfoWindowTabs[tabX++]=new GInfoWindowTab(TXT_FACILITIES,"<div id='infoWndH'>"+branch+"</div><div align=left class='infoWnd3'><table  border=1 style='font-size:9pt;'>"+tab3+"</table></div>");
	}

	G_MARKERS[i].bindInfoWindowTabsHtml(gInfoWindowTabs);
	//  2009/09/07
	GEvent.addListener(G_MARKERS[i],"infowindowbeforeclose",function(){NMapPanTo(lat,lng);});

	ASTmap.addOverlay(G_MARKERS[i]);

	// DOM Event Listener
	var elm=document.getElementById(dom);
	if(elm!=null){
		GEvent.addDomListener(elm,"click",function(){
			GEvent.trigger(G_MARKERS[i],"click",null);
			NMapPanTo(lat,lng,zoom);
		});
		GEvent.addDomListener(elm,"dblclick",function(){
			GEvent.trigger(G_MARKERS[i],"click",null);
			NMapPanTo(lat,lng,zoom+3);
		});

		elm.innerHTML=branch;

		NSetCellMouseOver(elm);

	} else {
		GLog.write("BranchID '"+dom+"' not found.");
 	}
}


function NSetCellMouseOver(elm){
	NSetEventListener(elm,"mouseover",function(e){e.srcElement.style.backgroundColor="#99cccc" ;},false);
	NSetEventListener(elm,"mouseout" ,function(e){e.srcElement.style.backgroundColor="#eaeaea" ;},false);
}


function NMapPanTo( lat ,lng ,z ) {
 var latlng=new GLatLng(lat,lng);
	if(z != null){ ASTmap.setZoom(z); }
	ASTmap.panTo(latlng);
}

function NGetGoogleMapUrl(lat,lng,z,cid){
 var url;
 var latlng;

	if((lat==null)||(lng==null)||(z==null)){
		latlng=ASTmap.getCenter();
		z = ASTmap.getZoom();
	} else {
		latlng=new GLatLng(lat,lng);
	}

	if(cid == null){
		z=18;
		url = "http://maps.google.co.jp/maps?f=q";
	} else {
		url = "http://maps.google.co.jp/maps/place?";
		url += "cid=" + cid ;
	}

	url += "&ll=" + latlng.toUrlValue();
	url += "&hh=" + TXT_COUNTRY;
	url += "&ie=UTF8&z="+z+"&iwloc=addr&om=1";

	return(url);
}
