
(function($) {
	$(function(){
		$.ababaizer.Depth();		// 階層取得
		$.ababaizer.TableWidth();	// テーブル幅自動調整
		$.ababaizer.ColumnSize();	// カラムサイズ自動調整
		$.ababaizer.BackToTop();	// 下層ページは戻る追加
		$.ababaizer.EasingScroll();	// アンカーをイージングスクロール
		$.ababaizer.ExternalLink();	// 外部リンクは強制新規窓
		$.ababaizer.Tab();			// dl.tab にタブ機能
		$.ababaizer.Slideshow();	// dl.slideshow にスライドショー機能 .prev .next .btn .btn_current .count
		$.ababaizer.CurrentPage();	// カレントページへのリンクにcurrentクラス付与
		$.ababaizer.HeadingBG();	// id付きのリンク・見出しに背景画像追加
	});
	var depth_str = "./";
	
	$.ababaizer = {
		// 階層取得
		Depth:function(){
			var url_depth = 0;
			if( -1<location.href.indexOf("sample",0) ){
				url_depth = location.href.split('/sample/')[1].split('/').length-2;
			}else if( location.href.indexOf("http",0)==0 ){
				url_depth = location.href.split('/').length-3;
			}else{
				url_depth = location.href.split('/HTML/')[1].split('/').length-1;
			}
			for( var i=0 ; i<url_depth ; i++ ){
				depth_str += "../";
			}
		},
		// テーブル幅自動調整
		TableWidth:function(){
			$("table").each( function(){
				if( $(this).css("float")=="none" )
					$(this).css( {"width":$(this).parent().width()-$(this).css("marginLeft").replace("px","")-$(this).css("marginRight").replace("px","")+"px"} );
			} );
		},
		// カラムサイズ自動調整
		ColumnSize:function(){
			$("ul[class*=column]").each( function(){
				var classes = $(this).attr("class").split(" ");
				var cols = 1;
				for( var i=classes.length-1 ; 0<=i ; i-- )
					if( classes[i].match(/column/) )
						cols = classes[i].replace("column","");
				var w = $(this).width()/cols;
				$(this).children("li").css( {"width":w+"px"} );
			} );
		},
		// 下層ページは戻る追加
		BackToTop:function(){
			$("body:not(.home) #section div.article").append("<p class=\"back_to_top\"><a href=\"#top\">▲ページトップへ戻る</a></p>");
		},
		// アンカーをイージングスクロール
		EasingScroll:function(){
			$("a[href*=\"#\"]").easingScroll();
		},
		// 外部リンクは強制新規窓
		ExternalLink:function(){
			$("a[href*=\"http\"]").attr("target","_blank");
		},
		// タブ機能
		Tab:function(){
			$("dl.tab").each( function(tab_index){
				var current = 0;
				var dt = $(this).children("dt");
				var dd = $(this).children("dd");
				// 初期タブ
				$(dd).appendTo(this).css( {margin:0,padding:0,display:"none",opacity:"0"} );
				$(dt).eq(0).addClass("current");
				$(dd).eq(0).css( {display:"block",opacity:"1"} );
				
				// クリック時
				$(dt).each( function(btn_index){
					$(this).css( {cursor:"pointer"} );
					$(this).click( function(){
						if( current==btn_index )return;
						var before = current;
						$(dt).eq(current).removeClass("current");
						$(this).addClass("current");
						$(dd).eq(current).animate({opacity:"0"},100,"linear",function(){
							$(dd).eq(before).css({display:"none"});
							$(dd).eq(btn_index).css({display:"block"}).animate({opacity:"1"},100,"linear");
						});
						current = btn_index;
					} );
				} );
			});
		},
		// スライドショー機能
		Slideshow:function(){
			$("dl.slideshow").each( function(){
				var current = 0;
				var max = $(this).children("dt").length;
				var dt = $(this).children("dt");
				var dd = $(this).children("dd");
				var w = $(this).width();
				var h = $(this).height();
				var ml = Math.floor( $(this).css("marginLeft").replace("px","") );
				$(this).css( {
					"position":"relative",
					"overflow":"hidden"} );
				// 合計
				var count = document.createElement("span");
				$(count)
					.text(current+1+" / "+max)
					.addClass("count")
					.css( {"position":"absolute","right":"0","top":h+"px"} );
				
				// ボタンたち
				var btns = document.createElement("div");
				$(btns).css({"position":"absolute","width":w+ml*2+"px","top":h+"px","text-align":"center"});
				for( var i=0 ; i<max ; i++ ){
					var btn = document.createElement("span");
					$(btn)
						.text("○")
						.addClass("btn")
						.css({"cursor":"pointer"})
						.click( function(photo_index){
							return function(){
								$.ababaizer.change_slide(btns,count,dt,dd,max,current,photo_index,w);
								current = photo_index;
							};
						}(i) );
					$(btns).append(btn);
				}
				$(btns).children().eq(current).text("●").addClass("btn_current");
				// 戻る
				var prev = document.createElement("span");
				$(prev)
					.text("＜")
					.addClass("prev")
					.click( function(){
						if( current==0 )return;
						$.ababaizer.change_slide(btns,count,dt,dd,max,current,current-1,w);
						current--;
					} )
					.css({"cursor":"pointer"});
				$(btns).prepend(prev);
				// 進む
				var next = document.createElement("span");
				$(next)
					.text("＞")
					.addClass("next")
					.click( function(){
						if( current==max-1 )return;
						$.ababaizer.change_slide(btns,count,dt,dd,max,current,current+1,w);
						current++;
					} )
					.css({"cursor":"pointer"});
				
				$(btns).append(next);
				
				$(this).wrap("<div></div>");
				
				$(this).parent()
					.css({
						"position":"relative",
						"marginBottom":10+"px",
						"width":w+ml+"px",
						"height":h+20+"px",
						"overflow":"visible"})
					.append(count)
					.append(btns);
				
				$(dt).each( function(photo_index){
					$(this).css( {
						"position":"absolute",
						"width":w+"px",
						"left":photo_index*w+"px",
						"top":0,
						"textAlign":"center",
						"margin":0} );
					$(this).children().css({"margin":"0 auto"});
				} );
				$(dd).each( function(photo_index){
					$(this).css( {
						"position":"absolute",
						"width":w+"px",
						"left":photo_index*w+"px",
						"bottom":0,
						"textAlign":"center",
						"margin":0} );
					$(this).children().css({"margin":0});
				} );
			} );
		},
		change_slide:function(btns,count,dt,dd,max,prev,current,span){
			$(btns).children().eq(1+prev).text("○").addClass("btn").removeClass("btn_current");
			$(btns).children().eq(1+current).text("●").addClass("btn_current").removeClass("btn");
			$(count).text(current+1+" / "+max);
			$(dt).each( function(photo_index){	$(this).stop().animate( {"left":(photo_index-current)*span+"px"} );	} );
			$(dd).each( function(photo_index){	$(this).stop().animate( {"left":(photo_index-current)*span+"px"} );	} );
		},
		// カレントページへのリンクにcurrentクラス付与
		CurrentPage:function(){
			var url_arr = location.href.split('/');
			$("a").each( function(){
				var href_arr = $(this).attr("href").split('/');
				var directory = href_arr[ href_arr.length-2 ];
				for( var i=url_arr.length-1 ; 0<=i ; i-- ){
					if( 0<=url_arr[i].indexOf(directory,0) ){
						$(this).addClass("current");
					}
				}
			} );
		},
		// id付きのリンク・見出しに背景画像追加
		HeadingBG:function(){
			$("#header #header_nav li a[id]").each(function(){
				var img_path = depth_str+"common_images/"+$(this).attr("id")+".png";
				$.ababaizer.set_img( this , img_path , 1.0 , 0.5 );
			});
			$("#header #header_nav2 li a[id]").each(function(){
				var img_path = depth_str+"common_images/"+$(this).attr("id")+".png";
				$.ababaizer.set_img( this , img_path , 1.0 , 0.5 );
			});
			$("#aside a[id]").each(function(){
				var img_path = depth_str+"common_images/"+$(this).attr("id")+".png";
				$.ababaizer.set_img( this , img_path , 0.5 , 1.0 );
			});
			$("#aside h2[id]").each(function(){
				var img_path = depth_str+"common_images/h2_"+$(this).attr("id")+".png";
				$.ababaizer.set_img( this , img_path , 1.0 , 1.0 );
			});
			$("#section h2[id],#section h3[id],#section h4[id]").each(function(){
				var img_path = "./images/"+$(this).get(0).tagName.toLowerCase()+"_"+$(this).attr("id")+".png";
				$.ababaizer.set_img( this , img_path , 1.0 , 1.0 );
			});
		},
		// 画像セット
		set_img:function(target,img_path,w,h){
			var img = new Image();
			$(img).load( function(){
				var size = $.ababaizer.img_true_size( img );
				$(target).css({"width":size.width*w+"px","height":size.height*h+"px"});
				img = null;
			} );
			img.src = img_path;
			$(target).css({"backgroundImage":"url("+img_path+")","display":"block","textIndent":"-9999px"});
		},
		// img_true_size
		// http://dogmap.jp/2009/06/17/javascript-image-natural-size-2/
		img_true_size:function(image){
			var w = image.width ,
				h = image.height ;
			
			// for Firefox, Safari, Chrome
			if ( typeof image.naturalWidth !== 'undefined' ){
				w = image.naturalWidth;
				h = image.naturalHeight;
			}
			// for IE
			else if( typeof image.runtimeStyle !== 'undefined' ) {
				var run = image.runtimeStyle;
				var mem = { w: run.width, h: run.height };  // keep runtimeStyle
				run.width  = "auto";
				run.height = "auto";
				w = image.width;
				h = image.height;
				run.width  = mem.w;
				run.height = mem.h;
			}
			// for Opera
			else{
				var mem = { w: image.width, h: image.height };  // keep original style
				image.removeAttribute("width");
				image.removeAttribute("height");
				w = image.width;
				h = image.height;
				image.width  = mem.w;
				image.height = mem.h;
			}
			
			return {width:w, height:h};
		}
	};
	
})(jQuery);

