/*
 *
 * JS for main index page
 *
 */

$(document).ready( function(){
  $('.rounded').corners("10px");
  $('.rounded5').corners("5px");
  $('.rounded5top').corners("5px top");
  
  // iPhone : hide Safari toolbar when viewing QuickTasks iPhone website
  window.scrollTo(0, 1);
  
  // event-handlers attach

  // form button submit
  $('#btn_sendfeedback').click( function(){submitFeedback();} );
  
  // btn disabling
  $("#btn_sendfeedback").ajaxStart(function(){
    $(this).attr("disabled", true);
  });
  $("#btn_sendfeedback").ajaxStop(function(){
    $(this).attr("disabled", false);
  });
  
  // eye candy
  $("#loading").ajaxStart(function(){
    $(this).css("visibility", "visible");
  });
  $("#loading").ajaxStop(function(){
    $(this).css("visibility", "hidden");
  });

});

function submitFeedback() {
  var temps = new Date().getTime();
  $.ajax( {
      type: 'post',
      url: 'save_comment.php',
      data: "name="+$('#in_name').val()+"&email="+$('#in_email').val()+"&comment="+$('#in_comment').val()+"&noCache="+temps,
      dataType: 'json', /*'text' 'xml' 'html'*/
      success: submitSuccess,
      error: submitError /* or directly: function() {alert('...');} */
    } );
}

function submitSuccess(answer) {
  if(answer.status == 1) {
    $('#in_name').val('');
    $('#in_email').val('');
    $('#in_comment').val('');
  }
  //if(isIPhone()) {
  //  alert(answer.message);
  //} else {
    $.popup.show(answer.title, answer.message, {main_class: "rounded5top", postDOM: function(){$('.rounded5top').corners("5px top");} });
  //}
}

function submitError(XMLHttpRequest, textStatus, errorThrown) {
  //alert("Unexpected server error...\nPlease try again in a moment.");
  //if(isIPhone()) {
  //  alert("Unexpected server error.\nPlease try again in a moment.\n\nIf you keep getting this error, please let us know at contact@medothis.com");
  //} else {
    $.popup.show("Server error", "Unexpected server error ("+textStatus+").\nPlease try again in a moment.\n\nIf you keep getting this error, please let us know at contact@medothis.com", {main_class: "rounded5top", postDOM: function(){jQuery('.rounded5top').corners("5px top");} });
  //}
}


