(function ($)
{
	$(document).ready(function ()
	{
		$.prepareExternalLinks();
		$.prepareWatermarks();
		$.prepareRotators();
		$.prepareVideoLightBoxes();
		$.setMinHeight();
	});
	
	$.prepareExternalLinks = function ()
	{
		$("a").each(function ()
		{
			var a = $(this);
			var href = a.attr("href");
			var rel = a.attr("rel");
			
			if (href && href != "" && rel && rel.toLowerCase() == "external")
			{
				a.attr("target", "_blank");
			}
		});
	};
	
	$.prepareWatermarks = function ()
	{
		$("input.watermarked").each(function ()
		{
			$(this).data("watermark", $(this).val())
			.bind("focus", function ()
			{
				if ($(this).data("watermark") == $(this).val() && $(this).hasClass("watermarked"))
				{
					$(this).val("").removeClass("watermarked");
				}
			})
			.bind("blur", function ()
			{
				if ($(this).val() == "" || $(this).val() == $(this).data("watermark"))
				{
					$(this).val($(this).data("watermark")).addClass("watermarked");
				}
			});
		});
	};
	
	$.rotators = [];
	$.rotatorDelay = 3000;
	$.rotatorDuration = 500;
	$.rotatorDurationHalf = Math.floor($.rotatorDuration / 2);
	$.rotatorRepeat = false;
	
	$.prepareRotators = function ()
	{
		$("div.rotator").each(function (index)
		{
			$.rotators[index] =
			{
				"images": $(this).children(),
				"current": 0,
				"active": false,
				"timeout": null,
				"dots": $("<ul />").addClass("dots")
			};
			
			$.rotators[index].images.show().fadeOut(0).first().fadeIn($.slideDurationHalf);
			
			$.rotators[index].images.each(function (image)
			{
				var a = $("<a />").attr("href", "javascript:void(0);").click(function ()
				{
					$.showImage(index, image, false);
				});
				
				if (image == 0)
				{
					a.addClass("active");
				}
				
				$.rotators[index].dots.append($("<li />").append(a));
			});
			
			$(this).append($.rotators[index].dots);
			
			$.rotators[index].timeout = setTimeout("jQuery.nextImage(" + index + ");", $.rotatorDelay);
		});
	};
	
	$.nextImage = function (index)
	{
		if ($.rotators.length > index)
		{
			var next = $.rotators[index].current + 1;
			
			if (next >= $.rotators[index].images.length)
			{
				next = 0;
			}
			
			$.showImage(index, next, true);
		}
	};
	
	$.showImage = function (index, image, auto)
	{
		if ($.rotators.length > index && $.rotators[index].current != image && !$.rotators[index].active)
		{
			$.rotators[index].active = true;
			
			if ($.rotators[index].timeout != null)
			{
				clearTimeout($.rotators[index].timeout);
			}
			
			$.rotators[index].images.eq($.rotators[index].current).stop(true, true).fadeOut($.rotatorDuration);
			
			$.rotators[index].current = image;
			
			$.rotators[index].images.eq($.rotators[index].current).stop(true, true).fadeIn($.rotatorDuration, function ()
			{
				$.rotators[index].active = false;
				
				if (auto && image != 0)
				{
					$.rotators[index].timeout = setTimeout("jQuery.nextImage(" + index + ");", $.rotatorDelay);
				}
			});
			
			var dotTimeout = setTimeout("jQuery.rotators[" + index + "].dots.find(\"a\").removeClass(\"active\").eq(" + image + ").addClass(\"active\");", $.rotatorDurationHalf);
		}
	};
	
	$.prepareVideoLightBoxes = function ()
	{
		$("a.video").colorbox(
		{
			"iframe": true,
			"width": "900px",
			"height": "700px"
		});
	};
	
	$.setMinHeight = function ()
	{
		var windowHeight = $(window).height();
		var pageHeight = $("#wrapper").outerHeight();
		
		if ($("#wpadminbar").length > 0)
		{
			pageHeight += 28;
		}
		
		if (pageHeight < windowHeight)
		{
			var contentWrapper = $("#content-wrapper");
			var contentWrapperHeight = contentWrapper.height();
			
			contentWrapper.css("min-height", (contentWrapperHeight + (windowHeight - pageHeight)) + "px");
		}
	};
	
	$.prepareTopBar = function ()
	{
		$.prepareMenus("logo-menu");
		
		$("#logo-menu li").first().show();
		
		jQuery("#sign-up-button").colorbox(
		{
			"inline": true,
			"width": "350px",
			"height": "400px",
			"opacity": 0.8
		});
		
		$("#social-menu").prepend($("<li />").addClass("facebook-like").append($("<a />").attr("href", "#facebook-like").attr("id", "facebook-like-button").text("Like")))
		
		$("#facebook-like-button").colorbox(
		{
			"inline": true,
			"width": "400px",
			"height": "450px"
		});
		
		var s = $("#top-bar #s");
		
		if (s.val() == "" || s.val() == "Search")
		{
			s.val("Search").addClass("watermarked");
		}
	};
	
	$.prepareNavBar = function ()
	{
		$.prepareMenus("main-menu");
		
		if (isNews || isBlog)
		{
			$("#main-menu li.menu-item-90").addClass("active");
		}
		
		var items = $("#main-menu").children("li").children("a");
		var width = Math.floor(964 / items.length);
		var leftover = (964 % items.length) + 1;
		
		items.each(function ()
		{
			var extra = (leftover > 0) ? 1 : 0;
			var total = width + extra;
			
			$(this).width(total).parent().children("ul").width(total - 1);
			
			leftover--;
		});
	};
	
	$.prepareLeftBar = function ()
	{
		$.prepareMenus("left-menu");
		
		if (isNews)
		{
			$("#left-menu li.menu-item-90").addClass("active");
		}
		
		var items = $("#left-menu li.active").first().children("ul").children("li");
		items.first().addClass("first");
		items.last().addClass("last");
		
		items.each(function ()
		{
			var ul = $(this).children("ul");
			
			if (ul.length > 0)
			{
				$(this).addClass("third-level");
			}
		});
	};
	
	$.prepareRightBar = function ()
	{
		$("select[name='archive-dropdown']").css("margin", "7px 0").width(178).after($("<p />").addClass("small-margin").append($("<a />").attr("href", blogUrl).text("All Archives"))).parent().addClass("with-border");
		
		$("div.right-bar-widget").children("ul").each(function ()
		{
			$(this).children("li").last().addClass("last");
		});
	};
	
	$.prepareHomeLeft = function ()
	{
		$("#home-buttons li").each(function (index)
		{
			if (index <= 3)
			{
				$(this).addClass("home-button-" + (index + 1)).show();
			}
		});
	};
	
	$.prepareContent = function ()
	{
		$("#content div.blue-box").after($("<div />").addClass("blue-box-bottom"));
		
		if ($("#left-menu li.active").length == 0)
		{
			var li = $("<li />").append($("<a />").text($("#content h1").first().text().replace(/\(Edit\)/g, ""))).addClass("active");
			
			$("#left-menu").append(li);
			
			li.show();
		}
		
		var last = $("#content").children().last();
		
		if (last.hasClass("post"))
		{
			last.removeClass("post").addClass("clear");
		}
	};
	
	$.prepareFooterNav = function ()
	{
		$.prepareMenus("footer-menu-1");
		$.prepareMenus("footer-menu-2");
		$.finalizeFooterNav(50);
	};
	
	$.finalizeFooterNav = function (count)
	{
		if (count > 0)
		{
			var leftover = 964 - 30 - $("#footer-menu-1").outerWidth();
			var items = $("#footer-menu-2").children("li");
			var length = items.length;
			
			items.each(function ()
			{
				if ($(this).hasClass("menu-item-52"))
				{
					$(this).remove();
					
					length--;
				}
				else
				{
					leftover -= $(this).width();
				}
			});
			
			if (leftover > 0)
			{
				var extra = Math.floor(leftover / items.length);
				
				items.each(function (index)
				{
					$(this).css("padding-left", extra + "px");
				});
			}
			
			var timeout = setTimeout("jQuery.finalizeFooterNav(" + (count - 1) + ");", 100);
		}
	};
	
	$.prepareMenus = function (menuId)
	{
		var selector = "#" + menuId + " li.current-menu-item, #" + menuId + " li.current-page-item, #" + menuId + " li.current-page-ancestor, #" + menuId + " li.current-post-ancestor, #" + menuId + " li.current-menu-parent, #" + menuId + " li.current-post-parent";
		
		if (isBlog)
		{
			selector += ", #" + menuId + " li.current_page_parent";
		}
		
		$(selector).addClass("active");
	};
})(jQuery);

