/**
 * Part of Showkase web site management package
 *
 * @package Showkase
 * @author Jack Hardie {@link http://www.jhardie.com}
 * @copyright Copyright (c) 2012, SimpleViewer Inc.
 */
// http://misterdai.wordpress.com/2010/06/04/jquery-form-changed-warning/
$(function() {
  $('form.changecheck').each(function() {
    $(this).data('initialForm', $(this).serialize());
  }).submit(function(e) {
    var formEl = this;
    var changed = false;
    $('form.changecheck').each(function() {
      if (this != formEl && $(this).data('initialForm') != $(this).serialize()) {
        changed = true;
        $(this).addClass('changed');
      }
      else {
        $(this).removeClass('changed');
      }
    });
    if (changed && !confirm('Another form on this page has been changed. Are you sure you want to continue with this form submission?')) {
      e.preventDefault();
    } else {
      $(window).unbind('beforeunload', catcher);
    }
  });
  $(window).bind('beforeunload', catcher);
});
var catcher = function() {
  var changed = false;
  // update editor textarea elements so they participate in the check
  if (typeof CKEDITOR == 'object')
  {
    for (var i in CKEDITOR.instances) {
      if(CKEDITOR.instances[i].checkDirty())
      {
        CKEDITOR.instances[i].updateElement();
      }
    }
  }
  $('form.changecheck').each(function() {
    if ($(this).data('initialForm') != $(this).serialize()) {
      changed = true;
      $(this).addClass('changed');
    } else {
      $(this).removeClass('changed');
    }
  });
  if (changed) {
    return 'One or more forms on this page have changed.  Are you sure you want to leave this page?';
  }
};