/*
 * yuga.js 0.7.1 - 優雅なWeb制作のためのJS
 *
 * Copyright (c) 2009 Kyosuke Nakamura (kyosuke.jp)
 * Licensed under the MIT License:
 * http://www.opensource.org/licenses/mit-license.php
 *
 * Since:     2006-10-30
 * Modified:  2009-01-27
 *
 * jQuery 1.3.1
 * ThickBox 3.1
 */

/*
 * [使用方法] XHTMLのhead要素内で次のように読み込みます。
 
<link rel="stylesheet" href="css/thickbox.css" type="text/css" media="screen" />
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/thickbox.js"></script>
<script type="text/javascript" src="js/yuga.js" charset="utf-8"></script>

 */
 
 
var _ua = (function(){
return {
ltIE6:typeof window.addEventListener == "undefined" && typeof document.documentElement.style.maxHeight == "undefined",
ltIE7:typeof window.addEventListener == "undefined" && typeof document.querySelectorAll == "undefined",
ltIE8:typeof window.addEventListener == "undefined" && typeof document.getElementsByClassName == "undefined",
ie:document.uniqueID,
firefox:window.globalStorage,
opera:window.opera,
webkit:!document.uniqueID && !window.opera && !window.globalStorage && window.localStorage,
mobile:/android|iphone|ipad|ipod/i.test(navigator.userAgent.toLowerCase())
}
})();
/*if(_ua.ltIE6){
document.write('あなたがお使いのブラウザはIE6以下っぽいです');
}else if(_ua.ltIE7){
document.write('あなたがお使いのブラウザはIE7以下っぽいです');
}else if(_ua.ltIE8){
document.write('あなたがお使いのブラウザはIE8以下っぽいです');
}else if(_ua.webkit){
document.write('あなたがお使いのブラウザはchromeもしくはsafariっぽいです');
}else if(_ua.firefox){
document.write('あなたがお使いのブラウザはFirefoxっぽいです');
}else if(_ua.opera){
document.write('あなたがお使いのブラウザはOperaっぽいです');
}else if(_ua.ie && !_ua.ltIE8){
document.write('あなたがお使いのブラウザはIE9以上っぽいです');
}else if(_ua.mobile){
document.write('あなたはスマートフォンとかタブレット端末っぽいです');
}
 */
(function($) {

	$(function() {
		$.yuga.menuLoad();
//		$.yuga.selflink({
//		  changeImgSelf: false,
//		  changeImgParents: false
//		});
//		$.yuga.rollover();
//		$.yuga.copyright();
		$.yuga.externalLink();
//		$.yuga.thickbox();
		$.yuga.scroll();
//		$.yuga.tab();
		$.yuga.stripe();
		$.yuga.css3class();
//		$.yuga.slider();
		$.yuga.btnsHover();
	});

	//---------------------------------------------------------------------

	$.yuga = {
		// URIを解析したオブジェクトを返すfunction
		Uri: function(path){
			var self = this;
			this.originalPath = path;
			//絶対パスを取得
			this.absolutePath = (function(){
				var e = document.createElement('span');
				e.innerHTML = '<a href="' + path + '" />';
				return e.firstChild.href;
			})();
			//絶対パスを分解
			var fields = {'schema' : 2, 'username' : 5, 'password' : 6, 'host' : 7, 'path' : 9, 'query' : 10, 'fragment' : 11};
			var r = /^((\w+):)?(\/\/)?((\w+):?(\w+)?@)?([^\/\?:]+):?(\d+)?(\/?[^\?#]+)?\??([^#]+)?#?(\w*)/.exec(this.absolutePath);
			for (var field in fields) {
				this[field] = r[fields[field]];
			}
			this.querys = {};
			if(this.query){
				$.each(self.query.split('&'), function(){
					var a = this.split('=');
					if (a.length == 2) self.querys[a[0]] = a[1];
				});
			}
		},
		//外部メニューの読み込み
		menuLoad: function(){
		$("body").each(function(){
			var shrUrl = $(".header h1 a").attr("href").replace('index.html', '');
			var filename = location.href.split("#")[0].split("?")[0].split("/").reverse()[0];
			var filepass = location.href.replace(filename, '');
			var filerootpass = filepass.replace(shrUrl, '');
			var c = $.extend({
				selfLinkAreaSelector:'body',
				postfix: '_cr',
				changeImgSelf:true,
				changeImgParents:true
			
			});
			$("body #sNav").ready(function(){
				/*$(this).find(".clearli,.last,.noclear,.tree,.clear,.px12").removeClass("px12").removeClass("clearli").removeClass("last").removeClass("noclear").removeClass("tree").removeClass("clear");
				$(this).find("li[style='background:none;']").attr("style","");
				$(this).find("a").each(function(){
					var pass = $(this).attr('href').replace(filepass,'');
					pass.replace(filepass,'');
					$(this).attr('href',shrUrl + pass);
				});
				$(this).find("img").each(function(){
					var pass = $(this).attr('src').replace(filepass,'');
					pass.replace(filepass,'');
					$(this).attr('src',shrUrl + pass);
				});*/
				$(c.selfLinkAreaSelector+((c.selfLinkAreaSelector)?' ':'')+'a[href]').each(function(){
					var href = new $.yuga.Uri(this.getAttribute('href'));
					var setImgFlg = false;
					if ((href.absolutePath == location.href) && !href.fragment) {
						//同じ文書にリンク
						$(this).addClass('current').closest("li").closest("ul").closest("li").each(function () {
							$(this).addClass('active').removeClass('noactive');
							$(this).find('>ul').css("display","block");
						});
						setImgFlg = c.changeImgSelf;
					}	
				})
				$(this).find(":last-child").addClass('lastChild');
				//move();
				pulldown();
			});
			});
		},
		//現在のページと親ディレクトリへのリンク
		selflink: function (options) {
			var c = $.extend({
				selfLinkAreaSelector:'body',
				selfLinkClass:'current',
				parentsLinkClass:'parentsLink',
				postfix: '_cr',
				changeImgSelf:true,
				changeImgParents:true
			}, options);
			$(c.selfLinkAreaSelector+((c.selfLinkAreaSelector)?' ':'')+'a[href]').each(function(){
				var href = new $.yuga.Uri(this.getAttribute('href'));
				var setImgFlg = false;
				if ((href.absolutePath == location.href) && !href.fragment) {
					//同じ文書にリンク
					$(this).addClass(c.selfLinkClass);
					setImgFlg = c.changeImgSelf;
				} else if (0 <= location.href.search(href.absolutePath)) {
					//親ディレクトリリンク
					$(this).addClass(c.parentsLinkClass);
					setImgFlg = c.changeImgParents;
				}
				if (setImgFlg){
					//img要素が含まれていたら現在用画像（_cr）に設定
					$(this).find('img').each(function(){
						this.originalSrc = $(this).attr('src');
						this.currentSrc = this.originalSrc.replace(new RegExp('('+c.postfix+')?(\.gif|\.jpg|\.png)$'), c.postfix+"$2");
						$(this).attr('src',this.currentSrc);
					});
				}
			});
		},
		//ロールオーバー
		rollover: function(options) {
			var c = $.extend({
				hoverSelector: '.btn, .allbtn img',
				groupSelector: '.btngroup',
				postfix: '_on'
			}, options);
			//ロールオーバーするノードの初期化
			var rolloverImgs = $(c.hoverSelector).filter(isNotCurrent);
			rolloverImgs.each(function(){
				this.originalSrc = $(this).attr('src');
				this.rolloverSrc = this.originalSrc.replace(new RegExp('('+c.postfix+')?(\.gif|\.jpg|\.png)$'), c.postfix+"$2");
				this.rolloverImg = new Image;
				this.rolloverImg.src = this.rolloverSrc;
			});
			//グループ内のimg要素を指定するセレクタ生成
			var groupingImgs = $(c.groupSelector).find('img').filter(isRolloverImg);

			//通常ロールオーバー
			rolloverImgs.not(groupingImgs).hover(function(){
				$(this).attr('src',this.rolloverSrc);
			},function(){
				$(this).attr('src',this.originalSrc);
			});
			//グループ化されたロールオーバー
			$(c.groupSelector).hover(function(){
				$(this).find('img').filter(isRolloverImg).each(function(){
					$(this).attr('src',this.rolloverSrc);
				});
			},function(){
				$(this).find('img').filter(isRolloverImg).each(function(){
					$(this).attr('src',this.originalSrc);
				});
			});
			//フィルタ用function
			function isNotCurrent(i){
				return Boolean(!this.currentSrc);
			}
			function isRolloverImg(i){
				return Boolean(this.rolloverSrc);
			}

		},
		copyright: function(options) {
			$("body #footer").empty().append("All Rights Reserved. Copyright &copy; 2011 INDUSTRIAL LEASING CORPORATION");

		},
		//外部リンクは別ウインドウを設定
		externalLink: function(options) {
			var c = $.extend({
				windowOpen:true,
				externalClass: 'externalLink',
				addIconSrc: ''
			}, options);
			var uri = new $.yuga.Uri(location.href);
			var e = $('a[href^="http://"]').not('a[href^="' + uri.schema + '://' + uri.host + '/' + '"]');
			if (c.windowOpen) {
				e.click(function(){
					window.open(this.href, '_blank');
					return false;
				});
			}
			if (c.addIconSrc) e.not(':has(img)').after($('<img src="'+c.addIconSrc+'" class="externalIcon" />'));
			e.addClass(c.externalClass); 
		},
		//画像へ直リンクするとthickboxで表示(thickbox.js利用)
		thickbox: function() {
			try {
				tb_init('a[href$=".jpg"]:not(.thickbox, a[href*="?"]), a[href$=".gif"][href!="?"]:not(.thickbox, a[href*="?"]), a[href$=".png"][href!="?"]:not(.thickbox, a[href*="?"])');
			} catch(e) {
			}	
		},
		//ページ内リンクはするするスクロール
		scroll: function(options) {
			//ドキュメントのスクロールを制御するオブジェクト
			var scroller = (function() {
				var c = $.extend({
					easing:100,
					step:30,
					fps:60,
					fragment:''
				}, options);
				c.ms = Math.floor(1000/c.fps);
				var timerId;
				var param = {
					stepCount:0,
					startY:0,
					endY:0,
					lastY:0
				};
				//スクロール中に実行されるfunction
				function move() {
					if (param.stepCount == c.step) {
						//スクロール終了時
						setFragment(param.hrefdata.absolutePath);
						window.scrollTo(getCurrentX(), param.endY);
					} else if (param.lastY == getCurrentY()) {
						//通常スクロール時
						param.stepCount++;
						window.scrollTo(getCurrentX(), getEasingY());
						param.lastY = getEasingY();
						timerId = setTimeout(move, c.ms); 
					} else {
						//キャンセル発生
						if (getCurrentY()+getViewportHeight() == getDocumentHeight()) {
							//画面下のためスクロール終了
							setFragment(param.hrefdata.absolutePath);
						}
					}
				}
				function setFragment(path){
					location.href = path
				}
				function getCurrentY() {
					return document.body.scrollTop  || document.documentElement.scrollTop;
				}
				function getCurrentX() {
					return document.body.scrollLeft  || document.documentElement.scrollLeft;
				}
				function getDocumentHeight(){
					return document.documentElement.scrollHeight || document.body.scrollHeight;
				}
				function getViewportHeight(){
					return (!$.browser.safari && !$.browser.opera) ? document.documentElement.clientHeight || document.body.clientHeight || document.body.scrollHeight : window.innerHeight;
				}
				function getEasingY() {
					return Math.floor(getEasing(param.startY, param.endY, param.stepCount, c.step, c.easing));
				}
				function getEasing(start, end, stepCount, step, easing) {
					var s = stepCount / step;
					return (end - start) * (s + easing / (100 * Math.PI) * Math.sin(Math.PI * s)) + start;
				}
				return {
					set: function(options) {
						this.stop();
						if (options.startY == undefined) options.startY = getCurrentY();
						param = $.extend(param, options);
						param.lastY = param.startY;
						timerId = setTimeout(move, c.ms); 
					},
					stop: function(){
						clearTimeout(timerId);
						param.stepCount = 0;
					}
				};
			})();
			$('a[href^=#], area[href^=#]').not('a[href=#], area[href=#]').each(function(){
				this.hrefdata = new $.yuga.Uri(this.getAttribute('href'));
			}).click(function(){
				var target = $('#'+this.hrefdata.fragment);
				if (target.length == 0) target = $('a[name='+this.hrefdata.fragment+']');
				if (target.length) {
					scroller.set({
						endY: target.offset().top,
						hrefdata: this.hrefdata
					});
					return false;
				}
			});
		},
		//タブ機能
		tab: function(options) {
			var c = $.extend({
				tabNavSelector:'.tabNav',
				activeTabClass:'active'
			}, options);
			$(c.tabNavSelector).each(function(){
				var tabNavList = $(this).find('a[href^=#], area[href^=#]');
				var tabBodyList;
				tabNavList.each(function(){
					this.hrefdata = new $.yuga.Uri(this.getAttribute('href'));
					var selecter = '#'+this.hrefdata.fragment;
					if (tabBodyList) {
						tabBodyList = tabBodyList.add(selecter);
					} else {
						tabBodyList = $(selecter);
					}
					$(this).unbind('click');
					$(this).click(function(){
						tabNavList.removeClass(c.activeTabClass);
						$(this).addClass(c.activeTabClass);
						tabBodyList.slideUp();
						$(selecter).slideDown();
						return false;
					});
				});
				tabBodyList.hide()
				tabNavList.filter(':first').trigger('click');
			});
		},
		//奇数、偶数を自動追加
		stripe: function(options) {
			var c = $.extend({
				oddClass:'odd',
				evenClass:'even'
			}, options);
			$('ul, ol').each(function(){
				//JSでは0から数えるのでevenとaddを逆に指定
				$(this).children('li:odd').addClass(c.evenClass);
				$(this).children('li:even').addClass(c.oddClass);
			});
			$('table, tbody').each(function(){
				$(this).children('tbody:odd').addClass(c.evenClass);
				$(this).children('tbody:even').addClass(c.oddClass);
			});
		},
		//css3のクラスを追加
		css3class: function() {
			//:first-child, :last-childをクラスとして追加
			$('body :first-child').addClass('firstChild');
			$('body :last-child').addClass('lastChild');
			//css3の:emptyをクラスとして追加
			$('body :empty').addClass('empty');
		},
		//トップページスライダー部分
		slider: function(options) {
	
			 $(function() {
				$('.dumbCrossFade .dumbItem').dumbCrossFade({'slideType':'fade','fadeInOut':true,'showTime':4000});
			});
		},
		btnsHover: function ()
		{		
			$('.visualNavigation .section').each(function()
			{
				var href = $(this).find('a').attr('href');
				
				$(this).css('cursor','pointer')
				.hover(function(){
					$(this).not(':animated').fadeTo('fast', 0.5);}
				,function(){
					$(this).fadeTo('fast', 1);})
				.click(function() {
					location.href = href;
				});
			});
		}
	};
})(jQuery);


      function switchSite(dropDown)
      {
        if (dropDown != null)
        {
          var href = dropDown.value;
          if (href == '') {return;}
          
          if (href.indexOf("http://") <= 0)
          {
            document.location.href = href; 
          }
          else
          {
            window.open(dropDown.value,"","toolbar=yes,location=yes,status=yes,menubar=yes,scrollbars=yes,resizable=yes,width=800,height=600");
          }
        }
      }   
var xmenu=function(){
	var t=15,z=50,s=3,a;
	function dd(n){this.n=n; this.h=[]; this.c=[]}
	dd.prototype.init=function(p,c){
		a=c; var w=document.getElementById(p), s=w.getElementsByTagName('ul'), l=s.length, i=0;
		for(i;i<l;i++){
			var h=s[i].parentNode; this.h[i]=h; this.c[i]=s[i];
			h.onmouseover=new Function(this.n+'.st('+i+',true)');
			h.onmouseout=new Function(this.n+'.st('+i+')');
		}
	}
	dd.prototype.st=function(x,f){
		var c=this.c[x], h=this.h[x], p=h.getElementsByTagName('a')[0];
		clearInterval(c.t); c.style.overflow='hidden';
		if(f){
			p.className+=' '+a;
			if(!c.mh){c.style.display='block'; c.style.height=''; c.mh=c.offsetHeight; c.style.height=0}
			if(c.mh==c.offsetHeight){c.style.overflow='visible'}
			else{c.style.zIndex=z; z++; c.t=setInterval(function(){sl(c,1)},t)}
		}else{p.className=p.className.replace(a,''); c.t=setInterval(function(){sl(c,-1)},t)}
	}
	function sl(c,f){
		var h=c.offsetHeight;
		if((h<=0&&f!=1)||(h>=c.mh&&f==1)){
			if(f==1){c.style.filter=''; c.style.opacity=1; c.style.overflow='visible'}
			clearInterval(c.t); return
		}
		var d=(f==1)?Math.ceil((c.mh-h)/s):Math.ceil(h/s), o=h/c.mh;
		c.style.opacity=o; c.style.filter='alpha(opacity='+(o*100)+')';
		c.style.height=h+(d*f)+'px'
	}
	return{dd:dd}
}();

function move(){
	$("#nav").ready(function ()
		{
			$(this).find('#nav .products').clone().appendTo("#fNav .columnA");
			$(this).find('#nav .plan,#nav .refdata,#nav .business').clone().appendTo("#fNav .columnB");
			$(this).find('#nav .profile,#nav .recruit').clone().appendTo("#fNav .columnC");
			$(this).find('#nav .other').clone().appendTo("#fNav .columnD");
			//gNavの処理
			$("#gNav>ul").remove();
			$(this).find('#nav>ul').clone().appendTo("#gNav");
			//sNavの処理
			$("body[id]").each(function(){
				var id = $(this).attr("id");
//				console.log(id);
				$("#sNav>ul").remove();
				if(id=="other") {
					
					$(this).find('#nav li.' + id).clone().appendTo("#sNav").wrapAll("<ul></ul>");
				}
				else if(id=="news") {
					$(this).find('#nav .' + id + '>ul').clone().appendTo("#sNav");
					$(this).find('#nav li.' + id).clone().prependTo("#sNav>ul").find("ul").remove();
				}
				else {
					$(this).find('#nav .' + id + '>ul').clone().appendTo("#sNav");
				}
			});
			$("#nav").remove();
		});
		deleteList();
		gNav();
		sNav();
};

function deleteList(){
	$("body").ready(function ()
		{
			$(this).find("#gNav li.NOgNav,#gNav ul.level2,#fNav .NOfNav,#fNav ul.level3").remove();
		});
};
function gNav(){
	$("#gNav").ready(function ()
		{
			$(this).find("#gNav li.plan a").addClass("twoLine").html("クレーン<br>設置計画");
			$(this).find("#gNav li.refdata a").addClass("twoLine").html("クレーン<br>参考資料");
			$(this).find("#gNav li.business a").addClass("twoLine").html("各種<br>サービス");
		});
};
function sNav(){
	$("#sNav").ready(function ()
		{
			$(this).find("#sNav li.NOsNav").remove();
		});
		pulldown();
};



function pulldown(){
	
/*	$("#sNav>ul>li").each(function ()
		{
			$("#sNav>ul>li:has('br')").addClass("twoLine");
			
			$(this).find("a").each(function() {
				var txt = $(this).html();
				$(this).html(txt.replace('ｸﾗｲﾐﾝｸﾞｸﾚｰﾝ ','クライミングクレーン<br>'))
			});	
		});
	$("#sNav>ul>li:not(:has('>ul'))").each(function ()
		{		
			var height = $(this).find("a").height();
			if(height >="35") {
				$(this).addClass("twoLineNoAllow");
			}
//			console.log(height);
		});
	$("#sNav>ul>li:has('>ul')").each(function ()
		{		
			var height = $(this).find("a").height();
			if(height >="35") {
				$(this).addClass("twoLine").removeClass("twoLineNoAllow");
			}
		});*/
	
	$('#sNav :first-child').addClass('firstChild');
	$('#sNav :last-child').addClass('lastChild');
	
	$('#sNav>ul>li:has(ul)').each(function () {
		$(this).each(function(){
			$(this).addClass('tree');
			//var indexhref = $(this).find("a").attr("href");
			//console.log("indexhref : " + indexhref);
			//$(this).find(">ul").prepend('<li class="myIndex"><a href="' + indexhref + '">一覧</a></li>');
		});	
		/*$(this).find(">a").each(function(){
			$(this).replaceWith($(this).html());
		});	*/
	});
	
	$("body[id]").each(function(){
		var id = $(this).attr("id");
//		console.log(id);
		$('#gNav li.' + id).addClass("currentCategory");
	});
	
	var shrUrl = $(".header h1 a").attr("href").replace('index.html', '');
	var filename = location.href.split("#")[0].split("?")[0].split("/").reverse()[0];
	var filename2 = location.href.split("#")[0].split("?")[0].split("/").reverse()[0].replace('.html','');
//		console.log("shrUrl is " + shrUrl);
//		console.log("filename is " + filename);
//		console.log("filename2 is " + filename2);
	if (shrUrl == "../") {
		var now = location.href.split('/');
		//親のディレクトリ取得
		var category = now.slice(now.length-2,now.length-1);
//		console.log("category is " + category);
		$("#sNav li." + filename2).addClass("currentSubCategory");
	}
	else if (shrUrl == "") {
		$("#sNav li." + filename2).addClass("currentSubCategory");
	}
	else if (shrUrl == "../../") {
		var now = location.href.split('/');
		//親のディレクトリ取得
		var subcategory = now.slice(now.length-2,now.length-1);
		var category = now.slice(now.length-3,now.length-2);
		var rootUrl = (category + "/" + subcategory + "/" + filename);
		$('#sNav>ul>li.' + subcategory).each(function () {
			$(this).addClass('active').removeClass('noactive')
			$(this).find(">ul").show();
			$(this).find(">ul>li." + subcategory + ">a").addClass('current');
		});	
//		console.log("now is " + now);
//		console.log("subcategory is " + subcategory);
//		console.log("category is " + category);
//		console.log("rootUrl is " + rootUrl);
//		console.log("indexUrl is " + indexUrl);
		var indexUrl = $("#sNav>ul>li." + subcategory + ">ul>li>a").attr("href").replace("../","");
		if (indexUrl == rootUrl) {
			$("#sNav>ul>li." + subcategory + " .myIndex a").addClass("current");
		}
		if (indexUrl == rootUrl) {
			$("#sNav>ul>li." + subcategory + " .myIndex a").addClass("current");
		};
		$("#sNav li." + subcategory).each(function(){
//		console.log("subcategory is " + subcategory);
			if ($(this).is(".twoLine")) {
				$(this).addClass("currentSubCategorytwoLine");
			}
			else {
				$(this).addClass("currentSubCategory");
			};
		})

	}
	else if (shrUrl == "") {
	}
	
	if(_ua.ltIE6){
		$("body").ready(function ()
		{
			$("#sNav>ul>li:not(.active)>ul").hide();
		});
	}else if(_ua.ltIE7){
		$("body").ready(function ()
		{
			$("#sNav>ul>li:not(.active)>ul").hide();
		});
	}else {
	}
	$("body #sNav>ul>li:not(.twoLine)").click(function ()
		{
			if($($(this).find("ul")).css("display")=="none"){
				$("#sNav>ul>li").addClass('noactive').removeClass('active');
				$(this).addClass('active').removeClass('noactive');
				$("#sNav>ul>li>ul").slideUp();
				$(this).find("ul").slideDown();
			}
			else if($($(this).find("ul")).css("display")!="none"){
				$(this).addClass('noactive').removeClass('active');
				$(this).find("ul").slideUp();
			}
		});
	$("body #sNav>ul>li.twoLine").click(function ()
		{
			if($($(this).find("ul")).css("display")=="none"){
				$("#sNav>ul>li").addClass('noactive').removeClass('active').removeClass('activeTwoLine');
				$(this).addClass('activeTwoLine').removeClass('noactive');
				$("#sNav>ul>li>ul").slideUp();
				$(this).find("ul").slideDown();
			}
			else if($($(this).find("ul")).css("display")!="none"){
				$(this).addClass('noactive').removeClass('activeTwoLine');
				$(this).find("ul").slideUp();
			}
		});
};
