<!--
$(document).ready(function(){
	// Safari3 未満は動作させない
	if ( jQuery.browser.safari ) {
		var tmp = jQuery.browser.version.split(".");
		if ( tmp[0] > 520 ) { var safari3 = 1; }
	}
	if ( jQuery.browser.msie || jQuery.browser.mozilla || jQuery.browser.opera || (jQuery.browser.safari&&safari3) ) {
		commentOpen(0);

		$("#commentMoreHref").click(function(){
//			$("#commentList").empty();
			commentOpen(1);
			$("#commentMore").css("display","none");
		});

		$("#commentFormButton").click(function(){
			if ( document.commentForm.nickname.value == "" || document.commentForm.nickname.value == "ニックネーム入力" ) {
				alert( "ニックネームを入力してください" );
				return;
			} else if ( document.commentForm.comment.value == "" || document.commentForm.comment.value == "コメント入力" ) {
				alert( "コメントを入力してください" );
				return;
			} else {
				$.ajax({
					url: "/photo/_prog/comment_send.php",
					type: 'POST',
					cache: false,
					data: {
						"c": $('input[@name=c]').val(),
						"nickname": encodeForAjax($('input[@name=nickname]').val()),
						"comment": encodeForAjax($('textarea[@name=comment]').val())
					},
					dataType: "text",
					beforeSend: function(){
						$("#commentFormBefore").css("display","none");
						$("#commentFormBeteen").css("display","block");
					},
					error: function(){
						$("#commentFormBefore").css("display","none");
						$("#commentFormBeteen").css("display","none");
						$("#commentFormFail").css("display","block");
					},
					success: function(text){
						$("#commentFormBefore").css("display","none");
						$("#commentFormBeteen").css("display","none");
						$("#commentFormAfter").css("display","block");
						$('input[@name=nickname]').val("");
						$('textarea[@name=rating_body]').val("");
					}
				});
			}
		});
	} else {
		$("#commentArea").css("display","none");
	}
});

function commentOpen( all )
{
	$.ajax({
		url: "/photo/_prog/comment_view.php?c="+$('input[@name=c]').val()+"&all="+all,
		type: 'GET',
		cache: false,
		dataType: "text",
		timeout: 5000,
		error: function(){
			$("#commentArea").css("display","none");
			$("#commentAreaFail").css("display","block");
		},
		success: function(text){
			tmp = text.split("::::");
			if ( all != 1 ) {
				$("#commentCount").append("("+tmp[0]+")");
			}
			$("#commentHref").css("visibility","visible");
			$("#commentList").append(tmp[2]);
			if ( all != 1 && parseInt(tmp[0]) > parseInt(tmp[1]) ) {
				$("#commentMore").css("display","block");
			}
		}
	});
}

//URL Encode (UTF-8)
function encodeForAjax(str) {
	var character = '';
	var unicode = '';
	var string = '';
	var i = 0;

	for (i = 0; i < str.length; i++) {
		character = str.charAt(i);
		unicode	 = str.charCodeAt(i);

		if (character == ' ') {
			string += '+';
		} else {
			if (unicode == 0x2a || unicode == 0x2d || unicode == 0x2e || unicode == 0x5f || ((unicode >= 0x30) && (unicode <= 0x39)) || ((unicode >= 0x41) && (unicode <= 0x5a)) || ((unicode >= 0x61) && (unicode <= 0x7a))) {
				string = string + character;
			} else {
				if ((unicode >= 0x0) && (unicode <= 0x7f)) {
					character	 = '0' + unicode.toString(16);
					string += '%' + character.substr(character.length - 2);
				} else if (unicode > 0x1fffff) {
					string += '%' + (oxf0 + ((unicode & 0x1c0000) >> 18)).toString(16);
					string += '%' + (0x80 + ((unicode & 0x3f000) >> 12)).toString(16);
					string += '%' + (0x80 + ((unicode & 0xfc0) >> 6)).toString(16);
					string += '%' + (0x80 + (unicode & 0x3f)).toString(16);
				} else if (unicode > 0x7ff) {
					string += '%' + (0xe0 + ((unicode & 0xf000) >> 12)).toString(16);
					string += '%' + (0x80 + ((unicode & 0xfc0) >> 6)).toString(16);
					string += '%' + (0x80 + (unicode & 0x3f)).toString(16);
				} else {
					string += '%' + (0xc0 + ((unicode & 0x7c0) >> 6)).toString(16);
					string += '%' + (0x80 + (unicode & 0x3f)).toString(16);
				}
			}
		}
	}

	return string;
}
//-->
