(function($) {
	$.fn.popbox = function(config) {
		return $(this).click(function() {			
			var _ = this;
			var c = $.extend({
				target : _,
				opacity : .3,
				// shadow : ['red','blue','green','yellow'],
				speedIn : 500,
				speedOut : 500,
				container : "body",
				width : 400,
				height : "auto",
				bubbleEvent : false
			},config);
			
			var o = {
				box : $("<div class='popbox_container'></div>"),
				overlay : $("<div class='popbox_overlay'></div>").appendTo("body").css({opacity : c.opacity}),
				close : $("<div class='popbox_close'></div>"),
				content : $("<div class='popbox_content'></div>")
			};
			
			o.close.appendTo(o.box);
			$(c.target).clone().appendTo(o.content.appendTo(o.box));
			
			o.box.addClass("popbox_wrapper").css({width : c.width, height : c.height});
			
			var destroy = function() {
				o.box.fadeOut(c.speedOut, function() {
					o.box.remove();
					$(window).unbind('keypress',onescape);
				});
				
				o.overlay.fadeOut(c.speedOut, function() {
					o.overlay.remove();
				});
			};
			
			var onescape = function(e) {
				if(e.which === 0) { destroy(); }
			};
			
			var win = {width : $(window).width(), height : $(window).height() };
			o.box.appendTo(c.container).show();
			var dim = {width : o.box.width(), height : o.box.height() };
			o.box.hide().css({
				left : (win.width - dim.width) / 2,
				top : (win.height - dim.height) / 2
			}).fadeIn(c.speedIn,function() {
				o.close.click(destroy);
				$(window).bind('keypress',onescape);
			});
			
			o.overlay.fadeIn(c.speedIn).click(destroy);
			
			return false;
		});
	}
})(jQuery);

$().ready(function() {	
	$("#header a[title]").removeAttr("title");
	
	var wm = "Search";
	$("[name='s']").val(wm).addClass("watermark")
		.focus(function() {
			if($(this).val() == wm) {
				$(this).val("").removeClass("watermark");
			}
		})
		.blur(function() {
			if(!$(this).val()) {
				$(this).val(wm).addClass("watermark");
			}
		});
	
	$(":submit").click(function() {
		if ($(this).prev().is("[name='s']")) {
			if ($(this).prev().val() == wm) {
				return false;
			}
		}
	});
	
	$("#searchform").submit(function() {
		return $("#s").val() != wm;
	});
	
	$(".calendarwidget .toggle_more").each(function() {
		var a = $("<a href='/events/calendar-of-events/'>Show more upcoming events</a>")
					.click(function() {
						var rest = $(this).next()
						$(this).fadeOut(function() {
							rest.slideToggle("slow");
						});
						
						return false;
					});
		$(this).before(a);
	});
	
	$(".calendarwidget .widgetrow").each(function(){
		var id = "cal_item" + $.data(this);
		$(this).data("desc", $(this).find("[title]").attr("title"));
		$(this).find("[title]").removeAttr("title");
		
		$(this).addClass("clickable").popbox({
			target : $(this).find(".details")
		});
	}).tooltip({
		showURL : false,
		bodyHandler: function() {
			return $(this).data("desc");
		}
	});
	
	$(".entry-content, .widgetcontainer, .widgetcontainer").find("a[title]").each(function() {
		var sub = $(this).find(".title");
		$(this).data("desc", sub.length > 0 ? sub.html() : $(this).attr("title"));
	}).tooltip({
		showURL : false,
		bodyHandler : function() {
			return $(this).data("desc");
		}
	});
});