// Set up global variables and possible headlines 
var headline = '';
var index = '';
var new_headline = '';

var headlines = new Array('blooming','soar','clarity','balance');

var headline_html = new Array(
	'<span id="helping">Helping You to Be </span><span id="blooming">Blooming </span><span id="marvellous">Marvellous</span>',
	'<span id="helping">Helping You </span><span id="soar">Soar</span>',
	'<span id="drop">A Drop <span>of </span></span><span id="clarity">Clarity </span><span id="can">Can Go a Long Way</span>',
	'<span id="helping">Helping You </span><span id="find">Find a Work Life </span><span id="balance">Balance</span>'
);



$(document).ready(function() {
	// Yay, we're in business
	$('#wrapper').attr('class', 'has_js');
	
  // Local scroll settings 
  $.localScroll({
    event:'click', //on which event to react
    axis:'y',
    duration:500,
    offset:-30,
    hash:true,
		stop:true      //avoid queuing animations
  });

	// Headline is styled by the hard coded last body class so...
	headline = 'blooming';
	$('.home #header .contents').hide();

	// Randomise the headline
	index = Math.floor(Math.random()*headlines.length);
	if (headlines[index] == headline) {show_header()}
	else {set_header();}
	$('<button class="next" title="View a different headline">Next</button>').insertAfter('.home #contact_link').click(next_header);
	
	// Form help prior to input
	$('<em class="help">Your full name is preferred</em>').insertAfter('#name');
	$('<em class="help">Enter your email address</em>').insertAfter('#email');
	$('<em class="help">This is optional</em>').insertAfter('#phone');
	$('#enquiry .help').hide();
  $('input.text,textarea').each(function() {
    $(this)
    	.val($(this).attr('title'))
      .data('default', $(this).val())
      .addClass('inactive')
      .focus(function() {
        $(this).removeClass('inactive').next('.help').fadeIn('fast');
        if($(this).val() == $(this).data('default') || '') {
          $(this).val('');
        }
      })
      .blur(function() {
        $(this).next('.help').fadeOut('fast');
        var default_val = $(this).data('default');
        if($(this).val() == '') {
          $(this).addClass('inactive');
          $(this).val($(this).data('default'));
        }
      });
  });
  
	// Validate the form inputs
	$('#enquiry').validate({
		// debug: true,
		rules: {
      name: 'namecheck',
      cs: 'wet',
      email: {
        required: true,
        email: true
      }
    },
		messages: {
      email: 'Please enter a valid email address'
    },
    success: function(label) {
      label.text('OK').addClass('valid');
    }
  });
  
	$.validator.addMethod('wet', function(value) {
		return value == 'wet';
	}, 'Please enter ‘wet’ (lower case)');

	jQuery.validator.addMethod('namecheck', function(value, element) { 
		return this.optional(element) || /^(?!.*Name).*$/i.test(value); 
	}, 'Please enter your name');

  // Initialise customer quotes sliding
	$('<button class="next" title="View next customer quote">Next</button>').insertAfter('#satisfied h3');
  $('#satisfied .next').click(next_quote);
  $('#satisfied li,#facebook').hide();
  $('#satisfied li:first-child').addClass('current');
	$('.current').slideDown(400);
	$('#facebook').delay(400).slideDown(400);  
});



function next_quote() {
	$('.current').slideUp(400);
	
	// If we've got to the end of the quotes
	if ($('.current').next().length == 0) {
		$('.current').removeClass('current');
		// Go back to the beginning
		$('#satisfied li:first-child').addClass('current');
	}
	else {
		$('.current').next().addClass('current').prev().removeClass('current');
	}
	$('.current').delay(600).slideDown(400);
}

function next_header() {	  
  if (index < (headlines.length - 1)) {
		// If we aren't already on the last of the possible headlines...
		index = index + 1;
	}
	else {
		// ...Go back to the beginning 
		index = 0; 
	}
	$('#header .contents').fadeOut(400);
	// Make sure headline has faded out before we change it!
	window.setTimeout(set_header,500);
}

function set_header() {	  
	new_headline = headlines[index];	
	$('body').removeClass(headline).addClass(new_headline);
	$('#header h2').html(headline_html[index]);
	headline = new_headline;
	show_header();
}

function show_header() {
	// Preload background image
	/* var img_path = 'img/' + new_headline + '.jpg';
	$('<img />').attr('src', img_path).attr('class','off').load(function() {
		$('body').append($(this)); */
		$('.home #header .contents').delay(500).fadeIn(400);
	/*}); */
} 


