/*
 * add a new task
 */
fn.task.add = function(){
	// clone the tempate and give it an id 
	var task = $('#templates .task').clone(true);
	task.attr('id', '_' + fn.functions.uniqId() );

	// add it to the list
	task.prependTo('#tasksList');

	// make the title editable
	fn.task.editH3.call( $('h3 .label', task) );	
};

/**
 * Change the task state
 */
fn.task.state = function(e){
	// get clicked and make sure it's an anchor
	var clicked = $(e.target);
	if ( clicked.is(':not(a)') ) return;

	// get variables
	var task = clicked.closest('.task');
	var prev = $('.states .selected', task);
	var prevId = prev.attr('title');
	if ( undefined === prevId ) prevId = '';
	var nextId = clicked.attr('title');

	// add remove the state as a class
	task
	.removeClass( prevId )
	.addClass( nextId );
	
	// update states' classes
	$(prev).removeClass('selected');
	clicked.addClass('selected');
	
	// update task's state flag
	$('.state', task)
	.text( clicked.text() )
	.css({
		backgroundColor: clicked.css('backgroundColor')
	});

	// show comment and make it editable
	$('.comment', task).removeClass('hidden');
	fn.task.editComment.call($('.comment span', task));
	
	fn.account.save();
};

/**
 * Make h3 of task editable
 */
fn.task.editH3 = function(){
	// set 'title' to send into event functions
	var title = this;
	
	// save edit function
	function saveEdit()
	{
		$(title).text( $(this).val() );
		fn.account.save();
	};

	// replace html and assign save actions
	$(title)
	.html( '<input type="text" name="title" value="' + $(title).text() + '" />' )
	.find('input:first')
	.keyup(function(e) {
		// on 'enter' key, set and save value
		if(e.keyCode == 13) 
		{
			saveEdit.call(this);
			return false;
		};
	})
	.blur(function(e) {
		saveEdit.call(this);
	})
	.focus();
};

/**
 * make comment editable
 */
fn.task.editComment = function(){
	var span = this;

	// save edit function
	function saveEdit ()
	{
		// if there's no comment, hide the comment element 
		if ( $(this).val() == '' ) $(this).parents('.comment').addClass('hidden');
		
		// get and format the existing value
		var text = fn.functions.trim( $(this).val() );
		text = fn.functions.url2Link(text); // parse urls
		text = fn.functions.n2Br(text);	// parse \n's
		$(span).html( text );
		fn.account.save();
	};
	
	// get currect value
	var text = fn.functions.trim( $(span).text() );
	text = fn.functions.br2N(text);	// parse \n's
	
	// replace text w html form, add events
	$(span)
	.html( '<textarea>' + text + '</textarea>' )
	.find('textarea:first')
	.keyup(function(e) {
		if(e.shiftKey==1 && e.keyCode == 13) 
		{
			saveEdit.call(this);
			return false;
		};
	})
	.blur(function(e) {
		saveEdit.call(this);
		fn.account.save();
	})
	.focus()
	.autoResize({
		animateDuration : 0, 
		extraSpace : 0
	})
	.keyup()
	.select();
};

fn.task.populate = function(task){
	var html = $('#templates .task').clone(true);
	html.attr('id', task.id );

	html.appendTo('#tasksList');
	
	$('h3 .title', html).text(task.title);
	var selected = $('.states a[title=' + task.state + ']', html)
	.addClass('selected');

	$(html).addClass(task.state);

	$('.state', html)
	.text( selected.text() )
	.css({
		backgroundColor: selected.css('backgroundColor') 
	});

	if ( task.comment.length > 0)
	{
		var comment = $('.comment', html);
		comment.removeClass('hidden');
		$('span', comment).text(task.comment);		
	};
};

/**
 * remove a task
 */
fn.task.remove = function(){
	$(this).remove();
	fn.account.save();
};

/**
 * filter tasks by state clicked
 */
fn.tasks.filter = function(e)
{
	// get clicked and ignore if already selected
	var clicked = $(e.target);
	if ( clicked.is(':not(a), .selected') ) return;
	
	var filterList = clicked.closest('.filterList');

	// set selected flag on correct state
	$('.selected', filterList).removeClass('selected');
	clicked.addClass('selected', this);

	// show all
	if ( clicked.is('.all') ) 
	{
		$('#tasksList > li' ).show();
		return;
	};

	// filter tasks
	$('#tasksList > li:not(.' + clicked.attr('title') + ')' ).hide();
	$('#tasksList > li.' + clicked.attr('title') ).show();
};