;(function ($) {
	/**
	 * Keep track of extra loaded jquery plugins
	 */
	var included_extras = false;
	var loading_extras = false;

	/**
	 * Keep a list of classes to attach events to
	 */
	var list_classes = [];
	var tab_classes = [];
	var button_classes = ["big_green_button", "blue_button", "green_button", "shadow_button", "smooth_button"];

	/**
	 * Apply list classes
	 */
	function apply_list_classes(context) {
		$(list_classes).each(function () {
			$("." + this + " li", context).toggleClass(this + "_button", true);
		});
	};

	/**
	 * Add mouseover effects to particular classes
	 */
	function apply_mouseover_effects(context) {
		$(tab_classes).add(button_classes).each(function () {
			$("." + this, context).attr("hover_class", this).hover(function () {
				if (!this.disabled) {
					if ($(this).hasClass($(this).attr("hover_class") + "_active")) $(this).toggleClass($(this).attr("hover_class") + "_active_hover", true);
					else $(this).toggleClass($(this).attr("hover_class") + "_hover", true);
				}
			}, function () {
				$(this).toggleClass($(this).attr("hover_class") + "_active_hover", false);
				$(this).toggleClass($(this).attr("hover_class") + "_hover", false);
			});
		});
	};

	/**
	 * Add button click effects
	 */
	function apply_click_handling(context) {
		$(button_classes).each(function () {
			$("." + this, context).click(function (event) {
				if (!$(event.target).is("a")) $("a:first", this).click().each(function () {
					var href = $(this).attr("href");
					if (href != "" && href != "#") document.location = href;
				});
			});
		});
		$(tab_classes).each(function () {
			$("." + this, context).attr("tab_class", this).click(function (event) {
				if ($(this).hasClass($(this).attr("tab_class") + "_active") && $(this).hasClass('toggleable')) {
					$(this).toggleClass($(this).attr("tab_class") + "_active", false);
					if ($(this).hasClass('slide_effect')) $("#" + $(this).attr("name")).slideUp();
					else $("#" + $(this).attr("name")).fadeOut();
				} else {
					$(this).toggleClass($(this).attr("tab_class") + "_active", true);
					if ($(this).hasClass('slide_effect')) $("#" + $(this).attr("name")).slideDown();
					else $("#" + $(this).attr("name")).fadeIn();
					$("." + $(this).attr("tab_class") + '[tabgroup="' + $(this).attr("tabgroup") + '"]').not(this).each($(this).hasClass('slide_effect') ? function () {
						$(this).toggleClass($(this).attr("tab_class") + "_active", false);
						$("#" + $(this).attr("name")).slideUp();
					} : function () {
						$(this).toggleClass($(this).attr("tab_class") + "_active", false);
						$("#" + $(this).attr("name")).hide();
					});
					if (!$(event.target).is("a")) $("a:first", this).click().each(function () {
						var href = $(this).attr("href");
						if (href != "" && href != "#") document.location = href;
					});
				}
			});
		});
	}

	/**
	 * Add clear action to elements that should clear on focus
	 */
	function apply_input_formatting(context) {
		$(".clear_on_focus", context).each(function () {
			$(this).attr("reset_value", $(this).val());
		}).focus(function () {
			if ($(this).attr("reset_value") == $(this).val()) $(this).val("").css('color', '#000');
		}).blur(function () {
			if ($(this).val() == "") $(this).val($(this).attr("reset_value")).css('color', null);
		});
	}

	/**
	 * Ajax target loading or iframe simulation
	 */
	function apply_ajax_load_target(context) {
		$(".ajax_load_target", context).click(function () {
			var a = this.href || false;
			var g = this.rel || false;
			var t = $("#" + g);
			if (g && a && t.length > 0) {
				$("#" + g).slideUp().load(a, {
					popup: 2
				}, function () {
					$(this).apply_common().slideDown();
				});
				this.blur();
				return false;
			}
		});
	}

	/**
	 * Add automatic row alternating to certain table styles
	 */
	function apply_table_classes(context) {
		$("table.table_list1", context).each(function () {
			$("tr:odd", this).addClass("alt");
		});
		$("table.table_list2, table.table_list5, table.table_list8, table.flat_table", context).each(function () {
			$("tr:even", this).addClass("alt");
		});
		$("table.flat_table", context).each(function () {
			$("tr :first-child", this).css('text-align','left');
		});
	}

	/**
	 * Load jquery extensions and apply extra stuff
	 */
	function apply_extras(context) {

		if (included_extras) {
			$('.numeric', context).numeric();
			$('.cornered', context).corner("top 10px");
			$(".big_search_box", context).autocomplete("/suggestions.js?format=list",{matchCase:true,width:"320px"});
		}
		else if (!loading_extras) {
			loading_extras = true;
			var needed=[];
			if(!jQuery().autocomplete) needed.push("autocomplete");
			if(!jQuery().numeric) needed.push("alphanumeric");
			if(!jQuery().corner) needed.push("corner");
			if(needed.length>0)
			{
				$.ajaxSetup({
					cache: true
				});
				$.getScript("/jquery/skip_deps&"+needed.join("&")+".js", function () {
					included_extras = true;
					apply_extras(context);
				});
				$.ajaxSetup({
					cache: null
				});
			}
			else
			{
				loaded_extras = true;
				apply_extras(context);
			}
		}
	}

	/**
	 * Other stuff removed from the footer
	 */
	function apply_compatibility(context) {
	}

	$.fn.apply_common = function () {
		return this.each(function (index) {
			apply_list_classes(this);
			apply_table_classes(this);
			apply_mouseover_effects(this);
			apply_click_handling(this);
			apply_input_formatting(this);
			apply_ajax_load_target(this);
			apply_extras(this);
			apply_compatibility(this);
		});
	};
})(jQuery);

$(function() {
	$(document).apply_common();
});

//Iexplore hack for layered backgrounds or something, can't remember
$(window).load(function() {
	try
	{
		document.execCommand("BackgroundImageCache", false, true);
	}
	catch(err)
	{
	}
});
