/**
 * Position the text within the image container
 **/
function Reposition()
{
	Cufon.replace('#home_top_image > strong > span', { autoDetect : true });
	$('#home_top_image_controls > span > span').css('padding-top', (($('#home_top_image_controls > span').height() / 2) - ($('#home_top_image_controls > span > span').height() / 2)));
}
function set_header_content(title, description, url)
{
	$('#home_top_image strong span').text(title);
	$('#home_top_image_controls > span > span').text(description);
	$('#home_top_image_controls button').attr('Url', url);
}
function load_twitter(content, count)
{
	$('#twitter div p').show();
	count = count || 4;
	if(content)
	{
		$('#twitter div ul').html('');
		for(var i in content)
		{
			var d = new Date(content[i].created_at);
			$('#twitter div ul').append($('<li><a target="_blank" href="http://twitter.com/'+twitter_user+'/status/'+content[i].id+'" title="'+date('h:ia F jS Y', d)+'"><span>'+date('h:ia', d)+'</span> '+date('F jS Y', d)+'</a><span>' + content[i].text + '</span></li>'));
		}
		$('#twitter div p').hide();
		return;
	}
	$.getScript('http://twitter.com/status/user_timeline/'+twitter_user+'.json?count=' + count + '&callback=load_twitter');
}
function home_slide()
{
	$('#home_top_image_controls_next').click();
	setTimeout(home_slide, 5000);
}
$(document).ready(function()
{
	Cufon.replace('#special_offers h1', { fontFamily : 'bold' });
	Reposition();
	$('#home_top_image_controls_previous').click(function(e)
	{
		e.preventDefault();
		var current = $('#home_top_images img.active');
		var previous = current.prev();
		if(previous.length == 0) previous = $('#home_top_images img').last();
		current.fadeOut(400, function() { $(this).removeClass('active'); });
		previous.fadeIn(400, function() { $(this).addClass('active'); var parts = $(this).attr('title').split(' - '); set_header_content(parts[0], parts[1], parts[2]); Reposition(); });
	});
	$('#home_top_image_controls_next').click(function(e)
	{
		e.preventDefault();
		var current = $('#home_top_images img.active');
		var next = current.next();
		if(next.length == 0) next = $('#home_top_images img').first();
		current.fadeOut(400, function() { $(this).removeClass('active'); });
		var parts = next.attr('title').split(' - '); set_header_content(parts[0], parts[1], parts[2]); 
		Reposition();
		next.fadeIn(400, function() { $(this).addClass('active'); });
	});
	$('#home_top_image_controls button').click(function(e)
	{
		e.preventDefault();
		if($(this).attr('Url'))
			window.location.href = $(this).attr('Url');
	});
	setTimeout(home_slide, 5000);
	load_twitter();
	Cufon.replace('#special_offers h1', { textShadow : '#000 1px 1px 1px' });
	$('.modal_inner > a').click(function(e)
	{
		e.preventDefault();
		$('.modal').fadeOut(400, function()
		{
			$('.modal_page').removeClass('active');
			$('.modal_page').first().addClass('active');
			$('#form1 form input, #form1 form textarea').val('');
			$('.form-step').removeClass('active').first().addClass('active');
		});
	});
	$('#modal_page_left .AskForDetails').click(function(e)
	{
		e.preventDefault();
		$('.modal_page').removeClass('active');
		$('.form-step').removeClass('active').first().addClass('active');
		$('#form1').addClass('active');
	});
	$('#reset').click(function(e)
	{
		$('#form1 form input, #form1 form textarea').val('');
		$('.form-step').removeClass('active').first().addClass('active');
	});
	$('#next').click(function(e)
	{
		var errors = [];
		if($('#so_name').val().length == 0) errors.push('Your name is required');
		if($('#so_email').val().length == 0) errors.push('Your email address is required');
		if($('#so_phone').val().length == 0) errors.push('Your telephone no is required');

		if(errors.length > 0)
			alert('Please correct the following errors:\n\n' + errors.join('\n'));
		else
		{
			$('.form-step').removeClass('active');
			$('#step2').addClass('active');
		}
		
	});
	$('#send').click(function(e)
	{
		e.preventDefault();

		if($(this).hasClass('busy'))
			return;

		var me = $(this);
			me.attr('disabled', 'disabled');

		var errors = [];
		if($('#so_name').val().length == 0) errors.push('Your name is required');
		if($('#so_email').val().length == 0) errors.push('Your email address is required');
		if($('#so_phone').val().length == 0) errors.push('Your telephone no is required');

		if(errors.length > 0)
		{
			alert('Please correct the following errors:\n\n' + errors.join('\n'));
		}
		else
		{
			$(this).addClass('busy');
			var d = $('#form1 form').serialize();
				d+='&Send=Send';
			var k = $('u').text().split('|');
				d+='&'+k[0]+'='+k[1];
			$.ajax(
			{
				url : window.location.href,
				type : 'post',
				data : d,
				dataType : 'json',
				success : function(resp)
				{
					me.attr('disabled', 0).removeClass('busy');		
					if(resp.Error)
					{
						alert(resp.ErrorDescription.replace(/\\n/g, '\n'));
					}
					else
					{
						$('.form-step').removeClass('active');
						$('#complete').addClass('active');
					}
				},
				error : function()
				{
					me.attr('disabled', 0).removeClass('busy');		
					alert('An unknown error has occurred, please try again');
				}
			});
		}
	});
	$('#special_offers li a').click(function(e)
	{
		e.preventDefault();
		$('#modal_page_right img').removeClass('active');
		var img = $('#modal_page_right img[title=' + $(this).attr('title') + ']').addClass('active');
		/*var t = parseFloat(img.attr('rel'));
		if(isNaN(t)) t = 105;
		$('#h3_top').css('margin-top', t);*/

		$('#modal_page_left h1').text(img.attr('title'));
		Cufon.replace('#modal_page_left h1', { fontFamily : 'bold' });
		$('#email_modal').fadeIn();
	});
	$('.modal').click(function(e)
	{
		e.preventDefault();
		if(e.target == this)
			$(this).fadeOut();
	});
	$('#special_offers .ViewExamples').click(function(e)
	{
		e.preventDefault();
		$('.modal').fadeIn();
	});
	if(window.location.hash == '#special-offers')
		$('#special_offers .ViewExamples').click();
	$('#modal_page_left_previous').click(function(e)
	{
		e.preventDefault();
		var active = $('#modal_page_right img.active');
		if(active.length == 0) active = $('#modal_page_right img').first();
		var next = active.prev();
		if(next.length == 0) next = $('#modal_page_right img').last();
		next.addClass('next');

		/*var t = parseFloat(next.attr('rel'));
		if(isNaN(t)) t = 105;
		$('#h3_top').css('margin-top', t);*/

		$('#modal_page_left h1').text(next.attr('title'));
		Cufon.replace('#modal_page_left h1', { fontFamily : 'bold' });
		active.hide('slide', { direction : 'left' }, 1000, function()
		{
			$('#modal_page_right img').removeClass('active').show();
			next.removeClass('next').addClass('active');
		});
	});
	$('#modal_page_left_next').click(function(e)
	{
		e.preventDefault();
		var active = $('#modal_page_right img.active');
		if(active.length == 0) active = $('#modal_page_right img').last();
		var next = active.next();
		if(next.length == 0) next = $('#modal_page_right img').first();
		next.addClass('next');

		/*var t = parseFloat(next.attr('rel'));
		if(isNaN(t)) t = 105;
		$('#h3_top').css('margin-top', t);*/
		
		$('#modal_page_left h1').text(next.attr('title'));
		Cufon.replace('#modal_page_left h1', { fontFamily : 'bold' });
		active.hide('slide', { direction : 'right' }, 1000, function()
		{
			$('#modal_page_right img').removeClass('active').show();
			next.removeClass('next').addClass('active');
		});
	});
});
