﻿function voteSyame(approot, photo_id, category_id, gal_id) {
	if (voted_count >= 2) {
		alert('投票は1日' + vote_max + '回までです');
		return;
	}
	jQuery.ajax({
		type: 'POST',
		url: approot + '/ajax/member_vote_syame.aspx',
		cache: false,
		data: 'pid=' + photo_id + '&cid=' + category_id + '&gid=' + gal_id,
		success: function() {
			jQuery('div[id^="voted' + photo_id + '"]').css("display", "block");
			jQuery('div[id^="vote' + photo_id + '"]').css("display", "none");
			voted_count++;
		}
	});
}

function addSyameGood(approot, photo_id, good_count) {
	jQuery.ajax({
		type: 'POST',
		url: approot + '/ajax/add_photo_good.aspx',
		cache: false,
		data: 'pid=' + photo_id,
		success: function() {
			jQuery('span[id^="photo_good_' + photo_id + '"]').html('<span class="photo_good">ｲｲﾈ(<span id="good_count" style="color:#33E;">' + (good_count + 1) +
				'</span>)</span>');

			viewTextArea(approot, photo_id);
		}
	});
}

function viewTextArea(approot, photo_id) {
	if (commentState != 3) {
		//まずliのheight、コメントフォームをすべて非表示に
		var elements = document.getElementsByClassName("comment_box");
		for (var i = 0; i < elements.length; i++) {
			elements[i].innerHTML = '';
		}
		var li_elements = document.getElementsByClassName("li_photo");
		for (var i = 0; i < li_elements.length; i++) {
			li_elements[i].style.height = "290px";
		}
		//対象と同じ行のliのheightを調整
		var targetArr = new Array();
		for (var i = 0; i < heightArr.length; i++) {
			for (var j = 0; j < heightArr[i].length; j++) {
				if (heightArr[i][j] == photo_id) {
					targetArr = heightArr[i];
					break;
				}
			}
			if (targetArr.length > 0) break;
		}
		for (var i = 0; i < targetArr.length; i++) {
			jQuery('li[id^="li_photo_' + targetArr[i] + '"]').css("height", "390px");
		}
	
		document.getElementById('box_gal_' + photo_id).innerHTML +=
			'<div style="text-align:center;" class="comment_box" id="comment_box_' + photo_id + '"><textarea id="comment_' + photo_id +
			'"></textarea><br /><input type="button" value="コメント投稿" onclick="postComment(\'' + approot + '\', ' + photo_id +
			 ');" /><br /><a href="' + baseUrl + 'syame_viewer/' + photo_id + '.html?com=on#comment_post" style="font-size:11px;">長いコメントを入力する場合はこちら</a></div>';
		document.getElementById('comment_' + photo_id).focus();
	}
}

function delSyameGood(approot, photo_id, good_count) {
	jQuery.ajax({
		type: 'POST',
		url: approot + '/ajax/delete_photo_good.aspx',
		cache: false,
		data: 'pid=' + photo_id,
		success: function() {
			jQuery('span[id^="photo_good_' + photo_id + '"]').html('<a href="#li_photo_' + photo_id + '" onclick="addSyameGood(\'' + approot + '\', ' + photo_id
			+ ', ' + (good_count - 1) + ');"">ｲｲﾈ(<span id="good_count" style="color:#E66;">' + (good_count - 1) + '</span>)</a>');
		}
	});
}

function postComment(approot, photo_id) {
	if (confirm('投稿します。よろしいですか？') == true) {
		var obj = jQuery('textarea[id^="comment_' + photo_id + '"]');
		if (obj != null && obj.val().replace(/^\s+|\s+$/g, "") != '') {
			var comment = obj.val().replace(/^\s+|\s+$/g, "");

			if (comment.length > commentLength) {
				alert('コメントは' + commentLength + '文字までで入力してください\r\n現在' + comment.length + '文字');
				return;
			}
			jQuery.ajax({
				type: 'POST',
				url: approot + '/ajax/post_photo_comment.aspx',
				cache: false,
				data: 'pid=' + photo_id + '&comm=' + encodeURI(comment),
				success: function() {
					if (IsAuthGal == false) {
						if (commentState == 1) {
							alert("投稿を受け付けました。コメントは「お店がチェックした後」表示されます");
						} else if (commentState == 2) {
							alert("投稿を受け付けました。コメントは「女の子がチェックした後」表示されます");
						} else {
							alert("投稿しました");
						}
					}
					jQuery('div[id^="comment_box_' + photo_id + '"]').html('');

					var targetArr = new Array();
					for (var i = 0; i < heightArr.length; i++) {
						for (var j = 0; j < heightArr[i].length; j++) {
							if (heightArr[i][j] == photo_id) {
								targetArr = heightArr[i];
								break;
							}
						}
						if (targetArr.length > 0) break;
					}
					for (var i = 0; i < targetArr.length; i++) {
						jQuery('li[id^="li_photo_' + targetArr[i] + '"]').css("height", "290px");
					}
					//ｺﾒﾝﾄｶｳﾝﾄｱｯﾌﾟ
					if (commentState == 0 || IsAuthGal) {
						var comm_cnt = jQuery('input[id^="comment_cnt_' + photo_id + '"]').val();
						jQuery('span[id^="comment_cnt_' + photo_id + '"]').html(comm_cnt - (-1));
						jQuery('span[id^="comment_cnt_s_' + photo_id + '"]').html(comm_cnt - (-1));
					}
				},
				Error: function() {
					alert("エラーが発生しました");
				}
			});
		}
	}
	return false;
}

function addSyameGoodOnePhoto(approot, gal_approot, url_pc, photo_id, comment_name, comment_id, shop_url_name, chihou_id) {
	jQuery.ajax({
		type: 'POST',
		url: approot + '/ajax/add_photo_good.aspx',
		cache: false,
		data: 'pid=' + photo_id,
		success: function() {
			jQuery('div[id^="photo_good"]').html('<div class="good_done">✔イイネ</div>');

			var obj = jQuery('span[id^="any_good"]');
			if (obj.html() != '') {
				var good_count = jQuery('span[id^="good_count"]').html() - (-1);
				obj.html('<span id="good_count">' + good_count + '</span>&nbsp;人がイイネ！と言っています');
			} else {
				obj.html('<span id="good_count">1</span>&nbsp;人がイイネ！と言っています');
			}

			var good_list = jQuery('div[id^="good_list"]');
			var add = good_list.html();
			if (IsAuthGal) {
				good_list.html('<span id="gal_' + comment_id + '">「<a href="' + approot + '/sc' + chihou_id + '/' + shop_url_name + '/gal/' +
					comment_id + '/" target="_blank">' + replace_emojicode_to_src2(comment_name) + '</a>」</span>' + add);
			} else {
				good_list.html('「<a href="' + url_pc + '/member/' + comment_id + '/" target="_blank" class="member"><span id="member_' + comment_id
					+ '">' + replace_emojicode_to_src2(comment_name) + '</span></a>」' + add);
			}
		}
	});
}

function delSyameGoodOnePhoto(approot, photo_id, comment_name, comment_id, shop_url_name, chihou_id) {
	jQuery.ajax({
		type: 'POST',
		url: approot + '/ajax/delete_photo_good.aspx',
		cache: false,
		data: 'pid=' + photo_id,
		success: function() {
			jQuery('div[id^="photo_good"]').html('<a href="javascript:void(0);" onclick="addSyameGoodOnePhoto(\'' + approot + '\', ' + 
				photo_id + ', \'' + comment_name + '\', ' + comment_id + ', \'' + shop_url_name + '\', ' + chihou_id + ');"><img src="' + 
				siteImageUrl + '/gal/bt_iine.gif" /></a>');

			var obj = jQuery('span[id^="good_count"]');
			var good_count = obj.html() - 1;
			obj.html(good_count);

			var good_list = document.getElementById('good_list');
			var child = document.getElementById((IsAuthGal ? 'gal_' : 'member_') + comment_id);
			if (child != null)
				good_list.removeChild(child);
		}
	});
}
