$(function() {
	$("input[type=checkbox][class=toggle]").bind("click", function(e) {
		var category = $(this).attr("value");
		if($(this).attr("checked")) {
			$("div.links").each(function() { showHideLinks($(this), "show", category); });
		} else {
			$("div.links").each(function() { showHideLinks($(this), "hide", category); });
		}
	});
	$("input[type=checkbox][class=toggle]").attr("checked","checked");
});
var showHideLinks = function(element, mode, category) {
	var count=0;
	var hidden=0;
	$(element).find("div."+category).each(function() {
		switch(mode) {
			case 'hide':
				$(this).slideUp();
				count--;
				hidden++;
				break;
			case 'show':
				$(this).slideDown();
				count++;
				hidden--;
				break;
			default:
				return;
		}
	});

	var nofSpan = $(element).find("h3 span.visible span.nofLinks");
	var noun = $(element).find("h3 span.visible span.noun");

	var hiddenSpan  = $(element).find("h3 span.hidden");
	var hiddenNofSpan  = $(element).find("h3 span.hidden span.nofLinks");

	var prevCount = parseInt(nofSpan.text());
	var newCount = prevCount+count

	var prevHidden = parseInt(hiddenNofSpan.text());
	var newHidden = prevHidden+hidden;
	
	if(prevCount != newCount) {
		//nofSpan.text(newCount);
		//noun.text(newCount==1 ? "link" : "links");

		if(newHidden>0) {
			hiddenNofSpan.text(newHidden);
			hiddenSpan.fadeIn("slow");
		} else {
			hiddenSpan.fadeOut("fast", function() {
				hiddenNofSpan.text(newHidden);
			});
		}
	}
}
var logout = function() {
	$.post("login.php", { "logoutSubmit":"true" }, function() { window.location="/"; } );
}
var deadlink = function(id) {
	if(confirm("Are you sure you want to report this link as being broken?")) {
		$.post("/index.php", { "deadlink":id }, function(data) { alert("Thank you for reporting this dead link."); } );
	}
	return false;
}
var validateAddLink = function(form) {
	var url		= $.trim($(form).find("input[name=url]").val());
	var title 	= $.trim($(form).find("input[name=title]").val());
	var description	= $.trim($(form).find("input[name=description]").val());
	var categoryId	= $.trim($(form).find("select[name=category_id]").val());
	var name	= $.trim($(form).find("input[name=name]").val());
	var email	= $.trim($(form).find("input[name=email]").val());

	if (url.length == 0) 		{ alert("empty url!"); return false; } else
	if (title.length == 0) 		{ alert("empty title!"); return false; } else
	if (description.length == 0) 	{ alert("empty description!"); return false; }

	if(!url.match(/https?:\/\/.*/)) {
		alert("invalid url: a url starts with \"http://\" or \"https://\"");
		return false;
	}
	if(email.length>0 && !email.match(/[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}/i)) {
		alert("invalid e-mail address: '"+email+"'");
		return false;
	}

	return true;
}
var confirmDeadLink = function(id) {
	if(confirm("Are you sure this is a dead link, and you want to REMOVE it?")) {
		$.post("admin.php", { "deadlink":"confirm", "id":id }, function(data) { console.log(data); $("li.dead"+id).slideUp() });
	}
	return false;
}
var disproveDeadLink = function(id) {
	if(confirm("Are you sure this is NOT a dead link, and you want to KEEP it?")) {
		$.post("admin.php", { "deadlink":"disprove", "id":id }, function(data) { console.log(data); $("li.dead"+id).slideUp() });
	}
	return false;
}
var formSubmit = function(form) {
	if($(form).val().length>0) {
		$(form).parent("form").submit();
	}
	return false;
}
var countHit = function(id) {
	$.post("/index.php", { "hit":id });
}
var switchTitleDescription = function() {
	var title = $("input[name=title]").val();
	var description = $("input[name=description]").val();

	$("input[name=title]").val(description);
	$("input[name=description]").val(title)
}

