var initialised = false; // This allows us to have a 'run once' setup, so we can run an initialisation just once and flick the switch so it doesn't run a second time
var has_set_link = ""; // This will be matched against the request_uri to check if we need to reset the nav link
var links_set = false;
var debug = false;
var debug_init = false;
var trace_count = 0;

jQuery('#content').ready(function(){
	//jQuery('#content').hide();
});

jQuery(document).ready(function(){
	if(loaded == false)
	{
		// Force loading of ajax content from hash tag
		initialise_state_from_url();
		loaded = true;
	}
});

// This function is called by the AJAX loader when it finishes.
// When this function fires, we know the page has been successfully loaded into the browser
// So we can call our initialisation procedures
function has_completed_page_load()
{
	DEBUG_trace('completed: ' + request_uri);
	
	initialise_page_once();
	
	set_breadcrumbs();
	// Find any new links on the page
	get_new_ajax_links();
	// Highlight the current page in the nav bar...
	set_links_by_href(request_uri);
	links_set = false;
}

function initialise_page_once()
{
	if(!initialised)
	{
		DEBUG_trace('page initialised');
		initialised = true;
		// Navigation link animation and behaviour...
		initialise_nav_links();
		// Catch links to load content via ajax...
		catch_ajax_links();
	}
}

function initialise_nav_links()
{
	/*jQuery('#nav a').hover(
		function(ev){
			ev.preventDefault();
			jQuery(this).stop();
			jQuery(this).animate({backgroundPosition: '0px 100%'}, 1000 );
		},
		function(ev){
			ev.preventDefault();
			jQuery(this).stop();
			jQuery(this).animate({backgroundPosition: '-156px 100%'}, 500 );
		}
		
	);*/
	DEBUG_trace('initialising nav links');
	// Set the background positons so that the animations work...
	jQuery('#nav a').css({backgroundPosition: '-156px 100%'});
	jQuery('#nav li.selected').removeClass('selected');
	jQuery('#nav li.selected a').css({backgroundPosition: '0px 100%'});
	
	// On click behaviour...
	jQuery('#nav a').click(function(){
		links_set = true;
		// Define the parent <li> of the clicked on link
		var li =  jQuery(this).parent('li');
		// Define the submenu of the clicked-on link
		var submenu = li.children('ul');
		// Define the sibling or child of the clicked-on link that is currently selected (which may be the clicked-on link)
		var selected_parent = li.parent('ul').children('li.selected');
		// Define the submenu which is currently open
		var open_submenu = selected_parent.find('ul');
		// Define list items within the open sub menu
		var nested_li = open_submenu.find('li');
		
		// If the clicked on link is not selected...
		if(li.hasClass('selected') == false)
		{
		// Hide the open submenu
		open_submenu.animate({left: '120%',opacity:0}, 'easeOutQuint', function(){
			// When fade out animation has stopped, hide the menu
			open_submenu.hide();
			// Remove the class .selected
			nested_li.removeClass('selected');
			// Reset the background position
			nested_li.find('a').css({backgroundPosition: '-156px 100%'});
		});
		}else{
			// Hide the background of the selected link within the open submenu
			nested_li.find('a').animate({backgroundPosition: '-156px 100%'}, 500, function(){nested_li.removeClass('selected')});
			nested_ul = nested_li.find('ul');
			nested_ul.animate({left: '120%',opacity:0}, 'easeOutQuint', function(){
				// When fade out animation has stopped, hide the menu
				nested_ul.hide();
			});

		}
		
		
		
		
		// If the clicked on link is not selected...
		if(li.hasClass('selected') == false)
		{
			// Clicked on link is not selected, so unset the currently selected one...
			selected_parent.removeClass('selected');
			// Add the class to the clicked on link
			li.addClass('selected');
			// Animate that background of the clicked on link
			jQuery(this).animate({backgroundPosition: '0px 100%'}, 1000);
		//li.parent('ul').children('li.selected a').animate({backgroundPosition: '-156px 100%',opacity:0.25}, 500);
		
			// Hide the background of the currently selected parent
			selected_parent.children('a').animate({backgroundPosition: '-156px 100%'}, 500);
			// Set the start position of the animation of the submenu
			submenu.css({left:'120%',opacity:0,display:'block'});
			// Animate the new submenu in
			submenu.animate({left: '100%',opacity:1}, {easing: 'easeOutQuint'} );
		}
		//jQuery('#nav li.selected').css({backgroundPosition:'-156px 100%'});
		//jQuery('#nav li.selected').animate({backgroundPosition: '-156px 100%',opacity:0.25}, 500);
		//jQuery('#nav li.selected').hide();
		
		
		
		
	});
	
	
	
}

function set_link(link_obj)
{
	DEBUG_trace('Link: '+has_set_link+' :: '+' Request: '+request_uri);
	if(has_set_link != request_uri)
	{	
	// Show this link...
	//alert('setting link '+link_obj.attr('href'));
	animate_link_in(link_obj);
	}
	
}

function animate_link_in(link_obj)
{
	DEBUG_trace('function: animate_link_in');
	var li = link_obj.parent('li');
	var submenu = li.children('ul');
	var siblings = li.siblings('li');
	siblings.each(function(){
		//alert(jQuery(this).children('a').attr('href'));
	});
	
	siblings.removeClass('selected');
	animate_link_out(siblings.children('a'));
	submenu.find('a').each(function(){
		DEBUG_trace('* animating link out *');
		$(this).animate({backgroundPosition: '-156px 100%'}, 500).parent('li').removeClass('selected');
	});
	if(li.hasClass('selected') == false)
	{
		li.addClass('selected');
		DEBUG_trace('animating...' + link_obj.attr('href'));
		link_obj.animate({backgroundPosition: '0px 100%'}, 1000);
		animate_link_out(siblings.children('a'));
		show_menu(submenu);
	}
}

function animate_link_out(link_obj)
{
	var li = link_obj.parent('li');
	var submenu = li.children('ul');
	if(li.hasClass('selected') == false)
	{
		//alert('parent not selected');
		li.removeClass('selected');
		link_obj.animate({backgroundPosition: '-156px 100%'}, 500);
		hide_menu(submenu);
	}
}

function show_menu(submenu)
{
	DEBUG_trace('showing submenu');
	submenu.css({left:'120%',opacity:0,display:'block'});
	submenu.animate({left: '100%',opacity:1}, {easing: 'easeOutQuint'} );	
}

function hide_menu(submenu)
{
	submenu.animate({left: '120%',opacity:0}, 'easeOutQuint', function(){
		submenu.hide();
		animate_link_out(submenu.find('a'));
	});
}

function catch_ajax_links()
{
	DEBUG_trace('getting ajax links on the page');
	// Get any links which start with a slash...
	jQuery('#nav a[href^=/], #nav a[href^=http://'+ http_host +'/]').click( function(ev) {
		links_set = true;
		jQuery(this).blur();
		// Get the href
		var href= jQuery(this).attr('href');
		
		// Set location to the requested href after the hash
		window.location = "/#"+href;
		// Show the loader...
		jQuery(this).append('<img src="/images/ajax-mini-loader.gif" id="link_load" />');
		jQuery('#link_load').fadeIn()
		// Stop the default link
		ev.preventDefault();
		// Send an ajax request instead...
		post(href);
    });
}

function get_new_ajax_links()
{
	DEBUG_trace('finding any new ajax links');
	//Panel link styles...
	jQuery('ul.panel_list a').hover(		
		function()
		{
			var li = jQuery(this).parent('li');
			var img = jQuery(this).children('img');
			var h3 = jQuery(this).children('h3');
			img.stop();
			img.animate({width:'120%',height:'120%',opacity:0.5});
			h3.stop();
			h3.animate({paddingBottom:'20px'});
			//li.animate({opacity:0.75});
		},
		function()
		{
			var li = jQuery(this).parent('li');
			var img = jQuery(this).children('img');
			var h3 = jQuery(this).children('h3');
			img.stop();
			img.animate({width:'100%',height:'100%',opacity:1,marginLeft:0,marginTop:0});
			h3.stop();
			h3.animate({paddingBottom:'2%'});
			//li.animate({opacity:1});
		}
		
	);
	// Get any links which start with a slash...
	jQuery('#data a[href^=/], #data a[href^=http://'+ http_host +'/]').click( function(ev) {
		
		jQuery(this).blur();
		// Get the href
		var href= jQuery(this).attr('href');
		
		href= href.replace('http://'+http_host,'');
		
		DEBUG_trace('*** HANDLING LINK '+href+' WITH AJAX ***');
		request_uri = href;
		set_links_by_href(href);
		
		//jQuery('#nav a[href^=/about-us/location]').children('li').find('ul').hide();
		// Set location to the requested href after the hash
		window.location = "/#"+href;
		// Show the loader...
		//jQuery(this).append('<img src="/images/ajax-mini-loader.gif" id="link_load" />');
		//jQuery('#link_load').fadeIn()
		// Stop the default link
		ev.preventDefault();
		// Send an ajax request instead...
		post(href);
  });   
    
}

function set_links_by_href(href)
{
	if(!links_set)
	{
		DEBUG_trace('setting links for '+href);
		var crumbs = href.split('/');
		var string = "/";
		var breadtrail = "";
		//jQuery('#nav li.selected').removeClass('selected');
		for(i=1;i<(crumbs.length-1);i++)
		{
			
			string += crumbs[i]+"/";
			DEBUG_trace(i + ': ' + string);
			//alert(string);			
			var this_link = jQuery('#nav a[href='+string+']');
			//this_link.parents('li').addClass('selected');
			//alert('setting link for '+string);
			
			set_link(this_link);
			has_set_link = string;			
		}
		links_set = true;
	}
}

function set_breadcrumbs()
{
	var output = $('#breadcrumb_output').html();
	$('#breadcrumbs').html(output);
		
	jQuery('#breadcrumbs a').click( function(ev) {
	
		jQuery(this).blur();
		// Get the href
		var href= jQuery(this).attr('href');
		
		href= href.replace('http://'+http_host,'');
		
		DEBUG_trace('*** HANDLING LINK '+href+' WITH AJAX ***');
		request_uri = href;
		//set_links_by_href(href);
		
		//jQuery('#nav a[href^=/about-us/location]').children('li').find('ul').hide();
		// Set location to the requested href after the hash
		//window.location = "/#"+href;
		// Show the loader...
		//jQuery(this).append('<img src="/images/ajax-mini-loader.gif" id="link_load" />');
		//jQuery('#link_load').fadeIn()
		// Stop the default link
		ev.preventDefault();
		// Send an ajax request instead...
		//post(href);
		
		jQuery('#nav a[href='+href+']').trigger('click');
	});
	
	$('#breadcrumbs').fadeIn('fast');	
}

function DEBUG_trace(msg)
{
	if(debug)
	{
		var debug_pane = '<div style="position:fixed;bottom:0;left:0;height:300px;width:100%;overflow:scroll;background:#FFF;z-index:9999;font-family:\'Courier New\', Courier, monospace;font-size:12px;" id="debug"></div>'
		if(!debug_init) $('html').append(debug_pane);debug_init = true;
		$('#debug').prepend((trace_count++)+': '+msg+"<br />--------------<br />");
		//alert(msg);	
	}
}

function start_page_load_request()
{
	$('#breadcrumbs').fadeOut();
}
