$(document).ready(function(){

  $('.navigation a').pngFix();

  $('.navigation a.request_a_tour').colorbox({innerWidth:"384px", inline:true, href:"#request_a_tour", opacity: 0.8});
  $('.navigation a.contact_us').colorbox({innerWidth:"384px", inline:true, href:"#contact_us", opacity: 0.8});
  $('.navigation a.directions').colorbox({innerWidth:"384px", inline:true, href:"#directions", opacity: 0.8});
  $('a.show_legal').colorbox({innerWidth:"384px", inline:true, href:"#legal", opacity: 0.8});

  // contact us event
  $('a[name=btnSendMessage]').click(function(evt){
    evt.preventDefault();

    // create loader
    $('<div/>').addClass('float_left button_loader').html('<img src="images/loading-24.gif" alt="Loading..." />').insertAfter(evt.currentTarget);
    $(evt.currentTarget).hide();

    $.ajax({
      type: 'POST',
      url: 'resources/sendMessage.php',
      data: {
        txtName: $('#contact_us input[name=txtName]').val(),
        txtPhone: $('#contact_us input[name=txtPhone]').val(),
        txtEmail: $('#contact_us input[name=txtEmail]').val(),
        txtMessage: $('#contact_us textarea[name=txtMessage]').val()
      },
      dataType: 'json',
      success: function(data){
        $('div.button_loader').prev().show().next().remove();
        if(data.success){
          alert('You message was sent successfully.');
          $.fn.colorbox.close();
        } else {
          alert('Unable to send message: ' + data.err_msg);
        }
      },
      error: function(){
        $('div.button_loader').prev().show().next().remove();
        alert('An unknown error has occurred. Please try again later.');
      }
    });
  }); // end contact us event

  // request tour event
  $('a[name=btnRequestTour]').click(function(evt){
    evt.preventDefault();

    // create loader
    $('<div/>').addClass('float_left button_loader').html('<img src="images/loading-24.gif" alt="Loading..." />').insertAfter(evt.currentTarget);
    $(evt.currentTarget).hide();

    $.ajax({
      type: 'POST',
      url: 'resources/requestTour.php',
      data: {
        txtName: $('#request_a_tour input[name=txtName]').val(),
        txtPhone: $('#request_a_tour input[name=txtPhone]').val(),
        txtEmail: $('#request_a_tour input[name=txtEmail]').val()
      },
      dataType: 'json',
      success: function(data){
        $('div.button_loader').prev().show().next().remove();
        if(data.success){
          alert('You message was sent successfully.');
          $.fn.colorbox.close();
        } else {
          alert('Unable to send message: ' + data.err_msg);
        }
      },
      error: function(){
        $('div.button_loader').prev().show().next().remove();
        alert('Unable to send message: ' + data.err_msg);
      }
    });
  }); // end request tour event


  $('a.colorbox_cancel').click(function(evt){
    evt.preventDefault();

    $.fn.colorbox.close();
  });

});
