/*================================================================

共通スクリプト

================================================================*/
$(function(){
	// フェード処理 （img要素に.fadeを追加して使用）
	$("img.fade").fadeTo(0,1.0);
	$("img.fade").hover(function(){
			$(this).fadeTo(200,0);
		},
		function(){
			$(this).fadeTo(300,1.0);
		});
	// ロールオーバー （img要素に.overを追加して使用）
	$("img.over").mouseover(function(){
			$(this).attr("src",$(this).attr("src").replace(/^(.+)(\.[a-z]+)$/, "$1_on$2"))
		}).mouseout(function(){
			$(this).attr("src",$(this).attr("src").replace(/^(.+)_on(\.[a-z]+)$/, "$1$2"));
		}).each(function(){
			$("<img>").attr("src",$(this).attr("src").replace(/^(.+)(\.[a-z]+)$/, "$1_on$2"))
	});
	// クラスの追加
	$('body :first-child').addClass('firstChild'); //最後の要素
	$('body :last-child').addClass('lastChild'); //最初の要素
});




/*----------------------------------------------------------------
スムーススクロール
----------------------------------------------------------------*/
(function(){var easing=0.25;var interval=40;var d=document;var targetX=0;var targetY=0;var targetHash='';var scrolling=false;var splitHref=location.href.split('#');var currentHref_WOHash=splitHref[0];var incomingHash=splitHref[1];var prevX=null;var prevY=null;addEvent(window,'load',init);function init(){setOnClickHandler();if(incomingHash){if(window.attachEvent && !window.opera){setTimeout(function(){scrollTo(0,0);setScroll('#'+incomingHash);},50);}else{scrollTo(0,0);setScroll('#'+incomingHash);}}}function addEvent(eventTarget,eventName,func){if(eventTarget.addEventListener){eventTarget.addEventListener(eventName,func,false);}else if(window.attachEvent){eventTarget.attachEvent('on'+eventName,function(){func.apply(eventTarget);});}}function setOnClickHandler(){var links=d.links;for(var i=0;i<links.length;i++){var link=links[i];var splitLinkHref=link.href.split('#');if(currentHref_WOHash==splitLinkHref[0] && d.getElementById(splitLinkHref[1])){addEvent(link,'click',startScroll);}}}function startScroll(event){if(event){event.preventDefault();}else if(window.event){window.event.returnValue=false;}setScroll(this.hash);}function setScroll(hash){var targetEle=d.getElementById(hash.substr(1));if(!targetEle)return;var ele=targetEle;var x=0;var y=0;while(ele){x+=ele.offsetLeft;y+=ele.offsetTop;ele=ele.offsetParent;}var maxScroll=getScrollMaxXY();targetX=Math.min(x,maxScroll.x);targetY=Math.min(y,maxScroll.y);targetHash=hash;if(!scrolling){scrolling=true;scroll();}}function scroll(){var currentX=d.documentElement.scrollLeft||d.body.scrollLeft;var currentY=d.documentElement.scrollTop||d.body.scrollTop;var vx=(targetX - currentX) * easing;var vy=(targetY - currentY) * easing;var nextX=currentX+vx;var nextY=currentY+vy;if((Math.abs(vx)<1 && Math.abs(vy)<1)||(prevX===currentX && prevY===currentY)){scrollTo(targetX,targetY);scrolling=false;location.hash=targetHash;prevX=prevY=null;return;}else{scrollTo(parseInt(nextX),parseInt(nextY));prevX=currentX;prevY=currentY;setTimeout(function(){scroll()},interval);}}function getDocumentSize(){return{width:Math.max(document.body.scrollWidth,document.documentElement.scrollWidth),height:Math.max(document.body.scrollHeight,document.documentElement.scrollHeight)};}function getWindowSize(){var result={};if(window.innerWidth){var box=d.createElement('div');with(box.style){position='absolute';top='0px';left='0px';width='100%';height='100%';margin='0px';padding='0px';border='none';visibility='hidden';}d.body.appendChild(box);var width=box.offsetWidth;var height=box.offsetHeight;d.body.removeChild(box);result={width:width,height:height};}else{result={width:d.documentElement.clientWidth||d.body.clientWidth,height:d.documentElement.clientHeight||d.body.clientHeight};}return result;}function getScrollMaxXY(){if(window.scrollMaxX && window.scrollMaxY){return{x:window.scrollMaxX,y:window.scrollMaxY};}var documentSize=getDocumentSize();var windowSize=getWindowSize();return{x:documentSize.width - windowSize.width,y:documentSize.height - windowSize.height};}}());
/*----------------------------------------------------------------
jquery.exfixed.js（ie6のposition:fixed;）
----------------------------------------------------------------*/
(function(b){b.ex=b.ex||{};var a=b.extend({},b.ex);a.defineExPlugin=function(d,c,e){b.fn[d]=function(g,l){var k=this,f=[];p=e||{eachTarget:true};if(p.eachTarget){k.each(function(i){f.push(new c(k.eq(i),g))})}else{f.push(new c(k,g))}var j=b(f);for(var h in c.prototype){(function(m){if(m.slice(0,1)!="_"){j[m]=function(){return j[0][m].apply(j[0],arguments)}}})(h)}j.target=function(){return k};k["get"+d.substr(0,1).toUpperCase()+d.substr(1)]=function(){return j};if(typeof l=="function"){j.each(l)}return this}};a.scrollEvent=function(e,d){var f=this;if(typeof d=="function"){d={callback:d}}var g=f.config=b.extend({},a.scrollEvent.defaults,d,{target:e});g.status=0;g.scroll=f.getPos();g.target.scroll(function(c){if(f.isMove()){g.status=(g.status==0?1:(g.status==1?2:g.status));g.callback(c,g)}if(g.tm){clearTimeout(g.tm)}g.tm=setTimeout(function(){f.isMove();g.status=0;g.callback(c,g)},g.delay)})};b.extend(a.scrollEvent.prototype,{isMove:function(){var f=this,h=f.config;var g=f.getPos();var d=(g.top!=h.scroll.top);var e=(g.left!=h.scroll.left);if(d||e){h.scrollY=d;h.scrollX=e;h.prevScroll=h.scroll;h.scroll=g;return true}return false},getPos:function(){var d=this,e=d.config;return{top:e.target.scrollTop(),left:e.target.scrollLeft()}}});a.scrollEvent.defaults={delay:100};b.ex.fixed=function(g,e){var h=this;var i=h.config=b.extend({},b.ex.fixed.defaults,e,{target:g,logicSize:{},rowSize:{},currentStyle:"",style:"",window:b(window),staticFixed:false,oldBrowser:b.browser.msie&&(b.browser.version<7||!b.boxModel)});if(i.baseNode){i.baseNode=$(i.baseNode)}var f=h._cleanSize(i);h._eachSizeSet(function(c,k,j){i.staticFixed=i.staticFixed||(f[k.pos1]==undefined&&f[k.pos2]==undefined)});if(i.oldBrowser){h._padPos(f,h._cleanSize(i.target[0].currentStyle))}else{if(i.staticFixed){return}}i.container=b.boxModel?b("html"):b("body");i.container.height();i.target.css("position",i.oldBrowser?"absolute":"fixed");if(i.oldBrowser&&!/hidden|scroll/i.test(i.target.css("overflow"))){i.target.css("overflow","hidden")}h._smoothPatch();h._fixed(f);i.window.resize(function(){if(i.oldBrowser||i.baseNode){h._fixed()}});if(!(i.fixedX&&i.fixedY)){if(i.oldBrowser){var d;i.window.scroll(function(){if(d){clearTimeout(d)}d=setTimeout(function(){h._fixed()},0)})}else{new a.scrollEvent(i.window,function(c,j){if((j.scrollX&&!i.fixedX)||(j.scrollY&&!i.fixedY)){if(j.status==1){h._fixed(i.logicSize,{unfixed:true})}else{if(j.status==0){h._fixed()}}}})}}};b.ex.fixed.config={smoothPatched:false};b.ex.fixed.defaults={baseNode:"",baseX:true,baseY:true,fixedX:true,fixedY:true};b.extend(b.ex.fixed.prototype,{_attn:[{size:"height",pos1:"top",pos2:"bottom"},{size:"width",pos1:"left",pos2:"right"}],_camel:[{size:"Height",pos1:"Top",pos2:"Bottom"},{size:"Width",pos1:"Left",pos2:"Right"}],_moveFixedFront:function(){var f=this,g=f.config;var d=g.target.parents();var e=d.filter(function(c){var h=d.eq(c);return !(/HTML|BODY/i.test(h[0].tagName))&&d.eq(c).css("position")!="static"});if(e.size()){e.eq(e.size()-1).after(g.target)}return f},_smoothPatch:function(){var e=this,f=e.config;e._moveFixedFront();if(!f.oldBrowser){return e}b.ex.fixed.config.smoothPatched=true;var d=b("html");if(d.css("background-image")=="none"){d.css({"background-image":"url(null)"})}d.css({"background-attachment":"fixed"});return e},_eachSize:function(k){var l=this,m=l.config;for(var h=0;h<l._attn.length;h++){var g=l._attn[h];for(var e in g){var d=g[e];k({idx:h,name:d,camel:d.slice(0,1).toUpperCase()+d.slice(1)})}}},_eachSizeSet:function(e){var g=this,h=g.config;for(var d=0;d<g._attn.length;d++){e(d,g._attn[d],g._camel[d],g._attn[1-d],g._camel[1-d])}},_parseSize:function(g,d){var f=this,h=f.config;if(g=="auto"){return undefined}if((g+"").indexOf("%")<0){return parseInt(g)||0}var e=h.container.attr(d?"clientWidth":"clientHeight");return Math.round(e*parseInt(g)/100)},_parseIntSize:function(f,d){var e=this,g=e.config;return parseInt(e._parseSize(f,d))||0},_cleanSize:function(e){var f=this,g=f.config;var d={};f._eachSize(function(c){if(/undefined|auto/i.test(e[c.name])){try{delete e[c.name]}catch(h){}}else{d[c.name]=e[c.name]}});return d},_padPos:function(d,e){var f=this,h=f.config;var g;f._eachSizeSet(function(c,j,i){if(d[j.pos1]==undefined&&d[j.pos2]==undefined){if((g=e[j.pos1])!=undefined){d[j.pos1]=g}else{if((g=e[j.pos2])!=undefined){d[j.pos2]=g}else{d[j.pos1]=0}}}if(d[j.size]==undefined){if((d[j.size]=e[j.size])==undefined){d[j.size]=h.target[j.size]()}}});return d},_calcRowSize:function(h,g){var i=this,j=i.config;var g=b.extend({abs:false,base:j.baseNode,unfixed:false},g);var f={};i._eachSize(function(c){var k=h[c.name];if(!(/undefined/i.test(k))){f[c.name]=i._parseIntSize(k,/width|left|right/i.test(c.name));if(g.abs&&/top|left/i.test(c.name)){f[c.name]+=j.window["scroll"+c.camel]()}}});if(g.base){var e=j.baseNode.offset();i._eachSizeSet(function(k,l,c){e[l.pos2]=j.container.attr("client"+c.size)-(e[l.pos1]+j.baseNode["outer"+c.size]())});i._eachSize(function(k){if(!(/height|width/i.test(k.name))&&f[k.name]==undefined&&((!k.idx&&j.baseY)||(k.idx&&j.baseX))){var c=k.name=="top"?"bottom":k.name=="bottom"?"top":k.name=="left"?"right":"left";f[c]+=e[c]}})}var d=g.unfixed&&!j.fixedX?-1:1;if(d==-1||(!g.unfixed&&!j.fixedY)){if(f.top!=undefined){f.top-=(j.window.scrollTop()*d)}if(f.bottom!=undefined){f.bottom+=(j.window.scrollTop()*d)}}var d=!g.unfixed&&!j.fixedX?-1:1;if(d==-1||(g.unfixed&&!j.fixedY)){if(f.left!=undefined){f.left+=(j.window.scrollLeft()*d)}if(f.right!=undefined){f.right-=(j.window.scrollLeft()*d)}}return f},_fixed:function(g,f){var h=this,i=h.config;var f=b.extend({unfixed:false},f);if(g){i.logicSize=h._padPos(h._cleanSize(g),i.logicSize)}if(!i.oldBrowser){i.target.css($.extend(i.baseNode||!(i.fixedX&&i.fixedY)?h._calcRowSize(i.logicSize,f):i.logicSize,{position:f.unfixed?"absolute":"fixed"}))}else{var e=h._calcRowSize(i.logicSize);var d=false;if(i.target.is(":hidden")){d=true;i.target.show()}h._eachSizeSet(function(j,l,c){i.target.css(l.size,e[l.size]);var k=e[l.pos1];if(k==undefined){k=i.container.attr("client"+c.size)-e[l.pos2]-i.target["outer"+c.size]()}var m=(k+i.target["outer"+c.size]())-i.container.attr("client"+c.size);if(m>0){m=i.target[l.size]()-m;if(m>0){i.target[l.size](m)}else{d=true}}if(!d){i.target[0].style.setExpression(l.pos1,k+((!j&&!i.fixedY)||(j&&!i.fixedX)?i.window["scroll"+c.pos1]():"+eval(document.body.scroll"+c.pos1+"||document.documentElement.scroll"+c.pos1+")"))}});if(d){i.target.hide()}}},target:function(){return this.config.target},fixedOpen:function(d){var e=this,g=e.config;if(g.staticFixed){return}if(g.oldBrowser){g.target[0].style.removeExpression("top");g.target[0].style.removeExpression("left")}if(d){setTimeout(function(){if(g.oldBrowser){g.target.css({top:"auto",left:"auto"});g.target.css(e._calcRowSize(g.logicSize,{abs:true}))}d()},100)}return e},fixedClose:function(d){var e=this,f=e.config;if(f.staticFixed){return}e._fixed(d);return e},fixedSize:function(d){var e=this,f=e.config;return e._calcRowSize(e._padPos(d,f.logicSize),{abs:f.oldBrowser})},resize:function(d){var e=this,f=e.config;e.fixedOpen(function(){e.fixedClose(d)});return e}});a.defineExPlugin("exFixed",b.ex.fixed)})(jQuery);
/*----------------------------------------------------------------
Droppy （globalNavの2階層目のドロップダウンメニュー）
----------------------------------------------------------------*/
/*
 * Droppy 0.1.2
 * (c) 2008 Jason Frame (jason@onehackoranother.com)
 */
$.fn.droppy=function(options){options=$.extend({speed:300},options||{});this.each(function(){var root=this,zIndex=1000;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);}},100);}function show(){var subnav=getSubnav(this);if(!subnav) return;$.data(subnav,'cancelHide',true);$(subnav).css({zIndex:zIndex++}).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');});});};
$(function(){
/*----------------------------------------------------------------
Droppy（globalNavドロップダウン）
----------------------------------------------------------------*/
$("#globalnav").droppy();  
/*----------------------------------------------------------------
jquery.exfixed.js（ie6のposition:fixed;）
----------------------------------------------------------------*/
$('#pagetop_f').exFixed(); // for IE6

});


/*----------------------------------------------------------------
Smooth scrolling
----------------------------------------------------------------*/
/* Smooth scrolling
   Changes links that link to other parts of this page to scroll
   smoothly to those links rather than jump to them directly, which
   can be a little disorienting.
   
   sil, http://www.kryogenix.org/
   
   v1.0 2003-11-11
   v1.1 2005-06-16 wrap it up in an object
*/

var ss = {
  fixAllLinks: function() {
    // Get a list of all links in the page
    var allLinks = document.getElementsByTagName('a');
    // Walk through the list
    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) ) && 
          (lnk.search == location.search)) {
        // If the link is internal to the page (begins in #)
        // then attach the smoothScroll function as an onclick
        // event handler
        ss.addEvent(lnk,'click',ss.smoothScroll);
      }
    }
  },

  smoothScroll: function(e) {
    // This is an event handler; get the clicked on element,
    // in a cross-browser fashion
    if (window.event) {
      target = window.event.srcElement;
    } else if (e) {
      target = e.target;
    } else return;

    // Make sure that the target is an element, not a text node
    // within an element
    if (target.nodeName.toLowerCase() != 'a') {
      target = target.parentNode;
    }
  
    // Paranoia; check this is an A tag
    if (target.nodeName.toLowerCase() != 'a') return;
  
    // Find the <a name> tag corresponding to this href
    // First strip off the hash (first character)
    anchor = target.hash.substr(1);
    // Now loop all A tags until we find one with that name
    var allLinks = document.getElementsByTagName('a');
    var destinationLink = null;
    for (var i=0;i<allLinks.length;i++) {
      var lnk = allLinks[i];
      if (lnk.name && (lnk.name == anchor)) {
        destinationLink = lnk;
        break;
      }
    }
    if (!destinationLink) destinationLink = document.getElementById(anchor);

    // If we didn't find a destination, give up and let the browser do
    // its thing
    if (!destinationLink) return true;
  
    // Find the destination's position
    var destx = destinationLink.offsetLeft; 
    var desty = destinationLink.offsetTop;
    var thisNode = destinationLink;
    while (thisNode.offsetParent && 
          (thisNode.offsetParent != document.body)) {
      thisNode = thisNode.offsetParent;
      destx += thisNode.offsetLeft;
      desty += thisNode.offsetTop;
    }
  
    // Stop any current scrolling
    clearInterval(ss.INTERVAL);
  
    cypos = ss.getCurrentYPos();
  
    ss_stepsize = parseInt((desty-cypos)/ss.STEPS);
    ss.INTERVAL =
setInterval('ss.scrollWindow('+ss_stepsize+','+desty+',"'+anchor+'")',10);
  
    // And stop the actual click happening
    if (window.event) {
      window.event.cancelBubble = true;
      window.event.returnValue = false;
    }
    if (e && e.preventDefault && e.stopPropagation) {
      e.preventDefault();
      e.stopPropagation();
    }
  },

  scrollWindow: function(scramount,dest,anchor) {
    wascypos = ss.getCurrentYPos();
    isAbove = (wascypos < dest);
    window.scrollTo(0,wascypos + scramount);
    iscypos = ss.getCurrentYPos();
    isAboveNow = (iscypos < dest);
    if ((isAbove != isAboveNow) || (wascypos == iscypos)) {
      // if we've just scrolled past the destination, or
      // we haven't moved from the last scroll (i.e., we're at the
      // bottom of the page) then scroll exactly to the link
      window.scrollTo(0,dest);
      // cancel the repeating timer
      clearInterval(ss.INTERVAL);
      // and jump to the link directly so the URL's right
      location.hash = anchor;
    }
  },

  getCurrentYPos: function() {
    if (document.body && document.body.scrollTop)
      return document.body.scrollTop;
    if (document.documentElement && document.documentElement.scrollTop)
      return document.documentElement.scrollTop;
    if (window.pageYOffset)
      return window.pageYOffset;
    return 0;
  },

  addEvent: function(elm, evType, fn, useCapture) {
    // addEvent and removeEvent
    // cross-browser event handling for IE5+,  NS6 and Mozilla
    // By Scott Andrew
    if (elm.addEventListener){
      elm.addEventListener(evType, fn, useCapture);
      return true;
    } else if (elm.attachEvent){
      var r = elm.attachEvent("on"+evType, fn);
      return r;
    } else {
      alert("Handler could not be removed");
    }
  } 
}

ss.STEPS = 25;

ss.addEvent(window,"load",ss.fixAllLinks);




/*----------------------------------------------------------------
Tab
----------------------------------------------------------------*/

$(document).ready(function(){
	
	// 初期設定
		
		// #nav直下の全li要素の中から最初のli要素に.selectを追加
		$("#nav > li:first").addClass("select");
		// #nav直下の全li要素にマウスオーバーしたらリンク要素に偽装
		$("#nav > li").hover(function(){
			$(this).css("cursor","pointer");
		},function(){
			$(this).css("cursor","default");
		});
		
		// #tab直下の全div要素を非表示
		$("#tab > div").hide();
		// #tab直下の全div要素の中から最初のdiv要素を表示
		$("#tab > div:first").show();
	
	// タブ切替処理
	$("#nav > li").click(function () {
		// #nav直下の全li要素のclass属性を削除
		$("#nav > li").removeClass("select");
		// クリックしたli要素に.selectを追加
		$(this).addClass("select");
		// #tab直下の全div要素を非表示
		$("#tab > div").hide();
		// クリックしたタブのインデックス番号と同じdiv要素をフェード表示
		$("#tab > div").eq($("#nav > li").index(this)).fadeIn();
	});
	
});

