// All praise be to jQuery
$(function() {
	
	$('.thompsonSlideShow').cycle({
		fx: 'fade'
	});
	
	$('.sf-menu').superfish({
		delay:       100,
		animation:   {opacity:'show',height:'show'},
		speed:       'fast',
		autoArrows:  false,
		dropShadows: false
	});

	if ($('#appendixRight').length == 1) {
		$('#appendixRight').load('/pages/ajax/id/70', function() {	
			if (window.location.href.indexOf('condition') != -1 ) {
				$('#appendixRight').css('position','relative');
				if (window.location.href.indexOf('conditions/category/Thompson/') != -1 ) {
					$('#appendixRight').css('top','41px');
				}
				else {
					$('#appendixRight').css('top','80px');
					$('#appendixa1').remove();
				}
			}
			if (window.location.href.indexOf('Naturopath-Helpline') != -1) {
				$('#appendixa2').remove();
			}
			if (window.location.href.indexOf('subscribe') != -1) {
				$('#appendixa3').remove();
			}
			if (window.location.href.indexOf('stockist') != -1) {
				$('#appendixa4').remove();
			}
		});
	}

	$('#subscribeForm').ajaxForm(function(d) {
		alert(d);
	});

	// Attach prettyPhoto lightboxes
	$("a[rel^='prettyPhoto']").prettyPhoto();

	$('#goToProduct').change(function() {
		window.location = '/product/'+$(this).val();
	});

	// Highlight current menu item
	$("#menu").find("a").each(function() {
		if ("/"+$(this).attr("href")==window.location.pathname || $(this).attr("href")==window.location.href) {
			$(this).addClass("active");
		}
	});

	// Confirm certain link clicks
	$(".confirm").click(function(e) {
		return(confirm("Really?"));
	});

	// Focus on username in login form
	$("#loginForm").find("#user_name").focus();

	// Form validation
	$("form").submit(function() {
		var alertMessage = "";
		$(this).find(".required").each(function() {
			if ($(this).val()=="") {
				alertMessage += $(this).attr("rel")+" is required\n";
			}
		});
		if (alertMessage.length) {
			alert(alertMessage);
			return false;
		}
		return true;
	});

	$('#searchLink').click(function(e) {
		e.preventDefault();
		$('#searchForm').submit();
	});

	if($.cookie('show_enviro_bag')===null){
		$(".enviro-bag-promo").show();
	}
	
	$(".close").click(function (event) {
		event.preventDefault();
      $(".enviro-bag-promo").hide("fast");
	  $.cookie('show_enviro_bag', '0');
    });  
	

});
