
$(document).ready(function() {
	
	$('.subNav').css('top','-200px').show();
		
	$(".subNav").each(function(){ 
		var width = $(this).width();
		var parentLeft = $(this).parent().position().left;
		var parentWidth = $(this).parent().width();
		var parentCenter = parentLeft + (parentWidth/2);
		var left = parentCenter - (width/2);
		if(left < 250) left = 250;
		if(left + width > 750) left = 750 - width;
		$(this).css('left', left);
	});
	
	$('#mainNav > li > a').css('opacity', 0).hover(function(){
    $('.subNav.activeSection').stop().animate({opacity: 0}, 250, 'linear', function() { $(this).hide(); });
    $('#mainNav a.activeSection').stop().animate({opacity: 0}, 250);
	  $(this).stop().animate({opacity: 1}, 150);
		if(!$(this).parent().find('.subNav').length) return;
		$(this).parent().find('.subNav').stop().show().animate({opacity: 1}, 250);
		$('#subNavBackground').stop().animate({opacity: 1}, 250);
	}, function(){
		$(this).stop().animate({opacity: 0}, 150);
		$(this).parent().find('.subNav').stop().animate({opacity: 0}, 250, 'linear', function() { $(this).hide(); });
    if(!$('.subNav.activeSection').length)
		  $('#subNavBackground').stop().animate({opacity: 0}, 250);
	});
	
	$('.subNav').css('opacity', 0).hide().hover(function(){
		$('.subNav.activeSection').stop().animate({opacity: 0}, 250, 'linear', function() { $(this).hide(); });
		$('#mainNav a.activeSection').stop().animate({opacity: 0}, 250);
		$(this).stop().show().animate({opacity: 1}, 250);
		$(this).parent().find('a:first').stop().animate({opacity: 1}, 150);
	  $('#subNavBackground').stop().animate({opacity: 1}, 250);
	}, function() {
		$(this).stop().animate({opacity: 0}, 250, 'linear', function() { $(this).hide(); });
		$(this).parent().find('a:first').stop().animate({opacity: 0}, 150);
		if(!$('.subNav.activeSection').length)
		  $('#subNavBackground').stop().animate({opacity: 0}, 250);
	});
	
	$('#mainNav').hover(function(){ return }, function(){
	  if($('.subNav.activeSection').length) {
	    $('.subNav.activeSection').stop().show().animate({opacity:1},250);
    	$('a.activeSection').stop().show().animate({opacity:1},250);
	  }
	});
	
	$('.subNav').css('top','35px');
	$('.activeSection').show().css('opacity',1);
	if(!$('.subNav.activeSection').length) {
	  $('#subNavBackground').css('opacity', 0);
	}
	
	$('.faq .answer').hide();
	$('.faq h3').css('cursor', 'pointer').click(function(){
	  if($(this).hasClass('active')) {
	    $(this).parent().find('.answer').stop().slideUp();
	    $(this).removeClass('active');
	    return;
	  }
	  $('.faq h3').removeClass('active');
	  $(this).addClass('active');
	  $('.faq .answer').removeClass('active').slideUp();
	  $(this).parent().find('.answer').stop().slideDown();
	});
	
	$('input.default').each(function(){
	  $(this).attr('defaultText', $(this).val());
	  $(this).focus(function() {
	    if($(this).val() == $(this).attr('defaultText')) $(this).val('');
	  });
	  $(this).blur(function(){
	    if($(this).val() == '') $(this).val($(this).attr('defaultText'));
	  });
	});
	
	$('#topLocationSearch').click(function(){
    var val = $('#topLocationZip').val();
    if(!val.match(/[0-9\-]{5,10}/)) {
      alert('You must enter a valid zip code');
      return;
    } 
    document.location = $(this).attr('baseurl')+'shop/locations?zip='+val;
	});
	
	$('#topLocationZip').keyup(function(e) {
  	if(e.keyCode == 13) {
  	  $('#topLocationSearch').click();
  	}
  });
  
  $('a.fancy').fancybox();
	
	$('.captchaFlower').click(function(){
	  $(this).find('input').attr('checked',true);
	});
	
  $('form.needsValidation').needsValidation();
	
	$('input[name=captcha]').closest('form').submit(function(){
	  if(!$(this).find('input[name=captcha]:checked').val()) {
	    $(this).find('.captchaNote').css('color','red');
	    return false;
	  } else {
	    $(this).find('.captchaNote').css('color','inherit');
	  }
	});
	

});

function getQSVar( name )
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}