$(document).ready(function() {
	if($('#imagecycle').length) {
		var $c = $('#cycle').cycle({
			timeout: 3000,
			before: onBefore
		});
	        $('#tabs li a').each(function(i) {
                  $(this).click(function() {$c.cycle(i);$c.cycle('pause');return false;});
                });
	}
	if($('#contactform').length) {
		fh.init();
	}
	if($('#testimonials').length) {
		$('#testimonials').cycle({fit: true,width:'918px'});
	}
});

function onBefore() {
	$('#tabs li').removeClass('highlight');
	$('#'+this.alt).addClass('highlight');
	$('#slogan a').html($('#slogan-'+this.alt).html());
	$('#slogan a').attr('href',$('#'+this.alt+' a').attr('href'));
	$('#subheading').html($('#subheading-'+this.alt).html());
}
var fh = {
  success: true,
  els: [{'display':'Your Name','valid':true,'id':'#name','error':''},{'display':'Your Email','valid':true,'id':'#email','error':''},{'display':'Subject','valid':true,'id':'#subject','error':''},{'display':'Message','valid':true,'id':'#message','error':''}],
  init: function(){
    $("#contactform").submit(function(e) {
      e.preventDefault();
      if(!fh.validate()) {
        fh.showError();
        return false;
      } else {
	      $('#contactform fieldset').fadeOut(200);
	      $.ajax({
			url: '/contact.php',
			data: $('#contactform').serialize() + '&action=send',
			type: 'post',
			cache: false,
			dataType: 'html',
			success: function (data) {
				$('#contactform fieldset').html(data).fadeIn(200);
			},
			error: fh.contactError
		  });
	  }
    });
    $('#contactform input').focus(function() {
    	if($(this).hasClass('formerror')) {
    	  $(this).removeClass('formerror').val('');
    	}
    });
    $('#contactform textarea').focus(function() {
    	if($(this).hasClass('formerror')) {
    	  $(this).removeClass('formerror').val('');
    	}
    });
  },
  validate: function () {
    fh.resetForm();
    for (i=0;i<fh.els.length;i++) {
      if(!($(fh.els[i].id).val().length>0)) {
        fh.els[i].error = fh.els[i].display + ' is required';
        fh.els[i].valid = false;
        fh.success = false;
      }
    }

    if(fh.els[1].valid && !fh.validateEmail($(fh.els[1].id).val())) {
      fh.els[1].error = 'Please enter a valid email';
      fh.els[1].valid = false;
      fh.success = false;
    }
    return fh.success;
  },
  validateEmail: function (email) {
      var at = email.lastIndexOf("@");

      // Make sure the at (@) sybmol exists and
      // it is not the first or last character
      if (at < 1 || (at + 1) === email.length)
        return false;

      // Make sure there aren't multiple periods together
      if (/(\.{2,})/.test(email))
        return false;

      // Break up the local and domain portions
      var local = email.substring(0, at);
      var domain = email.substring(at + 1);

      // Check lengths
      if (local.length < 1 || local.length > 64 || domain.length < 4 || domain.length > 255)
        return false;

      // Make sure local and domain don't start with or end with a period
      if (/(^\.|\.$)/.test(local) || /(^\.|\.$)/.test(domain))
        return false;

      // Check for quoted-string addresses
      // Since almost anything is allowed in a quoted-string address,
      // we're just going to let them go through
      if (!/^"(.+)"$/.test(local)) {
        // It's a dot-string address...check for valid characters
        if (!/^[-a-zA-Z0-9!#$%*\/?|^{}`~&'+=_\.]*$/.test(local))
          return false;
      }

      // Make sure domain contains only valid characters and at least one period
      if (!/^[-a-zA-Z0-9\.]*$/.test(domain) || domain.indexOf(".") === -1)
        return false;

      return true;
    },
    resetForm: function() {
      fh.success = true;
      for (i=0;i<fh.els.length;i++) {
      	if(!fh.els[i].valid) {
          fh.els[i].error = '';
          fh.els[i].valid = true;
        }
      }
    },
    showError: function () {
      for (i=0;i<fh.els.length;i++) {
        if(!fh.els[i].valid) {
          $(fh.els[i].id).addClass("formerror").val(fh.els[i].error);
        }
      }
    },
    contactError: function(xhr) {
    	alert(xhr.statusText);
    }
};

























