if (typeof(Ficia) == 'undefined') Ficia = {};
if (typeof(Ficia.UI) == 'undefined') Ficia.UI = {};
if (typeof(Ficia.UI.Dialog) == 'undefined') Ficia.UI.Dialog = {};

(function(){
Ficia.UI.Dialog.Photo = function(args){
	this.dialogs = {
		comment: new Ficia.UI.Dialog.Photo.Comment(args),
		good   : new Ficia.UI.Dialog.Photo.Good(args)
	};
	this.init();
};
Ficia.UI.Dialog.Photo.prototype = {
	init: function(){
		var self = this;
		$.each(this.dialogs, function(name){
			self.dialogs[name].init();
		});
	},
	_: ''
};


Ficia.UI.Dialog.Photo.Comment = function(args){
	this.photo          = args.photo;
	this.user_id        = args.user_id;
	this.viewer_user_id = args.viewer_user_id;
};
Ficia.UI.Dialog.Photo.Comment.prototype = {
	constructor: Ficia.UI.Dialog.CommentGood,
	photo: null,

	init: function(){
		var self = this;

		$("#comments-form-form").submit(function (e) {
			e.preventDefault();
			self.comment_submit();
		});
		$("#comments-form-submit").click(function (e) {
			e.preventDefault();
			self.comment_submit();
		});

		$("#comments-form-cancel").click(function (e) {
			e.preventDefault();
			$.dialog.close();
		});

		$("#comment-onephoto").click(function (e) {
			e.preventDefault();
			self.update();
		});

		if (self.viewer_user_id !== "guest") {
			$("#comments-form-nickname-div").hide();
		}
	},

	update: function(){
		var self = this;

		var photo = self.photo.photo_list[self.photo.slide_photo_selected_index];
		var id = photo.photo_id;
		//GET /photo/写真所持者のuser_id/写真のID/comments
		var url = "/photo/" + self.user_id + "/" + photo.photo_id + "/comments";
		var commentspane = $("#comments-form-comments");
		commentspane.css({'margin-bottom':'15px'});
		commentspane.empty();
		$("#comments-form-error").text("");
		$.ajax({
			url : url,
			type : "get",
			dataType: "json",
			cache: false,
			success : function(data){
				if (data.msg == "ng") {
					$("#comments-form-error").text("コメント取得に失敗しました");
					return;
				}

				for (i = 0; i < data.length; i++) {
					var obj = data[i];
					//コメントの日付
					var namespan = $("<SPAN/>").
						addClass("comments-name").
						text(obj.viewer_nickname);
					var datespan = $("<SPAN/>").
						addClass("comments-date").
						text("(" + epoch2datetime(obj.update_timestamp).replace(/:\d\d$/, "") + ")");
					var nametime = $("<DIV/>").
						append(namespan).
						append(datespan);
					var comment = $("<DIV/>").
						text(obj.comment).
						addClass("comments-comment");

					var border = i == 0 ? "" : "1px solid #000000";
					var elem = $("<DIV/>").
						addClass("dialog_list comments-list").
						css({
							'border-top':border
						}).
						append(nametime).
						append(comment);

					commentspane.append(elem);
				}

				photo.photo_comment_count = data.length;
				$("#comment-onephoto-count").text( photo.photo_comment_count );

				if (!photo.commentview) {
					photo.commentview = $("<DIV/>").addClass("view_comment").append(
						$("<SPAN/>").addClass("view_comment_label")
					);
					if (photo.goodview) {
						photo.commentview.insertBefore(photo.goodview);
					} else {
						if (photo.infoview) photo.infoview.append(photo.commentview);
					}
				}
				photo.commentview.find("span").text(photo.photo_comment_count);
				if (self.photo.is_comment_checked == false) {
					photo.commentview.hide();
				}

				// open or reopen
				$('#comments-form').dialog_open();
			}
		});
	},

	comment_submit: function(){
		var self = this;

		$("#comments-form-error").text("");
		var comment  = $("#comments-form-comment").val();
		var nickname = $("#comments-form-nickname").val();
		if (comment.length === 0) {
			return;
		}

		var data = { comment: comment };
		if (self.viewer_user_id === "guest") {
			if (nickname == "") {
				$("#comments-form-error").text("名前を入れてください");
				return;
			}
			data.nickname = nickname;
		}

		var photo = self.photo.photo_list[self.photo.slide_photo_selected_index];
		//PUT /photo/写真所持者のuser_id/写真のID/comment
		var url = "/photo/" + self.user_id + "/" + photo.photo_id + "/comment";
		$.ajax({
			url : url,
			data : data,
			type : "put",
			dataType: "json",
			cache: false,
			success : function(data){
				if (data.msg == "ng") {
					$("#comments-form-error").text("コメントの追加に失敗しました");
				} else {
					$("#comments-form-comment").val("");
					self.update();
				}
			}
		});
	},

	_: ''
};

Ficia.UI.Dialog.Photo.Good = function(args){
	this.photo          = args.photo;
	this.user_id        = args.user_id;
	this.viewer_user_id = args.viewer_user_id;
};
Ficia.UI.Dialog.Photo.Good.prototype = {
	constructor: Ficia.UI.Dialog.CommentGood,
	photo: null,

	init: function(){
		var self = this;

		$("#good-onephoto").click(function (e) {
			e.preventDefault();
			self.good_submit();
		});
	},

	good_submit: function(){
		var self  = this;
		var photo = self.photo.photo_list[self.photo.slide_photo_selected_index];
		var url = "/photo/" + self.user_id + "/" + photo.photo_id + "/good";
		//PUT /photo/写真所持者のuser_id/写真のID/good
		$.ajax({
			url : url, 
			type : "put",
			dataType: "json",
			cache: false,
			success : function(data){
				if (data.msg === "ok") {
					photo.photo_good_count = parseInt(photo.photo_good_count) + 1;
					$("#good-onephoto-count").text( photo.photo_good_count );

					if (!photo.goodview) {
						photo.goodview = $("<DIV/>").addClass("view_good").append(
							$("<SPAN/>").addClass("view_good_label")
						);
						photo.infoview.append(photo.goodview);
					}
					photo.goodview.find("span").text(photo.photo_good_count);
					if (self.photo.is_good_checked === false) {
						photo.goodview.hide();
					}
				}
			}
		});
		
	},

	_: ''
};

}());
