//構築完了したので整理書き
var _ua = uaCheck();

$(document).ready(function() {
	// init
	fontScalSet(); // fontSize
	imageOver(); // img over
	smoothScroll(); // scroll
	sideMenu(); // sideMenu
	
	$("#breadcrumbs-area ul li:last").addClass("current");

	tabContent(); // tab-content
	tabContentSpecList(); // tab-content lens spec list
	clossfade();// clossfade-content-js
	trigger();
	modalWindow();
});


/**************************************************************
  user agent check
**************************************************************/
function uaCheck() {
	var ulo = navigator.userAgent.toLowerCase();
	return {
		ie			:ulo.indexOf("msie") != -1,
		ie6			:ulo.indexOf("msie 6") != -1,
		ie7			:ulo.indexOf("msie 7") != -1,
		ie8			:ulo.indexOf("msie 8") != -1,
		ff			:ulo.indexOf("firefox") != -1,
		safari	:ulo.indexOf("safari") != -1,
		chrome	:ulo.indexOf("chrome") != -1
	};
}

/**************************************************************
  browser bug
**************************************************************/
// position bug
function footerUpdate(){
	if(_ua.ie6) $("#footer").css({"position":"static"}).css({"bottom":"0","position":"absolute"});
}


/**************************************************************
  font setting
**************************************************************/
function fontScalSet(){
	$("#header div.headnavi div.fontScaler").fontScaler({
		target:'.resizetext',
		containerclass:'fontSize',
		store:true,
		fixed:{size1:'85',size2:'93',size3:'108'}
	});
}

/**************************************************************
  img over event
**************************************************************/
function imageOver(){
	var $fade = $(".fade");
	$fade.hover(
		function(){$(this).fadeTo(50,0.5);},
		function(){$(this).fadeTo(250,1);}
	);
	$fade.click(
		function(){$(this).fadeTo(250,1);}
	);
}

/**************************************************************
  smooth scroll
**************************************************************/
function smoothScroll(){
	$('a[href*=#]').click(function() {
		if(location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname){
				var target = $(this.hash);
				target = target.length && target;
				if(target.length == undefined){
					target = $(this.hash.replace(/#/,'#Anchor-'));
					target = target.length && target;
				}
				if (target.length) {
					var targetOffset = target.offset().top;
					$('html,body').animate({scrollTop: targetOffset}, {duration:800, easing: "easeOutExpo"});
					return false;
				}
			}
		});
	
	//他ページアンカー
	//alert(location.href.split("#")[0]);
	if(location.href.split("#")[1] != undefined){
		var target = '#Anchor-' + location.href.split("#")[1];
		var targetOffset = $(target).offset().top;
		if($(window).height() + targetOffset > $(document).height()){
			targetOffset = $(document).height() - $(window).height();
		}
		$('html,body').delay(300).animate({scrollTop: targetOffset}, {duration:800, easing: "easeOutExpo"});
	}
}

/**************************************************************
  side menu
**************************************************************/
function sideMenu(){
	var $sidenavi = $("#side-navi");
	
	if($sidenavi.length){
		var path = location.pathname;
		var arrPath = path.split('/');
		
		// .last付加
		$(".top-level > li", $sidenavi).each(function(){
			$("li:last", this).addClass("last");
		});
			
		// index.htmlより深層部である場合
		if (path.indexOf('.html') != -1 && arrPath.length > 3) {
			delete arrPath[arrPath.length - 1];
			path = arrPath.join('/');
		};
		
		// event
		$("#Accordion-js > li > ul", $sidenavi).each(function(index){
			var $this = $(this).prev("p");
			var params = { height:"toggle" };
			
			if(_ua.ie6){
				$(this).hide();
				footerUpdate();
			}
			
			$this.click(function(){
				if($this.hasClass("open")){
					$this.next("ul")
						.animate(params, function(){ $this.removeClass("open"); footerUpdate(); });
					return false;
				}else{
					$this.toggleClass("open").next("ul").animate(params, function(){ footerUpdate();	});
					return false;
				}
			});
			
		});
			
		// .current付加
		$('a[href$="'+path+'"]', $sidenavi)
			.parent("li").addClass("current")
			.parentsUntil(this, "#Accordion-js > li").children("ul").show().prev("p").addClass("open");
		
	}
}

/**************************************************************
  tab content
**************************************************************/
function tabContent(){
	
	$(".tab-container").each(function(){
		var $this = $(this);
		var index = 0;
		
		
		var path = location.href;
		if (path.indexOf("tab=") != -1) index = path.slice(path.indexOf("tab=") + 4)
		
		$("ul.tabs li:eq("+index+")", $this).addClass("active").show();
		$(".tab-content:eq("+index+")", $this).show();
		footerUpdate();
		
		if($("body#frame").length){
			$(".tab-content", $this).each(function(index){
				$("ul li", $(this)).find("a").attr("href", function(i, value){ return value + "?tab=" + index });
			});
		}
		
		$("ul.tabs li", $this).click(function(){
			$("ul.tabs li", $this).removeClass("active");
			$(this).addClass("active");
			
			$this.css("height", $this.height());//height setting
			
			$(".tab-content", $this).hide();
			var activeTab = $(this).find("a").attr("href");
			
			if(_ua.ie) $("#" + activeTab).show();
			else $("#" + activeTab).fadeIn();
			
			$this.css("height", "auto");//height default
			footerUpdate();
			
			return false;
		});
		
	});
}

/**************************************************************
  tab content lens spec list
**************************************************************/
function tabContentSpecList(){
	$(function() {
		if($(".tab-content .commonlist").length){
			//alert("test");
			$(".tab-content").each(function(){
				//var $container = $(".tab-content");
				var $this = $(this);
				$(".commonlist", $this).hide();
				$("li:first", "ul.switch-js").addClass("active").show();
				$(".commonlist:first", $this).show();
				
				$("ul.switch-js li", $this).click(function(){
					
					var $index = $(this).parent().children().index(this);
					
					$("ul.switch-js li").removeClass("active");
					$("ul.switch-js").each(function(){$("li", this).eq($index).addClass("active");});
					
					$this.css("height", $this.height());
					$(".commonlist").hide();
					var activeTab = $(this).find("a").attr("href");
					$("." + activeTab).show();
					$this.css("height", "auto");
					return false;
				});												
			});
		}
	});
}

/**************************************************************
  clossfade content
**************************************************************/
function clossfade(){
		$(".clossfade-container-js").each(function(){
		var $this = $(this);
		var $fadetime = 1000;
		if($this.hasClass("nonfade-js")) $fadetime = 1;
		
		$(".viewer img", $this).each(function(i){
			$(this).attr("id","view" + (i + 1).toString());
			$(this).css({opacity:'0'}).next("p").hide();
		});
		$(".viewer img:first", $this).css({zIndex:"99"}).css({opacity:'1'}).next("p").show();
		$("ul li", $this).click(function(){
			var showCont = $(this).find("a").attr("href");
			//alert(showCont)
			$(".viewer img#" + (showCont), $this).siblings("img").stop().animate({opacity:'0'},$fadetime).next("p").hide();
			$(".viewer img#" + (showCont), $this).stop().animate({opacity:'1'},$fadetime).next("p").show();
			$(this).addClass("active");
			$(this).siblings().removeClass("active");
			return false;
		});
		$("ul li:first", $this).addClass("active");
	});
}

/**************************************************************
	.trigger-jsの次の兄弟要素に.trigger-content-jsを配置で動作
	.trigger-jsの要素に.active付加
**************************************************************/
function trigger(){
	// toggle 
	$(function(){
		
		footerUpdate();
		
		if($("body#lens").length){
			$(".trigger-content-js").hide();
		} else {
			$(".trigger-content-js").hide();
			$(".trigger-content-js:first").show();
			$(".trigger-js:first").addClass("switch-active");
		}
		
		//Switch the "Open" and "Close" state per click then slide up/down (depending on open/close state)
		$(".trigger-js").click(function(){
			// position bug IE
			if(navigator.userAgent.indexOf("MSIE") != -1){
				$(".clossfade-container-js div.viewer", $(this).next()).css("position","static");
				$(".clossfade-container-js div.viewer img", $(this).next()).css("position","static");
			}
			$(this).toggleClass("switch-active").next().slideToggle("slow", function(){
				footerUpdate();
				if(navigator.userAgent.indexOf("MSIE") != -1){
					$(".trigger-content-js .clossfade-container-js div.viewer").css("position","relative");
					$(".trigger-content-js .clossfade-container-js div.viewer img").css("position","absolute");
				}
			});
			
			return false; //Prevent the browser jump to the link anchor
		});
	});
}

/**************************************************************
	.tooltip-container
	modalWindow
**************************************************************/
function modalWindow(){
	$("a[rel*=tooltip-trigger-js]").click(function(){
		var $this = $(this);
		var $target;
																								 
		if($("body#lens").length){
			var $targetWindow = $this.parent().parent().parent().parent().prev(".window-set-js");
			$target = $("#" + $this.attr("href"), $targetWindow);
		} else {
			$target = $("#" + $this.attr("href"));
		}
		
		$(".tooltip-container-js").hide();
		$target.show();
		return false;
	});
	$(".tooltip-container-js").each(function(){
		var $this = $(this);
		$(".close", $this).click(function(){
			$this.hide();
			return false;
		});
	});
}



