$(document).ready(function(){

	// Add FadeToggle Function
	jQuery.fn.fadeToggle = function(speed, easing, callback) {
		return this.animate({opacity: 'toggle'}, speed, easing, callback);
	};

	// Rating for feedback form
	$('#stars').raty();

	// Form validation
	$("#feedback").validate();
	$("#enquiry").validate();

	// Navigation functionality

	// Page preview functionality
	$('#section-preview-nav li a').click( function(){
		var current = 	'#' + $('#section-preview .selected').attr('id');
		var view = $(this).attr('rel');
		var placeholder = '#section-preview-' + view;
		if(current != placeholder){

			$('#section-preview .selected').slideUp( function(){
				$(this).removeClass('selected');
			});

			$(placeholder).slideDown( function(){
				$(this).addClass('selected');
			});

		}
		return false;
	});

	// Showcase options
	$('#showcase-img').cycle({
		fx: 'fade',
		delay:  -3000
	});

	// Testimonials cycle

	$('#testimonials .items').cycle({
		fx: 'fade',
		fastOnEvent: 1000,
		height: 150,
		next: $('#testimonials .next'),
		prev: $('#testimonials .prev'),
		timeout: 6000
	});

	// Jargon cycle

	$('#jargon-tool .upper ul').cycle({
		height: 90,
		next: $('#jargon-tool .next'),
		prev: $('#jargon-tool .prev'),
		timeout: 0,
		before: function(currSlideElement, nextSlideElement, options, forwardFlag) {			
			$(currSlideElement).removeClass('active');
			$(nextSlideElement).addClass('active');
		},
		after: function() {
			$('#jargon-tool .jargon').show();
			$('#jargon-tool .result').hide();
		}
	});

	$('#jargon-tool .bust').click(function () {
		$('#jargon-tool .active .jargon').fadeOut();
		$('#jargon-tool .active .result').fadeIn();

		return false;
	});

	//Phone number spring out
	$("div#branding img.eps-phone").hide();

	setTimeout(function(){
		$("div#branding img.eps-phone").show("slide", { direction: "right" }, 1000);
	});


	// IE6 Navigation UX tweeks
	if($.browser.msie && $.browser.version.substr(0,1)<7) {

		$("div#main-navigation li ul li a").first().css('border','none');

		$("div#main-navigation ul li").hover( function(){
			$(this).find('ul').toggle();
		});
	}

	// Subscribe to newsletter functionality
	$('#newsletter_name').click( function() {
		var content = $(this).val();
		if(content == "Your name"){
			$(this).val('');
		}
	});

	$('#newsletter_name').focusout( function() {
		var content = $(this).val();
		if(content == ""){
			$(this).val('Your name');
		}
	});

	$('#newsletter_email').click( function() {
		var content = $(this).val();
		if(content == "Your email address"){
			$(this).val('');
		}
	});

	$('#newsletter_email').focusout( function() {
		var content = $(this).val();
		if(content == ""){
			$(this).val('Your email address');
		}
	});

	$('#call-name').click( function() {
		var content = $(this).val();
		if(content == "Your name"){
			$(this).val('');
		}
	});

	$('#call-name').focusout( function() {
		var content = $(this).val();
		if(content == ""){
			$(this).val('Your name');
		}
	});

	$('#call-phone').click( function() {
		var content = $(this).val();
		if(content == "Your telephone number"){
			$(this).val('');
		}
	});

	$('#call-phone').focusout( function() {
		var content = $(this).val();
		if(content == ""){
			$(this).val('Your telephone number');
		}
	});

	$('#message').click( function() {
		var content = $(this).val();
		if(content == "Your message"){
			$(this).val('');
		}
	});

	$('#message').focusout( function() {
		var content = $(this).val();
		if(content == ""){
			$(this).val('Your message');
		}
	});

	$('#subscribe-newsletter form').submit( function() {
		var name = $('#newsletter_name').val();
		var email = $('#newsletter_email').val();



		function IsEmail() {
			var regex = /^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
			return regex.test(email);
		}

		function IsName(){
			if(name) {
				if(name == "Your name"){ return false; } else { return true; }
				if(name.length < 2) { return false; } else { return true; }
			} else {
				return false;
			}
		}

		if(IsEmail() && IsName()){
			$.post("/wp-content/themes/eps/includes/newsletter-post.php", { name: name, email: email },
   			function(data){
   				$('#subscribe-newsletter form').html('<p>' + data + '</p>');
   			});
		} else {
			$('#newsletter_name').css('border', '1px solid #FF3333');
			$('#newsletter_email').css('border', '1px solid #FF3333');
			$('#newsletter_name').css('color', '#000000');
			$('#newsletter_email').css('color', '#000000');
		}
		return false;
	});

	$('.member').first().show();

	$('.member h6 a').click( function(){
		var $this = $(this).parent().parent();
		var $nextMember = $this.next('.member');

		$this.hide();

		if ($nextMember.length == 0) {
			$('.member').first().show();
		} else {
			$nextMember.show();
		}

		return false;
	});

	$('.meet-team:not(.member) h6 a').click( function(){
		$('.meet-team').toggle();
		return false;
	});


});

