jQuery(document).ready(function($){

	$("div.notice-box a.notice-close").live("click", function(){
		var ajax_url = $(this).attr("href");
		$(this).closest("div.notice-box").hide("slow");
		if (ajax_url != "#")
		{
			$.ajax({
				url: ajax_url,
				cache: false
			});
		}
		return false;
	});


	$("a.add-notice").live("click", function(){
		// The href of the link contains the notice parameters.
		// Links can be generated using the link method of the notices helper.
		var ajax_url = $(this).attr("href");
		
		$.ajax({
			url: ajax_url,
			cache: false,
			success: function(response){
				response = eval("("+response+")");
				if (response.status == "success"){
					$("div.notices-container").append(response.content);
					$("div.notices-container div.notice-box:last").show("slow", function(){
						$("div.notices-container div.notice-box").removeClass("notice-hidden");
						if(typeof $.scrollTo == "function"){
							$.scrollTo("div.notices-container", 800);
						}
					});
				}
			}
		});

		return false;
	});
	
});

function add_notice(type, message, persist){
	var ajax_url = '/notices/add';
	if (type != undefined){
		ajax_url += '/'+type;
		if (message != undefined){
			ajax_url += '/'+message;
			if (persist == true){
				ajax_url += '/TRUE';
			}
		}
	}

	$.ajax({
		url: ajax_url,
		cache: false,
		success: function(response){
			response = eval("("+response+")");
			if (response.status == "success"){
				$("div.notices-container").append(response.content);
				$("div.notices-container div.notice-box:last").show("slow", function(){
					$("div.notices-container div.notice-box").removeClass("hidden");
					if(typeof $.scrollTo == "function"){
						$.scrollTo("div.notices-container", 800, {axis: 'y'});
					}
				});
			}
		}
	});
}