// special thanks to...
// http://blog.rebeccamurphey.com/2007/12/24/anchor-based-url-navigation-jquery-plugin/

// functions
function set_up(){

	$('#content').children('div').hide();

	// look at the URL to see if it contains an anchor
	var url = document.location.toString();
    if (url.match('#')) {

		// if so, get it from the URL
		var url_anchor = url.split('#')[1];

		// show the panel that contains the url anchor
		show_section('#' + url_anchor);

		// prevent screen from dropping to the anchor
		window.scrollTo(0,0);
	}
	else
	{
		// there is no anchor in the url so show the home section
		show_section('#welcome');
		//window.scrollTo(0,0);
	}
}

function hide_show_section(show_id){
	var $current_section_id = $('ul#nav li.current a:first').attr('href');
	// hide current
	$('div' + $current_section_id).hide();
	// fade in page
	show_section(show_id);
}

function show_section(show_id){
	// update current
	$('ul#nav a[href="' + show_id + '"]').parent('li').addClass('current').siblings('li').removeClass('current');
	// fade in page
	$('div' + show_id).fadeIn(800);
}




// Document Ready
$(document).ready(function(){

	// call the page set up function, here we go
	//-------------------------------------------------------------------------------------
	set_up();


	// make the masthead clickable
	//-------------------------------------------------------------------------------------
	$('#in-masthead').css('cursor','pointer').click(function(evt){
		evt.preventDefault();
		hide_show_section('#welcome');
	});


	// Nav
	//-------------------------------------------------------------------------------------
	$('ul#nav a').click(function(evt){
		evt.preventDefault();

		var $id = $(this).attr('href');
		window.location.hash = this.hash;
		hide_show_section($id);
	});

	// links in the body
	//-------------------------------------------------------------------------------------
	$('a[href^="#"]', '#content').click(function(evt){
		evt.preventDefault();

		var $id = $(this).attr('href');
		window.location.hash = this.hash;
		hide_show_section($id);
	});

	// Contact form
	//-------------------------------------------------------------------------------------
	$("#response").hide();
	$("#contact-form").validate({
		rules: {
			name:{
			   required:true,
			   minlength:2
			},
			phone:{
			   required:true,
			   minlength:7
			},
			email: {
				required:true,
				email:true
			}
		},
		messages: {
			name:	'Please enter your name.',
			phone:	'Please enter a phone number at which we can reach you.',
			email:	'Please enter a valid email address.'
		},
		submitHandler: function(form) {
			var inputs = [];
			$("#response").html("Sending Form...").show().css({'background-color' : '#faf2c5', 'color' : 'black'});
			$('#contact-form input').each(function(){
			   inputs.push(this.name + '=' + escape(this.value));
			});

			$('#contact-form textarea').each(function(){
				inputs.push(this.name + '=' + escape(this.value));
			});

			jQuery.ajax({
				data: inputs.join('&'),
				url: "/_inc/tellform.php",
				timeout: 5000,
				error: function(){
				  $('#response').html("Uh oh, there was an error, not sure what to do...").css({'background-color' : 'red', 'color' : 'white'});
				},
				success: function(r){
				  $('#response').html(r).css({'background-color' : 'transparent', 'color' : 'green'});
				  $('#contact-form').fadeOut('slow');
				}
			});

			//return false;
		}
	});

});// End Document Ready
