/**
 * @file: contains custom javascript for all pcn sites.
 */

Drupal.behaviors.formSaving = function(context) {
  /*// this code doesn't take into account client-side validation
   var $form = $('form', context);
  if ($form.is('#node-form') || $form.is('#comment-form')) {
    var $button = $('#edit-submit', $form);
    $button.bind('click', function() {
      Drupal.librarySaving($form, $button);
    });
  }
  */
  $('form#node-form, form#comment-form', context).submit(function() {
    var $form = $(this);
    var $button = $('#edit-submit', $form);
    
    Drupal.librarySaving($form, $button);
  });
}

Drupal.librarySaving = function(form, button) {
  // insert the saving div now to cache it for better performance and to show the loading image
  var $saving = Drupal.theme('librarySaving');
  $saving.insertAfter(button);
  button.siblings("input:submit").hide();
  button.hide();
  $saving.show();

  // append notice if form saving isn't work, perhaps a timeout issue
  $notice = Drupal.theme('libraryNotice');
  setTimeout(function() {$notice.appendTo("#saving").fadeIn();}, 24000);
}

Drupal.theme.prototype.librarySaving = function() {
  return $('<div id="saving"><p class="saving">Saving&hellip;</p></div>');
}
Drupal.theme.prototype.libraryNotice = function() {
  return $('<p id="saving-notice">Not saving? Wait a few seconds, reload this page, and try again.</p>');
}

// modified from: http://drupal.org/project/saveguard

// $Id: saveguard.js,v 1.1 2007/11/27 20:48:36 starbow Exp $
/**
 *  Called when there is unsaved data on the page
 *    to warn users leaving (or reloading) page that they would lose their changes.
 *  The onbeforeunload event works in FF, IE and Safari, but not in Opera (yet).
 *  The form of the dialog is unmodifiable:
 *
 *  Are you sure you want to navigate away from this page?
 *  Your message here
 *  Press OK to continue, or Cancel to stay on the current page.
 *
 * NOTE: This doesn't work with Rich Text Editors like YUI or TinyMCE yet
 */
Drupal.behaviors.checkChanges = function(context) {
  var msg = Drupal.t('If you continue, your unsaved changes will be lost.');
  // only check for changes for these forms, not every form on the site
  // checkboxes, radio buttons and selects
  $('#node-form input, #node-form select, #comment-form input, #comment-form select, #user-register input, #user-register select').change(function() { Drupal.markPageUnsaved(msg) });
  // textfields and textareas
  $('#node-form input, #node-form textarea, #comment-form input, #comment-form textarea, #user-register input, #user-register textarea').keypress(function() { Drupal.markPageUnsaved(msg) });
};

Drupal.markPageUnsaved = function(msg) {
  // Only do it once
  if (!$('body').is('.has-unsaved-changes')) {
    $('body').addClass('has-unsaved-changes');
    if (!msg) { // default message
      msg = "If you continue your unsaved changes will be lost.";
    }
    window.onbeforeunload = function() { return msg; };
    $(':submit, input:image').click(function() {
      window.onbeforeunload = null;  // Turn off the warning before submit triggers it.
    });
  }
};

/**
 * Any form with class="auto-submit" will have it's submit button hidden
 * and any value in the select box chosen will submit the form and redirect page.
 */
Drupal.behaviors.autoSubmit = function() {
  var $form = $("form.auto-submit");
  $form.find(":submit").hide();
  // IE doesn't throw onChange for the form from changing a form element
  $form.find("select").change(function() {
    $form.submit();
  });
};

/**
 * Fix flickering background images in IE.
 * http://misterpixel.blogspot.com/2006/09/forensic-analysis-of-ie6.html
 */
Drupal.behaviors.fixIEFlickr = function() {
  if (jQuery.browser.msie) {
    try { 
      document.execCommand('BackgroundImageCache', false, true); 
    } catch(err) {}
  }
};
