// FTwitt List Management
// Coded by Marian Busoi, 4 Copimaj

// Deletes a list (either a saved list or just the form)
function lists_delete(obj) {
	obj = $(obj).parent().parent();
	
	if(obj.hasClass("new_list")){
		obj.remove();
	} else { 
		if(window.confirm("Are you sure you want to delete this list from your Twitter account?")) {
			// list is from Twitter
			var listID = obj.attr("id").substr(5); // fetch list id
			
			$.get('includes/ajax/lists_delete.php', {id: listID},
				  function (data) {
					if(data!='1') {
						$('#basic-modal-content').html("Could not delete list. Please try again!");
						$('#basic-modal-content').modal( { autoResize: false, maxWidth: 300, maxHeight: 150, position: ['30%'] } );
					} else {
						obj.remove();
					}
				  });
		}
	}
}

// Displays a form for editing a list
function lists_edit(obj) {
	var txt = $(obj).parent().siblings("a");

	var editListForm = "<input size='10' value='" + txt.html() + "' onkeypress='if(event.keyCode==13 || event.keyCode==10) lists_update($(this).siblings(\".gr_controls\").find(\".gr_edit\"));' onfocus='if(this.value==\"List name\") this.value=\"\";' onblur='if(this.value==\"\") this.value=\"List name\";' />";
	
	txt.replaceWith( $(editListForm) );
	
	var saveBtn = $('<a class="gr_edit" href="#" title="save" onclick="lists_update(this)">save</a>');
	$(obj).replaceWith( saveBtn );	
}

// Updates an existing list
function lists_update(obj) {
	var listID = $(obj).parent().parent().attr("id").substr(5); // fetch list id
	
	var inpt = $(obj).parent().siblings("input");
	
	if(inpt.val().length < 4) {
		$('#basic-modal-content').html("List name must be at least 4 characters long!");
		$('#basic-modal-content').modal( { autoResize: false, maxWidth: 300, maxHeight: 150, position: ['30%'] } );
	} 
	else {
		$.getJSON('includes/ajax/lists_edit.php?callback=?', {name: inpt.val(), id: listID},
				function (data) {
					if(data && data.error) {
						$('#basic-modal-content').html(data.error);
						$('#basic-modal-content').modal( { autoResize: false, maxWidth: 300, maxHeight: 150, position: ['30%'] } );					
					} 
					else if(data) {	// alter the DOM a bit
						inpt.replaceWith( $('<a class="l_link" href="#">' + inpt.val() + '</a>') );
						var editBtn = $('<a class="gr_edit" href="#" title="edit" onclick="lists_edit(this)">edit</a>');
						$(obj).replaceWith( editBtn );
					}
				});
	}
}

// Displays a form for adding a new list
function lists_new() {
	var addGroupForm = "<input size='10' value='List name' onkeypress='if(event.keyCode==13 || event.keyCode==10) lists_save($(this).siblings(\".gr_controls\").find(\".gr_edit\"));' onfocus='if(this.value==\"List name\") this.value=\"\";' onblur='if(this.value==\"\") this.value=\"List name\";' />";
	$("<li></li>").addClass('li_gr_wrapper new_list')
				  .appendTo( $('#l_listing') )
				  .append( $(addGroupForm) )
				  .append( $("<div></div>").addClass("gr_controls")
										   .append( $('<a class="gr_edit" href="#" title="save" onclick="lists_save(this)">save</a>') )
										   .append( $('<a class="gr_delete" href="#" title="delete list" onclick="lists_delete(this)">delete</a>') )
						  );
}

// Saves a new list
function lists_save(obj) {
	var inpt = $(obj).parent().siblings("input");
	
	if(inpt.val().length < 4) {
		$('#basic-modal-content').html("List name must be at least 4 characters long!");
		$('#basic-modal-content').modal( { autoResize: false, maxWidth: 300, maxHeight: 150, position: ['30%'] } );
	} 
	else {
		$.getJSON('includes/ajax/lists_add.php?callback=?', {name: inpt.val()},
				function (data) {
					if(data && data.error) {
						$('#basic-modal-content').html(data.error);
						$('#basic-modal-content').modal( { autoResize: false, maxWidth: 300, maxHeight: 150, position: ['30%'] } );					
					} 
					else if(data) {	// alter the DOM a bit
						inpt.parent().attr("id", "list_" + data.id);
						inpt.parent().removeClass("new_list");
						inpt.replaceWith( $('<a class="l_link" href="#" onclick="tm.addTabs(\'list:' + data.slug + '\', \'list\')">' + inpt.val() + '</a>') );
						$(obj).attr("title", "edit");
						$(obj).click(function () { lists_edit(this); });
						$(obj).html("edit");
					}
				});
	}
}