jQuery(document).ready(function()
{
	jQuery('#nav li').hover(function(){jQuery(this).find('ul').show()},function(){jQuery(this).find('ul').hide()});
	
	jQuery('.hidden').hide();
	
	// Run the default switch...
	set_switch('.infoSwitcher');
	
	// info switcher show hide...
	jQuery('.show').click(function(ev){
		ev.preventDefault();
		jQuery('.show').each(function(){$(this).parent('li').removeClass('selected')});
		jQuery(this).parent('li').addClass('selected');	
		set_switch('.infoSwitcher');
	});	
	
	/*
	
	// AJAX forms handler...
	$('form.ajax').append('<input type="hidden" name="ajax" value="true" />');
	
	
	$('form.ajax').submit(function() {
	  	// Serialise the form data
		var postdata = $(this).serializeArray();
		var action = $(this).attr('action');
		var loader = $(this).parents('.data');
		
		$(loader).load(action,postdata,function(data){
			
		});
		
		// Don't submit the form
	 	return false;
	});
	
	*/
	
	init_ajax_form('form.ajax');
	
	// Popup styling
	var screenWidth = $(window).width();
	var docHeight = $(document).height();
	var screenHeight = $(window).height();
	var scrollTop = $(document).scrollTop();
	
	
	$(".popup").width(screenWidth);
	$(".popup").height(docHeight);
	$('.popup').css('opacity',0);
	$('.popup').css('display','block');

	$('.popup').css('position','absolute');
	$('.popup').css('top',0);
	$('.popup').css('left',0);
	$('.popup').css('padding',0);
	$('.popup').css('margin',0);
	
	var libraryHeight = $(".window").outerHeight();
	var libraryWidth = $(".window").outerWidth();
	
	var negHeight = (screenHeight-scrollTop)-libraryHeight;
	var negWidth = screenWidth-libraryWidth;
	
	negHeight = Math.round(negHeight/2);
	negWidth = Math.round(negWidth/2);
	
	if(negHeight < 0) negHeight = 0;
	if(negWidth < 0) negWidth = 0;
	
	$(".window").css('position','absolute');
	$(".window").css('top',negHeight);
	$(".window").css('left',negWidth);
	$('.window').css('margin',0);
	
	$('.popup').animate({'opacity':1},'slow');
	$('.popup a.close').click(function(ev){$('.popup').fadeOut('slow');ev.preventDefault()});
	
});

/* SET SWITCH
--------------------------------------
- hide the .hiddens
- get the href of the clicked on link
- get an element by ID which matches that HREF
- show that element

*/

function set_switch(container)
{
	jQuery(container+' .hidden').hide();
	var showme = jQuery(container+' li.selected a').attr('href');
	if(showme) jQuery(showme).show();
}

/* INIT AJAX FORM
---------------------------------------
*/
function init_ajax_form(selector)
{
	$(selector).append('<input type="hidden" name="ajax" value="true" />');
	
	
	$(selector).submit(function() {
	  	// Serialise the form data
		var postdata = $(this).serializeArray();
		var action = $(this).attr('action');
		var loader = $(this).closest('.data');
		var form = $(this);
		
		$(loader).load(action,postdata,function(data){
			init_ajax_form(selector);
		});
		
		// Don't submit the form
	 	return false;
	});	
}


