$(document).ready(function(){
	
	// Hide on document.ready
	$('#showme').hide();
	
	$('#leaderstory, #livability-show').hide();
	
	/*$('#leaderstory, #livability-show').show();*/
	
	// Adds a class of "last-child" to the last <li> in a list
	$("li:last-child")
		.addClass("last-child");
	
	// Adds a class of "first-child" to the first <label> in <div class="iefix">
	$("div.iefix label:first-child")
		.addClass("first-child");
		
	// Adds a class to odd rows in every <table class="striped">
	$(".striped tr:nth-child(odd)")
		.addClass("oddrow");

	// Places the <label> from <form id="searchform"> inside the text input and makes it show/hide on blur/focus
	$("#searchform label").each(function() {
		var label = $(this);
		var input = $('#' + label.attr('for'));
		var initial = label.hide().text().replace(':', '');
		input.focus(function() {
			input.css('color', '#77787b');
			if (input.val() == initial) {
				input.val('');
			}
		}).blur(function() {
			if (input.val() == '') {
				input.val(initial).css('color', '#77787b');
			}
		}).css('color', '#77787b').val(initial);
	});
	
	// Accordion (expand/collapse) effect for <div class="faq">
	$(".faq div.answer").hide();
	$(".faq h2").hover(
		function(){ $(this).addClass("on"); },
		function(){ $(this).removeClass("on"); }
	);
	$(".faq h2").click(function() {
		var $nextDd = $(this).next();
		var $visibleSiblings = $nextDd.siblings('div.answer:visible');
		
		if ($visibleSiblings.length ) {
			$visibleSiblings.slideUp('fast', function() {
				$nextDd.slideToggle('fast');
			});
		} else {
			$nextDd.slideToggle('fast');
		}
	});

	// Flashembed (an alternative to SWFObject) - http://www.flowplayer.org/tools/flash-embed.html
	$("#find-bv").flashembed("http://www.belmontvillage.com/media/BV_map.swf", {src: "http://www.belmontvillage.com/media/BV_map.swf", w3c: true});
	
	// Curvy corners - http://www.atblabs.com/jquery.corners.html
	//$('dl.rounded').corners("10px");
	
	// Hide/show based on radio button choice
	$("input[@name=resident]").click(function() {
		if(this.value != 'yourself') {
			$('#showme').slideDown("fast");
		}
		else {
			$('#showme').slideUp("fast");
		}
	});
	
	// Form validation	
	$("#freeform").validate({
		rules: {
			firstname: "required",
			lastname: "required",
			street1: "required",
			city: "required",
			state: "required",
			postalcode: "required",
			"location[]": "required",
			email: {
				required: "#enews:checked",
				email: true
			},
			emailres: {
				email: true
			}
		},
		messages: {
			firstname: "Please enter your first name",
			lastname: "Please enter your last name",
			street1: "Please enter your street",
			city: "Please enter your city",
			state: "Please select your state",
			postalcode: "Please enter your 5-digit zipcode",
			"location[]": "Please select one or more locations",
			email: {
				required: "Please enter your email address to be subscribed to the e-Newsletter",
				email: "Your email address must be in the format of name@domain.extension"
			},
			emailres: {
				email: "Your email address must be in the format of name@domain.extension"
			}
		},
		errorPlacement: function(error, element) {
			if (element.attr("name") == "location[]" )
				error.insertAfter("label.last-child");
			else
				error.insertAfter(element);
		}
	});
	
	// Masks for form fields
	$("#phone1,#phoneres").mask("999-999-9999");
	$("#postalcode,#postalcoderes").mask("99999");
	
	// Font size Controller
	$("<div id='fontsizelabel'>Adjust Type Size</div>").appendTo("#masthead");
	$("<div id='fontsize'><strong>Adjust Font Size</strong></div>").appendTo("#masthead");
	fontSize("#fontsize", "body", 15, 16, 24);
	
	// Image fade in masthead
	$('#fadewrap').innerfade({
		speed: 2000,
		timeout: 5000,
		containerheight: '293px'
	});
	
	/*// Slider with pause on hover
	$("#slider").easySlider({
		loop: true,
		autoplayDuration: 5000,
		autogeneratePagination: true,
		restartDuration: 1000,
		nextId: 'next',
		prevId: 'prev',
		pauseable: true
	});*/
	
	$("#leaderstory").show();
	
	$("#leaderstory").wslide({
		width: 484,
		height: 220,
		autolink: false,
		horiz: true,
		fade: true,
		duration: 600
	});
	
	// Initialize Shadowbox
	Shadowbox.init();
	
});