// Blog functions.
var Blog = {

	// Deletes a blog post.
	delete_entry:function(post_id,title) {
		
		// Confirmation message will differ if there is a title.
		if(title==undefined) var phrase = 'this blog entry';
		else var phrase = 'the blog entry titled "'+title+'"';
		
		// Asks the user to confirm deletion.
		if(confirm('Are you sure you want to delete '+phrase+'?') && post_id!=undefined) {
			
			// The Ajax.
			new Ajax.Request(engineURL, { method:'post',
				parameters:{ control:'container', action:'blog_delete', post_id:post_id },
			onComplete:function(transport) {
				Container.refresh_blog_viewer();
				response = transport.responseText.substr(7);
				if($('blog_entry_'+response)) {
					Blog.refresh_entry(response);
				}
				else if(window.location=domainROOT+'/backstage') { window.location=window.location; }
			}});
		}
	},

	// Deletes an entry from the blog roll.
	delete_roll:function(roll_id) {
		
		// Requires the user to confirm.
		if(confirm('Are you sure you want to delete this link?')) {
		
			// Removes the link form the list.
			if($('blog_roll_link_'+roll_id)) $('blog_roll_link_'+roll_id).remove();
		
			// If there's nothing left, show a message.
			if($('popup_blog_roll_edit_0').innerHTML=='') $('popup_blog_roll_edit_0').update('<p>You don\'t have any links in your blog roll.</p>');
		
			// The Ajax.
			new Ajax.Request(engineURL, { method:'post',
				parameters:{ control:'container', action:'blog_roll_delete', roll_id:roll_id },
			onComplete:function(transport) {
				
				// Updates the blog roll container.
				Blog.update_roll();
			}});
		}
	},

	// Allows the user to edit his/her blog roll.
	edit_roll:function() {
		Container.show_popup('blog_roll_edit');
	},

	// Allows the user to post and edit blog entries.
	post_entry:function(post_id,cat_id) {
		Container.show_popup('blog_post',post_id==undefined ? 0 : post_id,cat_id==undefined ? 0 : cat_id);
	},
	
	// Refreshes the display of a blog entry.
	refresh_entry:function(post_id) {
		
		// Makes sure the post ID was specified and the entry is shown.
		if(post_id!=undefined && $('blog_entry_'+post_id)) {
			
			// The Ajax.
			new Ajax.Request(engineURL, { method:'post',
				parameters:{ control:'container', action:'blog_refresh', post_id:post_id },
			onComplete:function(transport) {
				if(transport.responseText=='') alert('error');
				else $('blog_entry_'+post_id).replace(transport.responseText);
			}});
		}
	},
	
	// The "Show More Blog Entries" link.
	show_more:function(blogger_id,shown_so_far,is_cat) {
		
		// Are we showing more for a category blog?
		if(is_cat==undefined || !is_cat) is_cat = false;
		else is_cat = true;
		
		// The Ajax.
		new Ajax.Request(engineURL, { method:'post',
			parameters:{ control:'container', action:'blog_show_more', blogger_id:blogger_id, shown_so_far:shown_so_far, is_cat:is_cat },
		onComplete:function(transport) {
			if(transport.responseText=='') alert('error');
			else $('show_more_blog_entries').replace(transport.responseText);
		}});
	},
	
	// Subscribing to or unsubscribing from a blog. Only shows a container on error.
	subscribe:function(username,unsubscribe) {
		if(username==undefined) Container.show_alert('You must specify a blog to subscribe to.','Error');
		else {
			if(unsubscribe==undefined) unsubscribe = false;
			Container.show_popup('blog_subscribe',username,unsubscribe ? '0' : '1');
		}
	},
	
	// Unsubscribes from a blog.
	unsubscribe:function(username) {
		Blog.subscribe(username,true);
	},
	
	// Updates the blog roll container.
	update_roll:function() {
		Container.refresh_container('blog_roll_'+Blog.blogger_id);
	}
}