/* ########################################################################### *
/* ***** DOCUMENT INFO  ****************************************************** *
/* ########################################################################### *
 * ##### NAME:  global.js
/* ########################################################################### */

/********************/
/* Initialisation	*/
/********************/
$(document).ready(custom_init);
$(document).ready(gh_init);

function custom_init()
{	
	// Custom functions
	custom_init_navigation();
}

function gh_init()
{
	// Setup links
	gh_init_links();
}


/********************/
/* CUSTOM FUNCTIONS */
/********************/

/* Navigation */
function custom_init_navigation()
{
	// Setup navigation
	$('#logo h1 a, #nav_primary li h2 a, a.navLink').live('click', function() 
	{
		custom_navigate($(this).attr('href'));
		return false;
	});
}

// Display the home / back button if required
function custom_refreshBackButton()
{
	var title = $('h1 .title').html();
	if (title == 'George Hiley' || title == '')
	{
		// Hide the back button
		$('.link_home').fadeOut("slow", function() 
		{
			$('.link_home_wrapper').remove();
		});
	}
	else
	{
		if ($('.link_home').length < 1)
		{
			// Show the home button
			$('h1 .logo-block').after('<span class="link_home_wrapper"></span>');
			//$('#pageContent').after('<span class="link_top_wrapper"></span>');
			
			
			$('.link_home_wrapper').hide().html('<span class="link_home">Back</span>').fadeIn();
			//$('.link_top_wrapper').hide().html('<span class="link_home"><a href="index.php">Back</a></span>').fadeIn();
		}
	}
}

function custom_navigate(href)
{
	if (!custom_isAnimating())
	{
		// Show loading signifier
		custom_showLoading();
		
		// Hide the current page
		$('#pageContent').hide("slide", { direction: "left" }, 500, function() 
		{
			// Hide the page title
			$('h1 .title').hide("slide", { direction: "left" }, 200, function()
			{	
				// Load html via ajax
				$.get(href, function(data)
				{
					// Set & show the page title
					$('h1 .title').html($(data).find('h1 .title').html()).show("slide", { direction: "right" }, 200, function()
					{
						// Assign the page content
						$('#pageContent').html($(data).find('#pageContent').html())
						
						// Setup any stuff in #pageContent
						custom_init_pageContent();
					
						$('#pageContent').show("slide", { direction: "right" }, 500, function() 
						{
							// Show new generated HTML
							$('#screenshots').slideDown();
							
							// Toggle the back button
							custom_refreshBackButton();
						});
					});
				});	
			});
		});
	}
}

function custom_init_pageContent()
{
	// Hide screenshot contenet if exists
	$('#screenshots').hide();
	
	// Hide nav_screenshots / accessible resource content
	$('#screenshots .nav_screenshots').hide();
	
	var newHtml = '';
	$('#screenshots .nav_screenshots li').each(function ()
	{	
		var title = $(this).find('a:first').html();
		var url = $(this).find('a:first').attr('href');
		newHtml += '<div class="screenshot"><h3>' + title + '</h3><img src="' + url + '" alt="' + title + '" /></div>';
	});
	newHtml += '<a href="index.php" class="navLink backLink">Back</a>';
	
	$('#screenshots .nav_screenshots').after(newHtml);
	
	$('#screenshots .screenshot').click(function() 
	{
		$(this).toggleClass('fullView');
		$(this).blur();
		
		return false;
	});
	
	
	// If has screenshots list, change to correct format for display
	//var screenshots = $(data).find('#screenshots');
	//$('body').append('<div class="newData hidden">' + screenshots + '</div>');
	
	
	// screenshots = $(screenshots).find('h2').remove().html();
	// data = $(data).find('#screenshots h2, #screenshots ul').remove();
	//data = $($(data).find('#screenshots').html()).find('h2').html('TEsting');
	//alert($(data).html());
	
	//alert($('body #screenshots').html());
}

function custom_showLoading()
{
	$('#logo h1 .logo-block').fadeTo(100, '0.9', function()
	{
		$(this).fadeTo(100, '1', function()
		{	
			if (custom_isAnimating())
			{
				custom_showLoading();
			}
		});
	});
}

function custom_isAnimating()
{
	if ($('*:animated').not('#logo h1 .logo-block').length > 0)
	{
		return true;
	}
	else
	{
		return false;
	}
}


/********************/
/* GLOBAL FUNCTIONS */
/********************/

// Links
function gh_init_links()
{
	$('._blank').live('click', function() 
	{
		$(this).attr('target', '_blank');
	});
}




/**********************/
/* END */
