$(function() {
  load_gallery();
  search_label();
  configure_enquiry();
  init_fancybox();
  $("#quote_form").live("submit", function(){
    $.post("/ajax/quote.php", $("#quote_form").serialize(), function(data){
      $.fancybox(data);
    });
    return false;
  });
});

function init_fancybox(){
  $('#trigger, .gmaplink, .quotelink').fancybox();  
}

function load_gallery(){
  $('.field-content', '#home-gallery').cycle({
    pager: '#cyclenav'
  });
}

function search_label(){
  var slab = 'Search our site';
  $('#edit-search-block-form-1').val(slab);
  $('#edit-search-block-form-1').focus(function(){
    if($(this).val() == slab) {
      $(this).val('');
    }
  });
  $('#edit-search-block-form-1').blur(function(){
    if($(this).val() == '') {
      $(this).val(slab);
    }
  });
  $('#search-block-form').submit(function() {
    if($('#edit-search-block-form-1').val() == slab){
      $('#edit-search-block-form-1').val('');
    }
  });
}

function configure_enquiry(){
  $('#edit-name,#edit-name-1').val('Name *');
  $('#edit-name,#edit-name-1').data('phold','Name *');

  $('#edit-mail').val('Email *');
  $('#edit-mail').data('phold','Email *');

  $('#edit-field-company').val('Company Name');
  $('#edit-field-company').data('phold','Company Name');

  $('#edit-field-phone').val('Phone *');
  $('#edit-field-phone').data('phold','Phone *');
  
  $('#edit-message').val('Enquiry *');
  $('#edit-message').data('phold','Enquiry *');
  
  $('#edit-subject').val('Online Enquiry');

  $('.form-text,.form-textarea','#contact-form').focus(function(){
    if($(this).val() == $(this).data('phold')) $(this).val('');
  });
  $('.form-text,.form-textarea','#contact-form').blur(function(){
    if($(this).val() == '') $(this).val($(this).data('phold'));
  });

  $('#contact-mail-page-1').submit(function(){
    var errors = 0;
    if($('#edit-name,#edit-name-1').val() == 'Name *'){
      $('#edit-name,#edit-name-1').effect('shake',{
        times: 3,
        distance: 5
      },50);
      $('#edit-name,#edit-name-1').addClass('error');
      errors++;
    }else $('#edit-name,#edit-name-1').removeClass('error');
    
    if(!is_email($('#edit-mail').val())){
      $('#edit-mail').effect('shake',{
        times: 3,
        distance: 5
      },50);
      $('#edit-mail').addClass('error');
      errors++;
    }else $('#edit-mail').removeClass('error');

    if($('#edit-field-phone').val() == 'Phone *'){
      $('#edit-field-phone').effect('shake',{
        times: 3,
        distance: 5
      },50);
      $('#edit-field-phone').addClass('error');
      errors++;
    }else $('#edit-field-phone').removeClass('error');
    if($('#edit-message').val() == 'Enquiry *'){
      $('#edit-message').effect('shake',{
        times: 3,
        distance: 5
      },50);
      $('#edit-message').addClass('error');
      errors++;
    }else $('#edit-message').removeClass('error');
    if(errors) return false;
    else return true;
  });
}

function is_email(x){
  var atpos=x.indexOf("@");
  var dotpos=x.lastIndexOf(".");
  if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length){
    return false;
  }else{
    return true;
  }
}

function submit_quote(){
}
