$(document).ready(function() {
	$('#newsletter_error').hide();
	
	/* open links with rel="external" in new window and amend link title */
	$('a[rel*="external"]').each(function() {
		var title = $(this).attr('title') ? $(this).attr('title') + ' (opens in a new tab or window)' : 'This link will open in a new tab or window';
		$(this).attr('title',title).click(function() {
			window.open($(this).attr('href'));
			return false;
		});
	});
	
	$('#menu li ul').hide();

	$('#menu > li').hover(function() {
		var	_ul = $(this).find('ul'),
			_link = $(this).find('a:first');
		
		if(_ul) {
			_link.addClass('selected');
			_ul.slideDown();
		};
	}, function() {
		var	_ul = $(this).find('ul'),
			_link = $(this).find('a:first');
		
		_ul.slideUp('', function() {
			_link.removeClass('selected');
			
		});
	});

	$('#menu li ul').hover(function() {
		$(this).show()
	}, function() {
//		$(this).slideUp();
	});
	
	$('#menu li a.contact_us, #footer p a.contact_link, a.open_contact').click(function() {
		cls = $(this).attr('class');
		if($('#contact').hasClass('open')) {
			$(this).removeClass('selected');
			$('#contact').removeClass('open').slideUp();
		} else {
			$(this).addClass('selected');
			$('#contact').addClass('open').slideDown();
			
			if(cls == 'contact_link') {
				window.location = '#contact';
			};
		};
		return false;
	});
	
	$('#testimonials div').hide();
	$('#testimonials div.selected').show();
	
	setInterval(function() {
		var _cur = $('#testimonials div.selected');
		if(_cur.next('div').hasClass('testimonial')) {
			var _next = _cur.next('div');
		} else {
			var _next = $('#testimonials div:first');
		};
		
		_cur.removeClass('selected').fadeOut(500, function() {
			_next.addClass('selected').fadeIn(500);
		});
	}, 5000);
	
	
	$('#content_reveal').hide();
	$('#seo_reveal').click(function() {
		$('#content_reveal').slideToggle('normal', function() {
			window.location = '#content_reveal';
		});
		
		return false;
	});
	
	$('#newsletter_email').focus(function() {
		if($(this).val() == 'Enter Email') {
			$(this).val('');
		};
	}).blur(function() {
		if($(this).val() == '') {
			$(this).val('Enter Email');
		};
	});
	
	$('#search_bar').focus(function() {
		if($(this).val() == 'Search') {
			$(this).val('');
		};
	}).blur(function() {
		if($(this).val() == '') {
			$(this).val('Search');
		};
	});
	
	function newsletter_signup()
	{
		var e = $('#newsletter_email').val();
		
		$.post('/_ajax/post/newsletter/', {
			email:e
		}, function(data) {
			if(data) {
				$('#newsletter_error').html(data).fadeIn('slow');
			} else {
				$('#newsletter input').fadeOut(500, function() {
					$('#newsletter_error').html('Thank you. Your email address has been added to our newsletter.').fadeIn('slow');
				});
			};
			return false;
		});
	}
	
	$('#newsletter_submit').click(function() {
		newsletter_signup();
		return false;
	});
	
	$('#newsletter').submit(function() {
		newsletter_signup();
		return false;
	});
	
	
	function resizeText(multiplier) {
		if (document.body.style.fontSize == "") {
			document.body.style.fontSize = "1.0em";
		};
		document.body.style.fontSize = parseFloat(document.body.style.fontSize) + (multiplier) + "em !important";
		var p = document.getElementsByTagName('p');
		for(i=0;i<p.length;i++) {
			if(p[i].style.fontSize) {
				var s = parseInt(p[i].style.fontSize.replace("px",""));
			} else {
				var s = 12;
			};
			var newsize = parseFloat(s) + (parseFloat(multiplier)*5);
			p[i].style.fontSize = newsize+"px";
		};
	};
	
	$('a.size_l').click(function() {
		resizeText(0.2);
		return false;
	});
	
	$('a.size_xl').click(function() {
		resizeText(1);
		return false;
	});
	
	
	$('a.helper_link').click(function() {
		$(this).fadeOut(500, function() {
			$('#helper_wrapper').html('<iframe src="http://player.vimeo.com/video/37233242?title=0&amp;byline=0&amp;portrait=0;color=59a5d1&amp;autoplay=1" width="355" height="266" frameborder="0" webkitAllowFullScreen allowFullScreen></iframe>');
		});
		return false;
	});
	
	
	$('#contact_submit').click(function() {
		var form_name = $('#contact_name').val(),
			form_email = $('#contact_email').val(),
			form_telephone = $('#contact_telephone').val(),
			form_hear = $('#hear_about_us').val(),
			form_message = $('#contact_message').val();
		
		$.post('/_ajax/post/contact/', {
			name:form_name,
			email:form_email,
			telephone:form_telephone,
			hear:form_hear,
			message:form_message,
			action:'validate'
		}, function(data) {
			if(data == '' || !data) {
				$.post('/_ajax/post/contact/', {
					name:form_name,
					email:form_email,
					telephone:form_telephone,
					hear:form_hear,
					message:form_message,
					action:'submit'
				}, function(data2) {
					$('#contact_form').slideUp(250, function() {
						$('#contact').append('<p><strong>Thank you. Your enquiry has been sent successfully.</strong></p>');
					});
				});
			} else {
				$('#contact_error').html(data);
			};
		});
		return false;
	});
	
});

