//************************
//実行
//************************
$(document).ready(function(){
	frag();
	smartRollover();
	newwin();
	$("#primary a").attr("target","_blank");
	$("#nav .blank").attr("target","_blank");
	$("#nav ul").droppy();
	$("#sub-nav ul").droppy();
	$("#request ul").droppy();
	$("#side-contact").droppy();
	if(document.getElementById("side-contact")){$("#side-contact > li:eq(0)").attr("id","first");}
	displaytToggle();
	popup();
	tabUI("tab-ui");
});


//****************************************
//	Tab interfacec
//	 target : id
//	 transition : "normal" or "fade" or "slide" default "fade";
//	 navType : "text" or "image" default "image";
//****************************************


var tabUI = function(target,transition,navType){

	if(!transition){transition = "fade"}
	if(!navType){navType = "image"}

	$("#"+target+" .tab-source .tab-sec:gt(0)").hide();
	
	var defImg = $("#"+target+" .tab-nav li img:gt(0)").attr("src");
	if(defImg){
		var redefImg = defImg.replace("_off.","_cur.");
		$("#"+target+" .tab-nav li img:eq(0)").attr("src",redefImg);
	}
	
	$("#"+target+" .tab-nav li a").click(function(){
		var id = $(this).attr("href");
		$("#"+target+" .tab-sec").css("display","none");
		if(transition == "normal"){$(id).show();}else{$(id).fadeIn("first");}
		
		if(navType == "image"){
			var resetN = $("#"+target+" .tab-nav li").length;
			for(var i=0;i<resetN;i+=1){
				var curImg = $("#"+target+" .tab-nav li img:eq("+i+")").attr("src");
				var resetImg = curImg.replace("_cur.","_off.");
				$("#"+target+" .tab-nav li img:eq("+i+")").attr("src",resetImg);
			}
			var nowImg = $(this).find("img").attr("src");
			var setImg = nowImg.replace("_on.","_cur.");
			$(this).find("img").attr("src",setImg);
		}else{
			$("#"+target+" .tab-nav li a").removeClass('on');
			$(this).addClass('on');
		}
		
		return false;
	});
}

//************************
//表示のトグル 
//************************

var displaytToggle = function(){
	$("#detail").hide();
	$(".more a").click(function(){
		var target = $(this).attr("href");
		$(target).slideToggle();
		return false;
	});
}


//************************
//スマートロールオーバー 
//************************
var smartRollover = function () {  
	if(document.getElementsByTagName) {  
		var images = document.getElementsByTagName("img");  
			for(var i=0; i < images.length; i++) {  
				if(images[i].getAttribute("src").match("_off.")){  
					images[i].onmouseover = function() {  
					this.setAttribute("src", this.getAttribute("src").replace("_off.", "_on."));  
				}
				images[i].onmouseout = function(){
					this.setAttribute("src", this.getAttribute("src").replace("_on.", "_off."));  
					}
				}
			}
		}  
}

//************************
//スマートスクロール
//************************-
var frag = function (){
	$(".frag-nav a").click(function () {
		var target = $(this).attr("href");
		$(this).blur();
		$('html,body').animate({ scrollTop: $(target).offset().top }, 'fast');
		return false;
	});
}

//************************
//新規ウィンドウ
//************************
var popup = function (file){
	$(".popup").click(function () {
		window.open($(this).attr("href"),"win","scrollbars=yes,width=420,height=400");
		return false;
	});
}
var newwin = function (file){
	$(".new-win").click(function () {
		window.open($(this).attr("href"),"win","scrollbars=yes,width=700,height=800");
		return false;
	});
}

//************************
//ドロップダウンメニュー
//************************
$.fn.droppy = function(options) {
    
  options = $.extend({speed: 10}, options || {});
  
  this.each(function() {
    
    function getSubnav(ele) {
      if (ele.nodeName.toLowerCase() == 'li') {
        var subnav = $('> ul', ele);
        return subnav.length ? subnav[0] : null;
      } else {
        return ele;
      }
    }
    
    function getActuator(ele) {
      if (ele.nodeName.toLowerCase() == 'ul') {
        return $(ele).parents('li')[0];
      } else {
        return ele;
      }
    }
    
    function hide() {
      var subnav = getSubnav(this);
      if (!subnav) return;
      $.data(subnav, 'cancelHide', false);
      setTimeout(function() {
        if (!$.data(subnav, 'cancelHide')) {
          $(subnav).slideUp(options.speed);
        }
      }, 10);
    }
  
    function show() {
      var subnav = getSubnav(this);
      if (!subnav) return;
      $.data(subnav, 'cancelHide', true);
      $(subnav).slideDown(options.speed);
      if (this.nodeName.toLowerCase() == 'ul') {
        var li = getActuator(this);
        $(li).addClass('hover');
        $('> a', li).addClass('hover');
      }
    }
    
    $('ul, li', this).hover(show, hide);
    $('li', this).hover(
      function() { $(this).addClass('hover'); $('> a', this).addClass('hover'); },
      function() { $(this).removeClass('hover'); $('> a', this).removeClass('hover'); }
    );
    
  });
  
};

