var tinyMCE = tinyMCE || null;

if (tinyMCE) {
	tinyMCE.init({
		mode : "textareas",
		theme : "advanced",
		editor_selector : "mceEditor",
		theme_advanced_buttons1 : "bold,italic,underline,separator,strikethrough,justifyleft,justifycenter,justifyright,justifyfull,bullist,numlist,undo,redo,image,link,unlink,code",
		theme_advanced_buttons2 : "",
		theme_advanced_buttons3 : "",
		theme_advanced_toolbar_location : "top",
		theme_advanced_toolbar_align : "left",
		theme_advanced_statusbar_location : "none",
		content_css : "stylesheets/custom_tinymce.css?" + new Date().getTime(),
		extended_valid_elements : "a[name|href|target|title],img[class|src|border=0|alt|title|hspace|vspace|width|height|align|name],hr[class|width|size|noshade],font[face|size|color|style],span[class|align|style]",
		entity_encoding : "raw"
	});
}

var Post = {
	tmp: '', // for storing the comment innerHTML

	deletePost: function (pid) {
		if(confirm('Are you sure you want to delete this post?')) {
			var onComplete = function(transport) {
				
				if (transport.responseText.length > 0) {
					if (transport.responseText == "1") {
						// remove the box
						var box = $('post-' + pid);
						var container = box.parentNode;
						container.removeChild(box);

						postCount--;
						if(postCount < 1 ) {
							
							$('post-text-default').style.display = '';
							
						}
					}
				}
			}.bind(this);
			this.ajax('ajaxpost.php', {a: 1, id: pid}, onComplete);
		}
	},

	deletePostSingle: function(pid) {
		if (confirm('Are you sure you want to delete this post?')) {
			var onComplete = function(transport) {
				if (transport.responseText.length > 0) {
					if (transport.responseText == "1") {
						// send them to the dashboard, this should be a header() redirect really
						window.location = 'dashboard.php';
					}
				}
			}
			this.ajax('ajaxpost.php', {a:1, id: pid}, onComplete);
		}
	},
	
	blockComments: function (action, pid) {
		var onComplete = function(transport) {
			if (transport.responseText.length > 0) {
				$('block-comments').innerHTML = transport.responseText;
				if (action == 4) {
					var cf = $('comments-footer');
					var h = document.createElement('h5');
					h.className = 'message-header';
					h.innerHTML = 'Commenting has been turned off for this post.';
					this.tmp = cf.innerHTML;
					cf.innerHTML = '';
					cf.appendChild(h);
				} else if (action == 5) {
					if(this.tmp.length > 0) {
						$('comments-footer').innerHTML = this.tmp;
					} else { // the page has been reloaded
						$('comments-footer').innerHTML = $('add-comment-hidden-container').innerHTML;
					}
				}
			} else {
				$('block-comments').innerHTML = '';
			}
		}.bind(this);
		
		this.ajax('ajaxpost.php', {a: action, id: pid}, onComplete);
	},
	
	deleteComment: function (commentid, pid) {
		var onComplete = function(transport) {
			if (transport.responseText.length > 0) {
				if (transport.responseText == "1") {
					//grab the comment
					var c = $('c-' + commentid);
					//and delete it
					c.parentNode.removeChild(c);

					var countHeader = $('comment-count-header');
					if (countHeader) {
						count = parseInt(countHeader.innerHTML);
						count--;
						if(count == 1) {
							countHeader.innerHTML = "1 Comment";
						} else {
							countHeader.innerHTML = count + " Comments";
						}
					}
					if (commentCount) {
						commentCount--;
						if (commentCount < 1) {
							$('post-text-default').style.display = '';
						}
					}
				}
			}
		}.bind(this)

		this.ajax('ajaxpost.php', {a: 6, cid: commentid, id: pid}, onComplete);
	},
	
	editComment: function (commentid, pid) {
		var obj = $('cc-' + commentid);
		var content = obj.textContent;
		obj.textContent = '';

		var child = document.createElement('textarea');
		child.id = 'ce-' + commentid;
		child.className = 'edit-comment';
		child.textContent = content;
		
		var btn = document.createElement('button');
		btn.textContent = "Save";
		btn.className = 'save-comment';
		btn.onclick = function() {
			this.updateComment(commentid, pid, obj);
		}.bind(this);
		
		obj.appendChild(child);
		obj.appendChild(btn);
	},
	
	updateComment: function (commentid, pid, obj) {
		var content = $('ce-' + commentid).value;
		var onComplete = function(transport) {
			if (transport.responseText == "1") {
				obj.textContent = content;
			}
		}.bind(this);

		this.ajax('ajaxpost.php', {a: 13, cid: commentid, ctext: content, id: pid}, onComplete);
	},
	
	flagComment: function (action, commentid, pid, caller) {
		var onComplete = function(transport) {
			if (transport.responseText == "1") {
				var a = document.createElement('a');
				a.setAttribute('href', 'javascript:void(0);');
				a.style.right = caller.style.right;
				a.title = 'This comment has been flagged.';
				a.className = 'post-flag-selected-link';
				caller.parentNode.replaceChild(a, caller);
			}
		}.bind(this);

		this.ajax('ajaxpost.php', {a: 7, cid: commentid, id: pid}, onComplete);
	},
	
	flagPost: function (pid, caller) {
		var onComplete = function(transport) {
			if (transport.responseText == "1") {
				// regular users can't unflag
				var a = document.createElement('a');
				a.setAttribute('href', 'javascript:void(0);');
				a.style.right = caller.style.right;
				a.title = "Post is flagged.";
				a.className = 'post-flag-selected-link';
				caller.parentNode.replaceChild(a, caller);
			}
		}.bind(this);

		this.ajax('ajaxpost.php', {a: 8, id: pid}, onComplete);
	},
	
	addFavorite: function(postId, userId, caller) {
		onComplete = function(transport) {
			if(transport.responseText == '1') {
				caller.title = "Click if you don't like it";
				caller.className = 'post-like-selected-link';
				caller.setAttribute('onClick', '');
				caller.onclick = function() {
					Post.removeFavorite(postId, userId, caller);
				}.bindAsEventListener(this);
			}
		};
		this.ajax('ajaxrating.php', {a: 1, postId: postId, userId: userId}, onComplete);
	},
	
	/*
		For view 1, 3
	*/
	removeFavorite: function(postId, userId, caller) {
		onComplete = function(transport) {
			if(transport.responseText == '1') {
				caller.title = "Click if you like it";
				caller.className = 'post-like-link';
				caller.setAttribute('onClick', '');
				caller.onclick = function() {
					Post.addFavorite(postId, userId, caller);
				}.bindAsEventListener(this);
				//caller.setAttribute('onClick', 'javascript: Post.addFavorite(' + postId + ', ' + userId + ', this);');
				//caller.onclick = 'javascript: Post.addFavorite(' + postId + ', ' + userId + ', this);';
			}
		}
		this.ajax('ajaxrating.php', {a: 2, postId: postId, userId: userId}, onComplete);
	},
	
	/*
		For view 4, acts similar to deletePost()
	*/
	deleteFavorite: function(pid) {
		if (confirm('Are you sure you want to remove this post from your favorites?')) {
			var onComplete = function(transport) {
				if (transport.responseText.length > 0) {
					if (transport.responseText == "1") {
						// remove the box
						var box = $('post-' + pid);
						var container = box.parentNode;
						container.removeChild(box);

						postCount--;
						if(postCount < 1) {
							//there's no posts left, show the default
							$('post-text-default').style.display = '';
						}
					}
				}
			}.bind(this)
			this.ajax('ajaxrating.php', {a: 2, postId: pid}, onComplete);
		}
	},
	
	removeAnchor: function (pid) {
		if(confirm('Are you sure you want to remove this anchor?')) {
			var onComplete = function(transport) {
				if (transport.responseText.length > 0) {
					if (transport.responseText == "1") {
						// remove the box
						var box = $('post-' + pid);
						var container = box.parentNode;
						container.removeChild(box);

						postCount--;
						if(postCount < 1 ) {
							$('post-text-default').style.display = '';
						}
					}
				}
			}.bind(this);
			this.ajax('ajaxpost.php', {a: 12, id: pid}, onComplete);
		}
	},
	
	addAnchor: function (pid, caller) {
		var onComplete = function(transport) {
			if (transport.responseText.length > 0) {
				if (transport.responseText == "1") {
					var a = document.createElement('a');
					a.setAttribute('href', 'javascript:void(0);');
					a.style.right = caller.style.right;
					a.title = 'This post is an anchor.';
					a.className = 'post-anchor-selected-link';
					caller.parentNode.replaceChild(a, caller);
				}
			}
		}.bind(this);

		this.ajax('ajaxpost.php', {a: 11, id: pid}, onComplete);
	}

};

Object.extend(Post, BaseClass);
