window.variety = window.variety || {};
var variety = window.variety;

$(function() {
	$(document).on('submit', '#js-register_account_form', function(e) {
		e.preventDefault();
		e.stopPropagation();

		var $form = $(this);
		var url = $form.attr('action');
		var method = $form.attr('method');
		var form_data = $form.serialize();

		$.ajax({
			method: method,
			url: url,
			data: form_data,
			dataType: 'json',
		}).done(function(data) {
			if (data.status == 'success') {
				if (data.success) {
					if (typeof gtag !== 'undefined') {
						gtag('event', 'live_registration_pc');
					}

					window.location.reload();
				} else {
					var dummy = $('<div>');
					dummy.html(data.result_html);
					var sub_html = dummy.find('#js-register_modal_body');
					$('#js-register_modal_body').replaceWith(sub_html);
				}
			} else {
				console.error('新規登録失敗');
				var dummy = $('<div>');
				dummy.html(data.result_html);
				var sub_html = dummy.find('#js-register_modal_body');
				$('#js-register_modal_body').replaceWith(sub_html);
				$('#error_dialog_div_register').show();
			}
		}).fail(function(data) {
			console.error('新規登録失敗');
		});
	});

	$(document).on('submit', '#js-login_account_form', function(e) {
		e.preventDefault();
		e.stopPropagation();

		var $form = $(this);
		var url = $form.attr('action');
		var method = $form.attr('method');
		var form_data = $form.serialize();

		console.log('ログイン!!');
		$.ajax({
			method: method,
			url: url,
			data: form_data,
			dataType: 'json',
		}).done(function(data) {
			if (data.status == 'success') {
				if (data.success) {
					if (data.next_url) {
						window.location.href = data.next_url;
					} else {
						$('#error_dialog_div').hide();

						if (data.return_mail_cnt >= 3) {
							$.ajax({
								method: "POST",
								url: "/account/send_notification",
								data: {mail_member_id: data.male_member_id}
							}).done(function(data) {
								// success
							}).fail(function(data) {
								// failed
							});
						}

						if (data.return_mail_cnt >= 3 && getCookie(data.male_member_id + "email_delivery_fail_modal_do_not_show") != "true") {
							macheVariety.Modal_show('/Account/email_delivery_fail_modal');
							document.cookie = "email_delivery_fail_male_member_id=" + data.male_member_id;
						} else {
							window.location.reload();
						}
					}
				} else {
					var dummy = $('<div>');
					dummy.html(data.result_html);
					var sub_html = dummy.find('#js-login_modal_body');
					// 2016-06-18 martin Fix PDOException Mod S
					$('#js-login_modal_body').replaceWith(sub_html);
					//$('#js-register_modal_body').replaceWith(sub_html);
					// 2016-06-18 martin Fix PDOException Mod E
					$('#error_dialog_div').show();
					$('.js-inhibit_double_click').find(':submit').removeAttr('disabled');
				}
			} else {
				console.error('ログイン失敗');
				var dummy = $('<div>');
				dummy.html(data.result_html);
				var sub_html = dummy.find('#js-login_modal_body');
				$('#js-login_modal_body').replaceWith(sub_html);
			}
		}).fail(function(data) {
			console.error('ログイン失敗');
		});
	});


	$(document).on('submit', '#js-other-auth-register_account_form', function(e) {
		e.preventDefault();
		e.stopPropagation();

		var $form = $(this);
		var url = $form.attr('action');
		var method = $form.attr('method');
		var form_data = $form.serialize();

		$('#error_dialog_div_register').hide();

		$.ajax({
			method: method,
			url: url,
			data: form_data,
			dataType: 'json',
		}).done(function(data) {
			if (data.status == 'success') {
				if (data.success) {
					if (data.redirect_url) {
						window.location.href = data.redirect_url;
					} else {
						console.error('遷移先URL指定なし');
					}
				} else {
					var dummy = $('<div>');
					dummy.html(data.result_html);
					var sub_html = dummy.find('#js-register_modal_body');
					$('#js-register_modal_body').replaceWith(sub_html);
				}
			} else {
				console.error('新規登録失敗');
				var dummy = $('<div>');
				dummy.html(data.result_html);
				var sub_html = dummy.find('#js-register_modal_body');
				$('#js-register_modal_body').replaceWith(sub_html);
			}
		}).fail(function(data) {
			console.error('新規登録失敗');
		});
	});

	$(document).on('submit', '#js-password_reset_apply_modal_form', function(event) {
		event.preventDefault();
		event.stopPropagation();

		var $form = $(this);
		var url = $form.attr('action');

		$('#js-password_reset_apply_modal_form').find(".c-btn--registerFrom").prop("disabled", true);
		$.ajax({
			method: 'POST',
			url: url,
			data: $form.serialize(),
			dataType: 'json',
		}).done(function(data) {
			if(data.success) {
				var dummy = $('<div>');
				dummy.html(data.modal_html);
				var sub_html = dummy.find('#js-password_reset_apply_modal_body');
				$('#js-password_reset_apply_modal_modal_body').replaceWith(sub_html);
			} else {
				var dummy = $('<div>');
				dummy.html(data.modal_html);
				var sub_html = dummy.find('#js-password_reset_apply_modal_modal_body');
				$('#js-password_reset_apply_modal_modal_body').replaceWith(sub_html);
				$('#js-password_reset_apply_modal_form').find(".c-btn--registerFrom").prop("disabled", false);
			}
			console.log(data);
		}).fail(function(data) {
			// この部分については要検討
			// window.location.href = data.next_url;
			console.log(data);
			$('#js-password_reset_apply_modal_form').find(".c-btn--registerFrom").prop("disabled", false);
		});
	});

	// 商品購入履歴：ダウンロード写真（もっと見る）
	$(document).on('click', '.js-mypage_purchase_history_dl_photo_load_more_btn', function(event) {
		event.preventDefault();
		event.stopPropagation();

		var $self = $(this);
		var $load_more_box = $self.closest('.js-mypage_purchase_history_dl_photo_load_more');
		var $form = $self.closest('form');

		// ローディング
		$self.hide();
		$load_more_box.find('.js-mypage_purchase_history_dl_photo_load_more_loader').addClass('is-show');

		var url = $('#url').val();
		var page = $('.current_page:last').val();
		var max_tweet_id = $('.max_tweet_id').val();

		// for timeline
		var lasty = $('.date-y:last').text();
		var lastmd = $('.date-md:last').text();
		var lastdate = lasty + "/" + lastmd;
		var talent_code = $("#talent_code").val();
		var unit_code = $('#unit_code').val();
		var event_id = $('#event_id').val();

		var data = {'page' : page, 'last_date' : lastdate, 'talent_code' : talent_code, 'unit_code' : unit_code, 'event_id' : event_id, 'max_tweet_id' : max_tweet_id};

		$.ajax({
			method: "GET",
			url: url,
			data: data,
			dataType: "json",
		}).always(function(data) {
			$self.show();
			$load_more_box.find('.js-mypage_purchase_history_dl_photo_load_more_loader').removeClass('is-show');
		}).done(function(data) {
			var result_html = $('<div>').html(data.result_html);
			var content = result_html.find('li.load_more_items');
			var current_page = result_html.find('.current_page');

			$('#spinner_load_more').removeClass('is-show'); // hide spinner
			$('#display_item_list').append(content); // append new li items

			//INNR_VARIETY-750 and 878 FIX START 2016.09.02
			if($('.follower_page').length == 0) {
				$('.current_page').replaceWith(current_page); // updated current page
			}
			//INNR_VARIETY-750 and 878 FIX END 2016.09.02

		}).fail(function(xhr, textStatus, errorThrown) {
			console.log(xhr.responseText);
		});
	});

});
