var footerHeight = 0;
$(document).ready(function(){
	$('.onoff img,#header .lang img,#fnav1 img,.page_top .cont .l ul img,.page_top .cont .r img,h2 span.btn img,.page_company h2 img').each(function(ev){
			preload_img(this);
			$(this).mouseover(function(ev){
				img_on(ev.target);
			}).mouseout(function(ev){
				img_off(ev.target);
			});
	});
	
	
	if($('body.page_top').length){
		setInterval("doEqualHeight()",200);
	}

	$('.box_wpr dl').click(function(e){
		location.href = $(this).find('dt a').attr('href');
	});

	$('.box_wpr dl').mouseover(function(){
		$(this).addClass('hover');
	}).mouseout(function(){
		$(this).removeClass('hover');
	});

	var thisUrl=location.href;
	var hitFlg=0;
//	var file_url = thisUrl.substring(thisUrl.lastIndexOf("/")+1,thisUrl.length);
	$('#lnav li a').each(function(){
		var linkUrl=$(this).attr('href');
		if(linkUrl == thisUrl){
			$(this).parent().addClass("here");
			var pName=$(this).parent().parent().parent().get(0).tagName;
			pName=pName.toUpperCase();
			if(pName == "LI"){
				$(this).parent().parent().parent().addClass("here");
			}
			hitFlg=1;
		}
	}); 
	if(hitFlg == 0){
		urlArry=thisUrl.split("/");
		if((urlArry[3] == "company") && (urlArry[4] == "profile")){
			var check1data='/company/profile/';
			$('#lnav li a').each(function(){
				var linkUrl=$(this).attr('href');
				re = new RegExp(/\/company\/profile\/$/);
				if(linkUrl.match(re)){
					$(this).parent().addClass("here");
				}
			}); 
		}
	}

	$('a').each(function(idx){
		var url = $(this).attr('href');
		var mypdfCheck = url.lastIndexOf(".pdf");
		var mydoc1Check = url.lastIndexOf(".doc");
		var mydoc2Check = url.lastIndexOf(".docx");
		if((mypdfCheck != -1) || (mydoc1Check != -1) || (mydoc2Check != -1)){
			$(this).attr('target','_blank');
		}
	});
	var siteUri = 'http://www.takata-seiyaku.co.jp/';
	var externalLinks = $('a[href^="http://"]').not('a[href^="' + siteUri + '"]');
	externalLinks.click(function(){
		window.open($(this).attr('href'), '_blank');
		return false;
	});

});

function img_on(tgt){
	var src=$(tgt).attr('src');
	var newsrc=src.replace("_off","_on");
	$(tgt).attr('src',newsrc);
}

function img_off(tgt){
	var src=$(tgt).attr('src');
	var newsrc=src.replace("_on","_off");
	$(tgt).attr('src',newsrc);
}

function preload_img(tgt){
	var src=$(tgt).attr('src');
	var newsrc=src.replace("_off","_on");
	$('<img>').attr('src', newsrc);
}
function doEqualHeight() {

	if($('#footer').height() != footerHeight){
		footerHeight = $('#footer').height();
		resetVmTxt();
		$('.equal_height').each(function(ev){
			equalHeight($(this).find('.equal_height_cont'));
		});
		vmTxt();
	}
}

function equalHeight(group) {
	var tallest = 0;
	group.height('auto');
	group.each(function() {
		var thisHeight = $(this).height();
		if(thisHeight > tallest) {
			tallest = thisHeight;
		}
	});
	group.height(tallest);
}

function resetVmTxt() {
	$('.vmTxt').each(function(ev){
		$(this).css('position','relative');
		var tWidth = $(this).width();
		$(this).find('.vmTxt_cont').css('position','static').css('margin-top',0).width(tWidth);
	});
}

function vmTxt() {
	$('.vmTxt').each(function(ev){
		$(this).css('position','relative');
		var mt = $(this).find('.vmTxt_cont').height() / 2 * -1
		$(this).find('.vmTxt_cont').css('position','absolute').css('top','50%').css('margin-top',mt)
	});
}
