// author: moz
var start_date_set = false;
var end_date_set = false;
var branch_set = false;
var name_set = false;
var suff_insurance = true;

// document ready stuff
$(function() {
  
  // input field auto clear & repopulate
  $('input.clearme').click(function() {
    if ($(this).attr('title') == $(this).val()) {
      $(this).val('');
    };    
  });  
  $('input.clearme').blur(function() {
    if ($(this).val() == '') {
      $(this).val($(this).attr('title'));
    };
  });
  
  // login box
  $('a.login').click(function() {
    $('div.loginbox').slideToggle();
    setTimeout(function() { $('#username_header').focus().val(''); }, 450);
  });

  // login box (when opened)
  $('a.login-button').click(function() {
    $('div.loginbox').slideToggle();
  });  
  
  //  call back form
  $('a.callme').click(function() {
    $("div.container").css({ opacity: 0.3 });
    $('div#callmebox').show();
    $('div.callmebox').slideDown();
  });
  
  $('a#close').click(function() {
    $('div.callmebox').slideUp(function() {
      $('div#callmebox').hide();
      $("div.container").css({ opacity: 1 });
    });    
  }); 
  
  $('a.send').click(function() {
    $.ajax({
      type: "POST",
      url: "/callMeBack.php",
      data: "first_name_contact=" + $('input#first_name_contact').val() + 
            "&last_name_contact=" + $('input#last_name_contact').val() +
            "&email_contact=" + $('input#email_contact').val() +
            "&telephone_contact=" + $('input#telephone_contact').val() +
            "&message_contact=" + $('#message_contact').val() +
            "&submit_contact=" + $('input#submit_contact').val() +
            "&method_contact=" + $('input:radio[name=method_contact]:checked').val(),
      success: function(msg){
        $('div#callmebox').html(msg);
        apply_call_buttons();
      }
    });
  })
  
  function apply_call_buttons() {
    $('a#close').click(function() {
      $('div.callmebox').slideUp(function() {
        $('div#callmebox').hide();
        $("div.container").css({ opacity: 1 });
      });    
    }); 

    $('a.send').click(function() {
      $.ajax({
        type: "POST",
        url: "/callMeBack.php",
        data: "first_name_contact=" + $('input#first_name_contact').val() + 
              "&last_name_contact=" + $('input#last_name_contact').val() +
              "&email_contact=" + $('input#email_contact').val() +
              "&telephone_contact=" + $('input#telephone_contact').val() +
              "&message_contact=" + $('#message_contact').val() +
              "&submit_contact=" + $('input#submit_contact').val() +
              "&method_contact=" + $('input:radio[name=method_contact]:checked').val(),
        success: function(msg){
          $('div#callmebox').html(msg);
          apply_call_buttons();
        }
      });
    })
  }
  
  // result page page scroller
  $('.section-header a').click(function(){
    $.scrollTo( this.hash, 1200, {easing:'easeOutQuad'});
    return false;
  });
  
  $('a#submit_btn').click(function() {
    $('a#submit_btn').parents('form').submit();
  });
  
  $.easing.easeOutQuad = function (x, t, b, c, d) {
  		return -c *(t/=d)*(t-2) + b;
  };
	
});

// location finder
function locationMove(position, id, postcode) {
  new_position = position * 32;
  
  // fix for IE
  if (!$.support.leadingWhitespace) {
    new_position -= position;
  }
  
  $('.locations').animate(
    {
      backgroundPosition: "(0px " + (new_position - 1) + "px)"
    },
    {
      duration: 500
    }
  );

  $('ul.locations li a').removeClass('active');
  $('ul.locations li a.location-' + position).addClass('active');

  $.ajax({
    type: "GET",
    url: "/locationDetail.php",
    data: "id=" + id,
    success: function(msg){
      $('div#details').html(msg);
    }
  });
  
  $('#postcode_from').val(postcode);
  
  if (postcode.match(/\-?\d+\.\d+,\-?\d+\.\d+/)) {
    useLatLng(postcode, function (point) {
      map.panTo(point);
      placeMarkerAtPoint(point);
    });
    
  } else {
    usePointFromPostcode('U-Drive, ' + postcode, function (point) {
      map.panTo(point);
      placeMarkerAtPoint(point);
    });
  }
};

// handles our onclicks where we want to return nothing 
function nothing() {
  return false;
}
