﻿/**
 * common.js
 *
 *  version --- 3.4
 *  updated --- 2011/07/12
 */


/* !stack ------------------------------------------------------------------- */
jQuery(document).ready(function($) {
	rollover();
	popWindow();
	localNav();
	globalNav();
	addCss();
});


/* !isUA -------------------------------------------------------------------- */
var isUA = (function(){
	var ua = navigator.userAgent.toLowerCase();
	indexOfKey = function(key){ return (ua.indexOf(key) != -1)? true: false;}
	var o = {};
	o.ie      = function(){ return indexOfKey("msie"); }
	o.fx      = function(){ return indexOfKey("firefox"); }
	o.chrome  = function(){ return indexOfKey("chrome"); }
	o.opera   = function(){ return indexOfKey("opera"); }
	o.android = function(){ return indexOfKey("android"); }
	o.ipad    = function(){ return indexOfKey("ipad"); }
	o.ipod    = function(){ return indexOfKey("ipod"); }
	o.iphone  = function(){ return indexOfKey("iphone"); }
	return o;
})();
/* !fxPrint ----------------------------------------------------------------- */
(function(){
	setCSS = function(){
		elem = document.createElement('link');
		elem.setAttribute('rel','stylesheet');
		elem.setAttribute('type','text/css');
		elem.setAttribute('media','print');
		elem.setAttribute('href','/common/css/fx_print.css');
		document.getElementsByTagName('head')[0].appendChild(elem);
	}
	if( isUA.fx() ) window.addEventListener("load",setCSS,false); 
})();
/* !init Smart Devices ------------------------------------------------------ */
(function (){
	var parentNode = document.getElementsByTagName('head')[0];
	var viewport = {
		android : 'width=480, user-scalable=yes, ,initial-scale=0.3125 maximum-scale=3',
		ipad    : 'width=1024',
		//iphone  : 'width=device-width,minimum-scale=1.0,maximum-scale=1.0,user-scalable=0'
		iphone  : 'width=device-width'
	}
	meta = document.createElement('meta');
	meta.setAttribute('name','viewport');

	if( isUA.android() ){
		meta.setAttribute('content',viewport.android);
		parentNode.appendChild(meta);
	}else if( isUA.ipad() ){
		meta.setAttribute('content',viewport.ipad);
		parentNode.appendChild(meta);
	}else if( isUA.ipod() || isUA.iphone() ){
		meta.setAttribute('content',viewport.iphone);
		parentNode.appendChild(meta);
		window.addEventListener('load', function(){ setTimeout(scrollTo, 100, 0, 1);}, false);
	}else{
	}
})();
/* !rollover ---------------------------------------------------------------- */
var rollover = function(){
var suffix = { normal : '-no.', over   : '-on.'}
	jQuery('a.over, img.over, input.over').each(function(){
		var a = null;
		var img = null;

		var elem = jQuery(this).get(0);
		if( elem.nodeName.toLowerCase() == 'a' ){
			a = jQuery(this);
			img = jQuery('img',this);
		}else if( elem.nodeName.toLowerCase() == 'img' || elem.nodeName.toLowerCase() == 'input' ){
			img = jQuery(this);
		}

		var src_no = img.attr('src');
		var src_on = src_no.replace(suffix.normal, suffix.over);

		if( elem.nodeName.toLowerCase() == 'a' ){
			a.bind("mouseover focus",function(){ img.attr('src',src_on); })
			 .bind("mouseout blur",  function(){ img.attr('src',src_no); });
		}else if( elem.nodeName.toLowerCase() == 'img' ){
			img.bind("mouseover",function(){ img.attr('src',src_on); })
			   .bind("mouseout", function(){ img.attr('src',src_no); });
		}else if( elem.nodeName.toLowerCase() == 'input' ){
			img.bind("mouseover focus",function(){ img.attr('src',src_on); })
			   .bind("mouseout blur",  function(){ img.attr('src',src_no); });
		}

		var cacheimg = document.createElement('img');
		cacheimg.src = src_on;
	});
};
/* !localNav ---------------------------------------------------------------- */
var localNav = function(){
	var navClass = document.body.className;
	var parent = jQuery("#lNavi");
	var routing = [];
	jQuery("ul ul", parent).hide();
	if( navClass.indexOf("lDef") > 0 ){
		routing[0] = navClass.match(/l[\d]+_[\d]+_[\d]+/);
		routing[1] = navClass.match(/l[\d]+_[\d]+/);
		routing[2] = navClass.match(/l[\d]+/);
		if( routing[0] ){ routing[0].push(routing[0][0].match(/[\d]+/g)) };
		if( routing[1] ){ routing[1].push(routing[1][0].match(/[\d]+/g)) };
		if( routing[2] ){ routing[2].push(routing[2][0].match(/[\d]+/g)) };
		if( routing[0] != null ){
			var e = jQuery("a.lNav"+routing[0][1][0]+'_'+routing[0][1][1]+'_'+routing[0][1][2], parent);
				e.addClass('current');
				e.parent().parent().show()
				 .parent().parent().show();
		}else if( routing[1] != null ){
			var e = jQuery("a.lNav"+routing[1][1][0]+'_'+routing[1][1][1], parent);
				e.addClass('current');
				e.next().show();
				e.parent().parent().show();
		}else if( routing[2] != null ){
			var e = jQuery("a.lNav"+routing[2][1], parent);
				e.addClass('current');
				e.next().show();
		}else{
		}
	}
}
/* !globalNav ---------------------------------------------------------------- */
var globalNav = function(){
	var urlList = document.location.href.split('/');
	var crCategory = (urlList[3]=='en')?urlList[4]:urlList[3];
	var categoryList = new Array('what-we-do','works','updates','career','contact');
	jQuery('ul#gNavi a').each(function(i){
		if(categoryList[i] == crCategory){
			jQuery(this).parent().addClass('current');
		}
	});
}

/* !popWindow --------------------------------------------------------------- */
var popWindow = function (){
	var param = null;
	// param[0] = width
	// param[1] = height
	// param[2] = window.name
	jQuery('a[class^="js_popup"], area[class^="js_popup"]').each(function(i){
		jQuery(this).click(function(){
			var w = null;
			param = jQuery(this).attr('class').match(/[0-9]+/g);
			// get window.name
			param[2] = window.name ? window.name+'_' : '';
			w = window.open(this.href, param[2]+'popup'+i,'width='+param[0]+',height='+param[1]+',scrollbars=yes');
			w.focus();
			return false;
		});
	});
}
/* !defFunc ----------------------------------------------------------------- */
var defFunc = (function(){
	Print = function(){ window.print(); return false;}
	Close = function(){ window.close(); return false;}
})();
/* !Addition Fitst & Last --------------------------------------------------- */
var addCss = (function(){
	jQuery('.section:first-child:not(:last-child)').addClass('first');
	jQuery('.section:last-child').addClass('last');
	jQuery('li:first-child:not(:last-child)').addClass('first');
	jQuery('li:last-child').addClass('last');
});

/* !Search Text Focus ------------------------------------------------------- */
jQuery(function(){
	jQuery("#siteSearch").focus(function() {
		if(jQuery(this).val() == jQuery(this).attr('defaultValue'))
			jQuery(this).val('');
			jQuery(this).addClass('searchHover');
	}).blur(function() {
		if(jQuery.trim(jQuery(this).val()) == "") {
			jQuery(this).val(jQuery(this).attr('defaultValue'));
			jQuery(this).removeClass('searchHover');
		}
	}).end().blur();
});
/* !Block Hover ------------------------------------------------------------ */
jQuery(function(){
	jQuery("ul.ul-people>li,ul.ul-services>li,ul.ul-index01>li,ul.ul-works>li").click(function(){
		window.location=jQuery(this).find("a").attr("href");
		return false;
	});
	jQuery('ul.ul-people>li,ul.ul-services>li,ul.ul-index01>li,ul.ul-works>li').hover(function() {
		jQuery(this).addClass('hover');
	},function() {
		jQuery(this).removeClass('hover');
	});
});
/* ! ul column ------------------------------------------------------------- */
jQuery(function(){
	jQuery(".ul-works>li:odd:not(#ul-people>li,#ul-index01>li), .ul-updates>li:odd").addClass('even');
});

/* ! works effect ------------------------------------------------------------- */
jQuery(function(){
	jQuery('ul.ul-works>li').hover(
		function(){
			jQuery(this).find('.image').stop(true, false).animate( { height: '0' }, 400, "easeInOutCirc" );
		},function(){
			jQuery(this).find('.image').stop(true, false).animate( { height: jQuery(this).find('.image img').attr('height')+'px' }, 400, "easeInOutCirc" );
		});
	var viewNum = 4;
	var loadUnit = 4;
	jQuery('#work ul.ul-works>li').each(function(i){
		//console.log(i);
		if(i > viewNum-1){
			jQuery(this).hide();
		}
	});
	if(jQuery('#work ul.ul-works>li').length <= viewNum){
		jQuery('#work .txt-loadMore a').fadeOut();
	}
	jQuery('#work .txt-loadMore a').click(function(){
		jQuery('#work ul.ul-works>li').each(function(i){
			//console.log(i);
			if(viewNum <= i && i < viewNum+loadUnit ){
				jQuery(this).fadeIn();
			}
		});
		viewNum += loadUnit;
		if(jQuery('#work ul.ul-works>li').length <= viewNum){
			jQuery(this).fadeOut();
		}
		return false;
	});
});

/* entry thickbox ------------------------------------------------------------- */
jQuery(function(){
	jQuery(".entry-content a img:not(.ssl-seal a img, .seal a img)").parent().addClass('thickbox');
	//jQuery(".ssl-seal a img, .seal a img").parent().removeClass('thickbox');
});
/* ! Top Contact Japanese Version ------------------------------------------------------------- */
jQuery(function($){
	//contactフォームのアクション先をHTTPSに変換
	jQuery("#wpcf7-f1-p2-o1-id").attr("action", "https://www.eurorscg.co.jp/#wpcf7-f1-p2-o1");
	jQuery("p.send a").click(function(){
		//各inputデータをtrim取得
		var in_company = jQuery.trim($('#your-company').val());
		var in_name = jQuery.trim($('#your-name').val());
		var in_mail = jQuery.trim($('#your-email').val());
		var in_message = jQuery.trim($('#your-message').val());
		var regex = /^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z]{2,3})+$/;
		var send_flg = true;
		//会社名処理
		if(in_company == ""){
			jQuery("#valid-company").text("必須項目に記入もれがあります。");
			send_flg = false;
		}else{
			jQuery("#valid-company").text("");
		}
		//名前処理
		if(in_name == ""){
			jQuery("#valid-name").text("必須項目に記入もれがあります。");
			send_flg = false;
		}else{
			jQuery("#valid-name").text("");
		}
		//メール処理
		if(in_mail == ""){
			jQuery("#valid-email").text("必須項目に記入もれがあります。");
			send_flg = false;
		}else if(in_mail.match(/^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z]{2,3})+$/) == null){
			jQuery("#valid-email").text("メールアドレスの形式が正しくないようです。");
			send_flg = false;
		}else{
			jQuery("#valid-email").text("");
		}
		//メッセージ処理
		if(in_message == ""){
			jQuery("#valid-message").text("必須項目に記入もれがあります。");
			send_flg = false;
		}else{
			jQuery("#valid-message").text("");
		}
		//フォーム送信処理
		if(send_flg == true){
			jQuery(".wpcf7-response-output").hide();
			jQuery(".wpcf7-response-output").removeClass("wpcf7-validation-errors");
			jQuery(".wpcf7-response-output").text("");
			document.getElementById('wpcf7-f1-p2-o1-id').submit();
		//エラーメッセージエリア表示処理
		}else{
			jQuery(".wpcf7-response-output").hide();
			jQuery(".wpcf7-response-output").text("入力内容に不備があります。確認してもう一度送信してください。");
			jQuery(".wpcf7-response-output").addClass("wpcf7-validation-errors").slideDown('fast');
		}
		return false;
	});
});
