ListManager = function() {
    
}

ListManager.prototype = {
	listId : 0,
	total : 0,
	index : 0,
	page : 1,
	appDir : '',
	showCommentLabel : '',
	hideCommentLabel : '',
	username : '',
	
	selfTest : function(){
		console.log(this);
	},
	
	getList : function(){
		var thisObject = this;
		var getListCallback = function(response){
			if(response){
				for(i in response.ListItem){
					thisObject.addItem(response.ListItem[i]);
				}
			}
		}
	
		var param = '';
		if(this.listId != 0){
			param += '/' + this.listId;
		}
		
		this.page++;
		$.get('/' + this.appDir + 'tlists/ajax_list/' + this.username + '/' + this.page + param, null, getListCallback, 'json');
	},
	
	addItem : function(item, prepend){
		var thisObject = this;
	
		var itemIndex = this.index;
		if(!prepend){
			this.index--;
		}else{
			this.total++;
			itemIndex = this.total;
		}
		
		var div = $('#item-modal').clone();
		
		$(div).attr('id', item.id);
		$(div).find('.item-index').text(itemIndex);
		$(div).find('.item-header-date').prepend(item.created);
		$(div).find('.item-content-text').html(filterItemContent(item.content));
		$(div).find('.item-header-delete').click(function(){
			thisObject.deleteItem(item.id);
		});
		$(div).find('.favorite').click(function(){
			thisObject.favoriteItem(item.id);
		});
		$(div).find('.copy').click(function(){
			thisObject.copyItem(item.id);
		});
		$(div).find('.comment').click(function(){
			thisObject.commentItem(item.id);
		});
		
		$(div).find('.item-done').attr('id', 'item-done-' + item.id)
		$(div).find('.done').click(function(){
			thisObject.markItemAsDone(item.id);
		});
		$(div).find('.item-content').hover(
			function(){
				$(div).find('.item-content-action').show();
			},
			function(){
				$(div).find('.item-content-action').hide();
			}
		)
		
		if(!prepend){
			$('#list').append(div);
		}else{
			$('#list').prepend(div);
		}
			
		if(item.comment != undefined){
			$(div).find('.item-comment').text(item.comment.length);
			$(div).find('.show-comment').attr('title', showCommentLabel);
			$(div).find('.show-comment').click(function(){
				thisObject.showComment(item.id);
			});
			$(div).find('.show-comment').css('cursor', 'pointer');
			
			for(var i in item.comment){
				thisObject.addComment(item.id, item.comment[i]);	
			}
		}else{
			$(div).find('.item-comment').text(0);
		}
		
		$(div).fadeIn('slow');
	},
	
	addComment : function(itemId, comment){
		var div = $('#item-comment-modal').clone();
		
		$(div).attr('id', 'comment-' + comment.id);
		$(div).find('.list-item-comment-picture').html('<img src="http://www.gravatar.com/avatar/' + $.md5(comment.user.email) + '?s=35&r=g">');
		$(div).find('.comment-user').html('<a href="/' + comment.user.username + '">' + comment.user.full_name + '</a>');
		$(div).find('.comment-content').html(comment.comment);
		$(div).find('.comment-time').text(comment.created);
		
		$('#' + itemId).find('.list-item-comment-list').append(div);
		$(div).fadeIn('slow');
	},
	
	deleteItem : function(itemId){
		console.log(itemId);
	},
	
	copyItem : function(itemId){
		console.log(itemId);
	},
	
	commentItem : function(itemId){
		closeAllCommentInput();
		showComment(id, false);
		
		var div = $('#item-comment-input-modal').clone();
		
		$(div).removeClass('modal');
		
		$('#' + id).append(div);
		$(div).attr('id', 'commentInput-' + id);
		$(div).fadeIn('slow');
		
		$(div).find('.comment-submit').click(function(){
			if($(div).find('.comment-input').val() != ''){
				$(this).attr('disabled', true);
				
				var content = $(div).find('.comment-input').val();
				$.post('/' + appDir + 'tlists/ajax_add_comment', {content: content, list_item_id: id}, addCommentCallback, 'json');
			}
		});
		
		$(div).find('.comment-cancel').click(function(){
			closeAllCommentInput();
		});
	},
	
	markItemAsDone : function(itemId){
		console.log(itemId);
	},
	
	deleteList : function(listId, callback){
		if(confirm('All your ToDos under this list will be removed when you delete this list. Please confirm.')){
			$.get('/' + this.appDir + 'tlists/ajax_delete_list/' + listId, callback);
		}
	},
	
	editList : function(listId){
		$.fn.colorbox({href: "/" + this.appDir + "list/edit/" + listId, width: '450px', overlayClose: true, disableClose: false, iframe: true, innerWidth:425, innerHeight:350, scrolling: false });
	},
    
    toggleVisibility : function(listId){
        $.get('/' + this.appDir + 'tlists/ajax_toggle_visibility/' + listId, function(){
            document.location.reload(0);
        });
    }
}
