jQuery(document).ready(function() {
   // put all your jQuery goodness in here.
   // Do not instantiate elements until the document has finished loading
   
   	var cookieState = $.cookie('state');
   	var redirectOnce;
   	//alert(cookieState);
   	   	   	
   	if (cookieState == null) {
   		// select the overlay element - and "make it an overlay"
		$("#modal").overlay({
			top: 200,
 			expose: {
 				color: '#333',
 				loadSpeed: 200
 				//opacity: 0.9
			},
 			closeOnClick: false,
 			api: true
 		}).load();
   	}
   	
   	else if (cookieState != null) {
   		$("#ship-to").show();
   		$(".ship-to-state").html(cookieState + "&nbsp;<a class='state-change' href='#'>Change state</a>");
   		$("#state option[value='"+ cookieState +"']").attr('selected', 'selected');
   	}
   	
   	else if (cookieState == 'MA' || cookieState == 'CA' && redirectOnce == 'null' ) {
   		location.href = "/ComplianceRedirect.aspx"
   		redirectOnce = '1';
   		$("#ship-to").show();
   	}
   	
   	
   	$(".state-change").click(function () { 
    			$("#modal").overlay({
			top: 200,
 			expose: {
 				color: '#333',
 				loadSpeed: 200
 				//opacity: 0.9
			},
 			api: true
 		}).load();
    	return false;
    });


   
   
   

	// Left Navigation Accordion
	$('.accordion-list').not(':first').hide();

	$('.accordion-list:first').show();

	$('.accordion-header:first').addClass('active');	
	$('.accordion-list:first').addClass('content-active');

	$('.accordion-header').click(function () {
		$('.accordion-list:visible').slideUp().prev().removeClass('active');
		$('.accordion-list:visible').removeClass('content-active');
		$(this).addClass('active').next().slideDown();
		$('.accordion-list').addClass('content-active');
	
	});
	
	
	//Sign Up Form Validation
    	$(".email-wrapper #email-submit").click(function(){
        
		var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
		var email = $("#email-signup-input").val();
				
		if(email == '') {
			$(".email-error").replaceWith('<div class="email-error"><p>You forgot to enter an email address.</p></div>');
		} else if(!emailReg.test(email)) {
			$(".email-error").replaceWith('<div class="email-error"><p>Please enter a valid email address.</p></div>');
		} else {
		  		$(".track").html('<img src="http://directwines.bm23.com/public/actionpage/subscriber_add/?fn=Mail_ActionPage_FormResponse&pid=a5rewl261vv3isinsf52w5bg5noby&ssid=12040&email=' +  email + '&list_data[206971]=checked" width="0" height="0" border="0" alt=""/>');
		  		$(".email-wrapper").html('<p>Thank you, you have been subscribed.</p>')
		}
		return false;
		});
		

	
	// Homepage 4 Tab Slider
	$("#tabs ul").tabs("#panes > div", { 
         // enable "cross-fading" effect 
        effect: 'fade', 
        fadeOutSpeed: "slow", 
        // start from the beginning after the last tab 
        rotate: true 
    // use the slideshow plugin. 
    }).slideshow({ 
        autopause: true, 
        autoplay: true, 
        interval: 3500,
        clickable : false
            }); 
    
	// Product Details Tabs
   	// tabs requires jQuery Tools to be present
	$("ul.product-tabs").tabs("div.tab-panes > div"); 
		
	
	// Main Navigation Drop Down Menu
		$('li.tab-drop-down').hover(function() {
			$(this).find('.sub-menu').show();
			$(this).find('a.tab-link').addClass("tab-drop-hover");

		}, function() {
			$(this).find('.sub-menu').hide();
			$(this).find('a.tab-link').removeClass("tab-drop-hover");
		});

	
	// Main Navigation Highlight Script
	var path = location.pathname;  
    var home = "/";
    $("#navigation a[href='" + [ path ] + "']").each(function() { 
        $(this).addClass("selected");
    });
    
    var menuArray = ['wine_details.aspx', 'mixed_cases.aspx', 'March'];
    
    if (path == "/wine_details.aspx") {	
		$("#t-ourwines a").addClass("selected");
	}
	else if (path == "/mixed_cases.aspx") {	
		$("#t-ourwines a").addClass("selected");
	}
	else if (path == "/wine_mixed_details.aspx") {	
		$("#t-ourwines a").addClass("selected");
	}
	else if (path == "/wine_shop_results.aspx") {	
		$("#t-ourwines a").addClass("selected");
	}
	else {
	
	}
	
  	// Google Analytics - Event Tracking - Homepage Slider Offer Clicks
  	 $('.pane a').click(function() {
		    	var sourceRel = $(this).attr("rel");
		    	//alert(sourceLink);
  				_gaq.push(['_trackEvent', 'HomepageSlider', 'Click', sourceRel]);
				return true;
	});
	
  	// Google Analytics - Event Tracking - Top Review Clicks
  	 $('.top-review a').click(function() {
		    	var sourceRel = $(this).attr("rel");
		    	//alert(sourceLink);
  				_gaq.push(['_trackEvent', 'TopReview', 'Click', sourceRel]);
				return true;
	});
	
	 // Google Analytics - Event Tracking - Bottom Offer Clicks
  	 $('.hp-col a').click(function() {
		    	var sourceRel = $(this).attr("rel");
		    	//alert(sourceLink);
  				_gaq.push(['_trackEvent', 'BottomOffer', 'Click', sourceRel]);
				return true;
	});

	 // Google Analytics - Event Tracking - Reccomended Clicks
  	 $('.reccomended a').click(function() {
		    	var sourceRel = $(this).attr("rel");
		    	//alert(sourceLink);
  				_gaq.push(['_trackEvent', 'WSJRecc', 'Click', sourceRel]);
				return true;
	});
	
	
});




$(document).ready(function() {

$.validator.setDefaults({
	success: "valid"
});

jQuery.validator.addMethod("phoneUS", function(phone_number, element) {
    phone_number = phone_number.replace(/\s+/g, ""); 
	return this.optional(element) || phone_number.length > 9 &&
		phone_number.match(/^(1-?)?(\([2-9]\d{2}\)|[2-9]\d{2})-?[2-9]\d{2}-?\d{4}$/);
}, "Please specify a valid phone number - no spaces");
	
	// Registration Form Validation - using jquery.validate.min.js			
	
		$(".registration #aspnetForm").validate({
		rules: {
			ctl00$cphMaster$txtFirstName: "required",
			ctl00$cphMaster$txtLastName: "required",
			ctl00$cphMaster$txtEmailID: {
    			email: true,
    			required:true
    		},
    		ctl00$cphMaster$txtConfirmEmailID: {
    			equalTo: "#ctl00_cphMaster_txtEmailID",
    			email: true,
    			required: true
    		},
    		ctl00$cphMaster$txtPassword: {
    			minlength: 6,
    			required: true
    		},
    		ctl00$cphMaster$txtConfirmPassword: {
      			equalTo: "#ctl00_cphMaster_txtPassword",
      			minlength: 6,
      			required: true
    		},
    		ctl00$cphMaster$txtAddress1: {
    			required: true
    		},
    		ctl00$cphMaster$txtCity: {
    			required: true
    		},
    		ctl00$cphMaster$ddlState: {
    			required: true
    		},
    		ctl00$cphMaster$txtZipCode: {
    			required:true,
    			minlength: 5
    		},
    		ctl00$cphMaster$txtPhone: {
    			required: true,
      			phoneUS: true
    		}	
  		},
  		messages: {
  		ctl00$cphMaster$txtFirstName: "Please specify your first name",
  		ctl00$cphMaster$txtLastName: "Please specify your last name",
  		ctl00$cphMaster$txtConfirmEmailID: "Please confirm your email address",
  		ctl00$cphMaster$txtPassword: {
  		required: "Please enter a password",
  		minlength: "Your password needs to be at least 6 letters"
  		},
  		ctl00$cphMaster$txtConfirmPassword: {
  		required: "Please confirm your password",
  		minlength: "Your password needs to be at least 6 letters"
  		},
  		ctl00$cphMaster$txtAddress1: "Please enter your address",
  		ctl00$cphMaster$txtCity: "Please enter your city",
  		ctl00$cphMaster$txtZipCode: "Please enter your zip code"
  		}
	});
	
   
   function fnValidateJquery(){
	$(".shipping-address-form #aspnetForm").validate({debug: true});
   }

	
});


