jQuery.fn.idle = function (time) {
	return this.each(function () {
		var i = $(this);
		i.queue(function () {
			setTimeout(function () {
				i.dequeue();
			}, time);
		});
	});
};

$(document).ready(function () {

	$("a[title^=?:]")
		.each(function() {
			var msg =$(this).attr("title");
			$(this).data("confirm_message", msg.slice(2));
			$(this).attr("title", "");
		})
		.click(function (event) {
		 	if (!confirm($(this).data("confirm_message")))
		 		event.preventDefault();
		});

	$("a[title^=:clean-]")
		.each(function () {
			var input_id = $(this).attr("title").slice(7);
			$(this).data("clean-id", "#" + input_id);
			$(this).attr("title", "");
		})
		.click(function (event) {
			var input_id = $(this).data("clean-id");
			$(input_id).val("");
			event.preventDefault();
		});

	$("a[title^=:now-]")
		.each(function () {
			var input_id = $(this).attr("title").slice(5);
			$(this).data("now-id", "#" + input_id);
			$(this).attr("title", "");
		})
		.click(function (event) {
			var padZero = function (s) { return String("0" + s).slice(-2); }
			var input_id = $(this).data("now-id");
			var now = new Date();
			var new_date = 
				now.getFullYear() + "/" + 
				padZero(now.getMonth() + 1) + "/" + 
				padZero(now.getDate());
			var new_time = 
				padZero(now.getHours()) + ":" + 
				padZero(now.getMinutes()) + ":" + 
				padZero(now.getSeconds());
			$(input_id).val(new_date + " " + new_time);
			event.preventDefault();
		});

	$("a[title^=:toggle-]")
		.each(function () {
			var element_id = $(this).attr("title").slice(8);
			$(this).data("toggle-id", "#" + element_id);
			$(this).attr("title", "");
		})
		.click(function (event) {
			var element_id = $(this).data("toggle-id");
			$(element_id).toggle();
			event.preventDefault();
		});

	$("*[title=:focus]")
		.each(function () {
			$(this).focus();
			$(this).attr("title", "");
		})

	if ($("#flash").length) {
        $("#flash").click(function () { $(this).hide() });
    	$("#flash").idle(7000).fadeOut();
	}
});


