// JavaScript Document
/**
*	author : Milko Leporis
*	date : 29.08.2008
*	version : 0.1
*/

function Blog(){
	this.rating = 0;
}
//Blog.prototype.??? = function(){}

Blog.prototype.rate = function(rating){
	this.rating = rating;
	$("#Blog_formRating").html(this.rating);
}
Blog.prototype.sendComment = function(){
	var ratingAllowed = document.getElementById("Blog_ratingAllowed").value;

	if(ratingAllowed == "false")
		referentRating = -1;
	else
		referentRating = 0;

	if(blog.rating > referentRating){
		var author = document.getElementById("Blog_formCommentAuthor").value;	
		var postId = document.getElementById("Blog_formPostId").value;
		var text = document.getElementById("Blog_formCommentText").value;
		
		if(text.replace(/\s+/g, '').length > 0){
			$.facebox.loading();
			$.post("http://www.msjb.org/_server/server.blog.php", {
				action: "saveComment",
				postId: postId,
				user: author,
				text: text,
				rating: this.rating
				}, function(xml){
					if($('error',xml).text() == "No"){
						document.getElementById("Blog_formCommentText").value = "";
						
						var newCommentId = $('commentId',xml).text();
						var avatar = $('avatar',xml).text();
						var newCommentHTML = "";
						newCommentHTML += '<div class="Blog_comment" id="Blog_comment_'+newCommentId+'">';
						
						newCommentHTML += '<div class="Blog_commentText" id="Blog_commentText_'+newCommentId+'">';
						newCommentHTML += '<a href="http://www.msjb.org/'+author+'">';
						newCommentHTML += '<img src="http://www.msjb.org/_avatars/'+avatar+'" />';
						newCommentHTML += '</a>';
						newCommentHTML += text;
						newCommentHTML += '</div><!-- Blog_commentText -->';
						
						newCommentHTML += '<div class="Blog_commentMeta" id="Blog_commentMeta'+newCommentId+'">';
						newCommentHTML += 'napisao/la je <a href="http://www.msjb.org/'+author+'">'+author+'</a>'; 
						if(ratingAllowed == "true")
							newCommentHTML += ' i dao/la ocenu '+blog.rating+'<br/>';
						else 
							newCommentHTML += '<br/>';
						newCommentHTML += 'upravo sada';
						newCommentHTML += '</div><!-- Blog_commentMeta -->';
						
						newCommentHTML += '<div class="Blog_commentsEditing">';
						newCommentHTML += '<div class="LMUI_buttonBar LMUI_buttonBarRight" style="margin-top: -25px;">';
						//echo '<a href="javascript:deleteComment.ask(\'deleteComment\',\'javascript:blog.deleteComment(\\\''.$comment->getBLG_PST_COM_ID().'\\\',\\\''.$post->getBLG_PST_ID().'\\\',\\\''.$comment->getBLG_PST_COM_RATING().'\\\')\')">Obriši</a>';
						newCommentHTML += '<a href="javascript:deleteComment.ask(\'deleteComment\',\'javascript:blog.deleteComment(\\\''+newCommentId+'\\\',\\\''+postId+'\\\',\\\''+blog.rating+'\\\')\')">Obriši</a>';
						//newCommentHTML += '<a href="javascript:blog.deleteComment(\''+newCommentId+'\',\''+postId+'\',\''+blog.rating+'\')">Obriši</a>';
						newCommentHTML += '</div>';
						newCommentHTML += '</div><!-- Blog_commentsEditing -->';
						
						newCommentHTML += '</div><!-- Blog_comment -->';
						
						
						if($("#Blog_comments").html() == "Nema komentara.")
							$("#Blog_comments").html(newCommentHTML);
						else
							$("#Blog_comments").append(newCommentHTML);
						
						$.facebox.close();
					} else {
						$.facebox("Komentarisanje trenutno nije moguće.");
					}
			});
		} else {
			$.facebox("Morete napisati neki komentar!");	
		}
	} else {
		$.facebox("Morete date ocenu!");	
	}
}

Blog.prototype.deleteComment = function(commentId,postId, rating){
	$.facebox.loading();
	$.post("http://www.msjb.org/_server/server.blog.php", {
		action: "deleteComment",
		commentId: commentId,
		postId: postId,
		rating: rating
		}, function(xml){
			if($('error',xml).text() == "No"){
				$("#Blog_comment_"+commentId).fadeOut("slow");
				$.facebox.close();
				if($('count',xml).text() == '0'){
					$("#Blog_comments").html("Nema komentara.");	
				}
			} else {
				$.facebox("Brisanje komentara trenutno nije moguće.");
			}
	});
}
Blog.prototype.replyToComment = function(commentId){
	var html = "";
	html += '<textarea id="Blog_replyToCommentText_'+commentId+'" rows="5" style="width: 455px;"></textarea>';
	html += '<br/><br/><div class="LMUI_buttonBar LMUI_buttonBarRight">';
	html += '<a href="javascript:blog.replyToCommentSend(\''+commentId+'\')">Sačuvaj</a>';
	html += '<a href="javascript:blog.closeReplyToComment(\''+commentId+'\')">Zatvori</a>';
	html += '</div>';
	html += '<div style="clear:both"></div>';
	$("#Blog_replyToComment_"+commentId).show().html(html);
}
Blog.prototype.closeReplyToComment= function(commentId){
	$("#Blog_replyToComment_"+commentId).hide().html("");
}
Blog.prototype.replyToCommentSend = function(commentId){
	$.facebox.loading();
	var replyText = $("#Blog_replyToCommentText_"+commentId).val();
	$.post("http://www.msjb.org/_server/server.blog.php", {
		action: "replyToComment",
		commentId: commentId,
		text: replyText
		}, function(xml){
			if($('error',xml).text() == "No"){
				var oldCommentText = $("#Blog_commentText_"+commentId).html();
				var newCommentText = oldCommentText;
				newCommentText += '<div class="Blog_commentReply"><b>Odgovor autora : </b><br/><br/>';
				newCommentText += replyText;
				newCommentText += '</div>';
				$("#Blog_commentText_"+commentId).html(newCommentText);
				blog.closeReplyToComment(commentId);
				$.facebox.close();
			} else {
				$.facebox("<b>Ooops</b> <br/><br/>Odgovor na komentar trenutno nije moguće poslati.");
			}
	});
}

Blog.prototype.deletePost = function(postId, userId){
	$.facebox.loading();
	$.post("http://www.msjb.org/_server/server.blog.php", {
		action: "deletePost",
		postId: postId,
		userId: userId
		}, function(xml){
			if($('error',xml).text() == "No"){
				$("#Blog_dashPostMeta_"+postId).fadeOut("slow");
				$.facebox.close();
			} else {
				$.facebox("Brisanje posta trenutno nije moguće.");
			}
	});
}
Blog.prototype.unpublishPost = function(postId, userId){
	$.facebox.loading();
	$.post("http://www.msjb.org/_server/server.blog.php", {
		action: "unpublishPost",
		postId: postId,
		userId: userId
		}, function(xml){
			if($('error',xml).text() == "No"){
				var btn = '<a href="javascript:publishPost.ask(\'publishPost\',\'javascript:blog.publishPost(\\\''+postId+'\\\',\\\''+userId+'\\\')\')">Publish</a>';
				$("#Blog_togglePublishButton_"+postId).html(btn);
				var icon = '<img src="http://www.msjb.org/_images/imgBlogOptTrue.jpg" align="absmiddle" />';
				$("#Blog_togglePublishIcon_"+postId).html(icon);
				$.facebox.close();
			} else {
				$.facebox("Unpublish-ovanje posta trenutno nije moguće.");
			}
	});
}
Blog.prototype.publishPost = function(postId, userId){
	$.facebox.loading();
	$.post("http://www.msjb.org/_server/server.blog.php", {
		action: "publishPost",
		postId: postId,
		userId: userId
		}, function(xml){
			if($('error',xml).text() == "No"){
				var btn = '<a href="javascript:unpublishPost.ask(\'unpublishPost\',\'javascript:blog.unpublishPost(\\\''+postId+'\\\',\\\''+userId+'\\\')\')">Unpublish</a>';
				$("#Blog_togglePublishButton_"+postId).html(btn);
				var icon = '<img src="http://www.msjb.org/_images/imgBlogOptFalse.jpg" align="absmiddle" />';
				$("#Blog_togglePublishIcon_"+postId).html(icon);
				$.facebox.close();
			} else {
				$.facebox("Publish-ovanje posta trenutno nije moguće.");
			}
	});
}

Blog.prototype.showBlogerFeeds = function(id) {
	$("#"+id).slideDown("slow").toggle();
}


var blog = new Blog();