// ========================================================================================@ 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.addControl( new GLargeMapControl() );
		//this.map.addControl( new GMapTypeControl() );
		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 ( xmlFile, chilWinTempID, imgFile, shadowFile, options, staticPos ) {
		this.icon = new GIcon();
		this.icon.image = imgFile;
		if ( !options ) {
			this.icon.iconSize = new GSize( 21, 18 );
			this.icon.iconAnchor = new GPoint( 21, 18 );
			this.icon.infoWindowAnchor = new GPoint( 9, 9 );
		} else {
			this.icon.iconSize = options.iconSize;
			this.icon.shadowSize = options.shadowSize;
			this.icon.iconAnchor = options.iconAnchor;
			this.icon.infoWindowAnchor = options.infoWindowAnchor;
		}
		//this.windowTemp = $( chilWinTempID ).innerHTML;
		//$( chilWinTempID ).innerHTML = '';
		if ( /\?/.test( xmlFile ) ) var http = new JKL.ParseXML( xmlFile + '&cache=' + ( new Date() ).getTime() );
		var http = new JKL.ParseXML( xmlFile + '?cache=' + ( new Date() ).getTime() );
		this.markers = [];
		http.setOutputArrayElements( 'bk' );
		http.async( function ( data ) {
			for ( var i = 0, len = data.item.bk.length; i < len; i++ ) {
				this.markers[i] = this.createMarker( data.item.bk[i] );
				this.map.addOverlay( this.markers[i] );
			}
			if ( !staticPos ) {
				var s = this.getMpdata( data.item.bk );
				this.lat = s[0];
				this.lng = s[1];
				this.zoom = this.map.getZoom();
			}
		}.bindAsEventListener( this ) );
		http.parse();
		return this;
	},
	createMarker: function (d) {
		var marker = new GMarker( new GLatLng( d.lat, d.lng ), { icon : this.icon, title : d.k_name } );
		var ickid = d.kid;
		var detail_url = decodeURIComponent( d.detail_url );
		if ( detail_url == 'non' ) detail_url = 'http://www.polus.jp/kensaku/sc1/kbukken_detail.php?k=' + d.kid;
		/*var html = this.windowTemp;
		var lat = d.lat, lng = d.lng, zoom = d.zoom;
		html = html.replace( /#MPLINK#/g, a );
		html = html.replace( /#CLOSELINK#/g, '<a href="#" onClick="$(\'MT_GMAP_BOARD\').innerHTML=\'\';return false;">' );
		html = html.replace( /#K_NAME#/g, d.k_name );
		html = html.replace( /#SKOSU#/g, d.skosu );
		html = html.replace( /#HKOSU#/g, d.hkosu );
		html = html.replace( /#HPRICE#/g, d.hprice );
		html = html.replace( /#XKID#/g, d.kid );
		if ( d.img != 'non' ) var a = '<img src="' + d.img + '" width="80" height="60" />';
		else var a = '';
		html = html.replace( /#KDIMG#/g, a );*/
		GEvent.addListener( marker, 'click', function () {
			window.open( detail_url, "_blank", "");
			/*var url = 'bkwindow.php?k=' + ickid + '&cache=' + ( new Date() ).getTime();
			var data = new JKL.ParseXML.Text( url ).parse();
			this.board.style.left = this.xy[0] + 'px';
			this.board.style.top = this.xy[1] + 'px';
			this.board.innerHTML = data;
			this.board.show();
			var a1 = $( 'GMAPMT_KDLINK' );
			var a2 = $( 'GMAPMT_MPLINK' );
			a1.onclick = null;
			a2.onclick = null;
			a1.style.cursor = 'pointer';
			a2.style.cursor = 'pointer';
			Event.observe( a1, 'click', this.bkDetail.bindAsEventListener( this, detail_url ), false );
			Event.observe( a2, 'click', this.bkmpDetail.bindAsEventListener( this, lat, lng, zoom ), false );*/
		}.bindAsEventListener( this ) );
		return marker;
	},
	getMpdata: function ( d, callback ) {
		var maxLat = 0;
		var maxLng = 0;
		var minLat = 0;
		var minLng = 0;
		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 tkytw = new GLatLng( minLat, minLng );
		var hamarky = new GLatLng( maxLat, maxLng );
		var bound = new GLatLngBounds( tkytw, hamarky );
		var later = function () { this.map.panTo( bound.getCenter() ); }.bindAsEventListener( this );
		var zoom = this.map.getBoundsZoomLevel( bound );
		this.map.setCenter( bound.getCenter() );
		var a = bound.getCenter();
		if ( typeof( callback ) != 'undefined' ) callback( a.lat(), a.lng(), zoom );
		setTimeout( later, 200 );
		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;
		}
		x += 30;
		y -= 20;
		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 ( 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 );
	}
};

