// JavaScript Document

$(document).ready(function() {
  
  // CSS
  $("tr:nth-child(odd)").addClass("odd");
  $("li:nth-child(odd)").addClass("odd");
  $(".subnavi-box").addClass("clickable fade rounded");
  $(".subnavi-list").addClass("rounded");
  $(".subnavi-list li").addClass("rounded");
  $("#top-info li").addClass("clickable fade");
  
  // navi
  $("#subnavi").css("display","block");
  naviTimer = setTimeout(function(){},100);
  naviIndex = 0;
  naviLock = true; // ナビをクリック、ページ遷移後にサブナビが開くのを防ぐ
  function resetSubnavi(){
  	$("#navi").clearQueue();
  	$("#navi:not(:animated)").animate({height:"390px"},500);
  	$("#subnavi-1").css("display","none");
  	$("#subnavi-2").css("display","none");
  	$("#subnavi-3").css("display","none");
  	$("#subnavi-4").css("display","none");
  }
  $("#wrapper").one("mousemove",function () {
	  naviLock=false;
  });
  $("#navi-left").hover(function() {},function () {
	naviLock=false;
  	clearTimeout(naviTimer);
  	if($("#navi").css("height")!="50px"){
	  naviTimer = setTimeout(function(){
	  naviIndex=0;
	  $("#navi").delay(500).animate({height:"50px"},500);
	  },2000);
  	}
  });
  $("#navi-left > ul li").mouseover(function(){
	if(naviLock==false){
	  clearTimeout(naviTimer);
	  myIndex = eval($(this).attr('id').replace('navi',''));
	  if(myIndex != naviIndex){
		naviTimer = setTimeout(function(){
		naviIndex = myIndex;
		resetSubnavi();
		$('#subnavi'+myIndex).not(":animated").fadeIn("fast");
		},200);
	  }
    }
  });
  $("#subnavi").mouseover(function(){
  	clearTimeout(naviTimer);
  });
  
  // sidenavi
  file_name = location.href;
  file_name = file_name.substring(file_name.lastIndexOf("/")+1,file_name.length);
  if (file_name.indexOf("#") != -1) {
  file_name = file_name.substring(0,file_name.indexOf("#"));
  }
  
  //added at 20121220 by traction
  if (file_name.indexOf("?") != -1) {
    file_name = file_name.substring(0,file_name.indexOf("?"));
  }
  
  if (file_name == "") {
  file_name = "index.php";
  }
  file_name = jQEscape(file_name);
  $(".sidenavi-pink a[href="+file_name+"]").addClass("selected");
  $(".sidenavi-pink a[href="+file_name+"]").parents().removeClass("close");
  $(".sidenavi-pink a[href="+file_name+"] + ul").removeClass("close");
  
  // voice
  $("#voice-navi a[href="+file_name+"]").addClass("selected");
  
  // clickable block
  $(".clickable").css("cursor","pointer");
  $(".clickable").click(function() {
  window.location=$(this).find("a").attr("href");
  return false;
  });
  
  // toggle box
  $(".toggle-box").hide();
  $(".toggle-btn").click(function() {
	  $(this).next().not(":animated").slideToggle();
	  return false;
  });
  $(".toggle-close img").click(function() {
	  $(this).parent().parent().slideToggle();
	  return false;
  });

  // smooth scroll
  $('a[href*=#page_], a[href*=#wrapper]').click(function() {
  if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') 
  && location.hostname == this.hostname) {
  var $target = $(this.hash);
  $target = $target.length && $target || $('[name=' + this.hash.slice(1) +']');
  if ($target.length) {
  var targetOffset = $target.offset().top;
  $('html,body').animate({scrollTop: targetOffset}, 1000);
  return false;
  }
  }
  });
  
  // open in new window
  $("a[href^='http://']").attr("target","_blank");
  $("a[href^='https://']").attr("target","_blank");

  // fade
  $(".fade").hover(function(){
	$(this).fadeTo("fast", 0.7);
  },function(){
	$(this).fadeTo("fast", 1.0);
  });

  // rollover
  var image_cache = new Object();
  $(".swap").each(function(i) {
	var imgsrc = this.src;
	var dot = this.src.lastIndexOf('.');
	var imgsrc_on = this.src.substr(0, dot) + '-on' + this.src.substr(dot, 4);
	image_cache[this.src] = new Image();
	image_cache[this.src].src = imgsrc_on;
	if ($(this).hasClass('selected')) {
	this.src = imgsrc_on;
	} else {
	$(this).hover(
	function() { this.src = imgsrc_on; },
	function() { this.src = imgsrc; });
  }
  });
  
  // open window
  $("a.open-window").click(function() {
	url = $(this).attr("href");
	subWin = window.open(url, "new", "width=700,height=900,scrollbars=yes,resizable=yes,status=no,menubar=no,toolbar=no");
	subWin.focus();
	return false;
  });
  
  
});

// escape for jQuery selector
function jQEscape(str) {
  return str.replace(new RegExp("(#|;|&|,|\\.|\\+|\\*|~|'|:|\"|!|\\^|\\$|\\[|\\]|\\(|\\)|=|>|\\||\\/|\\\\)","g"),"\\$1");
}
