(function($) {
	jQuery.extend({
		yuden: {
			saveCookie: function(fontSize) {
				jQuery('#contents, #footer').css('font-size', fontSize);
				jQuery.cookie('font_size_cookie', null, {path: '/ut/'});
				jQuery.cookie('font_size_cookie', '', {path: '/ut/', expires: -1});
				jQuery.cookie('font_size_cookie', fontSize, {path: '/ut/', expires: 7});
	
				var pathname = location.pathname;
				var target = pathname.match(/search/);
				if(target) {
					$.adjustTabs();
				}
			}
		},
	
		subData : [],
		crumbsList : [],
		irregular : [],
		curHead : '',
		curPath : '',
		curPageTarget : '',
		subNo : 0,
		target : '',
		navi: {
			//crumb
			crumb: function() {
				var pathname = location.pathname;
				var dirArray = [];
				var head = '/';
				var path = '';
				var dirpath = '';
				var target = '';
				var dirname = '/';
	
				if(pathname) {
					var cutPosition = pathname.length;
					path = pathname.slice(3, pathname.length);
					if(!pathname.match(/\.[a-z]+$/)) {
						cutPosition = -1;
					}
					dirpath = pathname.slice(4, cutPosition);
					dirArray = dirpath.split('/');
					head += dirArray[0]+'/';
					$.curHead = head;
					$.curPath = path;
					if(dirArray[1]) {
						$.curPageTarget = '/' + dirArray[0] + '/' + dirArray[1] + '/';
					} else {
						$.curPageTarget = $.curHead;
					}
	
					//set target
					if(path.match(/\/company\/csr\/.+$/)) {
						target = '/company/csr/';
					} else if(path.match(/\/company\/softball\/.+$/)) {
						target = '/company/softball/';
					} else {
						target = head;
					}
					$.target = target;
	
					//build crumbs array
					for(var i = 0; i < dirArray.length; i++) {
						for(var level = 0; level <= i; level++) {
							dirname += dirArray[level];
							if(!dirname.match(/\.[a-z]+$/)) {
								dirname += '/';
							}
						}
						$.crumbsList[i] = dirname;
						dirname = '/';
					}
				}
	
				$.get('/ut/common/csv/directories.csv',function(data){
					var csv = $.csv()(data);
					var data = [];
					var dirname = '/';
					var crumbItem = '<a href="http://www.t-yuden.com/">HOME</a>';
					var subNo = 0;
					var crumbNum = 0;
	
					$(csv).each(function(i){
						if(this[1]) {
							data[this[1]] = {
								'name':this[0]
							};
							if(this[1].match('^'+target)) {
								var residual = '';
								var num = [];
								var numSize = 0;
	
								residual = this[1].replace(target, '');
								var work = residual.slice(1, residual.length - 1);
								num = work.split('/');
								numSize = num.length;
								if(numSize < 3) {
									$.subData[subNo] = {
										'path': this[1],
										'name' : this[0],
										'imgPath':this[2],
										'childPath':this[3],
										'targetBlank':this[4],
										'level' : numSize
									};
									for(var i = 0; i < $.crumbsList.length; i++) {
										if($.crumbsList[i] == this[1] && this[3] != '0') {
											$.irregular[this[1]] = this[3];
										}
									}
									subNo++;
								}
								$.subNo = subNo;
							}
						}
					});
	
					//build crumb
					if(!($.curPath.match(/\/recruit\//)
						|| $.curPath.match(/\/softball\//)
						|| $.curPath.match(/\/releasesnews\//)
						|| $.curPath.match(/\/new\//)
						|| $.curPath.match(/\/productnews\//)
						|| $.curPath.match(/\/blog\/topnews\//)	
						|| $.curPath.match(/\/newsannouncement\//)
						|| $.curPath.match(/\/product\/news\//)
						|| $.curPath.match(/\/release\//)
						|| $.curPath.match(/\/info\//)
						|| $.curPath == '/producttop/'
						)) {
	
						for(var i = 0; i < dirArray.length; i++) {
							for(var level = 0; level <= i; level++) {
								dirname += dirArray[level];
								if(!dirname.match(/\.[a-z]+$/)) {
									dirname += '/';
								}
							}
							if(data[dirname]) {
								if(i < dirArray.length - 1) {
									if($.irregular[dirname]) {
										crumbItem += ' > <a href="/ut'+$.irregular[dirname]+'">'+data[dirname].name+'</a>';
									} else {
										crumbItem += ' > <a href="/ut'+dirname+'">'+data[dirname].name+'</a>';
									}
								} else {
	
									crumbItem += ' > '+data[dirname].name;
								}
								dirname = '/';
							}
							else {
								dirname = '/';
							}
						}
						$('#path p').html(crumbItem);
	
						if(!($.curPath.match(/\/privacy\//) || $.curPath.match(/\/about\//) || $.curPath.match(/\/sitemap\//) || $.curPath.match(/\/contact\//) || $.curPath.match(/\/notice\//))) {
							$.navi.subNavi();
						}
					}
				});
	
			},
	
			//build sub navigation
			subNavi: function() {
	
				if($.subData[0]) {
					var region = '/ut';
	
					var titleImagePath = ''
					var subNavi = $('<div>');
					var menu = $('<div>');
					var title = $('<p>');
					var ulLevel1 = $('<ul>');
					var ulLevel2 = $('<ul>');
					var parentLi = null;
					var hasUlLevel2 = true;
	
					subNavi.attr('id', 'snavi');
					subNavi.addClass('section');
					menu.addClass('menu');
					ulLevel1.addClass('level1');
	
					subNavi.append(menu);
					menu.append(ulLevel1);
	
					title = $('<p class="title"><a href="../../ut/common/js/' + region + $.subData[0].path+'"><img src="../../ut/common/js/'+$.subData[0].imgPath+'" alt="'+$.subData[0].name+'" /></a></p>');
					subNavi.prepend(title);
	
					for(var i = 1; i < $.subNo; i++) {
						 if($.subData[i].level == 1)  {
							var li = $('<li>');
							var a = $('<a>');
							var p = $('<p>');
							p.addClass('item');
							ulLevel1.append(li);
							li.append(p);
							p.append(a);
	
							if($.subData[i].path == $.curPath) {
								li.addClass('curTop');
								p.addClass('first link');
							} else if($.subData[i].path.match($.curPageTarget)) {
								if($.curPath.match($.subData[i].path)) {
									li.addClass('cur');
								}
							}
							parentLi = li;
							a.text($.subData[i].name);
	
							if($.subData[i].targetBlank != 0) {
								a.attr('href', $.subData[i].targetBlank).attr('target', '_blank');
							} else {
								if($.subData[i].childPath != 0) {
									a.attr('href', region + $.subData[i].childPath);
								} else {
									a.attr('href', region + $.subData[i].path);
								}
							}
	
						} else if($.subData[i].level == 2)  {
							var li = $('<li>');
							var a = $('<a>');
							var p = $('<p>');
	
							var hasCur = false;
							if(parentLi.attr('class')) {
								hasCur = parentLi.attr('class').match(/^cur/);
							}
							if(hasCur) {
								if(hasUlLevel2) {
									ulLevel2.addClass('level2');
									parentLi.append(ulLevel2);
									hasUlLevel2 = false;
								}
								ulLevel2.append(li);
								li.append(p);
	
	
								if($.curPath.match($.subData[i].path)) {
									li.addClass('curPage');
								}
	
								p.append(a);
								p.addClass('item');
								a.text($.subData[i].name);
	
								if($.subData[i].targetBlank != 0) {
									a.attr('href', $.subData[i].targetBlank).attr('target', '_blank');
								} else {
									if($.subData[i].childPath != 0) {
										a.attr('href', region + $.subData[i].childPath);
									} else {
										a.attr('href', region + $.subData[i].path);
									}
								}
							}
						}
					}
	
					$('#sub #snavi').empty();
					subNavi.insertAfter('#sub .print');
					$('#snavi ul:not(.level2) p:last').addClass('last');
				}
			}
		}
	});
	
	$(document).ready(function() {
		try {
			document.execCommand('BackgroundImageCache', false, true);
		} catch(e) {}
		$.navi.crumb();
	
		//文字サイズクッキー
		if($.cookie('font_size_cookie')) {
			var fontSize = $.cookie('font_size_cookie');
			if(fontSize != '100%') {
				fontSize = Math.floor(fontSize);
				$('.textSizeUp img').attr('src', '/ut/common/images/btn_zoom_on.jpg');
				$('.textSizeNormal img').attr('src', '/ut/common/images/btn_normal.jpg');
			} else {
				fontSize = '100%';
				$('.textSizeUp img').attr('src', '/ut/common/images/btn_zoom.jpg');
				$('.textSizeNormal img').attr('src', '/ut/common/images/btn_normal_on.jpg');
			}
			$('#contents, #footer').css('font-size', fontSize);
		}
	
		//印刷ボタン
		$('.print .link a').click(function() {
			window.print();
		});
	
		//文字サイズ変更ボタン
		$('.textSizeNormal, .textSizeUp').hover(
			function() {
				$(this).css("cursor","pointer");
			},
			function() {
				$(this).css("cursor","default");
			}
		);
		$('.textSizeUp').click(function() {
			var newFontSize = parseInt($('#contents, #footer').css('font-size')) * 1.2;
			if (newFontSize < 15) {
				$('.textSizeUp img').attr('src', '/ut/common/images/btn_zoom_on.jpg');
				$('.textSizeNormal img').attr('src', '/ut/common/images/btn_normal.jpg');
				$.yuden.saveCookie(newFontSize);
			}
		});
		$('.textSizeNormal').click(function() {
			var newFontSize = '100%';
			$('.textSizeUp img').attr('src', '/ut/common/images/btn_zoom.jpg');
			$('.textSizeNormal img').attr('src', '/ut/common/images/btn_normal_on.jpg');
			$.yuden.saveCookie(newFontSize);
		});
	
		//ヘッダー検索ボックスロールオーバ
		$('.search p').hover(
			function() {
				$(this).addClass('hover');
			},
			function() {
				$(this).removeClass('hover');
			}
		);
		$('.search p input#headerSearchField').click(function() {
			if($(this).val() == 'Site Search') {
				$(this).val('');
				$(this).css('color', '#FFF');
			}
		});
		$('.search p input#headerSearchField').blur(function() {
			if($(this).val() == ''){
				$(this).val('Site Search');
				$(this).css('color', '#b6b6b6');
			}
		});
	
		//ヘッダー言語選択
		var regionTimer = 0;
		$('#header #hnavi .selectRegion dl').hover(
			function() {
				var element = this;
				regionTimer = setTimeout(function() {
					$(element).addClass('hover');
					$('#header #hnavi .selectRegion dd').show();
				}, 700);
			},
			function() {
				clearTimeout(regionTimer);
				$(this).removeClass('hover');
				$('#header #hnavi .selectRegion dd').hide();
			}
		);
		$('#header #hnavi .selectRegion li a').hover(
			function() {
				$(this).parent().addClass('hover');
			},
			function() {
				$(this).parent().removeClass('hover');
			}
		);
	
	
		//グローバルナビ子項目クラス
		$('#gnavi ul.level3 li:has(ul)').addClass('hasChild');
	
		//グローバルナビロールオーバ
		var gnaviTimer = 0;
		$('#gnavi :not(.contact) div.child').parent().hover(
			function() {
				var element = this;
				gnaviTimer = setTimeout(function() {
					var bridgePosition = '-603px';
					var GroundMenuIndex = $('#gnavi :not(.contact) div.child').parent().index(element);
					if(GroundMenuIndex == 2) {
						bridgePosition ='-114px';
					}
					
					$(element).addClass('hover bridge');
					$('.child', element).show();
					$('ul.level2, ul.level3', element).addClass('cur');
					$('ul.level2, ul.level3', element).show();
					$('.btn', element).show();
					$('#gnavi .child > ul[style*=block] + .btn').show();
	
					$('.child', element).append($('<iframe>'));
					
					$('.child', element).css({
						'background-position': bridgePosition + ' top'
					});
					$('iframe', element).css({
						'display': 'block',
						'position': 'absolute',
						'z-index': $('.child ul', element).css('z-index') - 10,
						'left': 0,
						'top': '14px',
						'width': $('.child ul', element).width() + parseInt($('.child ul', element).css('padding-right')),
						'height': $('ul', element).height() + parseInt($('ul', element).css('padding-bottom')),
						'border': 'none'
					});
					$('.child ul:first', element).addClass('borders');
				}, 700);
			},
			function() {
				clearTimeout(gnaviTimer);
				$('.child', this).hide();
				$(this).removeClass('hover bridge');
				$('.child ul:first', element).removeClass('borders');
				$('ul.level2, ul.level3', this).mouseout(function() {
					$('ul.level2', this).removeClass('cur');
				});
				if($('.cur', this).attr('class') == 'level2 cur') {
					$('ul.level2', this).hide();
					$('#gnavi .child .btn').hide();
				}
				$('iframe', this).remove();
			}
		);
		$('#gnavi ul.level4 li').hover(
			function() {
				$(this).addClass('hover');
			},
			function() {
				$(this).removeClass('hover');
			}
		);
		$('#gnavi ul.level3 li').hover(
			function() {
				$(this).addClass('hover');
				$('ul.level4', this).addClass('cur');
				$('ul.level4', this).show();
				$('ul.level4 li:last-child', this).css({'border-bottom':'none'});
				if($(this).is('.hasChild')) {
					$(this).append($('<iframe>'));
					$('iframe', this).css({
						'display': 'block',
						'position': 'absolute',
						'z-index': 10,
						'right': $('ul.level4', this).css('right'),
						'top': 0,
						'width': parseInt($('ul.level4', this).width()) + 2 + 'px',
						'height': $('ul.level4', this).height() + parseInt($('ul.level4', this).css('padding-bottom')),
						'border': 'none'
					});
				}
			},
			function() {
				$(this).removeClass('hover');
				$('ul.level4', this).hide();
				$('iframe', this).remove();
			}
		);
		$('#gnavi ul.level2 li').hover(
			function() {
				$(this).addClass('cur');
			},
			function() {
				$(this).removeClass('cur');
			}
		);
		$('#gnavi .child .btn img, #subSearchBtn').hover(
			function() {
				$(this).css('cursor', 'pointer');
			},
			function() {
				$(this).css('cursor', 'default');
		});
		$('#gnavi .child .btn img').click(function() {
			$('#gnavi ul.level2').hide();
			$(this).parent().hide();
			$('iframe').remove();
		});
//		$('#gnavi .contact').hover(
//			function() {
//				$(this).addClass('hover');
//			},
//			function() {
//				$(this).removeClass('hover');
//		});
	
	
		//サブナビロールオーバ
		$('#snavi ul li p').live('hover', function(e) {
			if(e.type == 'mouseover' || e.type == 'mouseenter'){
				$(this).parent().addClass('hover');
			}else{
				$(this).parent().removeClass('hover');
			}
		});
	
		//sub search
		$('#subSearchField').click(function() {
			if($(this).val() == 'Part Number') {
				$(this).val('');
				$(this).css('color', '#666');
			}
		});
		$('#subSearchField').blur(function() {
			if($(this).val() == ''){
				$(this).val('Part Number');
				$(this).css('color', '#999');
			}
		});
	
	
	
		//ページの先頭に戻る
		$.easing.easeOutQuad = function (x, t, b, c, d) {
			return -c *(t/=d)*(t-2) + b;
		}
		$('.pagetop a').click(function () {
			$('html,body').animate({ scrollTop: 0 }, 700, 'easeOutQuad');
			return false;
		});
	
		//Fancybox
		$('.fancybox').fancybox({
			'width':720,
			'height':620,
			'type':'iframe',
			'centerOnScroll':true
		});
	
		// フッタ 現在年表示
		var now = new Date();
		var year = now.getFullYear();
	
		$("#footer_year").html( year );
		$("#softball_foundation").html( (year-1984)+1 );
		$("#softball_consequence").html( (year-1987)+1 );
	});
 })(jQuery);

function wopen( jumpURL, winName, param ) {
	window.open( jumpURL, winName, param );
	return false;
}

