var loader = '<img src="/images/loader.gif" id="loader" class="loader" />';
var ajax = '<input type="hidden" value="true" name="ajax" />';
var popup = false; // is the popup on or off? off to start with...
var orig_selected; // what links were originally selected when we initialised the popup?
var callback = null; // Do we need to do anything after the ajax load?

$(document).ready(function()
{
	orig_selected = $('li.selected'); // get the originally selected link(s) and hold on to teh jQuery object
	
	// click event for close popup button
	$('#popup a.close').click(function(ev){
		ev.preventDefault();
		hide_popup();
	});
	
	// click events for ajax links: everything this selects will return its href to the popup rather than loading a whole page
	prepare_ajax_links();
	
});

// Filter the elements that will load their URLs into a popup box via ajax
function prepare_ajax_links()
{
	/*
	$('#main-navigation a, li.ajax a').click(function(ev){
		ev.preventDefault();
		$('li.selected').removeClass('selected');
		$(this).closest('li').addClass('selected');
		load_into_popup($(this).attr('href'));
	});
	*/
}

// show the popup (only called if popup is currently off)
function show_popup(href){
	popup = true;
	$('#popup .load').html(loader);
	$('#popup').css({'opacity':0,'display':'block'});
	$('#popup').animate({'opacity':1},function(){
		if(href) load_into_popup(href);
	});	
}

// hide the popup and re-select the origianlly selected links
function hide_popup()
{
	$('#popup').animate({'opacity':0},function(){
		$('#popup').css({'display':'none'});
		$('li.selected').removeClass('selected');
		orig_selected.addClass('selected');
		popup = false;
	});
		
}

// load an href into the popup (sending the post variable 'ajax' forces the system to bypass the layout and only return the parsed template)
function load_into_popup(href)
{
	if(!popup){
		show_popup(href);
	}else{
		$('#popup .load').html(loader);
		$('#popup .load').load(href,{'ajax':'true'},function(){
			prepare_ajax_form();
			prepare_nested_popup_links();
			do_callback();
		});	
	}
}

// append an ajax value to the form and intercept the submit so that the form is posted and returned into the popup
function prepare_ajax_form()
{
	var form = $('#popup .load').find('form.ajax');
	form.append(ajax);
	form.submit(function(){
		var formdata = form.serializeArray();
		var action = form.attr('action');
		$('#popup .load').html(loader);
		$('#popup .load').load(action,formdata,function(){prepare_ajax_form();do_callback();});
		return false;
	});	
}

// check the newly loaded popup for ajaxable links
function prepare_nested_popup_links()
{
	$('#popup a.button').click(function(ev){
		ev.preventDefault();
		var href = $(this).attr('href');
		$("a[href$='"+href.replace('http://'+host,'')+"']").closest('li').addClass('selected');
		load_into_popup($(this).attr('href'));
	});
}

// Check for a callback function to run and call it...
function do_callback()
{
	if(typeof(callback) == 'function') callback();
	// Reset callback for the next request
	callback = null;
}
