jQuery(function() {
	/* Pitch */
	jQuery('ul#pitch li:first-child').addClass('current');
	jQuery('ul#pitch li').each(function(index) {
		jQuery(this).addClass('slide' + index);
	});
	
	jQuery('ul#pitchControls li.control:first').addClass('current');
	jQuery('ul#pitchControls li.control').each(function(index) {
		jQuery(this).addClass('slide' + index);
		jQuery(this).click(function() {
			gotoPitch(index);
			return false;
		});
	});
	
	var pitchReady = 1;
	var pitchCurrentSlide = 0;
	var pitchLastSlide = jQuery('ul#pitch li').length - 1;
	
	function autoPitch() {
		if (pitchCurrentSlide < pitchLastSlide) {
			pitchCurrentSlide++;
		} else {
			pitchCurrentSlide = 0;
		}
		
		jQuery('ul#pitch li.slide' + pitchCurrentSlide).css('display', 'inline');
		jQuery('ul#pitch li.current').fadeOut(2000, function() {
			jQuery(this).removeClass('current');
			jQuery('ul#pitch li.slide' + pitchCurrentSlide).addClass('current');			
		});
		jQuery('ul#pitchControls li.current a img').fadeOut(2000, function() {
			jQuery('ul#pitchControls li.current a img').css('display', 'none');
			jQuery('ul#pitchControls li.current').removeClass('current');
		});
		jQuery('ul#pitchControls li.slide' + pitchCurrentSlide + ' a img').fadeIn(2000, function() {
			jQuery('ul#pitchControls li.current').removeClass('current');
			jQuery('ul#pitchControls li.slide' + pitchCurrentSlide).addClass('current');
		});
	}
	
	function gotoPitch(index) {
		if (pitchReady == 1) {
			pitchReady = 0;
			clearInterval(autoModePitch);
			jQuery('ul#pitchControls li.slide a img').css('display', 'none');
			pitchCurrentSlide = index;
			
			jQuery('ul#pitch li.slide' + pitchCurrentSlide).css('display', 'inline');
			jQuery('ul#pitch li.current').fadeOut(2000, function() {
				jQuery(this).removeClass('current');
				jQuery('ul#pitch li.slide' + pitchCurrentSlide).addClass('current');			
			});
			jQuery('ul#pitchControls li.current a img').fadeOut(2000, function() {
				jQuery('ul#pitchControls li.current a img').css('display', 'none');
				jQuery('ul#pitchControls li.current').removeClass('current');
			});
			jQuery('ul#pitchControls li.slide' + pitchCurrentSlide + ' a img').fadeIn(2000, function() {
				jQuery('ul#pitchControls li.current').removeClass('current');
				jQuery('ul#pitchControls li.slide' + pitchCurrentSlide).addClass('current');
				
				autoModePitch = setInterval(autoPitch, 6000);
				pitchReady = 1;
			});
		}
	}
	
	jQuery('ul#pitchControls li.next a').click(function() {
		if (pitchReady == 1) {
			if (pitchCurrentSlide < pitchLastSlide) {
				pitchCurrentSlide++;
			} else {
				pitchCurrentSlide = 0;
			}
			gotoPitch(pitchCurrentSlide);
		}
		return false;
	});
	
	jQuery('ul#pitchControls li.previous a').click(function() {
		if (pitchReady == 1) {
			if (pitchCurrentSlide > 0) {
				pitchCurrentSlide--;
			} else {
				pitchCurrentSlide = pitchLastSlide;
			}
			gotoPitch(pitchCurrentSlide);
		}
		return false;
	});
	
	var autoModePitch = setInterval(autoPitch, 6000);
	
	/* Postcode */
	jQuery('input.postcode').focus(function() {
		if (jQuery(this).val() == 'Enter Post Code') {
			jQuery(this).val('');
		}
	});
	
	var LUSTY = {
		showLinks: function() { // Links that require javascript to work are hidden using CSS, and then shown using JS
			jQuery('li.mail').css('display', 'block');
			jQuery('li.bookmark').css('display', 'block');
			jQuery('li.print').css('display', 'block');
		},
		
		emailFriend: function() {
			jQuery('form#emailToFriendForm').live('submit', function() {
				sender_name = encodeURIComponent(jQuery('input#sender_name').val());
				sender_email = jQuery('input#sender_email').val();
				recipient_name = encodeURIComponent(jQuery('input#recipient_name').val());
				recipient_email = jQuery('input#recipient_email').val();
				url = jQuery('input#url').val();
				jQuery('#TB_ajaxContent').load('http://66.147.244.223/~lustyind/wp/wp-content/themes/lusty/email-to-a-friend.php?sender_name=' + sender_name + '&sender_email=' + sender_email + '&recipient_name=' + recipient_name + '&recipient_email=' + recipient_email + '&url=' + url);
				return false;
			});
		},
		
		/* Contact form validation */	
		validateEnquiry: function() {
			var valid = true;
			
			if (jQuery('form#formContact textarea#enquiry').val()=='') {
				jQuery('form#formContact textarea#enquiry').css('background','#FFDFBF').focus();
				valid = false;
			} else {
				jQuery('form#formContact textarea#enquiry').css('background','#fff');
			}
			
			if (jQuery('form#formContact input#email').val()=='' || LUSTY.checkEmail(jQuery('form#formContact input#email').val()) == false) {
				jQuery('form#formContact input#email').css('background','#FFDFBF').focus();
				valid = false;
			} else {
				jQuery('form#formContact input#email').css('background','#fff');
			}
			
			if (jQuery('form#formContact input#lastname').val()=='') {
				jQuery('form#formContact input#lastname').css('background','#FFDFBF').focus();
				valid = false;
			} else {
				jQuery('form#formContact input#lastname').css('background','#fff');
			}
			
			if (jQuery('form#formContact input#firstname').val()=='') {
				jQuery('form#formContact input#firstname').css('background','#FFDFBF').focus();
				valid = false;
			} else {
				jQuery('form#formContact input#firstname').css('background','#fff');
			}
			
			if (jQuery('form#formContact input#salutation').val()=='') {
				jQuery('form#formContact input#salutation').css('background','#FFDFBF').focus();
				valid = false;
			} else {
				jQuery('form#formContact input#salutation').css('background','#fff');
			}
			
			if (valid == false) {
				return false;
			}
			
			if (jQuery('form#formContact input#newsSub').prop('checked')) {
				jQuery('form#frontendform input#nameInput').val(jQuery('form#formContact input#firstname').val() + ' ' + jQuery('form#formContact input#lastname').val());
				jQuery('form#frontendform input#emailInput').val(jQuery('form#formContact input#email').val());
				jQuery('form#frontendform').submit();
			}
			
			return true;
		},
		
		checkEmail: function(str) {
			var at="@";
			var dot=".";
			var lat=str.indexOf(at);
			var lstr=str.length;
			var ldot=str.indexOf(dot);
			if (str.indexOf(at) == -1 || str.indexOf(at) == -1 || str.indexOf(at) == 0 || str.indexOf(at) == lstr || str.indexOf(dot) == -1 || str.indexOf(dot) == 0 || str.indexOf(dot) == lstr || str.indexOf(at,(lat+1)) != -1 || str.substring(lat-1,lat) == dot || str.substring(lat+1,lat+2) == dot || str.indexOf(dot,(lat+2)) == -1 || str.indexOf(" ")!=-1) {
				return false;
			}
			return true;
		}
	}
	
	LUSTY.showLinks();
	LUSTY.emailFriend();
	
	jQuery('form#formContact').submit(function() {
		return LUSTY.validateEnquiry();
	});
	
	if (jQuery('#dealer input#user_postcode').val()!='Enter Post Code') {
		jQuery('#postcode input#addressInput').val(jQuery('#dealer input#user_postcode').val());
		jQuery('input#dealerLocatorSubmit').click();
	}
});
