$(document).ready(function() {

	// persistent UI functionality

	$(".list-item").live("mouseover", function() {
		$(this).addClass("highlight");
		if (typeof(makeTagsSelect) == "function") {
			console.log($(this));
		}

		// $(this).addClass("highlight").find(".list-item-tools").show();
	});
	$(".list-item").live("mouseout", function() {
		$(this).removeClass("highlight");
		// $(this).removeClass("highlight").find(".list-item-tools").hide();
	});
	
	// $(".list-item").live("click", function() {
	// 	$(".active").removeClass("active");
	// 	$(this).addClass("active");
	// });
	
	$(".list-item-tags li").live("mouseover", function() {
		$(this).addClass("highlight");
	});
	$(".list-item-tags li").live("mouseout", function() {
		$(this).removeClass("highlight");
	});
	
	$("#new-item input, #new-item textarea").focus(function() {
		$(this).prev("label").hide();
	});
	
	$("#new-item input, #new-item textarea").blur(function() {
		if ($(this).val() == "") $(this).prev("label").show();
	});
	
	tagLabelHandlers();
	
	$("#toggleItems").live("click", function() {
		$("#completed-items>ul").slideToggle();
		return false;
	});
	
	$(".close-item a").live("click", function() {
		$(this).parents(".list-item").toggleClass("active");
		return false;
	});
	
	
	$(".add-tags-form").live("submit", function() {
		addTag($(this).find("input[name=value]"));
		return false;
	});
	
	$("#my-lists-toggle").hover(function() {
		$("#my-lists").show();
		$("#my-lists").hover(function() {
			$(this).show();
		}, function() {
			$(this).hide();
		});
	}, function() {
		$("#my-lists").hide();
	});
	
	$("#new-list-toggle").live("click", function() {
		$("#add-new-list, #new-list-cancel").show();
		return false;
		
	});
	
	$("#new-list-cancel").live("click", function() {
		$("#add-new-list, #new-list-cancel").hide();
		return false;
	});
	
		
	$(".inline-label input[type=text]").each(function() {
		if ($(this).val() != "") $(this).prev("label").hide();
	});
	
	$(".inline-label input").focus(function() {
		$(this).prev("label").hide();
	});
	$(".inline-label input").blur(function() {
		if ($(this).val() == "") $(this).prev("label").show();
	});




	// first tag display
	firstTag();


	// insert item functionality

	// set up editables (item title, note)
	$(".editable").editable("/lib/php/save.php");
	
	

	// set up new tag boxes (not using editable, custom ajax)
	$(".new-tag form").submit(function() {
		var el = $(this).find("input")[1];
		addTag(el);
		return false;
	});

	$(".new-tag input").blur(function() {
		addTag(this);
	});
	
	
	$(".rename-list").live("click", function() {
		$(this).text("Cancel").removeClass("rename-list").addClass("cancel");
		li = $(this).parents("li").attr("id");
		str = $(this).parents("li").find("a").eq(0).text();
		$(this).parents("li").find("a").eq(0).hide();
		$(this).parents("li").append('<form id="rename-list"><input maxlength="32" type="text" name="newname" value="'+str+'" id="newname" /></form>');
		$("#newname").focus();
		
		$("#rename-list").live("submit", function() {
			if ($("#newname").val() != "") {
				
				$.post("/lib/php/ajax.php", {
					list : li,
					newname : $("#newname").val(),
					renameList : true
				}, function (data) {
					$("#lists").html(data);
					$("#my-lists").load("/lib/php/user-lists-view.php");
				});
			}
			
			return false;
		});

		return false;
		
	});
	
	$(".cancel").live("click", function() {
		$(this).text("Rename").removeClass("cancel").addClass("rename-list");
		$(this).parents("li").find("form").eq(0).remove();
		$(this).parents("li").find("a").eq(0).show();
		return false;
	});
	


	// new list item submit ajax

	$("#new-item-form").submit(function() {
		
		if ($("#new-item #item-title").val() != "") {
			$.post("/lib/php/ajax.php",
			 	{
					item_title : $("#new-item #item-title").val(),
					item_tags : $("#new-item #item-tags").val(),
					item_note : $("#new-item #item-note").val(),
					list : $("#new-item #list").val(),
					insert : "true"
				},
				function(data) {
					$("#list-items").html(data);
					$(".new input").val("").blur().prev("label").show();
					$(".new input").eq(0).focus();
					$("#list-tags ul").load("/lib/php/tags-view.php", function() {
						if (typeof(makeTagsSelect) == "function") {
							makeTagsSelect(false);
						}
					});
					tagLabelHandlers();
					firstTag();
					$(".editable").editable("/lib/php/save.php");


				});
		}
		return false;
	});
	
	
	$("#account-info #addnewlist").submit(function() {
		if ($("#list-name").val() != "") {
			$("#account-info #addnewlist .error").remove();
			$.post("/lib/php/ajax.php", {
				list_name : $("#list-name").val(),
				addnewlist : true
			}, function(data) {
				$("#my-lists").html(data);
			});
			return false;
		} else {
			$("#account-info #add-new-list").append('<span class="error">You need to enter a name for your list</span>');
			return false;
		}
	});



	// delete tag from item
	
	$(".tag-delete").live("click", function() {
		li = $(this).parents(".list-item").attr("id");
		$.post("/lib/php/delete.php", {
			deleteTag : 'true',
			li : li,
			tag : $(this).next("a").text()
		}, function(data) {
			$("#list-items").html(data);
			$("#list-tags ul").load("/lib/php/tags-view.php", function() {
				if (typeof(makeTagsSelect) == "function") {
					makeTagsSelect(false);
				}
			});
			
			$("#completed-items").load("/lib/php/completed-view.php");
			var i = "#"+li;
			$(i).addClass("active");
			
			tagLabelHandlers();
			firstTag();
			$(".editable").editable("/lib/php/save.php");
		});
		
		return false;
	
	});
	
	
	// delete item from list
	
	$(".list-item-delete").live("click", function() {
		li = $(this).parents(".list-item").attr("id");
		var theList = $(this).parents("#completed-items").length != 0 ? "completed" : "remaining";
		

		$.modal('<div class="delete-confirm"><p>Warning! Deleting an item can\'t be undone. Are you sure you want to delete this item?</p><p class="buttons"><button id="delete-yes">Yup</button><button id="delete-no">Nope, take me back!</button></p></div>');
		
		$("#delete-yes").click(function() {
			$.post("/lib/php/delete.php", {
				deleteItem : 'true',
				li : li
			}, function(data) {
				$("#list-items").html(data);
				$("#list-tags ul").load("/lib/php/tags-view.php", function() {
					if (typeof(makeTagsSelect) == "function") {
						makeTagsSelect(false);
					}
				});
				
				$("#completed-items").load("/lib/php/completed-view.php", function() {
					if (theList == "completed") $("#completed-items ul").show();
				});
				
				// $(".delete-confirm").remove();
				$.modal.close();
				
				tagLabelHandlers();
				firstTag();
				$(".editable").editable("/lib/php/save.php");
			});
			
		});
		
		$("#delete-no").click(function() {
			// $(".delete-confirm").remove();
			$.modal.close();
		});
		
		return false;
	});
	
	// mark item complete
	
	$(".list-item-complete").live("click", function() {
		li = $(this).parents(".list-item").attr("id");
		
		$.post("/lib/php/ajax.php", {
			markComplete : 'true',
			li : li
		}, function(data) {
			$("#list-items").html(data);
			$("#list-tags ul").load("/lib/php/tags-view.php", function() {
				if (typeof(makeTagsSelect) == "function") {
					makeTagsSelect(false);
				}
			});

			$("#completed-items").load("/lib/php/completed-view.php");
			
			tagLabelHandlers();
			firstTag();
			$(".editable").editable("/lib/php/save.php");
		});
		
		return false;

	});
	
	// mark item incomplete
	
	$(".list-item-incomplete").live("click", function() {
		
		li = $(this).parents(".list-item").attr("id");
		
		$.post("/lib/php/ajax.php", {
			markIncomplete : 'true',
			li : li
		}, function(data) {
			$("#list-items").html(data);
			$("#list-tags ul").load("/lib/php/tags-view.php", function() {
				if (typeof(makeTagsSelect) == "function") {
					makeTagsSelect(false);
				}
			});
			
			$("#completed-items").load("/lib/php/completed-view.php");
			$("#completed-items>ul").show();
			
			tagLabelHandlers();
			firstTag();
			$(".editable").editable("/lib/php/save.php");
		});
		
		return false;
		
	});
	
	
	// delete list
	
	$(".desktop .delete-list").live("click", function() {
		$.modal('<div class="delete-confirm"><p>Warning, this will also delete all of the items on this list. Are you sure you want to delete this one?</p><p class="buttons"><button id="delete-yes">Yup</button><button id="delete-no">Nope, take me back!</button></p></div>');
		list = $(this).parents("li").attr("id");	// returns "list-[ID]"
	
		$("#delete-yes").click(function() {
			$.post("/lib/php/ajax.php", {
				deleteList : 'true',
				list : list
			}, function(data) {
				$("#lists").html(data);
				$("#my-lists").load("/lib/php/user-lists-view.php");
				$.modal.close();
				// $(".delete-confirm").remove();
			});
		});
		
		$("#delete-no").click(function() {
			// $(".delete-confirm").remove();
			$.modal.close();
		});
		
		return false;
		
	});


	
	
});

function tagLabelHandlers() {

	$(".new-tag input").focus(function() {
		$(this).prev("label").hide();
	});
	
	$(".new-tag input").blur(function() {
		if ($(this).val() == "") $(this).prev("label").show();
	});
	
}

function listItemHighlight() {
	
}


function addTag(el) {

	if (($(el).val() != "") && ($(el).val() != " ")) {
		$.post("/lib/php/save.php", {
			id : $(el).attr("id"),
			value : $(el).val()
		}, function(data) {
			$("#list-items").html(data);
			$("#list-tags ul").load("/lib/php/tags-view.php");
			if (typeof(makeTagsSelect) == "function") {
				makeTagsSelect(false);
			}
			
			
			tagLabelHandlers();
			firstTag();
			$(".editable").editable("/lib/php/save.php");
		});
	}
}


function firstTag() {

	if (typeof(makeTagsSelect) == "undefined") {
		// makeTagsSelect is defined in the iphone JS file, so its being undefined means that we're on the desktop

		$("#list-items .list-item").each(function() {
			var href = $(this).find('.list-item-tags li:first a[href^=tag]').attr("href");
			var title = $(this).find('.list-item-tags li:first a[href^=tag]').attr("title");
			var str = $(this).find('.list-item-tags li:first a[href^=tag]').text();
		
			var tagLink = '<a href="'+href+'" title="'+title+'">Tagged: '+str;
			if ($(this).find('.list-item-tags li.group').length > 1) {
				tagLink += ' ...';
			}
			tagLink += '</a>';
		
		    $(this).find(".list-item-tags").before('<span class="first-tag"></span>');
		    $(this).find('.first-tag').html('<a class="tag-end" href="'+href+'" title="'+title+'"></a>'+tagLink);
		});
		
	}

}
