/*
 * JQuery for site behaviour
 * By Matt Finucane, September 2009
 */

$(document).ready(function()
{
	
	// Show the slideshow nav for users with javascript enabled 
	$('#portfolio-slideshow-nav > ul').css('display', 'inline');
	
	//Add behaviour to slideshow nav links
	$('#portfolio-slideshow-nav > ul > li > a').click(function()
	{
		hideAndShowPortfolioThumb($(this));
		return false;
	});
	
	//Autogrow the contact form text area on newline.
	$('#enquiry_body').autogrow();
	
	//Attach animated scroll behaviour to contact button
	$('#contact, #contact-us-now-button').click(function()
	{
		var $anchor_y_position = $('#keep-in-touch').position().top;
		$('html, body').animate({scrollTop: $anchor_y_position}, 1750, 'swing', function()
		{
			$('#enquiry-form > h2, #keep-in-touch > h2').effect('pulsate', { times: 1}, 400);
		}); 
		
		return false;
	});
	
	//All external links (rel="external") should open in a new window or tab
	$('a[rel="external"]').click(function()
	{
		$(this).attr('target', '_blank');
	});
	
	//Prime the conversion forms to clear on click
	$('#enquiry_name').focus(function()
	{
		if($(this).attr('value') == 'Your name / Company name')
		{
			$(this).attr('value','');
		}
	});
	$('#enquiry_name').blur(function()
	{
		if($(this).attr('value') == '')
		{
			$(this).attr('value', 'Your name / Company name');
		}
	});
	
	$('#enquiry_contact').focus(function()
	{
		if($(this).attr('value') == 'Email address / phone number')
		{
			$(this).attr('value', '');
		}
	});
	$('#enquiry_contact').blur(function()
	{
		if($(this).attr('value') == '')
		{
			$(this).attr('value', 'Email address / phone number');
		}
	});
	$('#enquiry_body').focus(function()
	{
		if($(this).attr('value') == 'How can we help you?')
		{
			$(this).attr('value', '');
		}
	});
	$('#enquiry_body').blur(function()
	{
		if($(this).attr('value') == '')
		{
			$(this).attr('value', 'How can we help you?');
		}
	});	
	
	if($('#portfolio-slideshow'))
	{
		preloadPortfolioWidgetImages();
	}
});	

function preloadPortfolioWidgetImages()
{
	$.ajax(
	{
		type: 'GET',
		url: 'http://' + window.location.hostname + '/portfolio.xml',
		dataType: 'xml',
		success: function(xml)
		{
			$(xml).find('client > images > thumb').each(function()
			{
				
				var $img_src = $(this).attr('filename');
				var preloaded_img = new Image();
				preloaded_img.src = $img_src;
			});
		}
	});
}

function hideAndShowPortfolioThumb(client_slug)
{
	var $portfolio_thumb = $('#portfolio-slideshow > img');
	$.ajax(
	{
		type: 'GET',
		url: 'http://' + window.location.hostname + '/portfolio.xml',
		dataType: 'xml',
		success: function(xml)
		{
			$('#portfolio-slideshow-nav h2, #portfolio-slideshow-nav p').fadeOut(200, function()
			{
				$(xml).find('client[slug="' + client_slug.attr('id') + '"]').each(function()
				{
					$('#portfolio-slideshow-nav h2 a').attr('href', 'http://' + window.location.hostname + '/view-portfolio/' + $(this).attr('slug') + '.html');
					$('#portfolio-slideshow-nav h2 a').attr('title', $(this).attr('name'));
					$('#portfolio-slideshow-nav h2 a').text($(this).attr('name'));
				});
			});
			
			$(xml).find('client[slug="' + client_slug.attr('id') + '"] > images > thumb').each(function()
			{
				
				var $img_src = $(this).attr('filename');
				var $img_alt = $(this).attr('alt');		
				$portfolio_thumb.fadeOut(300, function()
				{
					setImageSrc($portfolio_thumb, $img_src);
					$portfolio_thumb.attr('alt', $img_alt);
				});
				$portfolio_thumb.fadeIn(700, function()
				{
					$('#portfolio-slideshow-nav h2, #portfolio-slideshow-nav p').fadeIn(150);
				});
			});
		}
	});
}

function setImageSrc(image_object, image_source)
{
	image_object.attr('src', 'http://' + window.location.hostname + '/portfolio-images/landing/' + image_source);
}

