$(function()
{
	//val
	var bodySideMargin;
	var content = "home";
	var homeY;
	
	//init
	$('#wrapper').hide();
	initBG();
	
	
	//hashChange
	$(window).hashchange(function(){
		 if (location.hash!="") {
            path = location.hash.split('#!', 2)[1];
			switch(path){
				case "home":
					setHome();
					break;
				case "service":
					setService();
					break;
				case "service/solution":
					setSolution();
					break;
				case "service/bridge-net":
					setNet();
					break;
				case "service/solution/index":
					setSolutionIndex();
					break;
				case "service/bridge-net/index":
					setNetIndex();
					break;
				case "company":
					setCompany();
					break;
				case "company/outline":
					setCompanyOutline();
					break;
				case "company/index":
					setCompanyIndex();
					break;
				case "privacy_policy":
					setPrivacy();
					break;
				case "inquiry":
					setInquiry();
					break;
				case "recruit":
					setRecruit();
					break;
				case "topics":
					setTopics();
					break;
			}
        }else{
			bodySideMargin = getBodySideMargin();
			$('html').animate({backgroundPosition: bodySideMargin+' 600'}, 0);
			setHome();
		}
	});
	$(window).hashchange();
	
	
	//Timer
	var timerID = null;
	
	
	
	
	
	//scene:#home
	function setHome(){
		
		content="home";
		
		
		$('ul.image').css("left", "900");
		
		if (!$.browser.msie){
			$('title').text("株式会社ブリッジコーポレーション | Webサイト制作からWebシステムの開発まで総合的にサポート");
		}
		
		$('#wrapper').fadeOut(500);
		
		
		$('#body').load("home.html #home",function(){
			homeY = $('html').height()-200;
			bodySideMargin = getBodySideMargin();
			$('#wrapper').hide();
			$('html').animate({backgroundPosition: bodySideMargin+' '+homeY}, 1000,compBG);
		});
	
		function compBG(){
			$('#gnavi').load("home.html #header #gnavi ul",setRollOver);
			$('#body').load("home.html #home",function(){
				
				$('#image #philosophy').hide();
				$('#servicelist').hide();
				$('#wrapper').fadeIn(500);
				$('#image #philosophy').delay(1000).fadeIn(1000);
				$('#image #philosophy').delay(1000).fadeOut(1000,'',setHomeImage);
				
				if (!$.browser.msie){
					$('#servicelist').delay(4000).fadeIn(1000);
				}
			});
		}
		function setHomeImage(){
		
			
			
			var currentService;
			currentService = 1;
			setServiceX();
			if ($.browser.msie){
				$('#servicelist').show();
				
			}
			
			
			setTimerEvent();
			
			function setTimerEvent() {
			   if (timerID){
			   	clearTimeout(timerID);
			   }
			   timerID = setTimeout(changeService, 1000 * 6); //1000ms * 10s
			}
			
			function changeService(){
				if(currentService==9){
					currentService=1;
				}else{
					currentService++;
				}
				setServiceX();
				clearTimeout(timerID);
				timerID = setTimeout(changeService, 1000 * 6); //1000ms * 10s
			}
			
			$('#image').mousemove(function(e){
				setTimerEvent();
			});
			
			
			$('.btn-prev').click(function(){
				currentService--;
				setServiceX();
				setTimerEvent();
			});
			
			$('.btn-next').click(function(){
				currentService++;
				setServiceX();
				setTimerEvent();
			});
			
			
			function setServiceX(){
				if(currentService==1){
					$('.btn-prev').hide();
				}else{
					$('.btn-prev').show();
				}
				if(currentService==9){
					$('.btn-next').hide();
				}else{
					$('.btn-next').show();
				}
				var targetX = currentService*-900+900;
				$('ul.image').animate({"left": targetX+"px"}, "slow");
				for(i=1;i<=9;i++){
					$('.bnr'+i).css("background-image","url('images/index/vi_btn_0"+i+".gif')");
				}
				$('.bnr'+currentService).css("background-image","url('images/index/vi_btn_0"+currentService+"_c.gif')");
			}
			
				
			$('.bnr1').click(function(){
				currentService=1;
				setServiceX();
			});
			
			$('.bnr2').click(function(){
				currentService=2;
				setServiceX();
			});
			
			$('.bnr3').click(function(){
				currentService=3;
				setServiceX();
			});
			
			$('.bnr4').click(function(){
				currentService=4;
				setServiceX();
			});
			
			$('.bnr5').click(function(){
				currentService=5;
				setServiceX();
			});
			
			$('.bnr6').click(function(){
				currentService=6;
				setServiceX();
			});
			
			$('.bnr7').click(function(){
				currentService=7;
				setServiceX();
			});
			
			$('.bnr8').click(function(){
				currentService=8;
				setServiceX();
			});
			
			$('.bnr9').click(function(){
				currentService=9;
				setServiceX();
			});
		}
	}

	//scene:#service
	function setService(){
		
		content="service";
		
		if (!$.browser.msie){
			$('title').text("事業内容｜株式会社ブリッジコーポレーション");
		}
		
		$('#wrapper').fadeOut(500);
		
		bodySideMargin = getBodySideMargin()-1950;
		$('html').animate({backgroundPosition: bodySideMargin+' 180'}, 1000,loadBG);
		
		function loadBG(){
			$('#gnavi').load("service.html #header #gnavi ul",setRollOver);
			$('#body').load("service.html #service",function(){
				$('#wrapper').fadeIn(500);
			});
		}
	}
	
	function setRollOver(){
		$.getScript("js/smartRollover_01.js");
	}
	
	//scene:#company
	function setCompany(){
		
		content="company";
		
		if (!$.browser.msie){
			$('title').text("会社情報｜株式会社ブリッジコーポレーション");
		}
		
		$('#wrapper').fadeOut(500);
		
		bodySideMargin = getBodySideMargin()+350;
		$('html').animate({backgroundPosition: bodySideMargin+' -350'}, 1000,loadComplete);
		
		function loadComplete(){
			$('#gnavi').load("company.html #header #gnavi ul",setRollOver);
			$('#body').load("company.html #company",function(){
				$('#wrapper').fadeIn(500);
			});
		}
	}
	
	//scene:#company/outline
	function setCompanyOutline(){
		
		if(content!="company"){
			content="company";
			$('#wrapper').fadeOut(500);
			bodySideMargin = getBodySideMargin()+350;
			$('html').animate({backgroundPosition: bodySideMargin+' -350'}, 1000,loadComplete);	
			
		}else{
			hideIndex();
		}
		
		function loadComplete(){
			$('#gnavi').load("company.html #header #gnavi ul",setRollOver);
			$('#body').load("company.html #company",function(){
				$('#wrapper').fadeIn(500,hideIndex);
			});
		}
		function hideIndex(){
			$('#main').slideUp("slow",showOutline);
		}
		function showOutline(){
			$('#index').hide();
			$('#outline').show();
			$('#main').slideDown("slow");
		}
	}
	
	//scene:#company/index
	function setCompanyIndex(){
		$('#main').slideUp("slow",hideOutline);
		function hideOutline(){
			$('#index').show();
			$('#outline').hide();
			$('#main').slideDown("slow");
		}
	}
	
    //scene:#privacy_policy
	function setPrivacy(){
		
		content="privacy";
		
		if (!$.browser.msie){
			$('title').text("プライバシーポリシー｜株式会社ブリッジコーポレーション");
		}
		
		$('#wrapper').fadeOut(500);
		
		bodySideMargin = getBodySideMargin()-1950;
		$('html').animate({backgroundPosition: bodySideMargin+' -1300'}, 1000,compLoading);
		
		function compLoading(){
			$('#gnavi').load("privacy_policy.html #header #gnavi ul",setRollOver);
			$('#body').load("privacy_policy.html #privacy",function(){
				$('#wrapper').fadeIn(500);
			});
		}
	}
	
	//scene:#inquiry
	function setInquiry(){
		
		content="inquiry";
		
		if (!$.browser.msie){
			$('title').text("お問い合わせ｜株式会社ブリッジコーポレーション");
		}
		
		$('#wrapper').fadeOut(500);
		
		bodySideMargin = getBodySideMargin()-325;
		$('html').animate({backgroundPosition: bodySideMargin+' -1895'}, 1000,compBG);
		
		function compBG(){
			$('#gnavi').load("inquiry/index.php #header #gnavi ul",setRollOver);
			$('#body').load("inquiry/index.php #inquiry",function(){
				$('#wrapper').fadeIn(500);
				$.getScript("js/inquiry.js");
			});
		}
	}
	
	 //scene:#recruit
	function setRecruit(){
		
		content="recruit";
		
		if (!$.browser.msie){
			$('title').text("採用情報｜株式会社ブリッジコーポレーション");
		}
		
		$('#wrapper').fadeOut(500);
		
		bodySideMargin = getBodySideMargin()+350;
		$('html').animate({backgroundPosition: bodySideMargin+' -1400'}, 1000,compLoading);
		
		function compLoading(){
			$('#gnavi').load("recruit.html #header #gnavi ul",setRollOver);
			$('#body').load("recruit.html #recruit",function(){
				$('#wrapper').fadeIn(500);
			});
			
		}
	}

	//scene:#servise/solution
	function setSolution(){
		content="service02";
		if (!$.browser.msie){
			$('title').text("ソリューション事業｜事業内容｜株式会社ブリッジコーポレーション");
		}
		$('#wrapper').fadeOut(500);
		
		bodySideMargin = getBodySideMargin()-3500;
		$('html').animate({backgroundPosition: bodySideMargin+' 0'}, 1000,loadBG2);
		
		function loadBG2(){
			$('#gnavi').load("service_02.html #header #gnavi ul",setRollOver);
			$('#body').load("service_02.html #service02",function(){
				$('#wrapper').fadeIn(500);
				$('#bridge-net').hide();
			});
		}
	}

	//scene:#servise/bridge-net
	function setNet(){
		
		content="service02";
		
		if (!$.browser.msie){
			$('title').text("ブリッジネット事業｜事業内容｜株式会社ブリッジコーポレーション");
		}
		
		$('#wrapper').fadeOut(500);
		
		bodySideMargin = getBodySideMargin()-3500;
		$('html').animate({backgroundPosition: bodySideMargin+' 0'}, 1000,loadBG2);
		
		function loadBG2(){
			$('#gnavi').load("service_02.html #header #gnavi ul",setRollOver);
			$('#body').load("service_02.html #service02",function(){
				$('#wrapper').fadeIn(500);
				$('#solution').hide();
			});
		}
	}

	//scene:#servise/bridge-net/index
	function setNetIndex(){

		if(content!="service02"){
			content="service02";
			$('#wrapper').fadeOut(500);
		bodySideMargin = getBodySideMargin()-3500;
		$('html').animate({backgroundPosition: bodySideMargin+' 0'}, 1000,loadBG2);
			
		}else{
			hideIndex();
		}

		function loadBG2(){
			$('#gnavi').load("service_02.html #header #gnavi ul",setRollOver);
			$('#body').load("service_02.html #service02",function(){
				$('#wrapper').fadeIn(500,hideIndex);
			});
		}
		
		function hideIndex(){
			$('#main').slideUp("slow",hideOutline);
		}
		
		function hideOutline(){
			$('#bridge-net').show();
			$('#solution').hide();
			$('#main').slideDown("slow");
		}
		
		
	}

	//scene:#servise/solution/index
	function setSolutionIndex(){

		if(content!="service02"){
			content="service02";
			$('#wrapper').fadeOut(500);
		bodySideMargin = getBodySideMargin()-3500;
		$('html').animate({backgroundPosition: bodySideMargin+' 0'}, 1000,loadBG2);
			
		}else{
			hideIndex();
		}

		function loadBG2(){
			$('#gnavi').load("service_02.html #header #gnavi ul",setRollOver);
			$('#body').load("service_02.html #service02",function(){
				$('#wrapper').fadeIn(500,hideIndex);
			});
		}

		function hideIndex(){
			$('#main').slideUp("slow",hideOutline);
		}
		
		function hideOutline(){
			$('#solution').show();
			$('#bridge-net').hide();
			$('#main').slideDown("slow");
		}
		
	}

    //scene:#privacy_policy
	function setTopics(){
		
		content="topics";
		
		if (!$.browser.msie){
			$('title').text("トピックス｜株式会社ブリッジコーポレーション");
		}
		
		$('#wrapper').fadeOut(500);
		
		bodySideMargin = getBodySideMargin()-1950;
		$('html').animate({backgroundPosition: bodySideMargin+' -1300'}, 1000,compLoading);
		
		function compLoading(){
			$('#gnavi').load("topics.html #header #gnavi ul",setRollOver);
			$('#body').load("topics.html #topicsPage",function(){
				$('#wrapper').fadeIn(500);
			});
		}
	}
	////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	
	
	//initBG
	function initBG(){
		var bodySideMargin = getBodySideMargin();
		$('html').css("background-position", bodySideMargin+" 1000");
	}
	
	//returnVal4setBG
	function getBodySideMargin(){
		var bodySideMargin = ($(window).width()-900)/2+600;
		return bodySideMargin;
	}
	
	//reSize
	var resizeTimer = null;
	$(window).bind('resize', function() {
	if (resizeTimer) clearTimeout(resizeTimer);
		resizeTimer = setTimeout(resizeContainer, 200);
	});
	function resizeContainer() {
    	var bodySideMargin = getBodySideMargin();
		if(content=="home"){
			homeY = $('html').height()-200;
			$('html').animate({backgroundPosition: bodySideMargin+' '+homeY}, 100);
		}
		if(content=="service"){
			bodySideMargin-=1950;
			$('html').animate({backgroundPosition: bodySideMargin+' 180'}, 100);
		}
		if(content=="service02"){
			bodySideMargin-=3500;
			$('html').animate({backgroundPosition: bodySideMargin+' 0'}, 100);
		}
		if(content=="company"){
			bodySideMargin+=350;
			$('html').animate({backgroundPosition: bodySideMargin+' -370'}, 100);
		}
		if(content=="privacy"){
			bodySideMargin-=1950;
			$('html').animate({backgroundPosition: bodySideMargin+' -1300'}, 100);
		}
		if(content=="inquiry"){
			bodySideMargin-=325;
			$('html').animate({backgroundPosition: bodySideMargin+' -1895'}, 100);
		}
		if(content=="recruit"){
			bodySideMargin+=350;
			$('html').animate({backgroundPosition: bodySideMargin+' -1400'}, 100);
		}
    }
	
	var postfix = '_o';
	$('.hover').not('[src*="'+ postfix +'."]').each(function() {
		var img = $(this);
		var src = img.attr('src');
		var src_on = src.substr(0, src.lastIndexOf('.'))
		           + postfix
		           + src.substring(src.lastIndexOf('.'));
		$('<img>').attr('src', src_on);
		img.hover(function() {
			img.attr('src', src_on);
		}, function() {
			img.attr('src', src);
		});
	});
	
	
	
	
});

jQuery.preloadImages = function()
{
	for(var i = 0; i<arguments.length; i++)
	{
		jQuery("<img>").attr("src", arguments[i]);
	}
};
$.preloadImages("images/common/btn_01_o.gif","images/common/btn_01_c.gif","images/common/btn_02_o.gif","images/common/btn_02_c.gif","images/common/btn_03_o.gif","images/common/btn_03_c.gif","images/common/btn_04_o.gif","images/common/btn_04_c.gif","images/index/vi_btn_01_c.gif","images/index/vi_btn_02_c.gif","images/index/vi_btn_03_c.gif","images/index/vi_btn_04_c.gif","images/index/vi_btn_05_c.gif","images/index/vi_btn_06_c.gif","images/index/vi_btn_07_c.gif","images/index/vi_btn_08_c.gif","images/index/vi_btn_09_c.gif");


