﻿var CreateComment = {
	textbox : null,
	usernameTextbox : null,
	commentList : null,
	commentWroteText : null,
	lock:false,
	
	SendComment:function(commentId, commentListId, textboxId, commentWroteText, userNameTextboxId) {
		if(!CreateComment.lock){
			CreateComment.lock = true;
			CreateComment.textbox = $j('#'+textboxId);
			CreateComment.commentList = $j('#'+commentListId);
			CreateComment.usernameTextbox = $j('#'+userNameTextboxId);
			CreateComment.commentWroteText = commentWroteText;
			var text = CreateComment.textbox.attr('value');
			var username = CreateComment.usernameTextbox.attr('value');
			if(username == ''){			
				$j('#no-username').show();
				return;
			}
			else{			
				$j('#no-username').hide();
			} if(!username)username = '';
			CreateComment.textbox.attr('disabled', 'disabled');
			CreateComment.usernameTextbox.attr('disabled', 'disabled');
			Photo.Web.ScriptServices.Gallery.CommentService.SaveComment(
				commentId, 
				text, 
				username, 	
				CreateComment.SendCommentSuccess, 
				CreateComment.SendCommentFailiure);
		}		
	},
	SendCommentSuccess:function(res){
		CreateComment.textbox.parent().parent().hide();
		var date = new Date();
		var commentBOdy = htmlToText(CreateComment.textbox.attr('value'));	
		var usertext;
		if(typeof(current_user_userlink) != 'undefined')
			usertext = current_user_userlink;
		else if(typeof(current_guest_usertext) != 'undefined')
			usertext = current_guest_usertext;
		else if(typeof(CreateComment.usernameTextbox) != 'undefined')
			usertext = textToHtml(CreateComment.usernameTextbox.attr('value'));
			
		var commentHtml = '<div class="comment-separator">';
		commentHtml += 	'	<hr class="dotted" />';
		commentHtml += 	'	<div class="comment icon"></div>';
		commentHtml += 	'</div>';
		commentHtml +=  '	<div class="comment-item">';
		commentHtml += 	'	<div class="header">';
		commentHtml +=  '			<div class="comment-date">';
		commentHtml += 	'{0}-{1}-{2} {3}:{4}:{5}'.format(
					date.getFullYear(), 
					date.getMonth()+1, 
					date.getDate(),
					date.getHours(),
					date.getMinutes(),
					date.getSeconds());
		commentHtml += 	'			</div>';
		commentHtml += 	'		{0} {1}'.format(usertext, CreateComment.commentWroteText);					
		commentHtml += 	'	</div>';
		commentHtml += 	'	<div class="body fc">';
		commentHtml += 			textToHtml(CreateComment.textbox.attr('value'));							
		commentHtml += 	'	</div>';
		commentHtml += 	'</div>	';
		CreateComment.commentList.prepend($j(commentHtml));
	},
	
	SendCommentFailiure:function(res){
		alert(res.get_message());
		CreateComment.textbox.attr('disabled', '');
		CreateComment.usernameTextbox.attr('disabled', '');
	}
};