task = {
	id: 0,
	filter: "",
	sorting: "",
	
	setCloseState: function(close_state, on_load) {
		task_ui.setCloseState(close_state, on_load);
	},
	
	getProgress: function() {
		$.ajax({
			type: 'POST',
			url: '/task/check-stats/',
			data: {id: this.id},
			success: function(data) {
				eval(data);
			}
		});
	},
	
	addPost: function() {
		document.report_form.submit();
	},
	
	setFilter: function(filter) {
		this.filter = filter;
	},
	
	setSorting: function(sorting) {
		this.sorting = sorting;
	},
	
	setId: function(id) {
		this.id = id;
	},

	deletePost: function(id) {
		$.ajax({
			type: 'POST',
			url: '/posts/delete/',
			data: {id: id},
			success: function(data) {
				eval(data);
			}
		});
		return false;
	},

	checkClosed: function() {
		$.ajax({
			type: 'GET',
			url: '/task/is-writeble/id/'+this.id,
			success: function(data) {
				eval(data);
			}
		});
	},
	
	addDraught: function() {
		url = $("#draught_form input[name=site_url]").val();
		text = $("#draught_form textarea[name=message_text]").val();
		$.ajax({
			type: 'POST',
			url: '/posts/add/',
			data: {task_id: this.id, url:url, text:text},
			success: function(data) {
				task.updatePosts();
			}
		});
	},
	
	editDraught: function() {
		post_id = $("#draught_form input[name=post_id]").val();
		url = $("#draught_form input[name=site_url]").val();
		text = $("#draught_form textarea[name=message_text]").val();
		$.ajax({
			type: 'POST',
			url: '/posts/edit/',
			data: {id: post_id, url: url, text: text},
			success: function(data) {
				task.updatePosts();
			}
		});
	},
	
	addReserve: function() {
		$.ajax({
			type: 'GET',
			url: '/cashReserve/add/task_id/'+this.id,
			success: function(data) {
				eval(data);
			}
		});
	},
	
	updatePosts: function() {
		$.ajax({
			type: 'POST',
			url: '/posts/show/',
			data: {task_id: this.id, sort: this.sorting, filter: this.filter},
			success: function(data) {
				task_ui.viewPosts(data);
			}
		});
	}

};
















// client

function showComment(id) {
	alert($("#admin_descr_"+id).html());
	return false;
}
function checkTaskUpdates() {
	$.ajax({
		type: "GET",
		url: "/task/response/task_id/"+$("#create_report_form .task_id").val(),
		success: function(data) {
			eval(data);
		}
	});
	setTimeout(checkTaskUpdates, 60000);	
}

function showError() {
	alert('Возникла ошибка. Попробуйте позже.');
}
function edit_report(id) {
	url = $('input.id[value='+id+']').parent().parent().find("td.url").html();
	message_text = $('input.id[value='+id+']').parent().parent().find("td.infotd").find("span.hide").html();
	$("#create_report_form .report_id").val(id);
	$('#create_report_form .message_text').val(message_text);
	$("#create_report_form .site_url").val(url);
	$('#create_report_form').modal();
	return false;	
}

function delete_report(id) {
	if (confirm('Действительно удалить?')) {
		action = $('input.id[value='+id+']').html();
		$("#report"+id).find("td.action").html('<img src="/images/loader2.gif" style="text-align: center; margin: 0 auto;" />');
		$.ajax({
			type: "GET",
			url: "/task/deletereport/id/"+id,
			success: function(data){
				$("#report"+id).find("td.action").html(action);
				eval(data);
			},
			error: function(msg) {
				$("#report"+id).find("td.action").html(action);
				showError();
				return false;
			}
		});
	}
   	return false;
}

function edit_demand(id) {
	url = $('input.id[value='+id+']').parent().parent().find("td.url").html();
	message_text = $('input.id[value='+id+']').parent().parent().find("td.infotd").find("span.hide").html();
	$("#create_demand_form a.add").html("сохранить");
	$("#create_demand_form .demand_id").val(id);
	$('#create_demand_form .message_text').val(message_text);
	$("#create_demand_form .site_url").val(url);
	$('#create_demand_form').modal();
	return false;
}

function delete_demand(id) {
	if (confirm('Действительно удалить?')) {
		action = $('input.id[value='+id+']').parent().html();
		$('input.id[value='+id+']').parent().html('<img src="/images/loader2.gif" style="text-align: center; margin: 0 auto;" />');
		$.ajax({
			type: "GET",
			url: "/task/deletedemand/id/"+id,
			success: function(data){
				$('#demand'+id).find("td.action").html(action);
				eval(data);
			},
			error: function(msg) {
				$('#demand'+id).find("td.action").html(action);
				showError();
				return false;
			}
		});
	}
   	return false;
}

/*$(document).ready(function(){
   $('#tabs').tabify();  
	$("#report_form").ajaxForm({
		success:function(data){
			if (data.length>0) {
				eval(data);
			}
			$.modal.close();
			checkTaskUpdates();
		}
	});
   $("#create_report_form a.add").click(function(){
   		message_text = document.report_form.message_text.value;
   		valid = true;
   		$("#create_report_form .message_text").css("border", "1px solid #abadb3");
   		if (message_text.length==0) {
   			valid = false;
   			$("#create_report_form .message_text").css("border", "1px solid red");
   			$("#create_report_form .message_text").focus();
   		} 
   		if (valid) {
   			//$("#report_form").submit();
   			document.report_form.submit();
   		}
   });
   $("#create_demand_form a.add").click(function(){
   		site_url = document.demand_form.site_url.value;
   		message_text = document.demand_form.message_text.value;
   		task_id = document.demand_form.task_id.value;
   		site_id = document.demand_form.demand_id.value;
   		valid = true;
   		$("#create_demand_form .site_url").css("border", "1px solid #abadb3");
   		$("#create_demand_form .message_text").css("border", "1px solid #abadb3");
   		if (site_url=='http://') {
   			valid = false;	
   			$("#create_demand_form .site_url").css("border", "1px solid red");
   			$("#create_demand_form .site_url").focus();
   		} else if (message_text.length==0) {
   			valid = false;
   			$("#create_demand_form .message_text").css("border", "1px solid red");
   			$("#create_demand_form .message_text").focus();
   		} 
   		if (valid) {
   			$.ajax({
   				type: "POST",
   				url: "/task/adddemand/",
   				data: {id:site_id, task_id:task_id, url:site_url, text:message_text},
   				success: function(data){
	   				eval(data);
	   				$.modal.close();
	   			},
				error: function(msg) {
					showError();
					return false;
				}	
   			});
   		}
   		return false;
   });
   $('#create_demand').click(function(){
   		checkWriteble();
   });
   $('#create_demand_form a.cancel').click(function(){
   		$.modal.close();
   		return false;
   });
   $('#create_report_form a.cancel').click(function(){
   		$.modal.close();
   		return false;
   });
   incInput(null);
   setTimeout(checkTaskUpdates, 60000);
});*/