var addthis_pub = 'nicoswd';

var fields = {
	'subject' : 'Subject',
	'name'    : 'Name',
	'url'     : 'URL',
	'comment' : 'Comment',
	'code'    : 'Security code'
};


nicoswd.prototype.blog = function()
{
	/* Fancy constructor */
}


nicoswd.prototype.blog.toggleInfoBox = function(id, image)
{
	$('#' + id).animate({
		'height':  'toggle',
		'opacity': 'toggle'
    }, 'slow', function()
	{
		
		var img    = image.getElementsByTagName('img')[0];
		var isOpen = img.src.indexOf('open') == -1;
		img.src    = isOpen
			? img.src.replace('close', 'open')
			: img.src.replace('open', 'close');

/*
		if (isOpen)
			$(this).show().css({visibility: 'visible'});
		else
			$(this).show().css({visibility: 'hidden'});
*/

	});
	
	return false;
}


nicoswd.prototype.blog.displayForm = function(postid)
{
	var box = $('#commentform')[0];
	
	if (box.style.display == 'block')
	{
		for (var field in fields)
		{
			if ($.trim($('#' + field)[0].value))
			{
				if (confirm('Are you sure you want to close this box?'))
					break;
				else
					return false;
			}
		}
		
		// Hide error box, just in case.
		var errors = $('#errors');
		errors[0].innerHTML = '';
		errors.hide();
		
		$('#commentform').animate({
			'height': '0px'
		}, 1200, function() { $('#commentform').hide(); } );
		box.innerHTML = '';
		
		return false;
	}
	
	box.style.height = '0px';
	
	$('#commentform').animate({
		'height': '400px'
	}, 1200);
	
	box.style.textAlign = 'center';
	box.appendChild(nicoswd.loading_img());

	$.ajax({
		type: 'POST',
		url:   nicoswd.baseURL + '/xml.php',
		data: 'do=gettemplate&tpl=commentbox&postid=' + postid,
		dataType: 'xml',
		success: function(xml)
		{
			box.style.textAlign = '';
			box.innerHTML = xml.getElementsByTagName('tpl').item(0).firstChild.nodeValue;
			
			var fields = ['subject', 'name', 'url', 'comment', 'code'];
			var borderclass = 'blog_form_border';
			
			for (var i = 0; i < fields.length; i++)
			{
				$('#con_' + fields[i]).addClass(borderclass);
			
				$('#' + fields[i]).focus(highlight);
				$('#' + fields[i]).blur(unhighlight);
			}
			
			$('#code')[0].autocomplete = 'off';
		}
	});

	return false;
}


nicoswd.prototype.blog.postComment = function()
{
	var errorbox = new ErrorBox('errors', 542);
	var input    = [];
	
	for (var field in fields)
	{
		input[field] = $.trim($('#' + field)[0].value);
		
		if (!input[field])
		{
			errorbox.display('Please enter a ' + fields[field] + '.');
			$('#' + field)[0].focus();
			
			return false;
		}
	}
	
	// Everything okay so far.
	
	for (var field in fields)
	{
		$('#' + field)[0].disabled = true;
		$('#' + field)[0].readOnly = true;
	}
	
	var http = create_req_object();
	http.open('POST', BASE_URL + '/xml.php', true);
	
	
	return false;
}