;(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 = ["side_menu"];
	var tab_classes = ["glass_tab", "flat_tab", "header_tab", "flat_tab3", "flat_tab4", "side_menu_tab"];
	var button_classes = ["big_green_button", "blue_button", "flat_button1", "flat_button2", "glass_button", "green_button", "grey_button", "large_button", "larger_button", "search_button", "shadow_button", "side_menu_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);
		});
		/*
		$(".required",context).change(function () {
			$(this).css("background", (this.value == "") ? "#fee" : "#efe");
			$(this).css("border", "1px solid #ccc");
			if (this.name == "signup_1[email_address]") $(this).css("background", (isValidEmail(this.value)) ? "#efe" : "#fee");
			if (this.name == "signup_1[password1]") $(this).css("background", (this.value.length > 6) ? "#efe" : "#fee");
			if (this.name == "signup_1[password2]") {
				var match = getElement("signup_1[password1]");
				if (match != null) $(this).css("background", (this.value.length > 6 && this.value == match.value) ? "#efe" : "#fee");
			}
		}).css("background", "#fee").filter('[value!=""]').css("background", "#efe");
		$(".optional",context).change(function () {
			$(this).css("background", (this.value == "") ? "#fff" : "#efe");
			$(this).css("border", "1px solid #ccc");
		}).css("background", "#fff").filter('[value!=""]').css("background", "#efe");
		*/
	}

	/**
	 * 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) {
			//$('#header',context).corner("bevel bottom 12px");
			//$('#footer',context).corner("bevel bottom 12px");
			//$(".tools_window .window_noclient",context).corner("bevel tr 5px");
			$(".tb_show", context).click(function () {
				tb_show('<div class="orders_head"><img src="/images/shim.gif" alt="My Orders" title="My Orders"></div>', $(this).attr("href") + "&KeepThis=true&TB_iframe=true&height=260&width=600", false);
				return false;
			});
			$(".view_history", context).click(function () {
				tb_show('<div class="orders_head"><img src="/images/shim.gif" alt="My Orders" title="My Orders"></div>', $(this).attr("href") + "&KeepThis=true&TB_iframe=true&height=455&width=950", false);
				return false;
			});
			$('.numeric', context).numeric();
			$('.cornered', context).corner("top 10px");
			$('.credit_div', context).corner("round 9px").parent().css('padding', '2px').corner("round 10px");
		}
		else if (!loading_extras) {
			loading_extras = true;
			$.ajaxSetup({
				cache: true
			});
			$.getScript("/jquery/skip_deps&alphanumeric&corner&thickbox.js", function () {
				included_extras = true;
				apply_extras(context);
			});
			$.ajaxSetup({
				cache: null
			});
		}
	}

	/**
	 * Other stuff removed from the footer
	 */
	function apply_compatibility(context) {
		$("input[name='product_qty']", context).click(function () {
			var val = $(this).val();
			productQtyChanged(val);
			if (val == 0) updatePrices();
		});
		$("input[name='qty_other']", context).keyup(function () {
			updatePrices();
		});
		$("a.hide_signup", context).css("cursor", 'pointer').click(function () {
			$('#signup_box').hide("100000");
		});
		$("div.prod_btn", context).click(function () {
			window.location = $(this).children("a")[0];
		}).hover(function () {
			$(this).addClass("prod_btn_over");
		}, function () {
			$(this).removeClass("prod_btn_over");
		});
		$("div.btntop", context).hover(function () {
			$(this).addClass("btnover");
		}, function () {
			$(this).removeClass("btnover");
		});
		$(".div_top", context).hover(function () {
			$("#order_div_td").css('verticalAlign', 'top');
		}, function () {});
		$(".div_middle", context).hover(function () {
			$("#order_div_td").css('verticalAlign', 'middle');
		}, function () {});
		$(".div_bot", context).hover(function () {
			$("#order_div_td").css('verticalAlign', 'bottom');
		}, function () {});
		$(".cont_help", context).hover(function () {
			tt_in($(this).attr("rel"));
		}, function () {
			tt_out();
		}).click(function () {
			return false;
		}).css({
			'cursor': 'default'
		});
	}

	$.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)
	{
	}
});
