$(function(){
	// ---------------------------
	//	for IE6 - ie6_common.js
	// ---------------------------
	if($.browser.msie && $.browser.version == 6){
		ie6_common();
	}

	// ---------------------------
	//	for IE6,IE7 - before after
	// ---------------------------
	if($.browser.msie && $.browser.version == 6 || $.browser.version == 7){
		$("div[class*='headingLevel']").prepend('<span class="before"><img src="/assets/images/bg/space.gif" alt="" /></span>').append('<span class="after"><img src="/assets/images/bg/space.gif" alt="" /></span>');
		$("div.box").prepend('<span class="before"><img src="/assets/images/bg/space.gif" alt="" /></span>').append('<span class="after"><img src="/assets/images/bg/space.gif" alt="" /></span>');
	}

	// ---------------------------
	//	tooltip
	// ---------------------------
	$('.tooltip').tooltip({
		path : '/assets/images/bg/',
		containment:'#main'
	});

	// ---------------------------
	//	add external class
	// ---------------------------
	$("div#main a[target='_blank']:not(:has(img)):not(a.noicon):not([href*='.pdf'])")
		.wrap('<span class="external"></span>')
		.after('<img src="/assets/images/icon/external.gif" />');
	
	// ---------------------------
	//	add pdf file class
	// ---------------------------
	$("a[href*='.pdf']:not(:has(img)):not(a.noicon)")
		.wrap('<span class="pdfFile"></span>')
		.after('<img src="/assets/images/icon/pdf.gif" alt="PDFファイル" />');

	// ---------------------------
	//	add border link image
	// ---------------------------
	$("img[src*='_thumb.gif']").addClass('cover');
	$("img[src*='_thumb.jpg']").addClass('cover');

	// ---------------------------
	//	print
	// ---------------------------
	$("div[class*='headingLevel'] ul.inline li a img[src*='_print_off.gif']").click(function(){
		print();
	});

	// ---------------------------
	//	stripe pattern
	// ---------------------------
	$('div#main .stripePattern > :nth-child(odd)').addClass("stripePattern-odd");
	$('div#main .stripePattern > :nth-child(even)').addClass("stripePattern-even");

	// ---------------------------
	//	height
	// ---------------------------
	if($.browser.safari) {	//webkitだけ、値が取得できない
		$(window).load(function(){
			colHeight();
			TextResizeDetector.TARGET_ELEMENT_ID = 'container'; 
			TextResizeDetector.addEventListener(colHeight);
		});
	}else {
		colHeight();
		TextResizeDetector.TARGET_ELEMENT_ID = 'container'; 
		TextResizeDetector.addEventListener(colHeight);
	}

	// ---------------------------
	//	$.fn.extend
	// ---------------------------
	$.fn.extend({

		// ---------------------------
		//	showExample
		// ---------------------------
		showExample : function(options) {
			var settings = $.extend({}, $.fn.showExample.defaults, options);
			
			return this.each(function() {
				var $t = $(this);
				var $input = $(".input", $(this).parent());
				var o = $.metadata ? $.extend({}, settings, $t.metadata()) : settings;

				// init
				if($input.val() == ""){
					$t.attr("style", "visibility: visible;");
				}else{
					$t.attr("style", "visibility: hidden;");
				}

				// click
				$t.click(function (){
					$input.focus();
				});

				// focus
				$input.focus(function (){
					if($input.val() == ""){
						$t.attr("style", "visibility: hidden;");
					}
				});

				// blur
				$input.blur(function (){
					if($input.val() == ""){
						$t.attr("style", "visibility: visible;");
					}
				});

			});
		},
		// ---------------------------
		//	setColHeight
		// ---------------------------
		'setColHeight' : function(_target,_num) {
			var settings = $.extend({
			                        	targ : _target,
			                        	num  : _num 
			}, $.fn.setColHeight.defaults);
			return this.each(function() {
				var $t = $(this);
				var o= $.metadata ? $.extend({}, settings, $t.metadata()) : settings;
				var i = 0;j = 0;
				var length;
				var newHeight = 0;
				$t.num = Number(Math.floor(o.num));
				
				var init = function() {
					length = $(o.targ,$t).length;
					if(!$t.num || $t.num <= 0) {
						$t.num = length;
					}
					$(o.targ,$t).css({height : 'auto'});
					setHeight(0);
				}
				
				var setHeight = function(count) {
					for(i=0; i<$t.num; i+=1) {
						var index = i + count * $t.num;
						if($(o.targ+':eq('+index+')',$t)) {
							var targetHeight = $(o.targ+':eq('+index+')',$t).height();
							if(newHeight < targetHeight){
								 newHeight = targetHeight;
							}
						}
					}
					for(j=0; j<$t.num; j+=1) {
						index = j + count * $t.num;
						if($(o.targ+':eq('+index+')',$t)) {
							$(o.targ+':eq('+index+')',$t).css({height : newHeight + 'px'});
						}
					}
					count += 1;
					newHeight = 0;
					if(count*$t.num < length) {
						setHeight(count);
					}
				}
					
				init();
			});
		}
	});
	// ---------------------------
	//	デフォルト設定
	//  + showExample()
	//  + setColHeight()
	// ---------------------------
	$.fn.showExample.defaults = {
	};
	$.fn.setColHeight.defaults = {
	};
	// ------------------------------------------------------
	//	colHeight()
	// ------------------------------------------------------
	function colHeight() {
		$("div.layout2col").setColHeight("div.box div.boxText",2);
		$("div.layout3col").setColHeight("div.box div.boxText",3);
		$("div.layout4col").setColHeight("div.box div.boxText",4);
		$("div.tabMenu").setColHeight("ul.inline li a");
	};
});
