/*
* shared.js with jQuery
*
*/
/*------------------------------------------------------------------------------
	layout
------------------------------------------------------------------------------*/

var isScroll = false;


var currentScene = 'top';
var prevScene = '';

var sceneList			= ['top','mission','team','company','lab','system','service','staff','career','works','message'];

//シーン遷移
var stateList			= new Array();
stateList['top']		= 1;
stateList['mission']	= 0;
stateList['team']		= 0;
stateList['company']	= 0;
stateList['lab']		= 0;
stateList['system']		= 0;
stateList['service']	= 0;
stateList['staff']		= 0;
stateList['career']		= 0;
stateList['works']		= 0;
stateList['message']	= 0;

var $window = $(window);


/*------------------------------------------------------------------------------
	for SmartScroll
------------------------------------------------------------------------------*/
function smartScroll (){	
	var easing = 0.2;
	var interval = 30;
	var allLinks = document.links;
	for (var i=0;i<allLinks.length;i++){
		var lnk = allLinks[i];
		if ((lnk.href && lnk.href.indexOf('#') != -1) && ((lnk.pathname == location.pathname) || ('/'+lnk.pathname == location.pathname))){
			var myHash = lnk.hash.replace(/#/g,"");
			if (!(myHash.length == 0)){
				lnk.onclick = function (){
					var hash = this.hash;
					var targetId = hash.replace(/#/g,"");
					if (!document.getElementById(targetId)) return;
					var element = document.getElementById(targetId);
					var targetY = 0;
					while(element){
					   targetY += element.offsetTop;
					   element = element.offsetParent;
					}					
					var scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
					var windowHeight = window.innerHeight || document.documentElement.clientHeight;
					var bodyHeight = Math.max(document.body.scrollHeight, document.documentElement.scrollHeight);
					var footHeight = bodyHeight - targetY;
					var adjust = windowHeight - footHeight;
					if (windowHeight > footHeight){				
						var toY = targetY - scrollTop - adjust;
					} else	{
						var toY = targetY - scrollTop;
					}
										
					function windowScroll (){
						var moveY = Math.floor(toY*easing);	
						window.scrollBy(0,moveY);
						toY -= moveY; 
						myTimer = setTimeout(windowScroll,interval);
						if (moveY == 0){
							clearTimeout(myTimer);
							isScroll = false;
							//changeState(targetId);
						}
					}
					isScroll = true;
					windowScroll();
					return false;					
				};
			}
		}
	}	
}

$(function(){
	
	setlayout();
	smartScroll();
	
	/*
	$('#nengaimg').click(function(){
		$('html,body').animate({ scrollTop: 0 }, {duration:0});
		$('#nengaoverlay').fadeOut(700, function() {
			$('#topbody').show();
			$('#topbody').addClass('topanime');
  		});
	});
	*/
	
	$('#systempage01 p.btn01').click(function(){
		$('#systempage01 p').fadeOut(100, function() {
			$('#systempage02 p').fadeIn(700);
  		});
	});
	
	$('#systempage02 p.btn01').click(function(){
		$('#systempage02 p').fadeOut(100, function() {
			$('#systempage01 p').fadeIn(700);
  		});
	});
	
	$('#messagepage01 p.btn01').click(function(){
		$('#messagepage01 p').fadeOut(100, function() {
			$('#messagepage02 p').fadeIn(700);
  		});
	});
	
	$('#messagepage02 p.btn01').click(function(){
		$('#messagepage02 p').fadeOut(100, function() {
			$('#messagepage01 p').fadeIn(700);
  		});
	});
	
	var $top = $('#top');
	var $mission = $('#mission');
	var $team = $('#team');
	var $company = $('#company');
	var $lab = $('#lab');
	var $system = $('#system');
	var $service = $('#service');
	var $staff = $('#staff');
	var $career = $('#career');
	var $works = $('#works');
	var $message = $('#message');
	
	var resizeTimer 			= null;
	
	var a = 0;
	var b = 0;
	
	//getElementScrollTop('missionbgcontainer')
	
	$window.bind('scroll', function(){
		var winHeight				= $(window).height()<700 ? 700 : $(window).height();
		var scrollPos				= $window.scrollTop();
	
		a = winHeight -  Math.round(scrollPos);
		
		$('#topbgcontainer').css({marginBottom:  a + 'px'});
		
	});
	
	
	$(window).bind('resize',function(){
		if(resizeTimer){
			clearTimeout(resizeTimer);
		};
		resizeTimer = setTimeout(setlayout,100);
		
	});
	
});



function setlayout(){

	var $contents				= $('div.bgcontainer,div.topcontainer,div.pagecontainer,#top,#mission,#team,#company,#lab,#system,#service,#staff,#career,#works,#message');
	var $contentsBg				= $('#topbg,#missionbg,#teambg,#companybg,#labbg,#systembg,#servicebg,#staffbg,#careerbg,#worksbg,#messagebg');
	var $contentsBgCenterMiddle	= $('#topbg,#teambg,#messagebg');
	var $contentsBgCenter		= $('#companybg,#systembg');

	var defaultWidth			= 1600;
	var defaultHeight			= 980;
	
	var winWidth				= $(window).width()<960 ? 960 : $(window).width()
	var winHeight				= $(window).height()<700 ? 700 : $(window).height();
	
	var scaleWidth				= winWidth / defaultWidth;
	var scaleHeight				= winHeight / defaultHeight;
	
	var scale					= Math.max(scaleWidth,scaleHeight);
	
	var bgImgWidth				= defaultWidth * scale;
	var bgImgHeight				= defaultHeight * scale;
	
	var bgPosX 					= Math.floor(bgImgWidth / 2);
	var bgPosY					= Math.floor(bgImgHeight / 2);
	
	$contentsBg.css({width:bgImgWidth +'px', height:bgImgHeight +'px'});
	$contents.css({width:winWidth +'px', height:winHeight +'px'});
	
	$contentsBgCenterMiddle.css({marginLeft:'-' + bgPosX + 'px',marginTop:'-' + bgPosY + 'px'});
	$contentsBgCenter.css({marginLeft:'-' + bgPosX + 'px'});
	
	$('div.bgcontainer').css({marginBottom:winHeight + 'px'})
	$('#messagebgcontainer').css({marginBottom:'0px'})
	
};

function getScrollTop(){

    if(typeof pageYOffset!= 'undefined'){
    
        return pageYOffset;
    }
    else{
        var B= document.body;
        var D= document.documentElement;
        D= (D.clientHeight)? D: B;
        return D.scrollTop;
    }
}

function getElementHeight(_elm){
	if(outerHeight == true){
		var height = _elm.outerHeight(true);
	}else{
		var height = _elm.height();
	}
	
	return height;
};
