var Ideas = {
	commentCache: new Array(),
	spinner: null,
	request: null,
	
	vote: function(friendly_title, id)
	{
		if($('logged_in'))
		{
			new Ajax.Request('/vote/'+friendly_title+'?ajax=1', {asynchronus: true, evalScripts: true});
		}
		else
		{
			window.location = '/login';
		}
		return false;
	},

	removeVote: function(friendly_title, id)
	{
		if($('logged_in'))
		{
			new Ajax.Request('/vote/'+friendly_title+'/remove?ajax=1', {asynchronus: true, evalScripts: true});
		}
		else
		{
			window.location = '/login';
		}
		return false;
	},

	editComment: function(id)
	{
		this.spinner = new ActivityIndicator("body", {image: "/images/spinner_big.gif"});
		this.request = new Ajax.Request('/comments/edit/'+id+'?ajax=1', {method: 'get', onComplete: function(request) { Ideas.editCommentLoaded(request, id); }});
		return false;
	},

	editCommentLoaded: function(request, id)
	{
		if(this.request.header('Content-type').match(/^text\/javascript/i))
		{
			this.spinner.destroy();
			this.spinner = '';
			return;
		}
		if(request.responseText)
		{
			var container = document.createElement('blockquote');
			container.id = 'edit_comment_form_'+id;
			container.className = 'edit_comment';
			container.innerHTML = request.responseText;
			Element.hide('comment-'+id);
			$('comment_container_'+id).appendChild(container);
			$('edit_comment_'+id).focus();
		}
		this.spinner.destroy();
		this.spinner = '';
	},

	saveCommentEdit: function(id)
	{
		comment = $('edit_comment_'+id).value;
		if(comment == '')
		{
			return false;
		}
		this.spinner = new ActivityIndicator("body", {image: "/images/spinner_big.gif"});
		postData = "comment="+encodeURIComponent(comment).replace(/\+/g, "%2B");
		this.request = new Ajax.Request('/comments/edit/'+id+'?ajax=1', {method: 'post', postBody: postData, onComplete: function(request) { Ideas.commentSaved(request, id); }});
		return false;
	},

	commentSaved: function(request, id)
	{
		if(this.request.header('Content-type').match(/^text\/javascript/i))
		{
			this.spinner.destroy();
			this.spinner = '';
			return;
		}
		if(request.responseText)
		{
			$('comment-'+id).innerHTML = request.responseText;
			Element.remove('edit_comment_form_'+id);
			Element.show('comment-'+id);
		}
		this.spinner.destroy();
		this.spinner = '';
	},

	cancelCommentEdit: function(id)
	{
		Element.remove('edit_comment_form_'+id);
		Element.show('comment-'+id);
	},

	deleteComment: function(id)
	{
		if(confirm('Are you sure you wish to delete this comment?'))
		{
			new Ajax.Request('/comments/delete/'+id, {method: 'post', postBody: 'ajax=1', asynchronus: true, evalScripts: true});
		}
		return false;
	}
};