$(function(){
	window.imgSwitch = function(skipFlg){

		var ua_num = userAgent(skipFlg);


		if(ua_num != 1) {
			var timer = false;

			imgChange();

			$(window).resize(function() {
				if (timer !== false) {
					clearTimeout(timer);
				}
				timer = setTimeout(function() {
					imgChange();
				}, 200);
			});
		};
	};


	function userAgent(skipFlg){
	    if ("1" == skipFlg) {
            $('.nav_step > img').each(function() {
                $(this).attr("src",$(this).attr("src").replace('step_', 'step_skip_'));
            });
        }


		var _ua = (function(u){
			return {
				Tablet:u.indexOf("ipad") != -1 || (u.indexOf("android") != -1 && u.indexOf("mobile") == -1) || (u.indexOf("firefox") != -1 && u.indexOf("tablet") != -1) || u.indexOf("kindle") != -1 || u.indexOf("silk") != -1 || u.indexOf("playbook") != -1,
				Mobile:(u.indexOf("windows") != -1 && u.indexOf("phone") != -1) || u.indexOf("iphone") != -1 || u.indexOf("ipod") != -1 || (u.indexOf("android") != -1 && u.indexOf("mobile") != -1) || (u.indexOf("firefox") != -1 && u.indexOf("mobile") != -1) || u.indexOf("blackberry") != -1,
				IE:u.indexOf('msie') != -1 || u.indexOf('trident') != -1
			};
	    	})(window.navigator.userAgent.toLowerCase());
		var _ap = (function(ap){
	        return {
	          OldIE:
	          ap.indexOf("msie 6.") !=  -1 ||
	          ap.indexOf("msie 7.") !=  -1 ||
	          ap.indexOf("msie 8.") !=  -1
	      };
	    })(window.navigator.appVersion.toLowerCase());

		var ua_num = 0;
		if(_ua.Mobile){ //Mobile
			ua_num = 3;
		}else if(_ua.Tablet){ //Tablet
			ua_num = 2;
		}else if(_ua.IE && _ap.OldIE){ //OLD IE
			ua_num = 1;
		}

	    return ua_num;
	}

	function imgChange(){
	  var wid = $(window).width();
	  if( wid < 640 ){
	    $('.js_imgChange').each(function(){
	      $(this).attr("src",$(this).attr("src").replace('_pc', '_sp'));
	    });
	  } else {
	    $('.js_imgChange').each(function(){
	      $(this).attr("src",$(this).attr("src").replace('_sp', '_pc'));
	    });
	  }
	}


	function footer_link(ua_num){
		var inv_policy_link = "https://www.aioinissaydowa.co.jp/corporate/policy/invitation.html";
		var pri_policy_link = "https://www.aioinissaydowa.co.jp/corporate/policy/privacy.html";

		if(ua_num == 2 || ua_num == 3) {
			inv_policy_link = "https://www.aioinissaydowa.co.jp/corporate/policy/invitation.html";
			pri_policy_link = "https://www.aioinissaydowa.co.jp/corporate/policy/privacy.html";
		}

		// 勧誘方針
		$('#inv_policy_link').attr('href', inv_policy_link);
		// 個人情報保護方針
		$('#pri_policy_link').attr('href', pri_policy_link);
	}
	footer_link(userAgent());


  $('a[href^=#]').click(function() {
    var speed = 500;
    var href= $(this).attr("href");
    var target = $(href == "#" || href == "" ? 'html' : href);
    var position = target.offset().top;
    var body = 'body';
    if (navigator.userAgent.match(/MSIE/)||navigator.userAgent.match(/Trident/)||navigator.userAgent.match(/Firefox/)) {
      /*IE6.7.8.9.10 + FF*/
      body = 'html';
    }
    $(body).animate({scrollTop:position}, speed, 'swing');
    return false;
  });

  window.radioCssChange = function (radioName) {
	$("input[name=" + radioName + "]").each(function() {
		$(this).parent().parent().parent().removeClass('input_error');
		$(this).removeClass('active').parent().removeClass('active');

		 if($(this).is(":checked")){
			 $(this).parent().addClass("active");
			 $(this).addClass('active');
		 }
	});
  };

  window.selectCtrl = function() {
	$('select').each(function() {
		if ($(this).find("option:selected").text()) {
			$(this).next().find('span').text($(this).find("option:selected").text());
		} else {
			$(this).next().find('span').html("&nbsp;");
		}

		var divmaxWidth = parseInt($('body').find("div").first().width(), 10);
		if ($(this).parent().parent().hasClass('box_selectbox_wrapper')) {
			var selectWidth = 0;
			var divchildWidthStr= $(this).parent().css('width');
			if(divchildWidthStr.indexOf("%") > 0){
				var divchildWidth = parseInt($(this).parent().css('width'), 10);
				selectWidth = (divmaxWidth-40)*divchildWidth/100-2;
			} else {
				 selectWidth = divchildWidthStr;
			}
			$(this).css('width', selectWidth);

		} else {
			$(this).css('width', (divmaxWidth-40));
		}
	});
  };


	 readMethodChange = function (radioName) {
			$("input[name=" + radioName + "]").each(function() {
				$(this).parent().children("img").attr("src",$(this).parent().children("img").attr("src").replace('_on', '_off'));
				 if($(this).is(":checked")){
					$(this).parent().children("img").attr("src",$(this).parent().children("img").attr("src").replace('_off', '_on'));
				 }
			});
	};


});