// ========================================================================================@ MTechno
//  Class_Gmap.js ---- JavaScript Common Library ver.1.0
// -------------------------------------------------------------------------------------------------
//  prototype.js 必須  (このスクリプトより前に読み込むこと)
// =================================================================================================
//   GOOGLEMAP CLASS
//--------------------------------------------------------------------------------------------------
var GmapMT = Class.create();
GmapMT.prototype = {
	initialize: function ( element, lat, lng, zoom ) {
		this.element = $( element );
		this.map = new GMap2( this.element );
		this.map.setCenter( new GLatLng( lat, lng ), zoom );
		this.map.addControl( new GScaleControl() );
		//this.map.enableContinuousZoom();
		this.lat = lat;
		this.lng = lng;
		this.zoom = zoom;
		this.board = document.createElement( 'div' );
		document.getElementsByTagName( 'body' )[0].appendChild( this.board );
		this.board = $( this.board );
		this.board.id = 'MT_GMAP_BOARD';
		this.board.setStyle( { position: 'absolute', display: 'none' } );
		Event.observe( document, 'mousemove', this.getMouseXY.bindAsEventListener( this ) );
		return this;
	},
	registMarker: function ( jsonFile, chilWinTempID, imgFile, options, staticPos, nrb ) {
		this.icon = new GIcon();
		this.icon.image = imgFile;
		if ( !options ) {
			this.icon.iconSize = new GSize( 22, 22 );
			this.icon.iconAnchor = new GPoint( 22, 22 );
			this.icon.infoWindowAnchor = new GPoint( 9, 2 );
		} else {
			this.icon.iconSize = options.iconSize;
			this.icon.iconAnchor = options.iconAnchor;
			this.icon.infoWindowAnchor = options.infoWindowAnchor;
		}
		var self = this, http = [];
		var funcs = function ( data ) {
			for ( var i = 0, len = data.length; i < len; i++ ) {
				var markers = self.createMarker( data[i] );
				self.map.addOverlay( markers );
			}
			if ( !staticPos ) {
				var s = self.getMpdata( data );
				self.lat = s[0];
				self.lng = s[1];
				self.zoom = self.map.getZoom();
			}
		};
		for ( var i = 0, n = nrb.length; i < n; i++ ) {
			if ( /\?/.test( jsonFile ) ) http[i] = new JKL.ParseXML.JSON( jsonFile + '&cache=' + ( new Date() ).getTime() + '&ck=' + nrb[i] );
			else http[i] = new JKL.ParseXML.JSON( jsonFile + '?cache=' + ( new Date() ).getTime() + '&ck=' + nrb[i] );
			http[i].async( funcs );
			http[i].parse();
		}
		return this;
	},
	createMarker: function (d) {
		var marker = new GMarker( new GLatLng( d.lat, d.lng ), { icon : this.icon } );
		var ickid = d.kid;
		GEvent.addListener( marker, 'click', function () {
			$('irekaeArea').innerHTML = new JKL.ParseXML.Text( 'bkwindow.php?k=' + ickid + '&cache=' + ( new Date() ).getTime() ).parse();
			//this.board.show();
		}.bindAsEventListener( this ) );
		return marker;
	},
	getMpdata: function ( d, callback ) {
		var maxLat = 0, maxLng = 0, minLat = 0, minLng = 0, a = arguments;
		if ( a.length > 3 ) {
			maxLat = a[0]; maxLng = a[1]; minLat = a[2]; minLng = a[3];
		} else {
			for ( var i = 0, len = d.length; i < len; i++ ) {
				d[i].lat = parseFloat( d[i].lat );
				d[i].lng = parseFloat( d[i].lng );
				if ( !maxLat || maxLat < d[i].lat ) maxLat = d[i].lat;
				if ( !minLat || minLat > d[i].lat ) minLat = d[i].lat;
				if ( !maxLng || maxLng < d[i].lng ) maxLng = d[i].lng;
				if ( !minLng || minLng > d[i].lng ) minLng = d[i].lng;
			}
		}
		if ( ( maxLat + maxLng + minLat + minLng ) == 0 ) return;
		var bound = new GLatLngBounds( new GLatLng( minLat, minLng ), new GLatLng( maxLat, maxLng ) );
		var zoom = this.map.getBoundsZoomLevel( bound );
		if ( zoom > 16 ) zoom = 16;
		if ( zoom < 11 ) zoom = 11;
		this.map.setCenter( bound.getCenter() );
		var a = bound.getCenter();
		if ( typeof( callback ) == 'function' ) callback( a.lat(), a.lng(), zoom );
		this.changeZoom( zoom );
		return [ a.lat(), a.lng(), zoom ];
	},
	changeZoom: function ( zoom, callback ) {
		zoom = parseInt( zoom );
		if ( zoom < 10 ) zoom = 17 - zoom;
		this.map.setZoom( zoom );
		if ( typeof( callback ) != 'undefined' ) callback( zoom );
		else if ( typeof( this.zoom_callback ) != 'undefined' ) this.zoom_callback( null, null, zoom );
		return zoom;
	},
	changeMapType: function ( t, callback ) {
		switch (t) {
			case 1:
				this.map.setMapType( G_NORMAL_MAP );
				break;
			case 2:
				this.map.setMapType( G_SATELLITE_MAP );
				break;
			case 3:
				this.map.setMapType( G_HYBRID_MAP );
				break;
		}
		if ( typeof( callback ) != 'undefined' ) callback(t);
	},
	getMouseXY: function (e) {
		if ( window.createPopup ) {
			x = window.event.clientX + document.body.scrollLeft;
			y = window.event.clientY + document.body.scrollTop;
		} else {
			x = e.pageX;
			y = e.pageY;
		}
		y -= 70;
		this.xy = [ x, y ];
		return this.xy;
	},
	setZoomCallback: function (f) { this.zoom_callback = f; },
	bkDetail: function ( e, url ) { window.open( url, '_blank' ); },
	bkmpDetail: function ( e, lat, lng, zoom ) {
		lat = parseFloat( lat );
		lng = parseFloat( lng );
		zoom = parseInt( zoom );
		if ( !lat || !lng || !zoom ) return false;
		if ( zoom < 8 ) zoom = 17 - zoom;
		this.map.setCenter( new GLatLng( lat, lng ), zoom );
		var a = this.element.getDimensions();
		this.board.style.left = ( this.element.offsetLeft + a.width / 2 ) + 'px';
		this.board.style.top = ( this.element.offsetTop + a.height / 2 ) + 'px';
		if ( typeof( this.zoom_callback ) != 'undefined' ) this.zoom_callback( null, null, zoom );
	}
};
