﻿// 2007/09/29 making NSK k.matsuda
//
// <HTML> tag add ->  XMLNS:v="urn:schemas-microsoft-com:vml"
// example
// <HTML XMLNS="http://www.w3.org/1999/xhtml" XMLNS:v="urn:schemas-microsoft-com:vml">
//

/*
// exzample
var opt1 = {
        tmpl: { linklist:{ use:'on', url:'/modelcourse/tmpl.linklist', innerid:'linklist' } },
        baseicon: {
                icon: { size:{width:28,height:42}, anchor:{x:13,y:42} },
                shadow: { url:'http://www.google.com/mapfiles/shadow50.png', size:{width:49,height:42} },
                info: { window_anchor:{x:13,y:2}, shadow_anchor:{x:18,y:25} },
                marker: { url:'/image/gmarker/marker', sufix:'.png' }
        },
        data: { listurl:'/ajax/easy-map.php', listone:'/ajax/easy-mapOne.php' },
	opt: { fitzoomadd:-1 }
};

var mclist = { 'list' : [
{ id:'900-ht000108',title:'',memo:'',lat:'',lon:'',move_method:'',move_interval:''},
{ id:'900-ht100033',title:'',memo:'',lat:'',lon:'',move_method:'',move_interval:''}
]};
var gitem = new pointgmap("map",mclist,opt1);
*/


var pointgmap = function() { this.initialize.apply(this,arguments); };
pointgmap.prototype = {
        'initialize' : function(gmap_divid,vmclist,opt)
        {
		this._init = { lat:0.0, lon:0.0, zoom:16 };
		this._gmap_divid = gmap_divid;
		this._mclist = vmclist;
		this._gitem = { gmap:undefined, baseicon:undefined, marker:{ obj:[], use:[] } };
		this._infotab = [];
		this._tmpl = { linklist:'' };
		this._opt = opt;

		// template load
		this._tmpl.linklist = this.ajax_request_raw(this._opt.tmpl.linklist.url);
	},
	'load' : function()
	{
		if (GBrowserIsCompatible()) {
			if(typeof(LoadingWait) == 'object') { LoadingWait.show(this._gmap_divid); }

			// gmap base icon initialize
			this._gitem.gmap = new GMap2(document.getElementById(this._gmap_divid));
			this.gfunction( { gsmall:'on', gmaptype:'on' } );
			this.gctr_setcenter(this._init.lat,this._init.lon,this._init.zoom);

			// gmap marker icon initialize
			this._gitem.baseicon = new GIcon();
			this._gitem.baseicon.iconSize = new GSize(this._opt.baseicon.icon.size.width, this._opt.baseicon.icon.size.height);
			this._gitem.baseicon.iconAnchor = new GPoint(this._opt.baseicon.icon.anchor.x, this._opt.baseicon.icon.anchor.y);
			this._gitem.baseicon.shadow = this._opt.baseicon.shadow.url;
			this._gitem.baseicon.shadowSize = new GSize(this._opt.baseicon.shadow.size.width, this._opt.baseicon.shadow.size.height);
			this._gitem.baseicon.infoWindowAnchor = new GPoint(this._opt.baseicon.info.window_anchor.x, this._opt.baseicon.info.window_anchor.y);
			this._gitem.baseicon.infoShadowAnchor = new GPoint(this._opt.baseicon.info.shadow_anchor.x, this._opt.baseicon.info.shadow_anchor.y);

			this.getparameter();

			if(typeof(LoadingWait) == 'object') { LoadingWait.delayhide(this._gmap_divid,3); }
		}
	},
	'ajax_request_raw' : function(url)
	{
		var data='';
		var req = new Ajax.Request(url,{method:'post', asynchronous:false, onSuccess:function (http){data=http.responseText;}});
		return data;
	},
	'ajax_request_json' : function(url)
	{
		var data;
		var req = new Ajax.Request(url,{method:'post', asynchronous:false, onSuccess:function (http){data=eval('('+http.responseText+')');}});
		return data;
	},
	'getparameter' : function()
	{
		var selfthis = this;

		var f='';
		var ids='';
		for(var i=0;i<this._mclist.list.length;i++) { ids += f + this._mclist.list[i].id; f=","; }
		var parameter = $H({ids:ids}).toQueryString();

		var req = new Ajax.Request(
			this._opt.data.listurl,
			{
				method		:	'post',
				postBody	:	parameter,
				onSuccess	:	function (http)
				{
					var json = eval('(' + http.responseText + ')');

					for(var i=0;i<json.informations.length;i++)
					{
						selfthis._mclist.list[i].no    = json.informations[i].no;
						selfthis._mclist.list[i].idx   = json.informations[i].idx;
						selfthis._mclist.list[i].key   = json.informations[i].key;
						selfthis._mclist.list[i].title = json.informations[i].title;
						selfthis._mclist.list[i].memo  = json.informations[i].memo;
						selfthis._mclist.list[i].lat   = json.informations[i].lat;
						selfthis._mclist.list[i].lon   = json.informations[i].lon;
					}

					selfthis._makingdata();
				}
			}
		);
	},
	'_makingdata' : function()
	{
		for(var i=0;i<this._mclist.list.length;i++)
		{
			if(this._mclist.list[i].lat=='' || this._mclist.list[i].lon=='')
			{
				this._gitem.marker.use[i]=0;
			}
			else
			{
				this._gitem.marker.use[i]=1;
			}
		}
		for(var i=0;i<this._mclist.list.length;i++)
		{
			this._infotab.push({prev:-1,next:-1});

			for(var j=i-1;j>=0;j--)
			{ if(this._gitem.marker.use[j]==1) { this._infotab[i].prev=j;break; } }

			for(j=i+1;j<this._mclist.list.length;j++)
			{ if(this._gitem.marker.use[j]==1) { this._infotab[i].next=j;break; } }
		}

		for(i=0;i<this._mclist.list.length;i++){
			var marker=this.create_marker(this._mclist.list[i],this._mclist.list[0].idx);
			this._gitem.marker.obj.push(marker);
			if(this._gitem.marker.use[i]==1) { this._gitem.gmap.addOverlay(marker); }
		}

		this.fitcenter();
		this.tmpl_replace_go();
	},
	'create_marker' : function(info,start_idx)
	{
		var selfthis = this;
		var marker;
		var latlng = new GLatLng(info.lat, info.lon);

		if (info.idx <= 100){
			var icon = new GIcon(this._gitem.baseicon);
			icon.image = this._opt.baseicon.marker.url + info.idx + this._opt.baseicon.marker.sufix;
			marker = new GMarker(latlng,icon);
		}else{
			marker = new GMarker(latlng);
		}

		GEvent.addListener(marker, "click", function() {
				var parameter = $H({info_key:info.key,start_idx:start_idx,info_no:(info.no+1),info_idx:info.idx,markermax:selfthis._gitem.marker.obj.length,marker_prev:selfthis._infotab[info.no].prev,marker_next:selfthis._infotab[info.no].next}).toQueryString();
				var req = new Ajax.Request(
					selfthis._opt.data.listone,
					{
						method		:	'post',
						postBody	:	parameter,
						onSuccess	:	function (http){
							var res = http.responseText;
							var tabHtml = res.split('**********');
							var tabList = [
								new GInfoWindowTab("観光情報",tabHtml[0]),
								new GInfoWindowTab("解説",tabHtml[1])
							];
							marker.openInfoWindowTabsHtml(tabList);
						}
					}
				);
			}
		);

		return marker;
	},
	'gfunction' : function(ctr)
	{
		if(ctr.glarge != undefined)
		{
			if(ctr.glarge == 'on') { this._gitem.gmap.addControl(new GLargeMapControl()); }
		}
		if(ctr.gsmall != undefined)
		{
			if(ctr.gsmall == 'on') { this._gitem.gmap.addControl(new GSmallMapControl()); }
		}
		if(ctr.goverview != undefined)
		{
		if(ctr.goverview == 'on') { this._gitem.gmap.addControl(new GOverviewMapControl()); }
		}
		if(ctr.gmaptype  != undefined)
		{
			if(ctr.gmaptype  == 'on') { this._gitem.gmap.addControl(new GMapTypeControl()); }
		}
	},
	'gctr_setcenter' : function(lat,lon,zoom)
	{
		if(zoom == undefined)
		{
			this._gitem.gmap.setCenter(new GLatLng(lat,lon));
		}
		else
		{
			this._gitem.gmap.setCenter(new GLatLng(lat,lon), zoom);
		}
	},
	'gmap_showtab' : function(tabno)
	{
		GEvent.trigger(this._gitem.marker.obj[tabno], 'click');
	},
	'gmap_zoomset' : function(zoom)
	{
		this._gitem.gmap.setZoom(zoom);
	},
	'gmap_zoomin' : function()
	{
		this._gitem.gmap.zoomIn();
	},
	'gmap_zoomout' : function()
	{
		this._gitem.gmap.zoomOut();
	},
	'get_gmapobj' : function()
	{
		return this._gitem.gmap;
	},
	'tmpl_replace' : function(no,info,data)
	{
		data=data.replace(/_##LIST.IDX##_/g,no+1);
		data=data.replace(/_##LIST.NO##_/g,no);
		data=data.replace(/_##LIST.ID##_/g,info.id);
		data=data.replace(/_##LIST.TITLE##_/g,info.title);
		data=data.replace(/_##LIST.MEMO##_/g,info.memo);
		data=data.replace(/_##LIST.MOVE_METHOD##_/g,info.move_method);
		data=data.replace(/_##LIST.MOVE_INTERVAL##_/g,info.move_interval);
		data=data.replace(/_##LIST.LAT##_/g,info.lat);
		data=data.replace(/_##LIST.LON##_/g,info.lon);
		return data;
	},
	'tmpl_replace_go' : function()
	{
		if(this._opt.tmpl.linklist.use=='on')
		{
			var linklist = '';
			for(var i=0;i<this._mclist.list.length;i++)
			{
				linklist += this.tmpl_replace(i,this._mclist.list[i],this._tmpl.linklist);
			}
			document.getElementById(this._opt.tmpl.linklist.innerid).innerHTML = linklist;
		}
	},
        '_get_center' : function(gmapobj,ar)
	{
		var lat,lon;
		var flag=0;
		var latmin=999;
		var lonmin=999;
		var latmax=-1;
		var lonmax=-1;
		var fitzoom = 16;
		var clat = 0;
		var clon = 0;

		for(var i=0;i<ar.length;i++)
		{
			var lat = parseFloat(ar[i].lat);
			var lon = parseFloat(ar[i].lon);

			if(latmin>lat){ latmin=lat; flag=1; }
			if(latmax<lat){ latmax=lat; flag=1; }
			if(lonmin>lon){ lonmin=lon; flag=1; }
			if(lonmax<lon){ lonmax=lon; flag=1; }
		}

		if(flag)
		{
			clat = (latmax+latmin)/2.0;
			clon = (lonmax+lonmin)/2.0;
			fitzoom = this._get_fitzoom(gmapobj,{lat:{max:latmax,min:latmin},lon:{max:lonmax,min:lonmin}});
		}

		return ({flag:flag, latmax:latmax, latmin:latmin, lonmax:lonmax, lonmin:lonmin, clat:clat, clon:clon, fitzoom:fitzoom });
	},
	'_get_fitzoom' : function (gmapobj,p)
	{
		var fitzoom = undefined;
		if(typeof(gmapobj)=='object')
		{
			fitzoom = gmapobj.getBoundsZoomLevel(
					new GLatLngBounds(
						new GLatLng(p.lat.min,p.lon.min),
						new GLatLng(p.lat.max,p.lon.max)
						)
					);
		}
		return fitzoom;
	},
	'_fitcenter' : function(gmapobj,ar,val)
	{
		if(val==undefined) { val=0; }
		val = parseInt(val);
		var p = this._get_center(gmapobj,ar);
		// gmapobj.setCenter(new GLatLng(p.clat,p.clon),p.fitzoom);
		gmapobj.setZoom(p.fitzoom+val+this._opt.option.fitzoomoffset);
		gmapobj.panTo(new GLatLng(p.clat,p.clon));
	},
	'fitcenter' : function(val)
	{
		this._fitcenter(this._gitem.gmap,this._mclist.list,val);
	}
};



