$(document).ready(function(){

	//Post Slideshow functionality
	$(".post-thumbs .img-s").click(function(event){
		var newImg = $(this).attr("href");											
		//var test = $(this).parents(".post").find(".post-slug img").attr("src");
		//alert(test);
		$(this).parents(".post").find(".post-slug img")
			.fadeOut(function(){
				$(this).attr("src", newImg);						  
			})
		event.preventDefault();	
	});
	
	$(".post-slug img").load(function(){
  		$(this).fadeIn();
	});
	
	//Recent posts functionality
	//Onclick, scroll to post
	$("a.img-m").click(function(event){
		target = $(this).attr("href");
		targetOffset = $(target).attr("offsetTop");
		$('html,body').animate({scrollTop: targetOffset}, 1000);
		event.preventDefault();
	});
	
	//Newsletter Functionality
	//Add Ajax call for form to 'action' value
	$("#newsletter-form .submit").click(function(event){
		var formAction = $("#newsletter-form").attr("action");
		var dataStr = 'name='+$("#name").attr("value")+'&email='+$("#email").attr("value");
		var errorTimeout = null;
		newsletterLoading();
		$.ajax({
	    	type: "POST",
			url: formAction,
			data: dataStr,
			success: function(msg){
				$("#newsletter-form fieldset").fadeOut();
				$("#newsletter-results").html(msg).fadeIn();
				errorTimeout = setTimeout('clearMessage()', 3500);
			}
		});
		event.preventDefault();
	});
	
	
	//wp-email override
	//Add Ajax call for wp-email forms
	$(".email-to-friend .wp-email-submit").click(function(event){
		parent = $(this).parents(".email-to-friend");
		form = $(parent).find("form");
		formAction = $(form).attr("action");
		formResults = $(parent).find(".wp-email-loading");
		dataStr = 'friendemail='+$(parent).find("input[name=friendemail]").val()+'&p='+$(parent).find("input[name=p]").val()+'&popup='+$(parent).find("input[name=popup]").val();
		dataStr += '&wp-email=1';
		$(parent).find("form").animate({'opacity':.25},500);
		$(formResults).show();
		$.ajax({
	    	type: "POST",
			url: formAction,
			data: dataStr,
			success: function(msg){
				$(formResults).html(msg);
				$(form).animate({'opacity':0}, 500);
				setTimeout('resetWPEmail(parent)',3000);
			},
			error: function (XMLHttpRequest, textStatus, errorThrown) {
				$(formResults).html(textStatus);
			}
		});
		event.preventDefault();													  
	});
	
	
	//Form Functionality
	//clear text inputs when focused on
	$("input:text").focus(function(){
		$(this).val('');
	});
	
	//Google Map Call
	if ( $("#map").length > 0 ) {
		setupMap();
		$(window).unload(function(){
			GUnload();
		});
	}
});

function newsletterLoading(){
	$("#newsletter-form fieldset").animate({
    	opacity: .25
	},500);
	$("#newsletter-results").html('loading...').fadeIn();
}

function clearMessage() {
	$("#newsletter-form fieldset").show().animate({
    	opacity: 1
	},500);
	$("#newsletter-results").fadeOut(300, function(){
		$(this).html('');
	});
}

function resetWPEmail(target) {
	$(target).find("form").animate({opacity:1},500);
	$(target).find(".wp-email-loading").fadeOut(300, function(){
  		$(this).html('<img src="http://spectators.com/wp-content/plugins/wp-email/images/loading.gif" width="16" height="16" alt="Loading ..." title="Loading ..." class="wp-email-image" />&nbsp;Loading ...');
	});
}
