// 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);





jQuery(function($) {
	
	
	// FIXHEIGHT
	$(".fixHeight").fixHeight();


	// CHECK NAVI
	var cat = location.href.split('#')[0].split('/').reverse()[1];
	var href = cat+'/'+(location.href.split('#')[0].split('/').reverse()[0]);
	var postfix = '_o';
//	alert(href);
	
	if (href=="electronics/index.html" || href=="functions/index.html" || href=="international/index.html" || href=="photovoltaic/index.html" ){// use rollover state when in business page
	//alert(href);
			$("#gnavi3 a").wrap("<span class='active'></span>")
			.before($("#gnavi3 a").childNodes)
			.remove();
	}
			
	$('#gnavi a , #hnavi a').each(function() {
//			var theId = $(this).parent().attr('title');
			var theCat = this.href.split('#')[0].split('/').reverse()[1];
			var theHref = theCat+'/'+(this.href.split('#')[0].split('/').reverse()[0]);
			

		if (href == theHref){
		$(this)
			 .wrap("<span class='active'></span>")
			 .before(this.childNodes)
			 .remove();
//		}else if(theId == cat) {
//			$(this).addClass('current');
		}
	});
		$('#snavi a').each(function() {
//			var postfix = '_o';
//			var theId = $(this).parent().attr('title');
			var theCat = this.href.split('#')[0].split('/').reverse()[1];
			var theHref = theCat+'/'+(this.href.split('#')[0].split('/').reverse()[0]);
			
		if (href == theHref){
			var img = $(this).children();
			var src = img.attr('src');
			var src_on = src.substr(0, src.lastIndexOf('.'))
				   + postfix
				   + src.substring(src.lastIndexOf('.'));
			if (href != "recruit/recruit.html" && href != "company/outline.html"){// exclude recruit/index page and english/company/index page
				img.attr('src', src_on);
			}
			$(this)
			 .before(this.childNodes)
			 .remove();
		}
	});

	
	
	
	
	// HOVER
	$(".hover,#domestic_network a img,#overseas_network 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);
			}
		);
	});
	
	


	// POPUP WINDOW  put .popup on <a>
	$(".popup_h280").click(function(){
			window.open(this.href,"pop","width=300,height=280,resizable=yes,scrollbars=yes");
			return false;
	});
	$(".popup_h380").click(function(){
			window.open(this.href,"pop","width=300,height=380,resizable=yes,scrollbars=yes");
			return false;
	});
	$(".popup_w600_h600").click(function(){
			window.open(this.href,"pop","width=600,height=600,resizable=yes,scrollbars=yes");
			return false;
	});
	$(".popup_w600_h400").click(function(){
			window.open(this.href,"pop","width=600,height=400,resizable=yes,scrollbars=yes");
			return false;
	});
	
	
	
	
	// CLOSE POPUP WINDOW
	$(".bt_close").click(function(){
		close();
	});
	
	// CLOSE POPUP WINDOW
	$(".opener").click(function(){
		close();
		window.opener.location.href=$(this).attr('href');
		return false;
	});
	
	
	
});




