/*
  jq_topicbranch.js
  for Bridgestone GlobalWebSite
*/

(function(){
	/**
	 * ネームスペース対応
	 */
	var branchWindow = window.bsjs.branchWindow = {};



	/**
	 * 定数定義
	 */

	//
	var ID_TB_BG_LAYER = "branchWindowBG";

	//
	var ID_TB_CONTAINER = "branchWindow-Container";

	//
	var CLASS_TB_FRAME = "TB-frame";

	//
	var CLASS_TB_CLOSE = "TB-close";

	//
	var CLASS_MIN_LIMITED = "limited";

	//
	var MARGIN_TOP_AND_BOTTOM = 80; /* 40 * 2 */

	//
	var HEIGHT_MIN_LIMIT = 150;

	//
	var HTML_TB_BG_LAYER = '<div id="' + ID_TB_BG_LAYER + '"></div>';

	//
	var HTML_TB_FLOAT_BOX =
		'<div id="' + ID_TB_CONTAINER + '">' +
		'  <div class="' + CLASS_TB_FRAME + '">' +
		'    <iframe src="about:blank" id="bs_branchWindow" frameborder="0"></iframe>' +
		'  </div>' +
		'  <div class="' + CLASS_TB_CLOSE + '">close</div>' +
		'</div>';


	branchWindow.iframe = null;

	branchWindow._isOpened = false;
	branchWindow.open = function( myURL ){
		//二重起動防止
		if( branchWindow._isOpened ) return;
		branchWindow._isOpened = true;

		/* safari2.0以下の場合、DOM生成 */
		if( $.browser.safari && $.browser.version -0 < 500 ){
			$( document.body ).append( HTML_TB_BG_LAYER );
			$( "div#contents" ).append( HTML_TB_FLOAT_BOX );

			$("#" + ID_TB_CONTAINER + ", " + "#" + ID_TB_BG_LAYER).css("visibility","hidden");
			$("#" + ID_TB_CONTAINER + ", " + "#" + ID_TB_BG_LAYER).css("display","none");

			branchWindow.iframe = $("#" + ID_TB_CONTAINER + " iframe");
		}

		/* イベント設定A */
		branchWindow.iframe.bind("load", branchWindow._show );

		$("#" + ID_TB_CONTAINER + ", " + "#" + ID_TB_BG_LAYER).css("display","block");
		branchWindow.iframe.attr("src",myURL);
	}
	branchWindow._show = function(){
		/* イベント設定A解除 */
		branchWindow.iframe.unbind("load", branchWindow._show );

		/* イベント設定B */
		if( $.browser.safari && $.browser.version -0 < 400 ){
			//ToDo:safari1系用 イベント設定
		}
		else {
			branchWindow.Window.bind("resize", branchWindow._ajust );
		}
		if( $.browser.msie && $.browser.version -0 < 7 ){
			branchWindow.Window.bind("scroll", branchWindow._ajust );
		}
		$("#" + ID_TB_CONTAINER + " ." + CLASS_TB_CLOSE + ", #" + ID_TB_BG_LAYER).bind("click", branchWindow._close );

		branchWindow._ajust();
		$("#" + ID_TB_CONTAINER + ", " + "#" + ID_TB_BG_LAYER).css("visibility","visible");
		$("#" + ID_TB_CONTAINER + " " + "." + CLASS_TB_FRAME).css("display","block").css("visibility","visible");

		//IE7.0以上の場合、マウスホイール操作禁止
		if( $.browser.msie && $.browser.version -0 >= 7 ){
			document.body.attachEvent("onmousewheel",branchWindow._cancelWheel);
			branchWindow.iframe.get(0).contentWindow.document.body.attachEvent("onmousewheel",branchWindow._cancelWheel);
		}
	}
	branchWindow._close = function(){
		/* イベント設定B解除 */
		if( $.browser.safari && $.browser.version -0 < 400 ){
			//ToDo:safari1系用 イベント設定
		}
		else {
			branchWindow.Window.unbind("resize", branchWindow._ajust );
		}
		if( $.browser.msie && $.browser.version -0 < 7 ){
			branchWindow.Window.unbind("scroll", branchWindow._ajust );
		}
		$("#" + ID_TB_CONTAINER + " ." + CLASS_TB_CLOSE + ", #" + ID_TB_BG_LAYER).unbind("click", branchWindow._close );

		/* safari2.0対策のため、DOMを毎回破棄 */
		if( $.browser.safari && $.browser.version -0 < 500 ){
			$("#" + ID_TB_CONTAINER + ", " + "#" + ID_TB_BG_LAYER).remove();
			branchWindow.iframe = null;
		}

		//IE7.0以上の場合、マウスホイール操作禁止解除
		if( $.browser.msie && $.browser.version -0 >= 7 ){
			document.body.detachEvent("onmousewheel",branchWindow._cancelWheel);
			branchWindow.iframe.get(0).contentWindow.document.body.detachEvent("onmousewheel",branchWindow._cancelWheel);
		}

		if( !$.browser.safari ) branchWindow.iframe.height(0);
		$("#" + ID_TB_CONTAINER + ", " + "#" + ID_TB_BG_LAYER).css("visibility","hidden");
		$("#" + ID_TB_CONTAINER + ", " + "#" + ID_TB_BG_LAYER).css("display","none");

		branchWindow._isOpened = false;
	}
	branchWindow._ajust = function(){
		var targetContainer = $( "#" + ID_TB_CONTAINER );
		var targetFrame = ( branchWindow.iframe !== null )? branchWindow.iframe.get(0): false;
		if( !targetFrame ) return;

		var screenHeight = ( window.innerHeight || document.documentElement.clientHeight );

		if( $.browser.safari ) branchWindow.iframe.height("auto");

		var inFrameHeight =
			( !targetFrame.contentWindow )? Infinity:
				targetFrame.contentWindow.document.documentElement.scrollHeight + 1; /* +1：firefox対策 */

		var calculatedHeight = Math.min( screenHeight - MARGIN_TOP_AND_BOTTOM, inFrameHeight );

		var positonY = 0;
		if(
			calculatedHeight > HEIGHT_MIN_LIMIT &&
			!( $("#" + ID_TB_CONTAINER).hasClass("macFFPatch") && calculatedHeight !== inFrameHeight )
		){
			targetContainer.removeClass(CLASS_MIN_LIMITED);
			$( targetFrame ).height( calculatedHeight );

			positonY += ( screenHeight - targetContainer.height() ) /2;
			if(
				( $.browser.msie && $.browser.version -0 < 7 ) ||
				$("#" + ID_TB_CONTAINER).hasClass("macFFPatch")
			){
				positonY += ( document.body.scrollTop || document.documentElement.scrollTop );
			}
		}
		else {
			targetContainer.addClass(CLASS_MIN_LIMITED);
			$( targetFrame ).height( inFrameHeight );

			if( $.browser.msie && $.browser.version -0 < 7 ){
				positonY += MARGIN_TOP_AND_BOTTOM;
			}
			else {
				positonY += ( document.body.scrollTop || document.documentElement.scrollTop ) + MARGIN_TOP_AND_BOTTOM;
			}
		}
		targetContainer.css("top", positonY + "px" );

		if( $.browser.msie && $(targetFrame).height() < inFrameHeight ){
			try {
				var styleSheet = targetFrame.contentWindow.document.styleSheets[0];
				if( styleSheet ) styleSheet.addRule("html", "overflow-x:hidden");
			} catch( myEx ){}
		}
	}
	branchWindow._cancelWheel = function( myEv ){
		if( window.event ){
			window.event.cancelBubble = true;
			window.event.returnValue = false;
		}
		if( bs_branchWindow.event ){
			bs_branchWindow.event.cancelBubble = true;
			bs_branchWindow.event.returnValue = false;
		}
		return false
	}




	/**
	 * 初期表示処理
	 */
	var onLoadFunc = function(){
		if( $.browser.safari && $.browser.version -0 < 400 ) return;

		/* 同じ要素を使いまわすと、safari2.0で内容が更新されない不具合が発生 */
		if( !($.browser.safari && $.browser.version -0 < 500) ){
			$( document.body ).append( HTML_TB_BG_LAYER );
			$( "div#contents" ).append( HTML_TB_FLOAT_BOX );

			$("#" + ID_TB_CONTAINER + ", " + "#" + ID_TB_BG_LAYER).css("visibility","hidden");
			$("#" + ID_TB_CONTAINER + ", " + "#" + ID_TB_BG_LAYER).css("display","none");
			
			branchWindow.iframe = $("#" + ID_TB_CONTAINER + " iframe");

			if( $.browser.mozilla && navigator.appVersion.indexOf("Mac") !== -1 ){
				$("#" + ID_TB_CONTAINER).addClass("macFFPatch");
			}
		}
		branchWindow.Window = $(window);

		$(".topicBranches a, a.topicBranche").click( function(myEv){
			myEv.preventDefault();
			myEv.stopPropagation();
			bsjs.branchWindow.open(this.href);
			return false;
		} )
	}

	if( $.browser.safari ){
		window.addEventListener( "load", onLoadFunc, false );
	}
	else {
		$(document).ready(onLoadFunc);
	}


})();
