// General Support v1.0
// Andrew Pettican (June 2010)
//
// Changelog:
// v1.0 - 11/06/2010 - Initial build
//////////////////////////////////////////////////////////////////

// ---------------------------------------------------------------
// Parameters
// ---------------------------------------------------------------

var gs_nav_fade_duration_in = 500; // Duration to fade nav links in
var gs_nav_fade_duration_out = 500; // Duration to fade nav links out
var gs_background_cycle_delay = 5000; // Delay between background cycles
var gs_background_cycle_duration = 2000; // Duration of a cycle fade
var gs_project_fade_duration_in = 1500; // Duration of a project image fade in
var gs_project_fade_duration_out = 750; // Duration of a project image fade out
var gs_insert_contact_id_prefix = "insert_contact-"; // Insert contact link prefix

// ---------------------------------------------------------------
// Parameters End
// ---------------------------------------------------------------



// ---------------------------------------------------------------
// JS Events
// ---------------------------------------------------------------

$(document).ready(function()
{
	// When the mouse hovers over a ul.nav link element, animate the colour change
	//////////////////////////////////////////////////////////////
	$('#nav ul li a').live("mouseover mouseout", function(event)
	{
		// Determine if we are activating or deactivating the hover
		var activate_hover = (event.type == "mouseover");
		// alert("nav hover event - " + event.type);
		
		// Fetch the parent li element
		var parent_li = this.parentNode;
		
		// Animate the link based on the parent element class and if we are activating or deactivating the hover state
		if($(parent_li).hasClass('active'))
		{
			// Active element: <ul><li.active><a>
			if(activate_hover) {
				$(this).stop().animate({ color: '#000000' }, gs_nav_fade_duration_in);
			} else {
				$(this).stop().animate({ color: '#000000' }, gs_nav_fade_duration_out);
			}
		}
		else
		{
			// Inactive element: <ul><li><a>
			if(activate_hover) {
				$(this).stop().animate({ color: '#000000' }, gs_nav_fade_duration_in);
			} else {
				$(this).stop().animate({ color: '#B29759' }, gs_nav_fade_duration_out);
			}
		}
	});
	
	
	// When the mouse hovers over a p.nav_footer link element, animate the colour change
	//////////////////////////////////////////////////////////////
	$('p.nav_footer a').live("mouseover mouseout", function(event)
	{
		// Determine if we are activating or deactivating the hover
		var activate_hover = (event.type == "mouseover");
		// alert("nav_footer event - " + event.type);
		
		if(activate_hover) {
			$(this).stop().animate({ color: '#000000' }, gs_nav_fade_duration_in);
		} else {
			$(this).stop().animate({ color: '#B29759' }, gs_nav_fade_duration_out);
		}
	});
	
	
	// Create all contact links
	//////////////////////////////////////////////////////////////
	$('a.insert_contact').each(function()
	{
		// Determine the email address
		var contact_name = $(this).attr('rel').substr(gs_insert_contact_id_prefix.length);
		var contact_email = generate_email(contact_name);
		
		// Update the link with the correct email address
		$(this).attr('href', "mailto:"+contact_email);
		if($(this).html() == "")
		{
			$(this).html(contact_email);
		}
	});
	
	
	// A project thumbnail was clicked
	//////////////////////////////////////////////////////////////
	var loading_image = "";
	$('#project_thumbs a').click(function()
	{
		// Find the thumbnail within this <a> tag
		var thumb = $(this).children('img:first');
		if(thumb.length >= 1)
		{
			// Determine the new and old img id and src
			var old_img_id = $('#project_image img:first').attr("id");
			var new_img_id = $(thumb).attr("id");
			
			// Only switch the images if the id's differ
			if(old_img_id != new_img_id)
			{
				// What is the new img src?
				var new_img_src = $(thumb).attr("src").replace("/project_thumbs/", "/project/");
				
				// Fade out the old image?
				if($('#project_image img').length > 0)
				{
					$('#project_image img').stop().fadeOut(gs_project_fade_duration_out, function()
					{
						// Precaution to prevent multiple fadeIn events triggering
						if(loading_image != new_img_id)
						{
							// Set flag so we know which image is loading
							loading_image = new_img_id;
							
							// Remove the old image
							$('#project_image img').remove();
							
							// Generate the new image tag
							$('#project_image').append('<img src="'+new_img_src+'" id="'+new_img_id+'" alt="" style="display:none;" />');
							
							// Fade in the new image
							$('#project_image img').fadeIn(gs_project_fade_duration_in);
						}
					});
				}
				
				// There isn't an image to fade out, fade the new one in now
				else
				{
					// Generate the new image tag
					$('#project_image').append('<img src="'+new_img_src+'" id="'+new_img_id+'" alt="" style="display:none;" />');
					
					// Fade in the new image
					$('#project_image img').fadeIn(gs_project_fade_duration_in);
				}
			}
		}
		return false;
	});
	
	
	// Load the page in a new window
	//////////////////////////////////////////////////////////////
	$('a[rel=external], a.external_link').each(function()
	{
		// Set target, clear rel and update titles
		$(this).attr("target", "_blank").attr("rel", "");
		var old_title = $(this).attr("title");
		if(old_title !== "")
		{
			$(this).attr("title", old_title+" [Opens in new window]");
		}
		else
		{
			$(this).attr("title", "[Opens in new window]");
		}
	});
});

// Fires after everything has downloaded
$(window).load(function()
{
	// Start cycling the backgrounds?
	//////////////////////////////////////////////////////////////
	if($('#background img').length > 0)
	{
		$('#background').cycle({ 
			timeout: gs_background_cycle_delay, 
			speed: gs_background_cycle_duration,
			random: 1
		});
	}
});

// ---------------------------------------------------------------
// JS Events End
// ---------------------------------------------------------------



// ---------------------------------------------------------------
// Functions
// ---------------------------------------------------------------

/**
 * Email link generator
 */
function insertContact(theName, linkText, linkclass)
{
	var theDomain = ('st' + 'james' + 'cd' + '.' + 'com');
	
	var theClass = "";
	if(typeof(linkclass) == "string")
	{
		theClass = 'class="' + linkclass + '" ';
	}
	
	var theAddress = (theName + '&#064' + theDomain);
	if(typeof(linkText) != "string")
	{
		linkText = theAddress;
	}
	document.write('<a ' + theClass + 'href="mailto:' + theAddress +'">' + linkText + '<\/a>');
}


/**
 * Email address generator
 */
function generate_email(theName)
{
	var theDomain = ('st' + 'james' + 'cd' + '.' + 'com');
	return (theName + '@' + theDomain);
}

// ---------------------------------------------------------------
// Functions End
// ---------------------------------------------------------------
