$(document).ready(function() {
	
	$("#main-menu li:last a").attr({id:"last"});
	$("#main-menu li:last").addClass("menu-last");
	
	// form
	$(".datetype").click(function() {
		if($(this).val() == 1) {
			$(".div-dateinput:last").show();
		}
		else {
			$(".div-dateinput:last").hide();
		}
	});
	
	// browse categories
	if($("ul.children").length > 0) {
		//$(this).css({background:"#000",height:"100px"});
	}
	
	$("input[name=rdocounty]").click(function() {
		var theval = $(this).val();
		
		if(theval == 1) {
			$(".wrap-counties").show("slow");
		}
		else {
			$(".chkcounty").each(function() {
				$(this).removeAttr("checked");
			});
			$(".wrap-counties").hide("slow");
		}
	});
	$("input[name=rdocategories]").click(function() {
		var theval = $(this).val();
		
		if(theval == 1) {
			$(".wrap-categories").show("slow");
		}
		else {
			$(".chkcategories").each(function() {
				$(this).removeAttr("checked");
			});
			$(".wrap-categories").hide("slow");
		}
	});
	
	$("a.link_title").toggle(function() {
		var theid = $(this).attr("title");
		$("#"+theid+" input").attr("checked","checked");
		$(".root"+theid).attr("checked","checked");
		$(this).prepend("Un");
	},function() {
		var theid = $(this).attr("title");
		$("#"+theid+" input").removeAttr("checked");
		$(".root"+theid).removeAttr("checked");
		$(this).empty().append("Select All");
	});
	
	if($(".event-result").length > 0 && $("form.attraction").length > 0) {
		$(".wrap-categories").hide("slow");
		$(".wrap-counties").hide("slow");
		
		$("input[name=rdocounty]").removeAttr("checked");
		$("input[name=rdocategories]").removeAttr("checked");
		
		$("input[name=rdocounty]:first").attr("checked","checked");
		$("input[name=rdocategories]:first").attr("checked","checked");
	}
	
	if($(".browse-categories").length > 0) {

		var newarr = new Array();
		var i = 0;
		$("ul.browse-categories li").each(function() {
			// get id
			var theclass = $(this).attr("class");
			var theid = theclass.match(/\d{1,}/);
			if(theid == "null") {}
			else {
				newarr[i] = theid;
				i++;
			}
		});
		
		
		$.ajax({
			url: "http://chooseireland.com/ajax.php",
			method: "GET",
			dataType : 'json',
			data: "theids="+newarr,
			success: function(data) {
				
				for(i=0;i<data.id.length;i++) {
					if(data.num[i] == 0) {
						$("li.cat-item-"+data.id[i]).hide();
					}
					else {
						$("li.cat-item-"+data.id[i]+" a:first").after("<span class='num-count'>&nbsp;("+data.num[i]+")</span>");
					}
				}
				
			}
		});
		
	}
});