/**=========================================

 filename:  tab.js
 create:    2009/
 copyright: 

=========================================**/
(function(){
//---------------------------------------------------------------------------------------------------------------------tab名前空間
var tab = function (){}
var MEMBERS = tab.prototype;
var btns = [];//id格納。左から
var contents = [];//id格納。btnsと並列に
var select = 0;
var wicths = [];

//---------------------------------------------------------------------------------------------------------------------introduction用
/**
 * これを呼び出すといく
 * @param {Object} btnIds
 * @param {Object} contentIds
 */
MEMBERS.initTab = function()// btnIds, contentIds, selected )
{
	//filter
	//sectionが存在しない場合、jsは何もしない
	if( $( ".section" ).size() == 0 )
	{
		return;
	}
	
	//ボタンの存在不在をとる
	//コンテンツのdivも消えてないとバグる
	var index = 0;
	if( $( "#tabBtn1" ).size() > 0 )
	{
		$( '#tabBtn1' ).attr("selectId", index);
		btns.push( $( '#tabBtn1' ) );
		contents.push( $( '#tabColumn1' ) );
		
		index ++;
	}
	if( $( "#tabBtn2" ).size() > 0 )
	{
		$( '#tabBtn2' ).attr("selectId", index);
		btns.push( $( '#tabBtn2' ) );
		contents.push( $( '#tabColumn2' ) );
		
		index ++;
	}
	if( $( "#tabBtn3" ).size() > 0 )
	{
		$( '#tabBtn3' ).attr("selectId", index);
		btns.push( $( '#tabBtn3' ) );
		contents.push( $( '#tabColumn3' ) );
		
		index ++;
	}
	if( $( "#tabBtn4" ).size() > 0 )
	{
		$( '#tabBtn4' ).attr("selectId", index);
		btns.push( $( '#tabBtn4' ) );
		contents.push( $( '#tabColumn4' ) );
		
		index ++;
	}
	
	//20100226
	if( btns.length == 0 && contents.length == 0 )
	{
		return;
	}
	/* 20100607
	//
	if( btns.length == 1 )
	{
		$( ".tabNav" ).css( "display", "none" );
	}
	*/
	
	//default 選択
	select = 0;
	
	//
	MEMBERS.createTab();
	
	
	//$( window.parent.document.getElementById( "sb-title-inner" )).css( "opacity" , "0" );
	//$( window.parent.document.getElementById( "sb-title-inner" )).animate({ opacity:1}, 10 );
}


/**
 * タブを作成
 * 
 */
MEMBERS.createTab = function()
{
	MEMBERS.setBtnArea( select );
	$( "#tabColumn1" ).css('visibility', "visible");
	MEMBERS.setContentAreaInntro( select );
}


MEMBERS.setBtnArea = function(select)
{
	//if( btns.length == 0 ) return;
	
	//open クラス指定でアクティブ色
	var btn = "";
	var btnLabels = [];
	var i,m=btns.length;
	for( i=0; i<m; i++ )
	{
		//<a>  test</ br>test   </a>
		var text = $( btns[ i ].html() ).html();
		btnLabels.push( text );
	}
	
	btns[ 0 ].css( "position", "relative" );
	btns[ 0 ].css( "z-index", "100" );
	
	//一度全てを<a>の形にする
	for( i=0; i<m;i++ )
	{
		btn = btns[ i ];
		btn.removeClass( "open" );
		btn.removeClass( "firstChild" );
		btnChild = btn.children();
		btnChild.replaceWith( "<a href='javascript:;'>" + btnLabels[ i ] + "</a>" );
		
		btn.click( function()
		{
			$( this ).css( "position", "relative" );
			$( this ).css( "z-index", "100" );
			select = $( this ).attr( 'selectId' );
			MEMBERS.setContentAreaInntro( select );
			MEMBERS.setBtnArea( select );
		});
	}
	
	//選択状態のものの設定
	btns[ select ].addClass( "open" );
	btns[ select ].addClass( "firstChild" );
	btns[ select ].unbind( "click" );
	btnChild = btns[ select ].children();
	btnChild.replaceWith( "<span>" + btnLabels[ select ] + "</span>" );
	
	var max = btns[ 0 ].children().height()
	for (i = 1; i < m; i++)
	{
		if( max < btns[ i ].children().height() )
		{
			max = btns[ i ].children().height();
		}
	}
	
	if( max<38 )
	{
		max = 38;
	}
	
	for (i = 0; i < m; i++)
	{
		if( i==select )
		{
			btns[ i ].children().css( "height", (max+1)+"px" );
		}else
		{
			btns[ i ].children().css( "height", (max)+"px" );
		}
	}
	
}

MEMBERS.setContentAreaInntro = function( id )
{
	//if( contents.length == 0 ) return;
	
	var i,m=contents.length;
	for( i=0; i<m;i++ )
	{
		contents[i].css('display', 'block');
	}
	
	for (i = 0; i < m; i++) {
		if (i != id) {
			contents[i].css('display', 'none');
		}
	}
	
	var params = {'opacity':0.01 };
	contents[id].css('visibility', "hidden").animate(params, 1, function()
	{
		contents[id].css('visibility', "visible").animate( {'opacity':1  } );
	});
}



//---------------------------------------------------------------------------------------------------------------------access用

var animating = false;
var activeTab = "illust"; // or map
MEMBERS.initTabAccess = function(btnIds, contentIds, selected)
{
	if( $("#tabBtn1").size() == 0 ) return;
	if( $("#tabBtn2").size() == 0 ) return;
	
	$("#btnAccessTab001").css( "cursor", "pointer" );
	$("#btnAccessTab002").css( "cursor", "pointer" );
	
	var images1 = getImgNameOnOffActive( "btnAccessTab001" );
	var images2 = getImgNameOnOffActive( "btnAccessTab002" );
	
	if( activeTab == "illust" )
	{
		//アクティブ画像の設定
		$("#btnAccessTab001").attr( "src", images1[2] );
		window.utils.addRollAction( $("#btnAccessTab002"), images2[0], images2[1] );
		
		$("#tabBtn2").click( function(){
			tabGoogleMapClick(images1, images2)
		});
		
	}else
	if( activeTab == "map" )
	{
		//アクティブ画像の設定
		$("#btnAccessTab002").attr( "src", images2[2] );
		window.utils.addRollAction($("#btnAccessTab001"), images1[0], images1[1]);
		
		$("#tabBtn1").click( function(){
			tabIllustClick(images1, images2)
		});
	}
	
	
	//ボタン部分、コンテンツ部分jquery化、配列格納
	var i, m=btnIds.length;
	for( i=0; i<m; i++ )
	{
		btns.push( $( '#' + btnIds[ i ] ) );
		contents.push( $( '#' + contentIds[ i ] ) );
	}
	///default 選択
	select = selected - 1;
	
}
function tabIllustClick( _images1, _images2 )
{
	if( animating || activeTab == "illust" ) return;
	animating = true;
	activeTab = "illust";
	
	MEMBERS.setContentAreaAccess( 0 );
	
	$("#tabBtn2").click( function(){
		tabGoogleMapClick( _images1, _images2)
	});
	
	window.utils.addRollAction( $("#btnAccessTab002"), _images2[0], _images2[1] );
	$("#btnAccessTab002").attr( "src", _images2[1] );
	window.utils.removeRollActionById( "btnAccessTab001" );
	$("#btnAccessTab001").attr( "src", _images1[2] );
	
}
function tabGoogleMapClick( _images1, _images2 )
{
	if (animating || activeTab == "map") return;
	animating = true;
	activeTab = "map";
	
	//最初に一度だけ
	if ($("#googleMap").css("position") == "absolute") {
		$("#googleMap").css({
			"position": "relative",
			"top": "0px",
			"left": "0px"
		});
	}
	MEMBERS.setContentAreaAccess(1);
	
	$("#tabBtn1").click( function(){
		tabIllustClick( _images1, _images2)
	});
	window.utils.addRollAction($("#btnAccessTab001"), _images1[0], _images1[1]);
	$("#btnAccessTab001").attr( "src", _images1[1] );
	window.utils.removeRollActionById( "btnAccessTab002" );
	$("#btnAccessTab002").attr("src", _images2[2]);
	
}




/**
 * 
 * @param {Object} id
 */
MEMBERS.setContentAreaAccess = function( id )
{
	var params = {opacity:"toggle"};
	var i,m=contents.length;
	for( i=0; i<m;i++ )
	{
		contents[ i ].css( 'display', 'none' );
	}
	contents[ id ].animate(params, { complete:function()
	{
		animating = false;
	} });
}


function getImgNameOnOffActive( id )
{
	var img = window.utils.getImg( id );
	var imgNames = window.utils.getImgSrc(img);
	var activeImgName = "";
	
	var str = imgNames[1];
	var imgName = str.substring( 0, str.lastIndexOf( "." ) ) + "_active";
	var imgType = str.split( "." )[ str.split( "." ).length-1 ];
	activeImgName = imgName + "." + imgType;
	
	imgNames.push( activeImgName );
	return imgNames;
}




//---------------------------------------------------------------------------------------------------------------------共通





window.tab = new tab();
//---------------------------------------------------------------------------------------------------------------------tab名前空間
})();
