jQuery(document).ready(function ($)
{
	//
	// Named functions first
	//
	
	// Stop the propagation of an event
	function stopEvent(e)
	{
		if(!e) e = window.event();
		e.cancelBubble = true;
		if(e.stopPropagation) e.stopPropagation();
		if(e.preventDefault) e.preventDefault();
	}

	// Conditionally clear a form field (e) based on what's in the form compared to check
	function conditionalClear(e, check)
	{
		var val = e.val();

		if(val == check) 
			e.val('');
	}
	// Reverse of conditionalClear, for forms
	function conditionalReplace(e, check)
	{
		var val = e.val();

		if(val == '') 
			e.val(check);
	}

	// A utility to give us a look at an object
	function objdump(obj)
	{
		var output = '';
		for (property in obj) 
		{
			output += property + ': ' + obj[property]+'; ';
		}
		alert(output);
	}

	// Navigation management - remove the selected class from all menu items
	function removeSel()
	{
		$(this)
			.find('a') // find all the A elements
				.removeClass('select') // remove from all
			.end() // go back to all A elements
		// Don't forget the home button
		$('#header-button2').removeClass('select');
	}

	if(location.hash)
		trigger({ id : location.hash.substr(1)});

	// Trigger function that the slider navigation calls when it's done putting up a pagelet
	function trigger(data) 
	{
			
		// Load the bottom part of the page by making an AJAX call
		loadPageBottom(data.id);
		
		// Remove the selections from the whole menu
		removeSel.call($($('#header-nav .menu-header').find('ul:first').get(0)));
		
		// Now add the selection indicator to the appropriate place
		if('home' == data.id)		// special case for home button, which is a hash link but not a menu item
			$('#header-button2').addClass('select');
		else
		{
			$($('#header-nav .menu-header').find('a[href$="' + data.id + '"]').get(0)).addClass('select');
			var elem = $('#header-nav .menu-header').find('a[href$="' + data.id + '"]').parent().parent().parent().get(0);
			if(elem != '[object HTMLDivElement]')
			$($('#header-nav .menu-header').find('a[href$="' + data.id + '"]').parent().parent().parent().find('a').get(0)).addClass('select');
		}
	}

	function loadPageBottom(elemId)
	{
		var script = themeLoc + 'pageload.php';
		$.get(script, {url: elemId + '/bottom'},
		function(data)
		{
			var postInfo = JSON.parse(data);
			if(postInfo.status == 'page found')		// Page was found, so put the content in the right spot
			{
				$("#page-bottom").html(postInfo.post_content);
				eval(postInfo.post_scripts);
			}
			else		// If pageload can't find the page, then try and go to the original url
			{
				$("#page-bottom").html('page bottom missing');
			}
   		});
	}

	//
	// Event handlers
	//
	//	Form validate and submit
	var formSent = 0;
	$("#page-bottom-button").click(function(e)
	{
		stopEvent(e);
		if(formSent == 1)
		{
			$("#footercol-heading2-msg").html('Message already sent, thanks!');
			formSent = formSent + 1;
			return;
		}
		else if(formSent == 2)
		{
			$("#footercol-heading2-msg").html('Seriously - we got it!');
			formSent = formSent + 1;
			return;
		}
		else if(formSent > 2)
		{
			$("#footercol-heading2-msg").html('Really?! Stop pushing the button!');
			return;
		}
		var addr = $(this).attr('href');
		var script = themeLoc + 'form-submit.php';
		var name = $("#page-bottom-name").val();
		var email = $("#page-bottom-email").val();
		var msg = $("#page-bottom-message").val();
		if(name == '' || name == 'Your Name')
		{
			$("#footercol-heading2-msg").html('Please fill in your name');
			return;
		}
		if(email == '' || email == 'Your Email')
		{
			$("#footercol-heading2-msg").html('Please fill in your email address');
			return;
		}
		if(msg == '' || msg == 'Your Message')
		{
			$("#footercol-heading2-msg").html('Please fill in a message');
			return;
		}
		$.get(script, {name: name, email: email, message : msg},
		function(data)
		{
			var result = JSON.parse(data);
			if(result.status == '1')
			{
				$("#footercol-heading2-msg").html(result.post_content);
				formSent = formSent + 1;
			}
			else
				$("#footercol-heading2-msg").html('error - try again later');
   		});
	});

	$("#page-bottom-name").focus(function(e)
	{
		conditionalClear($("#page-bottom-name"), "Your Name");
	});
	$("#page-bottom-name").blur(function(e)
	{
		conditionalReplace($("#page-bottom-name"), "Your Name");
	});

	$("#page-bottom-email").focus(function(e)
	{
		conditionalClear($("#page-bottom-email"), "Your Email");
	});
	$("#page-bottom-email").blur(function(e)
	{
		conditionalReplace($("#page-bottom-email"), "Your Email");
	});

	$("#page-bottom-message").focus(function(e)
	{
		conditionalClear($("#page-bottom-message"), "Your Message");
	});
	$("#page-bottom-message").blur(function(e)
	{
		conditionalReplace($("#page-bottom-message"), "Your Message");
	});

	// Load our header slideshow
	$('#header-label').rsfSlideshow({interval: 4,
									 transition: 750,
									 effect: 'slideDown', 
									 slide_container_class:'header-slides',
									 data_container:'ol.header-slide'});
	$('#header-label').rsfSlideshow('nextSlide');
	// Setup the scrolling panels
	var $panels = $('#slider .scrollContainer > div');
	var $container = $('#content .scrollContainer');
	
	// if false, we'll float all the panels left and fix the width 
	// of the container
	var horizontal = true;
	
	// float the panels left if we're going horizontal
	if (horizontal) 
	{
		$panels.css({
			'float' : 'left',
			'position' : 'relative' // IE fix to ensure overflow is hidden
		});
		$container.css('width', $panels[0].offsetWidth * $panels.length);
	}
	var $scroll = $('#slider .scroll').css('overflow', 'hidden');
	$scroll
		.before('<img class="nav-buttons nav-prev" src='+themeLoc+"images/nav-prev.png"+' />')
		.after('<img class="nav-buttons nav-next" src='+themeLoc+"images/nav-next.png"+' />');
	
	// bind the navigation clicks to update the selected nav:
//	$('#header-nav .menu-header').find('a').click(selectNav);
	
	var scrollOptions = 
	{
		target: $scroll, // the element that has the overflow
		
		// can be a selector which will be relative to the target
		items: $panels,
		
		navigation: '.navigation a',
		
		// selectors are NOT relative to document, i.e. make sure they're unique
		prev: 'img.nav-prev', 
		next: 'img.nav-next',
		
		// allow the scroll effect to run both directions
		axis: 'xy',
		
		onAfter: trigger, // our final callback
		
		offset: 0,
		
		// duration of the sliding effect
		duration: 500,
		
		// easing - can be used with the easing plugin: 
		// http://gsgd.co.uk/sandbox/jquery/easing/
		easing: 'swing'
	};

	var offset = parseInt((horizontal ? 
	$container.css('paddingTop') : 
	$container.css('paddingLeft')) 
	|| 0) * -1;
	$('#slider').serialScroll(scrollOptions);
	$.localScroll(scrollOptions);
});

