/*********************************************
 Google Map Script
 2006.08.21 beatup Takashi Yamamoto
 
 param zoom : 1 - 17 (zoomLevel)
       stid : null   ->  display all
	          1 - 16 ->  display station and
			             zoomlevel to 15
*********************************************/

var gMap;
//default zoom level
var zoom_level = 13;
var stid = 0;
//default location
var st_location = {
	'lng' : 139.52207565307617,
	'lat' : 35.32619020925125
};

st_location_ar = new Array();
var st_imgpath = "http://treep.jp/enoden/images/stations/";


/*********************************************
              onLoad function
*********************************************/
function Call(class_id){
	//Browser check for Google map...
	if(GBrowserIsCompatible()){
		
		//Call Google Map API and set default values
		initMap(class_id);

		//Get value
		var url = document.location.href;
		
		//Get zoom_level
		if(url.indexOf('zoom') > 0){
			if(url.match(/zoom=([\d\.]+)/i)){
				var zl =  parseInt(RegExp.$1);
				if(0 < zl && zl <= 17){
					zoom_level = zl;
				}
			}
		}
		
		//Get station id
		if(url.indexOf('stid') > 0){
			if(url.match(/stid=([\d\.]+)/i)){
				var getstid =  parseInt(RegExp.$1);
				if(0 < getstid && getstid <= 16){
					zoom_level = 15;
					stid = getstid;
					
					loadTempleXML(stid);
				}
			}
		}		
		
		
	} else {
		var html =	'大変申し訳ありません。お使いのブラウザでは本サービスのご利用はできません。\n' +
				'現在当サービスのご利用可能なブラウザは以下となっております。\n' +
					'・IE 5.5～6　(Windows)\n' +
					'・Firefox 0.8以降　(Windows, Mac, Linux)\n' +
					'・Safari 1.2.4以降　(Mac)\n' +
					'・Netscape 7.1以降　(Windows, Mac, Linux)\n' +
					'・Mozilla 1.4以降　(Windows, Mac, Linux)\n' +
					'・Opera 7.5以降　(Windows, Mac, Linux)\n';
		$(class_id).innerHTML=html;
	}
}

/*********************************************
                    Init
*********************************************/
function initMap(id){	
	
	gMap = new GMap2($(id));
	gMap.addControl(new GMapTypeControl());
	gMap.addControl( new GScaleControl() );
	gMap.addControl( new GLargeMapControl() );
	
	gMap.setCenter(new GLatLng(st_location['lat'], st_location['lng']), zoom_level);

	//Load station.xml...
	loadStationXML();

	//Load event.xml...
	loadEventXML();

	//Load noriori.xml...
	loadNorioriXML();

	GEvent.addListener(gMap, "click", function(overlay, point) {
		if(overlay == null) {
			gMap.closeInfoWindow();
		}
	});
	
	//GEvent.addListener(gMap, "infowindowopen", function() {	
	//});
	
	//GEvent.addListener(gMap, "infowindowclose", function() {
	//});

}

/*********************************************
            Load XML for stations
*********************************************/
function loadStationXML() {
	var filepath = "data/station.xml";
	
	//for no cache
	time = new Date();
	
	new Ajax.Request(
		filepath,
		{
			method:'get',
			parameters:"?"+time.getTime(),
			onLoading:function(){
			},
			onComplete: parse_station_list
		}
	);
}

/*********************************************
            Parse XML for stations
*********************************************/
function parse_station_list(httpObj) {
	var res = httpObj.responseXML;
	var station_n = res.getElementsByTagName("station");
	var station_len = station_n.length;
	
	var lat = 0;
	var lng = 0;
	for(var i=0;i<station_len;i++) {
		lat = parseFloat(station_n[i].getAttribute("lat"));
		lng = parseFloat(station_n[i].getAttribute("lng"));
		
		//set station's lat and lng
		st_location_ar[i+1] = {'lng' : lng,'lat' : lat}
		
		var point = new GLatLng(lat,lng);

		//Write station icons
		createStationMarker(point,
						   station_n[i].getAttribute("name"), 
						   station_n[i].getAttribute("img"),
						   station_n[i].getAttribute("url"),
						   station_n[i].getAttribute("str"));
	}
	
	//move senter
	if(stid > 0 && stid < 17) {
		gMap.setCenter(new GLatLng(st_location_ar[stid]["lat"], st_location_ar[stid]["lng"]), zoom_level);
	}
}

/*********************************************
            Create station markers
*********************************************/
function createStationMarker(point, name, imgfname, url, str) {
	var sitepath = "";
	var icon = new GIcon();
	
	icon.image = "images/icon_st.png";
	icon.iconSize = new GSize(20, 34);
	icon.iconAnchor = new GPoint(10, 34);
	icon.infoWindowAnchor = new GPoint(10, 1);

	var marker = new GMarker(point, icon);
	
	//Setup html for Gmap infowindow
	var html = '<div id="infowin">';
	html += 
	'<div class="content-box">'+
	'<div class="content-pic"><img alt="'+name+'画像" src="'+st_imgpath+imgfname+'" /></div>'+
	'<div class="content-t-box-pic">'+
	'<div class="content-title"><h3><a href="../'+url+'" target="_top">'+name+'</a></h3></div>'+
	'<div class="content-text"><a href="../'+url+'" target="_top">'+str+'</a></div>'+
	'</div>'+
	'<div class="clr"></div>'+
	'</div></div>';
		
	GEvent.addListener(marker, "click", function() {marker.openInfoWindowHtml(html);});
	gMap.addOverlay(marker);
}

/*********************************************
            Load XML for Event
*********************************************/
function loadEventXML() {
	var filepath = "data/event.xml";
	
	new Ajax.Request(
		filepath,
		{
			method:'get',
			parameters:null,
			onLoading:function(){
			},
			onComplete: parse_event_list
		}
	);
}

/*********************************************
            Parse XML for event
*********************************************/
function parse_event_list(httpObj) {
	var res = httpObj.responseXML;
	var event_n = res.getElementsByTagName("event");
	var event_len = event_n.length;
		
	var lat = 0;
	var lng = 0;
	for(var i=0;i<event_len;i++) {
		lat = parseFloat(event_n[i].getAttribute("lat"));
		lng = parseFloat(event_n[i].getAttribute("lng"));

		var point = new GLatLng(lat,lng);

		//Write event icons
		createEventMarker(point,
						   event_n[i].getAttribute("date"), 
						   event_n[i].getAttribute("add"),
						   event_n[i].getAttribute("inf_tl"),
						   event_n[i].getAttribute("inf_by"));
	}
}

/*********************************************
            Create event markers
*********************************************/
function createEventMarker(point, date, add, inf_tl, inf_by) {
	var icon = new GIcon();
	icon.image = "images/icon_et.png";
	icon.iconSize = new GSize(20, 34);
	icon.iconAnchor = new GPoint(10, 34);
	icon.infoWindowAnchor = new GPoint(10, 1);

	var marker = new GMarker(point, icon);
	
	//Setup html for Gmap infowindow
	var html = '<div id="infowin">';
	html += 
	'<div class="content-box">'+
	'<div class="content-t-box">'+
	'<div class="content-date">'+date+'</div>'+
	'<div class="content-title"><h3>'+add+'</h3></div>'+
	'<div class="content-add">'+inf_tl+'</div>'+
	'<div class="content-text">'+inf_by+'</div>'+
	'</div>'+
	'<div class="clr"></div>'+
	'</div></div>';
	
	GEvent.addListener(marker, "click", function() {marker.openInfoWindowHtml(html);});
	gMap.addOverlay(marker);
}

/*********************************************
            Load XML for Temple
*********************************************/
function loadTempleXML(stid) {
	var filepath = "data/temple"+stid+".xml";
	
	//for no cache
	time = new Date();
	
	new Ajax.Request(
		filepath,
		{
			method:'get',
			parameters:"?"+time.getTime(),
			onLoading:function(){
			},
			onComplete: parse_temple_list
		}
	);
}

/*********************************************
            Parse XML for temple
*********************************************/
function parse_temple_list(httpObj) {
	var res = httpObj.responseXML;
	var temple_n = res.getElementsByTagName("marker");
	var temple_len = temple_n.length;
		
	var lat = 0;
	var lng = 0;
	for(var i=0;i<temple_len;i++) {
		lat = parseFloat(temple_n[i].getAttribute("lat"));
		lng = parseFloat(temple_n[i].getAttribute("lng"));

		var point = new GLatLng(lat,lng);

		//Write temple icons
		createTempleMarker(point,
						   temple_n[i].getAttribute("name"), 
						   temple_n[i].getAttribute("kana"),
						   temple_n[i].getAttribute("copy"),
						   temple_n[i].getAttribute("access"),
						   temple_n[i].getAttribute("img"),
						   temple_n[i].getAttribute("url"));
	}
}

/*********************************************
            Create temple markers
*********************************************/
function createTempleMarker(point, name, kana, copy, access, img, url) {
	var tmple_imgpath = "http://www.treep.jp/";
	var icon = new GIcon();
	icon.image = "images/icon_tpl.png";
	icon.iconSize = new GSize(20, 34);
	icon.iconAnchor = new GPoint(10, 34);
	icon.infoWindowAnchor = new GPoint(10, 1);
	var marker = new GMarker(point, icon);
	
	//Setup html for Gmap infowindow
	var html = '<div id="infowin">';
	html += 
	'<div class="content-box">'+
	'<div class="content-pic"><img alt="'+name+'画像" src="'+tmple_imgpath+img+'" /></div>'+
	'<div class="content-t-box-pic">'+
	'<div class="content-title"><h3>'+name+'('+kana+')</h3></div>'+
	'<div class="content-text">'+copy+'</div>'+
	'<div class="content-text">'+access+'</div>'+
	'<div class="content-text"><a href="'+tmple_imgpath+url+'" target="_blank">&raquo;&nbsp;詳細を見る</a></div>'+
	'</div>'+
	'<div class="clr"></div>'+
	'</div></div>';

	GEvent.addListener(marker, "click", function() {marker.openInfoWindowHtml(html);});
	gMap.addOverlay(marker);
}

/*********************************************
            Load XML for Noriori
*********************************************/
function loadNorioriXML() {
	var filepath = "data/noriori.xml";
	
	new Ajax.Request(
		filepath,
		{
			method:'get',
			parameters:null,
			onLoading:function(){
			},
			onComplete: parse_noriori_list
		}
	);
}

/*********************************************
            Parse XML for noriori
*********************************************/
function parse_noriori_list(httpObj) {
	var res = httpObj.responseXML;
	var noriori_n = res.getElementsByTagName("noriori");
	var noriori_len = noriori_n.length;
		
	var lat = 0;
	var lng = 0;
	for(var i=0;i<noriori_len;i++) {
		lat = parseFloat(noriori_n[i].getAttribute("lat"));
		lng = parseFloat(noriori_n[i].getAttribute("lng"));

		var point = new GLatLng(lat,lng);

		//Write noriori icons
		createNorioriMarker(point,

						   noriori_n[i].getAttribute("cat"),
						   noriori_n[i].getAttribute("name"),
						   noriori_n[i].getAttribute("access"), 
						   noriori_n[i].getAttribute("tel"),
						   noriori_n[i].getAttribute("time"),
						   noriori_n[i].getAttribute("day"),
						   noriori_n[i].getAttribute("pri"));
	}
}

/*********************************************
            Create noriori markers
*********************************************/
function createNorioriMarker(point, cat, name, access, tel, time, day, pri) {
	var tmple_imgpath = "http://www.treep.jp/";
	var icon = new GIcon();
	icon.image = "images/icon_no.png";
	icon.iconSize = new GSize(20, 34);
	icon.iconAnchor = new GPoint(10, 34);
	icon.infoWindowAnchor = new GPoint(10, 1);

	var marker = new GMarker(point, icon);
	
	//Setup html for Gmap infowindow
	var html = '<div id="infowin">';
	html += 
	'<div class="content-box">'+
	'<div class="content-t-box">'+
	'<div class="content-title"><h3>['+cat+'] '+name+'</h3></div>'+
	'<div class="content-text">[住所] '+access+'</div>'+
	'<div class="content-text">[電話] '+tel+'</div>'+
	'<div class="content-text">[営業時間] '+time+'</div>'+
	'<div class="content-text">[定休日] '+day+'</div>'+
	'<div class="content-text">[のりおりくん特典] '+pri+'</div>'+
	'</div>'+
	'<div class="clr"></div>'+
	'</div></div>';
	
	GEvent.addListener(marker, "click", function() {marker.openInfoWindowHtml(html);});
	gMap.addOverlay(marker);
}