var slideSwitch = function() {
      var $active = $('#homepage_masthead>div.active');
  
      if ( $active.length == 0 ) $active = $('#homepage_masthead>div:last');
  
      var $next =  $active.next().length ? $active.next()
          : $('#homepage_masthead>div:first');
  
      $active.addClass('last-active');
  
      $next.css({opacity: 0.0})
          .addClass('active')
          .animate({opacity: 1.0}, 1000, function() {
              $active.removeClass('active last-active');
          });
};

var initHomepageMasthead = function() {

//  console.log('Initialising homepage masthead');
  
  var masthead_links = '';
  
  $('#homepage_masthead>div').hide();
  $('#homepage_masthead>div').each(function(i){
    title = $(this).find('h2').text();
    link = $(this).find('a').attr('href');
    link = $(this).attr('id');
    masthead_links += '<li><a href="#' + link + '" onclick="return false;">' + title + '</a></li>';
  });
  $('#homepage_masthead').prepend('<ul class="masthead-links">' + masthead_links + '</ul>');
  
  
  setInterval( "slideSwitch()", 1000 );
    
}; 

var initEnhancedInputs = function() {
  $(".cta-form input[type=text]").focus(function(srcc){
    if ($(this).val() == $(this)[0].title) {
      $(this).addClass("textbox-active");
      $(this).removeClass("textbox-inactive");
      $(this).val("");
    }
  });
  
  $(".cta-form input[type=text]").blur(function() {
    if ($(this).val() == "") {
      $(this).removeClass("textbox-active");
      $(this).addClass("textbox-inactive");
      $(this).val($(this)[0].title);
    }
  });
  
  $(".cta-form input[type=text]").blur();

}

var validateCallMeBack = function() {

  initEnhancedInputs();

  $('.cta-form #CallRelatingToExistingDetail').hide();

//  console.log($(".cta-form input[@type='radio']").val());
  
  $(".cta-form input[type=radio]").click(function() {
    
    if($(this).val()=='Enquiry') {
      $('.cta-form #CallRelatingToExistingDetail').show();
    } else {
      $('.cta-form #CallRelatingToExistingDetail').hide();    
    }
  });

  $(".cta-form #CallRelatingTo").change(function() {
    console.log($(this).val());
    if($(this).val()=='Enquiry') {
      $('.cta-form #CallRelatingToExistingDetail').slideDown('fast');
    } else {
      $('.cta-form #CallRelatingToExistingDetail').slideUp('fast');
    }
  });

  $('.cta-form').submit(function() {
  
    error = false;
    
    tel_no = $(this).find('#CallTelephoneNo');
    full_name = $(this).find('#CallFullName');
    postcode = $(this).find('#CallPostcode');
    relating_to = $(this).find('#CallRelatingTo');
    relating_to_detail = $(this).find('#CallRelatingToExistingDetail');
    

//    console.log('checking form - tel = ' + $(tel_no).attr('value') + '; name = ' +  + $(full_name).attr('value'));
    
    if($(full_name).val()=='' || $(full_name).val()==$(full_name).attr('title')) {
//      console.log('full name error');
      $(full_name).addClass('error');
      error = true;
    } else {
//      console.log('full name not error');
      $(full_name).removeClass('error');
    }

    if($(tel_no).val()=='' || $(tel_no).val()==$(tel_no).attr('title')) {
//      console.log('tel no error');
      $(tel_no).addClass('error');
      error = true;
    } else {
//      console.log('tel no not error');
      $(tel_no).removeClass('error');
    }
  
    if($('#CallRelatingTo').val()=='Enquiry' && $(relating_to_detail).val()=='') {
//      console.log('Reason required');
      $(relating_to_detail).addClass('error');
      error = true;
    } else {
//      console.log('tel no not error');
      $(relating_to_detail).removeClass('error');
    }
  
//    console.log('error = ' + error);
    if(error) return false;
    
  });

}

var initSuperSleight = function() {

//  alert('Supersleight ' + $.fn.supersleight);
  $('#logo').supersleight();
  $('#masthead .quote-me-now').supersleight();

};

$(document).ready(function(){
  $('#navigation_main').droppy();
  initHomepageMasthead();
  initSuperSleight();
  validateCallMeBack();
  
});

