var counter = 0;

function fixcursor() {
	console.log("here");
	if ($(".urlwrap").size() > 1) {
		$(".urlwrap").css("cursor", "move");
		$("#allurls").sortable("enable");
		$(".urlwrap").addClass("dnd");
	}
	else {
		$(".urlwrap").css("cursor", "default");
		$("#allurls").sortable("disable");
		$(".urlwrap").removeClass("dnd");
	}
}

function remover(e) {
	$(e.target).parent().parent().slideUp("fast", function() {
		$(e.target).parent().parent().remove();
		fixcursor();
	});
	
	e.preventDefault();
}

function urlnoteadder(e) {
	var link = $(e.target);
	var pr = link.parent().get(0);
	var el = $(".urlnote", pr);
	
	if (el.css("display") == "none") {
		el.slideDown("fast", function() {
			el.focus();
		});
	}
	else {
		el.slideUp("fast", function() {
			$(".longurl", pr).focus();
		});
	}
	
	e.preventDefault();
}

function noteadder(e) {
	var el = $("#note");
	var ellink = $("#addnote");
	
	if (el.css("display") == "none") {
		el.slideDown("fast", function() {
			ellink.text("remove description");
			el.focus();
		});
	}
	else {
		el.slideUp("fast", function() {
			ellink.text("add description");
			el.text("");				
		});
	}
	
	e.preventDefault();
}

$(document).ready(function() {
	$("#url0").focus();
	$(".removeurl").bind("click", remover);
	
	$("#addurl").bind("click", function(e) {
		counter++;
		$(".urlwrap:last").after(
			"<div class='urlwrap' id='url" + counter + "wrap'>" +
				"<input type='text' name='url' value='' class='longurl' id='url" + counter + "' maxlength='500' />" +
				"<a class='removeurl' href='#remove' title='remove this URL'><img src='/images/remove.gif' alt=''/></a> "+
				"<a class='urlnoteadder' href='#addurlnote' title='add URL note'><span>URL note</span></a>" +
				"<textarea type='text' name='urlnote' class='urlnote' id='url' + counter + 'note'> </textarea>" +
			"</div>");
		
		$(".removeurl").unbind("click", remover);
		$(".removeurl").bind("click", remover);
		
		$(".urlnoteadder").unbind("click", urlnoteadder);
		$(".urlnoteadder").bind("click", urlnoteadder);
		
		$(".urlwrap").slideDown("fast", function() {
			$(".longurl:last").focus();
		});
		
		fixcursor();
		e.preventDefault();
	});
	
	$(".urlnoteadder").bind("click", urlnoteadder);
	$("#addnote").bind("click", noteadder);
	
	var h = $("#hash");
	var p = $("#prefix");
	var hash_infocus = false;
	var hash_inmouse = false;
	
	h.bind("focus", function(e) {
		p.css("display", "inline-block");
		hash_infocus = true;
	});
	
	h.bind("mouseenter", function(e) {
		p.css("display", "inline-block");
		hash_inmouse = true;
	});
	
	h.bind("blur", function(e) {
		if (!hash_inmouse)
			p.css("display", "none");
		
		hash_infocus = false;
	});
	
	h.bind("mouseleave", function(e) {
		if (!hash_infocus)
			$("#prefix").css("display", "none");
		
		hash_inmouse = false;
	});
	
	$("#urllist li a").attr("title", function(arr) { return this.href; });
	
	// url drag'n'drop voodoo ftw!!!
	$("#allurls").sortable({
		opacity: 0.75,
		axis: "y",
		cursor: "move",
		forcePlaceholderSize: true,
		placeholder: ".ghost-place",
		start: function(e) {
			$(".removeurl").remove();
		},
		update: function(e) {
			$(".longurl").after("<a class='removeurl' href='#remove' title='remove this URL'><img src='/images/remove.gif' alt=''/></a>");
			$(".removeurl", "#" + $(".urlwrap:first", this).attr("id")).remove();
			$("#firstlabel").attr("for", $(".urlwrap:first input").attr("id"));
		
			$(".removeurl").unbind("click", remover);
			$(".removeurl").bind("click", remover);
	}});
	
	$("#allurls").disableSelection();
	fixcursor();
});

$(window).load(function() {
	// auto-expanding actions for url editing
	$(".urlwrap").slideDown("fast");
	
	$(".urlnote").each(function() {
		var jobj = $(this);
		
		if ($.trim(jobj.text()) != "")
			$(".urlnoteadder", jobj.parent().get(0)).trigger("click");
	});
	
	if ($.trim($("#note").text()) != "")
		$("#addnote").trigger("click");
});