$(document).ready(function(){

  // Open external links in new tab/window
  $("a[href*='http://']:not([href*='"+location.hostname+"'])").click(function(){
    this.target = "_blank";
  });

  // Return inputs to default value on blur.
  $('input[alt]').focus(function(){
    if ($(this).attr("value") === $(this).attr("defaultValue")) {
      $(this).attr("value", "");
    }
  }).blur(function(){
    if ($(this).attr("value") === "") {
     $(this).attr("value", $(this).attr("defaultValue"));
    }
  });

  //Homepage Cycle

  $('#home-page #banner').cycle({ 
      fx:     'fade', 
      speed:  'fast', 
      timeout: 10000, 
      pager:  '#pager',
      next:   '#next', 
      prev:   '#prev' 
  });

  //Search Show/Hide

  $("#global-search input").hide();
  $("#global-search a").click(function(){
    $(this).hide();
    $("#global-search input").fadeIn();
    $("#global-search .search-terms").focus();
  });

  //Filters Show/Hide
  $("#filters").not('.stockist').hide();
  $("#filter-button").toggle(function(){
    $("#filters").slideDown();
    $(this).addClass('active');
    return false;
  }, function(){
    $("#filters").slideUp();
    $(this).removeClass('active');
    return false;
  });

  //Comments Show/Hide
  $("#comments-section").hide();
  $("#comments-button").toggle(function(){
    $("#comments-section").fadeIn();
    return false;
  }, function(){
    $("#comments-section").fadeOut();
    return false;
  });

  if($("#errorExplanation").length == 1) {
    $("#comments-section").show();
  }

  //Colorbox Popups

  $("#nutrition").colorbox({inline:true, href:"#nutrition-popup", width: "500"});
  $(".subscribe-link,.subscribe-button").click(function(){
    $.colorbox({href: "/subscribers/new", iframe:true, height:"520px", width: "350px"}); 
    return false;
  });

  $("#global-subscribe").submit(function(){
    var subscriber_email = $("#footer_subscriber_email").val();
    if (subscriber_email == "Email Address") { subscriber_email = "" };
    $.colorbox({href: "/subscribers/new?subscriber[email]=" + subscriber_email,
                iframe:true, height:"580px", width: "350px"});

    return false;
  });

  //Related products/recipes Show/Hide
  if($("#related-recipes").length == 1) {
    $("#related-products").hide();
  }
  $("#related a:first").addClass("active");

  $("#related-recipes-button").click(function(){
    $("#related-recipes-button").addClass("active");
    $("#related-products-button").removeClass("active");
    $("#related-products").hide();
    $("#related-recipes").show();
    return false;
  });

  $("#related-products-button").click(function(){
    $("#related-products-button").addClass("active");
    $("#related-recipes-button").removeClass("active");
    $("#related-recipes").hide();
    $("#related-products").show();
    return false;
  });


  //Table Striping
  $("table tr:odd").addClass("even");


  //Navigation Dropdowns

  $(".dropdown").hide();

  $("#nav-subscribe").toggle(
    function(){
      $(".subscribe.dropdown").fadeIn();
    },
    function(){
      $(".subscribe.dropdown").stop(true, true).fadeOut();
    }
  );

  $("#navigation ul.dropdown li a").mouseover(function(){
    $(this).find("span").stop(true, true).animate({
      'border-top-width': 5
    }, 200);
  }).mouseout(function(){
    $(this).find("span").animate({
      'border-top-width': 1
    }, 200);
  });  


  $('ul.sf-menu').superfish({autoArrows:  false});
  
  $(function(){ $("input:checkbox, input:file, form select#state, form select#turn_in_order_state").uniform(); });


  // Stockists 
  $('#secondary #q').focus(function(e){
    if(this.value == 'Search') this.value = '';
    else $(this).select();
  });
  $('#secondary #q').blur(function(e){
    if(this.value == '') this.value = 'Search';
  });
  

	//Subnav Hide/show
	$('#secondary-nav li ul').hide();

	$("#secondary-nav .active").each(function(){
    $(this).parents("li").last().find("ul").show();
	});

	$('.show_subcats').toggle(function(){
		$(this).addClass('open');
		$(this).next('ul').show(300);
		return false;
	},
	function(){
		$(this).removeClass('open');
		$(this).next('ul').hide(300);
	});

  // Subscribe
  $("#footer_subscriber_email").focus(function() {
    $(this).val("");
  });
  
  //Downloads select
  $(".download_desc").click(function(){
    $(this).next('.select_download').find('span input').click();
  });

});

