/*
  top_contents.js
  for Bridgestone GlobalWebSite
*/

(function(){

	/**
	 * 定数定義
	 */

	/* JSが有効な場合に、追加展開するメインビジュアル */
	var HTML_ADD_MAINVISUALS =
		'<div><img src="/images/narrowband/tpc02_main.jpg" width="900" height="309" alt=""></div>' +
		'<div><img src="/images/narrowband/tpc03_main.jpg" width="900" height="309" alt=""></div>' +
		'<div><img src="/images/narrowband/tpc04_main.jpg" width="900" height="309" alt=""></div>';

	/* 新規ウィンドウ展開時のオプション */
	var STR_OPENWINDOW_OPTION = "menubar=yes,toolbar=yes,location=yes,status=yes,resizable=yes,scrollbars=yes";

	/* ナローバンド版への導線 */
	var HTML_TO_NARROWBAND =
		'<li><a href="index.html" class="toNarrowband">Narrowband</a></li>';

	// ユーザー設定フォントサイズのcookie保管用キー名
	var STR_BANDWIDTH_COOKIEKEY = 'bandwidthSetting';



	/**
	 * 初期表示処理
	 */

	var onLoadFunc = function(){

		/* ブロードバンド版の場合（設定bb && Flash） */
		if( $(document.body).hasClass("broadbandEnabled") ){

			// ヘッダーに、ナローバンド版への導線を追加
			$( "#header ul.toGeneralPages" ).prepend( HTML_TO_NARROWBAND );
			$( "#header ul.toGeneralPages a.toNarrowband" ).click( function(){
				sendAnalyticsEvent( "Switch_Narrowband" );
			} );

			// ナローバンド版の設定UIを初期化
			$( "#header .toGeneralPages .toNarrowband" ).click( function(){
				bsjs.setCookie(STR_BANDWIDTH_COOKIEKEY,"nb");
				location.reload();
				return false;
			} );
		}

		/* ナローバンド版が動作する場合（設定nb || NoFlash） */
		else {

			/* 汎用：fadeIn のレガシーUA対応 */
			if( !$("#mainVisual div").fadeIn ){
				$.fn.fadeIn = function(speed,callback){
					return this.css("display","block");
				}
			}


			// PR画像のJS展開分を追加
			$( "#mainVisual" ).append( HTML_ADD_MAINVISUALS );

			// PR画像のJS展開UIを初期化
			$( "#topPR ul.selectionTopics" ).attr("currentNumber","0");
			$( "#topPR ul.selectionTopics li" )
				.each( function( myPosition ){
					$(this).attr( "mvNumber", myPosition );
				} )
				.bind( "mouseenter", function(myEv){
					//$( "#mainVisual div" ).hide();
					//$($( "#mainVisual div" ).get( $(this).attr("mvNumber")-0 )).show();
					//return;

					var lastNumber = $(this.parentNode).attr("currentNumber")-0;
					var targetNumber = $(this).attr("mvNumber")-0;

					if( lastNumber === targetNumber ){ return false; }

					$( "#mainVisual div" ).css("zIndex","1");
					$($( "#mainVisual div" ).get( lastNumber )).css("zIndex","2");
					$($( "#mainVisual div" ).get( targetNumber ))
						.css("display","none")
						.css("zIndex","3")
						.fadeIn("fast")

					$(this.parentNode).attr("currentNumber",$(this).attr("mvNumber"));
				} )
				.click( function(){
					window.open(
						$(this).find("a").get(0).href,
						( $(this).find("a").get(0).target || "_self" ),
						STR_OPENWINDOW_OPTION
					);
				} )
				.find("a").click( function( myEv ){
					myEv.preventDefault();
				} );

			// ブロードバンド版の設定UIを初期化
			$( "#contents-notice .toBroadband" ).click( function(){
				bsjs.setCookie(STR_BANDWIDTH_COOKIEKEY,"bb");
				location.reload();
				return false;
			} );

			//トラッキング
			sendAnalyticsEvent("Narrowband");
		}
	}

	if( $.browser.safari ){
		window.addEventListener( "load", onLoadFunc, false );
	}
	else {
		$(document).ready(onLoadFunc);
	}



	/**
	 * JS読み込み時処理
	 */

	/* JSが有効なUA向けのCSS調整 (DOMReadyが怪しいUAがあるので、$(document).ready外に記述) */
	var myStyleSheets = document.styleSheets[0];


	/* トラッキング設定 */
	bsjs.tracking.add("#siteTopContents ul.selectionTopics li:nth-child(1)","topic01");
	//comドメイン bsjs.tracking.add("#siteTopContents ul.selectionTopics li:nth-child(2)","topic02");
	bsjs.tracking.add("#siteTopContents ul.selectionTopics li:nth-child(3)","topic03");
	//comドメイン bsjs.tracking.add("#siteTopContents ul.selectionTopics li:nth-child(4)","topic04");
	bsjs.tracking.add("#contents-aside li:nth-child(3) a","bnr-03");
	bsjs.tracking.add("#contents-notice div.requirement p a,#contents-notice div.requirement a.getFlashPlayer","to_FlashDL");
	bsjs.tracking.add("#contents-notice div.requirement a.toBroadband","Switch_Broadband");

})();
