var mod_blogs_incorrect_obj = {	
	ready : true,
	incorrect_id: 0,
	type: null,
	maxlength: 200,
	temp_comment: "",
	
	refresh_captha: function()
	{
		var captcha_img = $('.incorrect_container .captcha_code_img');
		var captcha_code = $('.incorrect_container .captcha_code');
		if (captcha_img!=null && captcha_code!=null)
		{
			captcha_code.val('');
			captcha_img.attr("src", "");
			captcha_img.attr("src", "/service/captcha/?r="+Math.random());
		}
	},

		
	hide: function()
	{
		var container = $('#incorrect_adv_div');
		container.append($('#incorrect_form'));		
		$('#incorrect_form .incorrect_reason option:first').attr('selected', 'yes');
		
		var captcha_img = $('.incorrect_container .captcha_code_img');		
		if (captcha_img!=null)
			captcha_img.attr("src", "");

				
		$('.incorrect_container').hide();		
		this.incorrect_id = 0;
	},
	
	show: function(id, _type) 
	{		
		if (!this.ready)
		{
			alert("В данный момент происходит обработка, попробуйте позднее");
			return false;
		}
		
		this.type = _type;
		
		if(this.incorrect_id == id)
		{
			this.hide();
			return;
		}	
		this.hide();		
		this.incorrect_id = id;
		
		var el = $('#incorrect_container_'+this.incorrect_id);		
		
		
		el.append($('#incorrect_form'));
		$('#incorrect_form').show();
		this.refresh_captha();
		el.show();		
		return false;
	},
	
	incorrect_adv_success: function(data, textStatus)
	{
		mod_blogs_incorrect_obj.ready = true;
		$('.incorrect_container .send_incorrect').removeAttr('disabled');
		if (data.status==0)
		{					
			alert(data.success);
			mod_blogs_incorrect_obj.hide();
			$('#incorrect_form .incorrect_reason option:first').attr('selected', 'yes');
		}
		else
		{
			alert(data.error);
			mod_blogs_incorrect_obj.refresh_captha();
		}
	},
	
	incorrect_adv_error: function(XMLHttpRequest, textStatus, errorThrown)
	{
		$('.incorrect_container .send_incorrect').removeAttr('disabled');
		alert("Ошибка");
		mod_blogs_incorrect_obj.ready = true;
		mod_blogs_incorrect_obj.refresh_captha();
	},
	
	incorrect_adv: function()
	{	
		if (!this.ready)
		{
			alert("В данный момент происходит обработка, попробуйте позднее");
			return false;
		}
		
		this.ready = false;
		$('.incorrect_container .send_incorrect').attr('disabled', 'true');
		$.ajax({
			url: '/' + $('#section_name').val() + '/complain/',
			type: 'POST',
			dataType: 'json',
			data: {
				ajax: 'true',
				action: 'incorrect',
				type: this.type,
				reasonType: $('.incorrect_container .incorrect_reason').val(),
				id: mod_blogs_incorrect_obj.incorrect_id,
				comment: $('.incorrect_container .incorrect_comments').val(),
				captcha_code: $('.incorrect_container .captcha_code').val()
			},
			success: this.incorrect_adv_success,
			error: this.incorrect_adv_error
		});
		
	},
	
	onchange_incorrect_reason: function()
	{		
		var incorrect_reason = $('.incorrect_container .incorrect_reason');
		if (incorrect_reason.val()=='other_marks')
			$('.incorrect_container .incorrect_comments').show();
		else
			$('.incorrect_container .incorrect_comments').hide();
	},
	
	textarea_maxlength: function()
	{
		var incorrect_comments = $('.incorrect_container .incorrect_comments');
		var comment = incorrect_comments.val();
		if(comment.length > this.maxlength) 
		{
			incorrect_comments.val(comment.substring(0, this.maxlength));
		}
	}
}
