/*
	Sidenav Name Hacks
	--
	For when they are too long to fit on one line.
	Add on a case-by-case basis.
*/

$(function(){
	$("#local-nav a:contains(Corporate Groups)").text("Corporate Groups");	
	$("#local-nav a:contains(Howqua River Wedding Venue)").text("Weddings");	
});


/*
	AnythingSlider
	--
	For slidey sections like testimonials.
*/

$(function(){

	// I dont like the extra markup needed to make this work. So lets inject it here.
	
	$("#testimonials blockquote").each(function(){
		$(this).wrap("<li></li>");
	});
	$("#testimonials")
		.wrapInner("<div class='wrapper'><ul></ul></div>")
		// Now call the plugin.
		.anythingSlider({ 
			easing: "swing",
			autoPlay: false,
			hashTags: false
		});
	
});




/*
	News Slider
	--
	Simple Next/Prev news item widget.
*/

$(function(){

	// Hide all the stories
	$("#news-feed .story").addClass("hidden");
	// Show the first one and label it as first
	$("#news-feed .story:first-child").addClass("active first").removeClass("hidden");
	// Label last story
	$("#news-feed .story:last-child").addClass("last");
	// Add paging into DOM, this isnt necessary in the code, only when JS is available
	$("#news-feed .section").append('<div id="news-nav"><a href="" id="news-nav-prev" class="btn-prev">Previous</a> <a href="" id="news-nav-next" class="btn-next">Next</a></div>');
	
	
	// Handle Next/Prev Links
	
	$("#news-nav-next").live("click",function(){
		
		if( $("#news-feed .active").hasClass("last") ){
			nextToShow = $("#news-feed .first");
		} else { nextToShow = $("#news-feed .active").next(); }
		
		$("#news-feed .active").removeClass("active").addClass("hidden");
		nextToShow.addClass("active").removeClass("hidden");
		
		return false;
	});
	
	$("#news-nav-prev").live("click",function(){
		
		if( $("#news-feed .active").hasClass("first") ){
			nextToShow = $("#news-feed .last");
		} else { nextToShow = $("#news-feed .active").prev(); }
		
		$("#news-feed .active").removeClass("active").addClass("hidden");
		nextToShow.addClass("active").removeClass("hidden");
		
		return false;
	});
	

});




/*
	AJAX FORM SUBMISSION
	From http://www.malsup.com/jquery/form/
*/


$(function() {

	if ($("form").size() > 0){
		var formheight = $("form").height();
		$("form").wrap("<div id='form-wrap' style='height:"+formheight+"px;'></div>");
		$('.error').hide();
		
		var options = {
			beforeSubmit:	validateForm,
			success:		showResponse				
		}
		
		$("form").submit(function(){
			$(this).ajaxSubmit(options);
			return false;
		});
	}
});

function validateForm() {

	var validated;
	var formContainer = $("form");
	var requiredFields = $(".required[value='']",formContainer);

	$(".required[value!='']").removeClass("error").siblings("span.error").remove();

	requiredFields.addClass("error");
	requiredFields.each(function(){
		if( $(this).hasClass("error") ) {
			if( $(this).siblings("span.error").size() != 1 ) {
				$(this).parent().append("<span class='error'>Required</span>").find(".error").fadeIn();
			}
		}
	});

	if (requiredFields.size() > 0) {
		return false;
	} else {				
	 	$(".submission input").val("Submitting...")
	}
		
}


/*
	CALENDAR AJAX
	Next/Prev month links in the mini calendar
*/
	
$(function(){
	$("#cal-months a.nav").live("click", function(){
		
		var cal = $(this).attr("href");
		$("#calendar-wrap")
			.prepend("<div class='loading' style='width:233px;height:230px;position:absolute;z-index:2'></div>")
			.find("#calendar > table").css("opacity","0.3")
			.end().load(cal + " #calendar");
		return false;
	
	});
});


/*
	CALENDAR DROP DOWN
	Takes you to the selected month event listing
*/

$(function(){
	$("#month-swap").change(function(){
		if ($(this).val != ""){
			window.location = $(this).val();
		}
	});
});


/*
	EVENT OCCURRENCES
	Hide/show additional event dates
*/

$(function(){

	$(".occurrences-link").click(function(){
		$(".occurrences").fadeIn("slow");
		return false;
	});
	
});


/*
	IMAGE SLIDESHOW
	Rotating images on the special events pages.
*/

$(function(){

	$(".block-imgs").cycle();

});

