jQuery(function($) {
	
	
	// FIXHEIGHT
	$(".fixHeight").fixHeight();


	
	// HOVER
	$(".hover, .logo, .pagetop, .anchorlink a, .tab_contents a, .bannerlist a img").hover(
		function(){   // マウスオーバー
		$(this).fadeTo(1, 0.7);
		},
		function(){   // マウスアウト
		$(this).fadeTo(1, 1);
		}
	);
	
	
	
	
	// ROLLOVER
	var postfix = '_o';
	$('.rollover a img').not('[src*="'+ postfix +'."]').each(function() {
		var img = $(this);
		var src = img.attr('src');
		var src_on = src.substr(0, src.lastIndexOf('.'))
				   + postfix
				   + src.substring(src.lastIndexOf('.'));
		$('<img>').attr('src', src_on);
		img.hover(
			function() {
				img.attr('src', src_on);
			},
			function() {
				img.attr('src', src);
			}
		);
	});
	
	

	// TAB
	$(".tab_contents").not(":first").hide();
	$("#tab li").click(function() {
		if(! ($(this).hasClass("select")) ){
			var num = $("#tab li").index(this);
			$(".tab_contents").hide();
			/*fadeIn使用時VISTA&IE7で文字にジャギーが出るバグの対処*/
			var ua = navigator.userAgent;
			if (ua.match(/Win(dows )?NT 6\.0/) && navigator.appVersion.indexOf("MSIE 7") != -1) {
				// Windows VistaでIE7の時
				$(".tab_contents").eq(num).fadeIn('fast',function(){ this.style.removeAttribute("filter"); });
			}else{
				$(".tab_contents").eq(num).fadeIn('fast');
			};
			/*ここまで*/
			$("#tab li").removeClass('select');
			$(this).addClass('select')
		}
		/*location.href="#tab";*/
	});
	$("#tab li").hover(function(){
		if(! ($(this).hasClass("select")) ){
			$(this).css("cursor","pointer");
		};
	}, function(){
		$(this).css("cursor","default");
	});

	
	
	
	// POPUP WINDOW  put class="popup" and rel="width,height" on <a>
	$(".popup").click(function(){
			var wh=$(this).attr("rel").split(",");
			var w=wh[0];
			var h=wh[1];;
			var arg = "width="+w+",height="+h+",resizable=yes,scrollbars=yes"
			window.open(this.href,"pop",arg);
			return false;
	});
	
	
	
	
	
	// GNAV SLIDE MENU
	$(".gsubnav").hide();
	$(".gnav>li").hover(
		function(){
			$(this).find("ul").not(":animated").slideDown("fast");
		},
		function(){
			$(this).find("ul").not(":animated").slideUp("fast");
		}
	);
	
	
	// ANCHOR LINK EFFECT
	$('a[href^="#"]').click(function(){
     $('html,body').animate({ scrollTop: $($(this).attr("href")).offset().top }, 'slow','swing');
     return false;
	 });

	
	
});






// FIXHEIGHT

/*
 * fixHeight - jQuery Plugin
 * http://www.starryworks.co.jp/blog/tips/javascript/fixheightjs.html
 *
 * Author Koji Kimura @ STARRYWORKS inc.
 * http://www.starryworks.co.jp/
 * 
 * Licensed under the MIT License
 *
 */


(function($){
	
	var isInitialized = false;
	var parents = [];
	var textHeight = 0;
	var $fontSizeDiv;
	
	$.fn.fixHeight = function() {
		this.each(function(){
			var childrenGroups = getChildren( this );
			
			$.each( childrenGroups, function(){
				
				var $children = $(this);
				if ( !$children.filter(":visible").length ) return;
				
				var row = [];
				var top = 0;
				$children.each(function(){
					if ( top != $(this).position().top ) {
						$(row).sameHeight();
						row = [];
						top = $(this).position().top;
					}
					row.push(this);
				});
				if ( row.length ) $(row).sameHeight();
			});
			
			
		});
		init();
		return this;
	}
	
	$.checkFixHeight = function( i_force ) {
		if ( $fontSizeDiv.height() == textHeight && i_force !== true ) return;
		textHeight = $fontSizeDiv.height();
		$(parents).fixHeight();
	}
	
	$.fn.sameHeight = function() {
		var maxHeight = 0;
		this.css("height","auto");
		this.each(function(){
			if ( $(this).height() > maxHeight ) maxHeight = $(this).height();
		});
		return this.height(maxHeight);
	}
	
	function getChildren( i_parent ) {
		var $parent = $( i_parent );
		
		if ( $parent.data("fixHeightChildrenGroups") ) return $parent.data("fixHeightChildrenGroups");
		var childrenGroups = [];
		
		var $children = $parent.find(".fixHeightChild");
		if ( $children.length ) childrenGroups.push( $children );
		
		var $groupedChildren = $parent.find("*[class*='fixHeightChild']:not(.fixHeightChild)");
		if ( $groupedChildren.length ) {
			var classNames = {};
			$groupedChildren.each(function(){
				var a = $(this).attr("class").split(" ");
				var i;
				var l = a.length;
				var c;
				for ( i=0; i<l; i++ ) {
					c = a[i].match(/fixHeightChild[a-z0-9_-]+/i);
					if ( !c ) continue;
					c = c.toString();
					if ( c ) classNames[c] = c;
				}
			});
			for ( var c in classNames ) childrenGroups.push( $parent.find("."+c) );
		}
		
		if ( !childrenGroups.length ) {
			$children = $parent.children();
			if ( $children.length ) childrenGroups.push( $children );
		}
		
		$parent.data("fixHeightChildrenGroups", childrenGroups );
		parents.push( $parent );
		
		return childrenGroups;
	}
	
	
	function init() {
		if ( isInitialized ) return;
		isInitialized = true;
		$fontSizeDiv = $(document).append('<div style="position:absolute;left:-9999px;top:-9999px;">s</div>');
		setInterval($.checkFixHeight,1000);
		$(window).resize($.checkFixHeight);
		$.checkFixHeight();
		$(window).load( function(){ $.checkFixHeight(true); } );
	}
	
})(jQuery);


