$(document).ready(function(){

	$("#nav ul, ul.pics, #column, .form-item, #map, #footer .container, #footer ul.nav-list, #house").addClass("clearfix");
	
	/* external links */
	$('a[rel="external"]').attr("target","_blank").addClass("external");
	
	$(":text").addClass("text");
	$("input[name='spam']").css("width","50px");
	
	$("#contact input[name='name'], #newsletters input[name='email']").focus();
	
	/* even out the heights of the primary and sidebar columns */
	if ($("#free ul").height() != $("#paid ul").height()) {
		if ($("#free ul").height() > $("#paid ul").height()) {
	 		var new_height = $("#free ul").height();
	 		$("#paid ul").height(new_height);
	 	} else {
	 		var new_height = $("#paid ul").height();
	 		$("#free ul").height(new_height);
	 	}
	}
	
	/* scroll the sidebar until the top, then fix it */
	$(window).scroll(function() {
		var scroller = $(window).scrollTop();
		if (scroller >= 100) {
			$("#prev div").css("position","fixed").css("top","20px");
		} else {
			$("#prev div").css("position","relative").css("top","0px");
		}
	});
	
	$("button").hover(function(){
		$(this).addClass("hover-button");
	},function(){
		$(this).removeClass("hover-button");
	});
	
	$("#extras h4:first").addClass("first-h4");
	
	/* spam note */
	$(".form-item a").toggle(function(){
		$(this).parent().parent().append('<p class="spam-why">This easy addition problem is to thwart robots who like flooding inboxes with spam.</p>');
		return false;
	},function(){
		$('.spam-why').remove();
		return false;
	});
	
	$("#newsletters form h3:first").css("padding-bottom",".25em");
	
	/* Cover hover */
	$("#cover a").hover(function(){
		if ($("#cover img").hasClass("already-on")) {
		
		} else {
			$("#cover img").addClass("already-on");
		}
		if ($("#cover h4").hasClass("already-on")) {
		
		} else {
			$("#cover h4").addClass("already-on");
		}
	},function(){
		$("#cover img, #cover h4").removeClass("already-on");
	});
	
	/* Contact form */
	
	var first = Math.floor(Math.random() * 5) + 1;
	var second = Math.floor(Math.random() * 5) + 1;
	$("label span:first").text(first);
	$("label span:eq(1)").text(second);
	
	var email_filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	
	$("#contact button").click(function(){
		var error_list = ''; var errors = ''; var $focusme = '';
		
   	if ($("input[name='spam']").val() != (first + second) && $("input[name='spam']").val() != '') {
   		error_list = '<li>The answer you gave for <strong>'+first+' + '+second+'</strong> is not correct.</li>';
   		$focusme = $("input[name='spam']");
   	}
   	if ($("input[name='spam']").val() == "") {
   		error_list = '<li>Please solve <strong>'+first+' + '+second+'</strong>.</li>' + error_list;
   		$focusme = $("input[name='spam']");
   	}
   	if ($("textarea[name='message']").val() == "") {
   		error_list = '<li><strong>Message</strong> is required.</li>' + error_list;
   		$focusme = $("textarea[name='message']");
   	}
   	if ($("select[name='topic']").val() == "0") {
   		error_list = '<li><strong>Topic</strong> is required.</li>' + error_list;
   		$focusme = $("select[name='topic']");
   	}		
		if (!email_filter.test($("input[name='email']").val()) && $("input[name='email']").val() != "") {
			error_list = '<li>The email address you provided for <strong>Email address</strong> is not valid.</li>' + error_list;
			$focusme = $("input[name='email']");
		}
   	if ($("input[name='email']").val() == "") {
   		error_list = '<li><strong>Email address</strong> is required.</li>' + error_list;
   		$focusme = $("input[name='email']");
   	}
		if ($('input[name="name"]').val() == '') {
			error_list = '<li><strong>Your name</strong> is required.</li>' + error_list;
			$focusme = $("input[name='name']");
		}
		
		if (error_list != '') {
   		errors = '<ul>' + error_list + '</ul>';
   		$("#errors").remove();
   		$(".address").before('<div id="errors"><div><h3>There are errors in your form.</h3>' + errors + '</div></div>');
   		$("#errors").slideDown("fast");
   		$.scrollTo("#errors", 400, {offset: -20});
   		$focusme.focus();
   		return false;
   	} else {
   		$(this).val("Submitting...");
   		$("#contact-us").submit();
   	}
	});
	
	$("#newsletters button").click(function(){
		var error_list = ''; var errors = ''; var $focusme = '';
	
   	if ($("input[name='field_data[55848]']").val() == "") {
   		error_list = '<li><strong>Last name</strong> is required.</li>' + error_list;
   		$focusme = $("input[name='field_data[55848]']");
   	}
   	if ($("input[name='field_data[55847]']").val() == "") {
   		error_list = '<li><strong>First name</strong> is required.</li>' + error_list;
   		$focusme = $("input[name='field_data[55847]']");
   	}
   	if (($("input[name='email']").val() != $("input[name='confirm']").val()) && $("input[name='email']").val() != "") {
   		error_list = '<li>Please confirm your <strong>Email address</strong>.</li>' + error_list;
   		$focusme = $("input[name='confirm']");
   	}
		if (!email_filter.test($("input[name='email']").val()) && $("input[name='email']").val() != "") {
			error_list = '<li>The email address you provided for <strong>Email address</strong> is not valid.</li>' + error_list;
			$focusme = $("input[name='email']");
		}
   	if ($("input[name='email']").val() == "") {
   		error_list = '<li><strong>Email address</strong> is required.</li>' + error_list;
   		$focusme = $("input[name='email']");
   	}
		if (error_list != '') {
   		errors = '<ul>' + error_list + '</ul>';
   		$("#errors").remove();
   		$(".address").before('<div id="errors"><div><h3>There are errors in your form.</h3>' + errors + '</div></div>');
   		$("#errors").slideDown("fast");
   		$.scrollTo("#errors", 400, {offset: -20});
   		$focusme.focus();
   		return false;
   	} else {
   		$(this).val("Submitting...");
   		$("#newsletters form").submit();
   	}
	});
	
	/* E*Info popout window */
	
	$("#nav ul").after('<div id="einfo-redirect"><h4>E*Info is no longer being offered.</h4><p>Contact Jeanine at 847-675-7400 x104 if you would like contact information for any of the advertisers featured in <em>Shopper Marketing</em>. <a href="../">hide this box</a></p></div>');
	$("li.einfo-nav a").click(function(){
		$("#einfo-redirect").hide().fadeIn(300);
		return false;
	});
	$("#einfo-redirect a").bind("click", function(){
		$(this).parent().parent().fadeOut(200);
		return false;
	});

});