//*===============================================================
//□初期化
//*===============================================================
$( document ).ready( function() {
	Zg.prototype.init();
});
//================================================================
//□Zg
//================================================================
var Zg = function() {
};
//================================================================
//□共通定数
//================================================================
Zg.c = {
	ID_GMAP			: '#Map'			, //
	DEFAULT_ZOOM	: 9					, //
	PNG_CLEAR		: '/common/images/pc/blank.gif',
	DUMMY_END		: 0					 //
};
//================================================================
//□共通オブジェクト
//================================================================
Zg.p = {
	gMap			: null				, //GoogleMapオブジェクト
	gEventsOld		: []				, //イベント：削除用
	gIcon			: []				, //アイコンオブジェクト
	gMarker			: null				, //マーカー
	gWindow			: null				, //別ウィンドウ
	DUMMY_END		: 0					 //
};
//*===============================================================
//□初期処理
//*===============================================================
Zg.prototype.init = function() {
	// PNG FIX(ロールオーバー)
	$('img.Imgover').each(function() { $('<img>').attr('src',$(this).attr('src').replace(/^(.+)(\.[a-z]+)$/,'$1_ov$2')); });
	if ( $.browser.msie && $.browser.version < 7 ) {
		$('img.Imgover').each( function() {$(this).data('src',$(this).attr('src')).attr('src',Zg.c.PNG_CLEAR).css('filter',"progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+$(this).data("src")+"',sizingMethod='scale')")})
			.mouseover(function() { $(this).css('filter',"progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+$(this).data('src').replace(/^(.+)(\.[a-z]+)$/, '$1_ov$2')+"',sizingMethod='scale')") })
			.mouseout (function() { $(this).css('filter',"progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+$(this).data('src')+"',sizingMethod='scale')") })
	} else {
		$('img.Imgover')
			.mouseover(function() { $(this).attr('src',$(this).attr('src').replace(/^(.+)(\.[a-z]+)$/   ,'$1_ov$2')); })
			.mouseout (function() { $(this).attr('src',$(this).attr('src').replace(/^(.+)_ov(\.[a-z]+)$/,'$1$2'   )); })
	}
	// PNG FIX
//	$('img').pngfix();
	DD_belatedPNG.fix('img');

	// ロールオーバー用のクラスの除去
	if ( $('#PageID').length == 1 ) {
		var id = '#' + $('#PageID').val();
		if ($(id).length == 1) {
			$(id).removeAttr('class');
			$(id).attr('src',$(id).attr('src').replace(/\.(gif|jpe?g|png)$/,'_ov.$1'));
		}
	}

	// GoogleMap
	if ( $( Zg.c.ID_GMAP ).get(0) != null ) {
		//◆Google Mapsが利用できるブラウザかどうか判断
		if ( GBrowserIsCompatible() ) {
			//◇利用可能
			//□地図のインスタンスを作成
			Zg.p.gMap = new GMap2( $( Zg.c.ID_GMAP ).get(0) , G_NORMAL_MAP );
			//□キーボードで操作
			new GKeyboardHandler( Zg.p.gMap );
			//□移動/縮尺ボタンをつける
			Zg.p.gMap.addControl( new GSmallMapControl() , new GControlPosition( G_ANCHOR_TOP_LEFT , new GSize( 5 , 32 ) ) );
			//□地図/衛星写真 切り替え
			Zg.p.gMap.addControl(
				new GMenuMapTypeControl( true , false ) ,
				new GControlPosition( G_ANCHOR_TOP_LEFT , new GSize( 5 , 5  ) )
			);
			Zg.p.gMap.addMapType( G_PHYSICAL_MAP );
			//□初期表示位置を設定
			Zg.p.gMap.setCenter(
				new GLatLng( parseFloat( CONST_DEFAULT_LAT ) , parseFloat( CONST_DEFAULT_LNG ) ) , parseInt( Zg.c.DEFAULT_ZOOM + 5 )
			);
			//□ズームイン時にスムーズ
			Zg.p.gMap.enableContinuousZoom();
			//□ダブルクリック時にズームイン
			Zg.p.gMap.enableDoubleClickZoom();
			//□初期化
			Zg.prototype.clearAllParameter();
			//□ポイント
			Zg.p.gMarker = new GMarker(
				new GLatLng( parseFloat( CONST_DEFAULT_LAT ) , parseFloat( CONST_DEFAULT_LNG ) ) , {draggable:false}
			);
			Zg.p.gMap.addOverlay( Zg.p.gMarker );
		}
	}
	//松山人気質度チェック
	if ( $('div.MatsuyamajinCheck').get(0) != null ) {
		Zg.prototype.reset();
		Zg.prototype.setEvents();
	}
};
//*===============================================================
//○初期化
//*===============================================================
Zg.prototype.clearAllParameter = function() {
//GLog.write('clearAllParameter');
	Zg.p.gMap.clearOverlays();
	for ( idx=0; idx < Zg.p.gEventsOld.length; idx++ ) {
		if ( Zg.p.gEventsOld[idx] != '______array' && Zg.p.gEventsOld[idx] != '' ) {
			GEvent.removeListener( Zg.p.gEventsOld[idx] );
		}
	}
	Zg.p.gEventsOld  = new Array();
};
//*===============================================================
//○計算
//*===============================================================
Zg.prototype.calculate = function() {
	//画面値取得
	var sum = 0;
	var all = 0;
	$.each( $( 'div.MatsuyamajinCheck td' ) , function( i , val ) {
		if ( $(this).children()[0].checked == true ) {
			sum += parseInt( $(this).children()[0].value );
		}
		all++;
	});
	var result = Math.round((sum/all)*100);

	if ( sum >= 10 ) {
		$( '#MatsuyamajinA' ).css ( 'display' , 'block' );
		$( '#MatsuyamajinA .degrees' ).html( '松山人 気質度 ' + result + '％' );
	} else
	if ( sum >= 4 ) {
		$( '#MatsuyamajinB' ).css ( 'display' , 'block' );
		$( '#MatsuyamajinB .degrees' ).html( '松山人 気質度 ' + result + '％' );
	} else {
		$( '#MatsuyamajinC' ).css ( 'display' , 'block' );
		$( '#MatsuyamajinC .degrees' ).html( '松山人 気質度 ' + result + '％' );
	}
	$( '#BtnRecheck' ).css ( 'display' , 'block' );
	$( '#Matsuyamajin,#BtnCheck,div.MatsuyamajinCheck' ).css ( 'display' , 'none' );
	$.scrollTo( $( 'h3' ) , 750 );
};
//*===============================================================
//○リセット
//*===============================================================
Zg.prototype.reset = function() {
	$( '#Matsuyamajin,#BtnCheck' ).css ( 'display' , 'block' );
	$( '#MatsuyamajinA,#MatsuyamajinB,#MatsuyamajinC,#BtnRecheck' ).css ( 'display' , 'none' );
	$( 'div.MatsuyamajinCheck input' ).attr( 'checked' , false );
	$.scrollTo( $( 'Header' ) , 750 );
};
//*===============================================================
//○もう一度計算する
//*===============================================================
Zg.prototype.retry = function() {
	$( '#Matsuyamajin,#BtnCheck,div.MatsuyamajinCheck' ).css ( 'display' , 'block' );
	$( '#MatsuyamajinA,#MatsuyamajinB,#MatsuyamajinC,#BtnRecheck' ).css ( 'display' , 'none' );
	$.scrollTo( $( 'div.MatsuyamajinCheck' ) , 750 );
};
//*===============================================================
//○イベント登録
//*===============================================================
Zg.prototype.setEvents = function() {
	$( 'input[type=image][name=submit]' ).click  ( Zg.prototype.calculate );
	$( 'input[type=image][name=reset]'  ).click  ( Zg.prototype.reset     );
	$( 'input[type=image][name=agein]'  ).click  ( Zg.prototype.retry     );
};