
$(function() {

//*********************************************************/
//変数設定
//*********************************************************/
//URL設定
//var IL_JP = "/dev/toshima/il/jp/top_2.php";
//var IL_EN = "/dev/toshima/il/en/top_2.php";
var IL_JP = "/jp/top_2.php";
var IL_EN = "/en/top_2.php";
//var IFFT_JP = "/jp/top_2.php";
//var IFFT_EN = "/en/top_2.php";
var IFFT_JP = "http://www.ifft-interiorlifestyleliving.com/jp/top_2.php";
var IFFT_EN = "http://www.ifft-interiorlifestyleliving.com/en/top_2.php";


//ウィンドウサイズ
var winW = 0;
var winH = 0;
//画像サイズ取得
var imgW = $('.visual').width();
var imgH = $('.visual').height();

//フラグ
var compFlg = 0;//外部サイトの読み込み完了フラグ
var clickFlg = 0;//クリックフラグ
var openFlg = 0;//オーップンフラグ

//変数
var interval = 500;//オープンして停止する秒数
var limitInterval = 5000;//我慢の限界を超えたらリダイレクト
var mainZindex = 10000;

var ifftWrapW = $('#index #ifft .wrapper').width();
var ifftWrapH = $('#index #ifft .wrapper').height();


//*********************************************************/
//画面サイズにフィット
//*********************************************************/
//読み込み時
fitImageSize();

//リサイズ時
$(window).resize(function(){
	fitImageSize();
});


//*********************************************************/
//マウスオーバーアクション
//*********************************************************/
$("#il").hover(
  function () {
    $("#il .layer").animate({opacity: 0.8},400);
  },
  function () {
    $("#il .layer").animate({opacity: 1},400);
  }
);
//ILボタン
$("#il .ja").hover(
  function () {
    $(this).css("background-color","#a6225e");
  },
  function () {
    $(this).css("background-color","");
  }
);
$("#il .en").hover(
  function () {
    $(this).css("background-color","#a6225e");
  },
  function () {
    $(this).css("background-color","");
  }
);


$("#ifft").hover(
  function () {
    $("#ifft .layer").animate({opacity: 0.8},400);
  },
  function () {
    $("#ifft .layer").animate({opacity: 1},400);
  }
);
//IFFTボタン
$("#ifft .ja").hover(
  function () {
    $(this).css("background-color","#005d88");
  },
  function () {
    $(this).css("background-color","");
  }
);
$("#ifft .en").hover(
  function () {
    $(this).css("background-color","#005d88");
  },
  function () {
    $(this).css("background-color","");
  }
);



//*********************************************************/
//クリックアクション
//*********************************************************/
//IL　日本語　クリック
$("#il .ja").click(function(){
	//クリック判定
	clickFlg = 1;
	//限界突破リダイレクト
	redirectURL = IL_JP;
	
	//ロゴ固定のためにクリック時のレイヤーのwidthを取得
	getwidth = $("#index #il").width()/2;
	
	$("#il .ja").unbind("click");
	$("#il .en").unbind("click");
	
	//スタイル設定
	$("#il .layer").css({
		'display':"none"
		//'zIndex':10,
		//opacity: 0
	});
	$("#il").css('zIndex',mainZindex+10);
	$("#ifft").css('zIndex',mainZindex+0);
	
	//オープンアニメ開始
	il_open(IL_JP,getwidth);//オープンアニメ開始
	
});

//IL　英語　クリック
$("#il .en").click(function(){
	//クリック判定
	clickFlg = 1;
	//限界突破リダイレクト
	redirectURL = IL_EN;
	
	//ロゴ固定のためにクリック時のレイヤーのwidthを取得
	getwidth = $("#index #il").width()/2;
	
	$("#il .ja").unbind("click");
	$("#il .en").unbind("click");
	
	//スタイル設定
	$("#il .layer").css({
		'display':"none"
		//'zIndex':10,
		//opacity: 0
	});
	$("#il").css('zIndex',mainZindex+10);
	$("#ifft").css('zIndex',mainZindex+0);
	
	//オープンアニメ開始
	il_open(IL_EN,getwidth);

});

//IFFT　日本語　クリック

$("#ifft .ja").click(function(){
	//document.location = IFFT_JP;
	//クリック判定
	clickFlg = 1;
	//限界突破リダイレクト
	redirectURL = IFFT_JP;
	
	//ロゴ固定のためにクリック時のレイヤーのwidthを取得
	getwidth = $("#index #ifft").width()/2;
		
	$("#ifft .ja").unbind("click");
	$("#ifft .en").unbind("click");
	
	//スタイル設定
	$("#ifft .layer").css({
		'display':"none"
	});
	$("#ifft").css('zIndex',mainZindex+10);
	$("#il").css('zIndex',9);
	
	//オープンアニメ開始
	ifft_open(IFFT_JP,getwidth);

});

//IFFT　英語　クリック
$("#ifft .en").click(function(){
	//document.location = IFFT_EN;

	//クリック判定
	clickFlg = 1;
	//限界突破リダイレクト
	redirectURL = IFFT_EN;
	
	//ロゴ固定のためにクリック時のレイヤーのwidthを取得
	getwidth = $("#index #ifft").width()/2;
	
	//スタイル設定
	$("#ifft .layer").css({
		'display':"none"
	});
	$("#ifft").css('zIndex',mainZindex+10);
	$("#il").css('zIndex',9);

	
	//オープンアニメ開始
	ifft_open(IFFT_EN,getwidth);

});



//*********************************************************/
//関数
//*********************************************************/
//タイマー関数
var close_timer = setInterval(close,500);
function close(){
	if(compFlg && openFlg){
		animateCloseL("#il");//クローズ
		animateCloseR("#ifft");
		clearInterval(close_timer);
	}
};

//限界突破関数
var limit_timer = setInterval(limitOver,limitInterval);
function limitOver(){
	if(!compFlg && openFlg){
		document.location = redirectURL;
	}
};

//同一ドメインページ読み込み
function getSamedomein(pass){
	//compFlg = 1;
	$('#container').load(
		pass,
		null,
		function(){// 通信完了後に実行されるコールバック関数
			compFlg = 1;
		}
	);	
}


//クロスドメイン
function getXdomein(uri){
jQuery.ajax({
	url: uri,
	type: 'GET', 
	success: function(data){
		//creating json object
		$('#container').html(data.responseText);
		compFlg = 1;		
		//$contents = $('div#container');
		//$('#container').html($contents.html());  
	}
});
}

//クローズレフト
function animateCloseL(css){
	setTimeout(function(){
		$("html").css('overflow-y','auto');
		
		$(css).animate({
			width: "0%",
			opacity: 1,
			zIndex: mainZindex+0
		},
		{
			duration: 800,
			easing: "easeOutCubic",
			complete: function(){
				$(this).css("display","none");
				
			}
		})
	}, interval );
}


//クローズライト
function animateCloseR(css){
	setTimeout(function(){
		$("html").css('overflow-y','auto');
		
		$(css).animate({
			width: 0,
			left: winW+10,
			opacity: 1,
			zIndex: mainZindex+0
		},
		{
			duration: 800,
			easing: "easeOutCubic",
			complete: function(){
				$(this).css("display","none");
				//$("html").css('overflow','auto');
			}
		})
	}, interval );
}

//ILオープン
function il_open(url,getwidth){
	$("#index #il .wrapper").css("left",getwidth);
	$("#il").animate(
		{
			width: "100%"
		},
		{//option
			duration: 800,
			easing: "easeOutCubic",
			complete: function(){
				openFlg = 1;
				//ページ読み込み
				//getXdomein(url);/*クロスドメイン*/
				getSamedomein(url);/*同一ドメイン*/
				
				$("#container").css('backgroundColor','#fff');
				$("#ifft").css('display','none');
			}
    	}
	);
}

//IFFTオープン
function ifft_open(url,getwidth){
	$("#ifft").animate(
		{
			width: winW+10,
			left:0
		},
		{//option
			duration: 800,
			easing: "easeOutCubic",
			complete: function(){
				openFlg = 1;
				//ページ読み込み
				getXdomein(url);/*クロスドメイン*/
				//getSamedomein(url);/*同一ドメイン*/
				
				$(this).css({
					//画面端対策
					'left':0
					//'width':winW+80
				});
				$("#container").css('backgroundColor','#fff');
				$("#il").css('display','none');
			}
    	}
	);
}


//ウィンドウサイズにフィット
function fitImageSize() {
	winW = $(window).width();
	winH = $(window).height();
	
	var scaleW = winW / imgW;
	var scaleH = winH / imgH;
	var fixScale = Math.max(scaleW, scaleH) + 0.15;
	var setW = imgW * fixScale;
	var setH = imgH * fixScale;
	var moveX = Math.floor((winW - setW) / 2);
	var moveY = Math.floor((winH - setH) / 2);
	
	
	if(clickFlg){
		$('#il').css({
			'width':winW, 
			'height':winH
		});
		
		$('#ifft').css({
			//'right':0,
			'left': 0,
			'width':winW+10,
			'height':winH
		});
	}else{
		$('#il').css({
			'width':winW/2, 
			'height':winH
		});
		
		$('#ifft').css({
			//'right':0,
			'left': winW/2,
			'width':winW/2+10,
			'height':winH
		});
	}
	
	
	$('.visual').css({
		'width': setW,
		'height': setH,
		//'left' : moveX,
		//'top' : moveY
	});
	$('#ifft .visual').css({
		'right':'0px'
	});
	
	$(".layer").css({
		'height':winH
	});
	
	//IFFTのロゴの位置
	$('#index #ifft .wrapper').css({
		'right':winW/4 - ifftWrapW/2,
		'top':winH/2 - ifftWrapH/2
	});

}

});



$(function() {
    var zIndexNumber = 100;
    $('div').each(function() {
        $(this).css('zIndex', zIndexNumber);
        zIndexNumber -= 10;
    });
});

