var Captcha = function() {

	/**
	 * Attaches click event to refresh captcha link
	 *
	 * @param void
	 * @return void
	 */
	var init = function()
	{
		jQuery("#refresh-captcha").click(function() {
			fetchCaptchaId();
			return false;
		});
	};

	/**
	 * Fetches new captcha id and image from the server.
	 * Sends old captcha id to server to be destroyed
	 *
	 * @param void
	 * @return void
	 */
	var fetchCaptchaId = function()
	{
		jQuery.ajax({
			'url': '/captcha/captcha.php',
			'data': {
				'captchaid': jQuery('#captcha-id').val()
			},
			'dataType': 'html',
			'type': 'post',
			'success': function(response) {
				response = response.split('``');
				jQuery("#captcha-id").val(response[0]);
				jQuery("#captcha-img").html(response[1]);
			},
			'error': function(XMLHttpRequest, textStatus, errorThrown){}
		});
	}

	return {
		init : function() {
			init();
		}
	}
}();

jQuery(document).ready(function() {
	Captcha.init();
});


